From 7f5236e87d3246b8b0f5bda20a4ca1576b37a3b3 Mon Sep 17 00:00:00 2001 From: Maksym Yankin Date: Tue, 6 Aug 2024 20:07:07 +0300 Subject: [PATCH] fixup! [FIX] webservice: WARNING message in logs --- webservice/models/webservice_backend.py | 2 +- webservice/tests/common.py | 48 ++++++++++++++++--------- webservice/tests/test_oauth2.py | 19 +++++----- webservice/tests/test_webservice.py | 4 +-- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/webservice/models/webservice_backend.py b/webservice/models/webservice_backend.py index f838d8a9..53bd7b8b 100644 --- a/webservice/models/webservice_backend.py +++ b/webservice/models/webservice_backend.py @@ -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": diff --git a/webservice/tests/common.py b/webservice/tests/common.py index b251a648..bf542d96 100644 --- a/webservice/tests/common.py +++ b/webservice/tests/common.py @@ -36,24 +36,40 @@ def setUpClass(cls): @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 r.url.startswith("https://localhost.demo.odoo/") or r.url.startswith( + "https://custom.url" + ): + response = Response() + response.status_code = 200 + response.url = r.url + response.request = r + response.headers["Content-Type"] = "application/json" + return response return super()._request_handler(s, r, **kw) @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 diff --git a/webservice/tests/test_oauth2.py b/webservice/tests/test_oauth2.py index e4326992..3683d341 100644 --- a/webservice/tests/test_oauth2.py +++ b/webservice/tests/test_oauth2.py @@ -53,15 +53,16 @@ def test_get_adapter_protocol(self): def test_fetch_token(self): duration = 3600 expires_timestamp = time.time() + duration + token_response = { + "access_token": "cool_token", + "token_type": "Bearer", + "expires_in": duration, + "expires_at": expires_timestamp, + } responses.add( responses.POST, f"{self.url}oauth2/token", - json={ - "access_token": "cool_token", - "expires_at": expires_timestamp, - "expires_in": duration, - "token_type": "Bearer", - }, + body=json.dumps(token_response).encode("utf-8"), ) responses.add(responses.GET, f"{self.url}endpoint", body="OK") @@ -153,7 +154,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( @@ -169,7 +170,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 @@ -183,7 +184,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=", } diff --git a/webservice/tests/test_webservice.py b/webservice/tests/test_webservice.py index b6c4b224..3c7fc312 100644 --- a/webservice/tests/test_webservice.py +++ b/webservice/tests/test_webservice.py @@ -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", @@ -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(