Skip to content

Commit 4ad9837

Browse files
committed
[chore] adding tests for the spp_api controller
1 parent 2793663 commit 4ad9837

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

spp_api/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
from . import test_res_users
88
from . import test_ir_model_fields
99
from . import test_apijsonrequest
10+
from . import test_main_controller
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from odoo.tests import tagged
2+
from odoo.tests.common import HttpCase
3+
4+
5+
@tagged("post_install", "-at_install", "test_main_controller")
6+
class TestOASController(HttpCase):
7+
@classmethod
8+
def setUpClass(cls):
9+
super().setUpClass()
10+
# Create test namespace
11+
cls.test_namespace = cls.env["spp_api.namespace"].create(
12+
{"name": "test", "version_name": "1.0", "active": True, "token": "test_token"}
13+
)
14+
15+
def setUp(self):
16+
super().setUp()
17+
# Set up request environment
18+
self.authenticate("admin", "admin")
19+
self.url_open("/") # This initializes the request object
20+
21+
def test_01_index(self):
22+
"""Test the index method of OAS controller"""
23+
# Test the index endpoint directly
24+
response = self.url_open("/doc/api-docs/index.html")
25+
self.assertEqual(response.status_code, 200)
26+
27+
def test_02_get_api_urls(self):
28+
"""Test the _get_api_urls method of OAS controller"""
29+
# Test the endpoint that uses _get_api_urls
30+
response = self.url_open("/doc/api-docs/index.html")
31+
self.assertEqual(response.status_code, 200)
32+
33+
# Verify the namespace is accessible
34+
namespace = self.env["spp_api.namespace"].sudo().search([("name", "=", "test"), ("version_name", "=", "1.0")])
35+
self.assertTrue(namespace)
36+
self.assertTrue(namespace.active)
37+
38+
def test_03_oas_json_spec_download(self):
39+
"""Test the oas_json_spec_download method of OAS controller"""
40+
# Test with valid namespace and token
41+
response = self.url_open(f"/api/test/1.0/swagger.json?token={self.test_namespace.token}")
42+
self.assertEqual(response.status_code, 200)
43+
self.assertEqual(response.headers["Content-Type"], "application/json")
44+
45+
# Test with invalid namespace
46+
response = self.url_open("/api/invalid/1.0/swagger.json?token=test_token", allow_redirects=False)
47+
self.assertEqual(response.status_code, 404)
48+
49+
# Test with invalid token
50+
response = self.url_open("/api/test/1.0/swagger.json?token=invalid_token", allow_redirects=False)
51+
self.assertEqual(response.status_code, 403)
52+
53+
def test_04_oas_document(self):
54+
"""Test the oas_document method of OAS controller"""
55+
# Test with valid namespace and token
56+
response = self.url_open(f"/api/swagger-doc/test/1.0?token={self.test_namespace.token}")
57+
self.assertEqual(response.status_code, 200)
58+
self.assertTrue(response.headers["Content-Type"].startswith("text/html"))
59+
60+
# Test with invalid namespace
61+
response = self.url_open("/api/swagger-doc/invalid/1.0?token=test_token", allow_redirects=False)
62+
self.assertEqual(response.status_code, 404)
63+
64+
# Test with invalid token
65+
response = self.url_open("/api/swagger-doc/test/1.0?token=invalid_token", allow_redirects=False)
66+
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)