Skip to content

Commit

Permalink
[FIX] Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilvioC2C committed Aug 19, 2024
1 parent 3be2570 commit 8c1e734
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion webservice/models/webservice_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _get_adapter_protocol(self):

@api.onchange("auth_type")
def _onchange_oauth2_auth_type(self):
# reset the auth2_flow when auth_type is not oaut2
# reset the oauth2_flow when auth_type is not oauth2
# using a compute method interfers with the server environment mixin
for rec in self:
if rec.auth_type != "oauth2":
Expand Down
45 changes: 28 additions & 17 deletions webservice/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from contextlib import contextmanager
from unittest import mock
from urllib.parse import urlparse

from requests import PreparedRequest, Response, Session
from requests import PreparedRequest, Session

from odoo.tests.common import tagged

Expand All @@ -30,30 +31,40 @@ def _setup_records(cls):

@classmethod
def setUpClass(cls):
cls._super_send = Session.send
super().setUpClass()
cls._setup_env()
cls._setup_records()

@classmethod
def _request_handler(cls, s: Session, r: PreparedRequest, /, **kw):
if r.url.startswith("http://localhost.demo.odoo/"):
r = Response()
r.status_code = 200
return r
if urlparse(r.url).netloc in ("localhost.demo.odoo", "custom.url"):
return cls._super_send(s, r)
return super()._request_handler(s, r, **kw)

Check warning on line 43 in webservice/tests/common.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/common.py#L43

Added line #L43 was not covered by tests


@contextmanager
def mock_cursor(cr):
with mock.patch("odoo.sql_db.Connection.cursor") as mocked_cursor_call:
org_close = cr.close
org_autocommit = cr.autocommit
try:
cr.close = mock.Mock()
cr.autocommit = mock.Mock()
cr.commit = mock.Mock()
mocked_cursor_call.return_value = cr
yield
finally:
cr.close = org_close
cr.autocommit = org_autocommit
# Preserve the original methods and attributes
org_close = cr.close
org_autocommit = cr._cnx.autocommit
org_commit = cr.commit

try:
# Mock methods and attributes
cr.close = mock.Mock()
cr.commit = mock.Mock()
# Mocking the autocommit attribute
mock_autocommit = mock.PropertyMock(return_value=False)
type(cr._cnx).autocommit = mock_autocommit

# Mock the cursor method to return the current cr
with mock.patch("odoo.sql_db.Connection.cursor", return_value=cr):
yield cr

finally:
# Restore the original methods and attributes
cr.close = org_close
cr.commit = org_commit
# Restore the original autocommit property
type(cr._cnx).autocommit = org_autocommit
10 changes: 5 additions & 5 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def test_fetch_token(self):
f"{self.url}oauth2/token",
json={
"access_token": "cool_token",
"expires_at": expires_timestamp,
"expires_in": duration,
"token_type": "Bearer",
"expires_in": duration,
"expires_at": expires_timestamp,
},
)
responses.add(responses.GET, f"{self.url}endpoint", body="OK")
Expand Down Expand Up @@ -153,7 +153,7 @@ def _setup_records(cls):
"oauth2_client_secret = shh_secret",
f"oauth2_token_url = {cls.url}oauth2/token",
f"oauth2_audience = {cls.url}",
f"oauth2_authorization_url = {cls.url}/authorize",
f"oauth2_authorization_url = {cls.url}authorize",
]
)
cls.webservice = cls.env["webservice.backend"].create(
Expand All @@ -169,7 +169,7 @@ def _setup_records(cls):
"oauth2_client_secret": "shh_secret",
"oauth2_token_url": f"{cls.url}oauth2/token",
"oauth2_audience": cls.url,
"oauth2_authorization_url": f"{cls.url}/authorize",
"oauth2_authorization_url": f"{cls.url}authorize",
}
)
return res
Expand All @@ -183,7 +183,7 @@ def test_authorization_code(self):
expected_action = {
"type": "ir.actions.act_url",
"target": "self",
"url": "https://localhost.demo.odoo//authorize?response_type=code&"
"url": "https://localhost.demo.odoo/authorize?response_type=code&"
"client_id=some_client_id&"
f"redirect_uri={quote(self.webservice.redirect_url, safe='')}&state=",
}
Expand Down
4 changes: 2 additions & 2 deletions webservice/tests/test_webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestWebService(CommonWebService):
@classmethod
def _setup_records(cls):
res = super()._setup_records()
cls.url = "http://localhost.demo.odoo/"
cls.url = "https://localhost.demo.odoo/"
cls.webservice = cls.env["webservice.backend"].create(
{
"name": "WebService",
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_web_service_get_url_combine(self):
def test_web_service_get_url_combine_full_url(self):
endpoint = "api/test"
responses.add(responses.GET, self.url + endpoint, body="{}")
result = self.webservice.call("get", url="http://localhost.demo.odoo/api/test")
result = self.webservice.call("get", url="https://localhost.demo.odoo/api/test")
self.assertEqual(result, b"{}")
self.assertEqual(len(responses.calls), 1)
self.assertEqual(
Expand Down

0 comments on commit 8c1e734

Please sign in to comment.