Skip to content

Commit

Permalink
[BKP][ADD]webservice
Browse files Browse the repository at this point in the history
Backport all changes done in v16: https://github.com/OCA/web-api/tree/16.0/webservice
  • Loading branch information
GuillemCForgeFlow committed Nov 7, 2024
1 parent bf44258 commit b310399
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 65 deletions.
2 changes: 1 addition & 1 deletion webservice/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
4 changes: 1 addition & 3 deletions webservice/components/request_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Check warning on line 174 in webservice/components/request_adapter.py

View check run for this annotation

Codecov / codecov/patch

webservice/components/request_adapter.py#L170-L174

Added lines #L170 - L174 were not covered by tests
# pylint: disable=E8106
Expand Down
4 changes: 1 addition & 3 deletions webservice/controllers/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Check warning on line 38 in webservice/controllers/oauth2.py

View check run for this annotation

Codecov / codecov/patch

webservice/controllers/oauth2.py#L37-L38

Added lines #L37 - L38 were not covered by tests
# after saving the token, redirect to the backend form view
uid = request.session.uid
user = request.env["res.users"].sudo().browse(uid)
Expand Down
1 change: 1 addition & 0 deletions webservice/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Enric Tobella <[email protected]>
* Alexandre Fayolle <[email protected]>
* Guillem Casassas <[email protected]>
22 changes: 9 additions & 13 deletions webservice/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 27 additions & 29 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,34 @@


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]",
"auth_type = oauth2",
"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
Expand All @@ -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")

Expand All @@ -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(
Expand All @@ -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")

Expand All @@ -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)
Expand All @@ -139,41 +138,40 @@ 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]",
"auth_type = oauth2",
"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
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 9 additions & 16 deletions webservice/tests/test_webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 18 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L15-L18

Added lines #L15 - L18 were not covered by tests
{
"name": "WebService",
"protocol": "http",
"url": cls.url,
"url": self.url,
"content_type": "application/xml",
"tech_name": "demo_ws",
"auth_type": "none",
Expand All @@ -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"})

Check warning on line 41 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L40-L41

Added lines #L40 - L41 were not covered by tests

msg = (

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

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L43

Added line #L43 was not covered by tests
r"Webservice 'WebService' "
Expand All @@ -57,19 +54,15 @@ 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"})

Check warning on line 57 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L56-L57

Added lines #L56 - L57 were not covered by tests

msg = (

Check warning on line 59 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L59

Added line #L59 was not covered by tests
r"Webservice 'WebService' "
r"requires 'API Key' authentication. "
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"})

Check warning on line 65 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L64-L65

Added lines #L64 - L65 were not covered by tests

@responses.activate

Check warning on line 67 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L67

Added line #L67 was not covered by tests
def test_web_service_get(self):
Expand All @@ -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(

Check warning on line 95 in webservice/tests/test_webservice.py

View check run for this annotation

Codecov / codecov/patch

webservice/tests/test_webservice.py#L90-L95

Added lines #L90 - L95 were not covered by tests
Expand Down

0 comments on commit b310399

Please sign in to comment.