From b31039976456d744df2a372e12c9a7184f697f71 Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Thu, 31 Oct 2024 10:39:25 +0100 Subject: [PATCH] [BKP][ADD]webservice Backport all changes done in v16: https://github.com/OCA/web-api/tree/16.0/webservice --- webservice/__manifest__.py | 2 +- webservice/components/request_adapter.py | 4 +- webservice/controllers/oauth2.py | 4 +- webservice/readme/CONTRIBUTORS.rst | 1 + webservice/tests/common.py | 22 ++++------ webservice/tests/test_oauth2.py | 56 ++++++++++++------------ webservice/tests/test_webservice.py | 25 ++++------- 7 files changed, 49 insertions(+), 65 deletions(-) diff --git a/webservice/__manifest__.py b/webservice/__manifest__.py index f0d962a2..90f0f2e9 100644 --- a/webservice/__manifest__.py +++ b/webservice/__manifest__.py @@ -8,7 +8,7 @@ "name": "WebService", "summary": """ Defines webservice abstract definition to be used generally""", - "version": "16.0.1.4.0", + "version": "13.0.1.0.0", "license": "AGPL-3", "development_status": "Production/Stable", "maintainers": ["etobella"], diff --git a/webservice/components/request_adapter.py b/webservice/components/request_adapter.py index 7d3527f3..9a016957 100644 --- a/webservice/components/request_adapter.py +++ b/webservice/components/request_adapter.py @@ -169,9 +169,7 @@ def _fetch_new_token(self, old_token): def _request(self, method, url=None, url_params=None, **kwargs): url = self._get_url(url=url, url_params=url_params) new_kwargs = kwargs.copy() - new_kwargs.update( - {"headers": self._get_headers(**kwargs), "timeout": None,} - ) + new_kwargs.update({"headers": self._get_headers(**kwargs), "timeout": None}) client = BackendApplicationClient(client_id=self.collection.oauth2_clientid) with OAuth2Session(client=client, token=self.token) as session: # pylint: disable=E8106 diff --git a/webservice/controllers/oauth2.py b/webservice/controllers/oauth2.py index 3e953519..575fdef0 100644 --- a/webservice/controllers/oauth2.py +++ b/webservice/controllers/oauth2.py @@ -35,9 +35,7 @@ def redirect(self, backend_id, **params): backend._get_adapter() ) # we expect an adapter that supports web_application token = adapter._fetch_token_from_authorization(code) - backend.write( - {"oauth2_token": json.dumps(token), "oauth2_state": False,} - ) + backend.write({"oauth2_token": json.dumps(token), "oauth2_state": False}) # after saving the token, redirect to the backend form view uid = request.session.uid user = request.env["res.users"].sudo().browse(uid) diff --git a/webservice/readme/CONTRIBUTORS.rst b/webservice/readme/CONTRIBUTORS.rst index bfbf32a6..1e97bc2f 100644 --- a/webservice/readme/CONTRIBUTORS.rst +++ b/webservice/readme/CONTRIBUTORS.rst @@ -1,2 +1,3 @@ * Enric Tobella * Alexandre Fayolle +* Guillem Casassas diff --git a/webservice/tests/common.py b/webservice/tests/common.py index 54312c34..b951b6cf 100644 --- a/webservice/tests/common.py +++ b/webservice/tests/common.py @@ -10,25 +10,21 @@ @tagged("-at_install", "post_install") class CommonWebService(TransactionComponentCase): - @classmethod - def _setup_context(cls): + def _setup_context(self): return dict( - cls.env.context, tracking_disable=True, test_queue_job_no_delay=True, + self.env.context, tracking_disable=True, test_queue_job_no_delay=True, ) - @classmethod - def _setup_env(cls): - cls.env = cls.env(context=cls._setup_context()) + def _setup_env(self): + self.env = self.env(context=self._setup_context()) - @classmethod - def _setup_records(cls): + def _setup_records(self): pass - @classmethod - def setUpClass(cls): - super().setUpClass() - cls._setup_env() - cls._setup_records() + def setUp(self): + super(CommonWebService, self).setUp() + self._setup_env() + self._setup_records() @contextmanager diff --git a/webservice/tests/test_oauth2.py b/webservice/tests/test_oauth2.py index 2c538204..3d7f05b9 100644 --- a/webservice/tests/test_oauth2.py +++ b/webservice/tests/test_oauth2.py @@ -19,11 +19,10 @@ class TestWebServiceOauth2BackendApplication(CommonWebService): - @classmethod - def _setup_records(cls): + def _setup_records(self): - res = super()._setup_records() - cls.url = "https://localhost.demo.odoo/" + res = super(TestWebServiceOauth2BackendApplication, self)._setup_records() + self.url = "https://localhost.demo.odoo/" os.environ["SERVER_ENV_CONFIG"] = "\n".join( [ "[webservice_backend.test_oauth2_back]", @@ -31,23 +30,23 @@ def _setup_records(cls): "oauth2_flow = backend_application", "oauth2_clientid = some_client_id", "oauth2_client_secret = shh_secret", - f"oauth2_token_url = {cls.url}oauth2/token", - f"oauth2_audience = {cls.url}", + f"oauth2_token_url = {self.url}oauth2/token", + f"oauth2_audience = {self.url}", ] ) - cls.webservice = cls.env["webservice.backend"].create( + self.webservice = self.env["webservice.backend"].create( { "name": "WebService OAuth2", "tech_name": "test_oauth2_back", "auth_type": "oauth2", "protocol": "http", - "url": cls.url, + "url": self.url, "oauth2_flow": "backend_application", "content_type": "application/xml", "oauth2_clientid": "some_client_id", "oauth2_client_secret": "shh_secret", - "oauth2_token_url": f"{cls.url}oauth2/token", - "oauth2_audience": cls.url, + "oauth2_token_url": f"{self.url}oauth2/token", + "oauth2_audience": self.url, } ) return res @@ -74,7 +73,7 @@ def test_fetch_token(self): with mock_cursor(self.env.cr): result = self.webservice.call("get", url=f"{self.url}endpoint") - self.webservice.invalidate_recordset() + self.webservice.invalidate_cache() self.assertTrue("cool_token" in self.webservice.oauth2_token) self.assertEqual(result, b"OK") @@ -89,7 +88,7 @@ def test_update_token(self): "token_type": "Bearer", } ) - self.webservice.flush_model() + self.webservice.flush() expires_timestamp = time.time() + duration responses.add( @@ -108,7 +107,7 @@ def test_update_token(self): result = self.webservice.call("get", url=f"{self.url}endpoint") self.env.cr.commit.assert_called_once_with() # one call with no args - self.webservice.invalidate_recordset() + self.webservice.invalidate_cache() self.assertTrue("cool_token" in self.webservice.oauth2_token) self.assertEqual(result, b"OK") @@ -123,12 +122,12 @@ def test_update_token_with_error(self): "token_type": "Bearer", } ) - self.webservice.flush_model() + self.webservice.flush() responses.add( responses.POST, f"{self.url}oauth2/token", - json={"error": "invalid_grant", "error_description": "invalid grant",}, + json={"error": "invalid_grant", "error_description": "invalid grant"}, status=404, ) responses.add(responses.GET, f"{self.url}endpoint", body="NOK", status=403) @@ -139,15 +138,14 @@ def test_update_token_with_error(self): self.env.cr.commit.assert_not_called() self.env.cr.close.assert_called_once_with() # one call with no args - self.webservice.invalidate_recordset() + self.webservice.invalidate_cache() self.assertTrue("old_token" in self.webservice.oauth2_token) class TestWebServiceOauth2WebApplication(CommonWebService): - @classmethod - def _setup_records(cls): - res = super()._setup_records() - cls.url = "https://localhost.demo.odoo/" + def _setup_records(self): + res = super(TestWebServiceOauth2WebApplication, self)._setup_records() + self.url = "https://localhost.demo.odoo/" os.environ["SERVER_ENV_CONFIG"] = "\n".join( [ "[webservice_backend.test_oauth2_web]", @@ -155,25 +153,25 @@ def _setup_records(cls): "oauth2_flow = web_application", "oauth2_clientid = some_client_id", "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_token_url = {self.url}oauth2/token", + f"oauth2_audience = {self.url}", + f"oauth2_authorization_url = {self.url}/authorize", ] ) - cls.webservice = cls.env["webservice.backend"].create( + self.webservice = self.env["webservice.backend"].create( { "name": "WebService OAuth2", "tech_name": "test_oauth2_web", "auth_type": "oauth2", "protocol": "http", - "url": cls.url, + "url": self.url, "oauth2_flow": "web_application", "content_type": "application/xml", "oauth2_clientid": "some_client_id", "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_token_url": f"{self.url}oauth2/token", + "oauth2_audience": self.url, + "oauth2_authorization_url": f"{self.url}/authorize", } ) return res @@ -243,7 +241,7 @@ def test_oauth2_flow_compute_with_server_env(self): }, ): server_env_mixin.serv_config = server_env._load_config() # Reload vars - ws.invalidate_recordset() # Avoid reading from cache + ws.invalidate_cache() # Avoid reading from cache if auth_type == "oauth2": self.assertEqual(ws.oauth2_flow, oauth2_flow) else: diff --git a/webservice/tests/test_webservice.py b/webservice/tests/test_webservice.py index 30b718d8..2ac5699d 100644 --- a/webservice/tests/test_webservice.py +++ b/webservice/tests/test_webservice.py @@ -12,15 +12,14 @@ class TestWebService(CommonWebService): - @classmethod - def _setup_records(cls): - res = super()._setup_records() - cls.url = "http://localhost.demo.odoo/" - cls.webservice = cls.env["webservice.backend"].create( + def _setup_records(self): + res = super(TestWebService, self)._setup_records() + self.url = "https://localhost.demo.odoo/" + self.webservice = self.env["webservice.backend"].create( { "name": "WebService", "protocol": "http", - "url": cls.url, + "url": self.url, "content_type": "application/xml", "tech_name": "demo_ws", "auth_type": "none", @@ -39,9 +38,7 @@ def test_auth_validation(self): r"However, the following field\(s\) are not valued: Username, Password" ) with self.assertRaisesRegex(exceptions.UserError, msg): - self.webservice.write( - {"auth_type": "user_pwd",} - ) + self.webservice.write({"auth_type": "user_pwd"}) msg = ( r"Webservice 'WebService' " @@ -57,9 +54,7 @@ def test_auth_validation(self): r"However, the following field\(s\) are not valued: API Key, API Key header" ) with self.assertRaisesRegex(exceptions.UserError, msg): - self.webservice.write( - {"auth_type": "api_key",} - ) + self.webservice.write({"auth_type": "api_key"}) msg = ( r"Webservice 'WebService' " @@ -67,9 +62,7 @@ def test_auth_validation(self): r"However, the following field\(s\) are not valued: API Key header" ) with self.assertRaisesRegex(exceptions.UserError, msg): - self.webservice.write( - {"auth_type": "api_key", "api_key": "foo",} - ) + self.webservice.write({"auth_type": "api_key", "api_key": "foo"}) @responses.activate def test_web_service_get(self): @@ -96,7 +89,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(