Skip to content

Commit

Permalink
[MIG] webservice: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SilvioC2C committed Sep 17, 2024
1 parent c750c9d commit 6f28230
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 53 deletions.
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
responses
6 changes: 2 additions & 4 deletions webservice/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
# @author Simone Orsi <simahawk@gmail.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


{
"name": "WebService",
"summary": """
Defines webservice abstract definition to be used generally""",
"version": "16.0.1.4.0",
"summary": """Defines webservice abstract definition to be used generally""",
"version": "17.0.1.0.0",
"license": "AGPL-3",
"development_status": "Production/Stable",
"maintainers": ["etobella"],
Expand Down
1 change: 1 addition & 0 deletions webservice/components/base_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BaseWebServiceAdapter(AbstractComponent):
@classmethod
def _component_match(cls, work, usage=None, model_name=None, **kw):
"""Override to customize match.
Registry lookup filtered by usage and model_name when landing here.
Now, narrow match to `_match_attrs` attributes.
"""
Expand Down
2 changes: 1 addition & 1 deletion webservice/controllers/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def redirect(self, backend_id, **params):
user = request.env["res.users"].sudo().browse(uid)
cids = request.httprequest.cookies.get("cids", str(user.company_id.id))
cids = [int(cid) for cid in cids.split(",")]
record_action = backend.get_access_action()
record_action = backend._get_access_action()
url_params = {
"model": backend._name,
"id": backend.id,
Expand Down
6 changes: 3 additions & 3 deletions webservice/security/ir_rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<record id="rule_webservice_backend_multi_company" model="ir.rule">
<field name="name">webservice_backend multi-company</field>
<field name="model_id" ref="model_webservice_backend" />
<field
name="domain_force"
>['|',('company_id','=',False),('company_id', 'in', company_ids)]</field>
<field name="domain_force">
['|', ('company_id','=',False), ('company_id', 'in', company_ids)]
</field>
</record>
</odoo>
3 changes: 2 additions & 1 deletion webservice/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_webservice, test_oauth2
from . import test_oauth2
from . import test_webservice
45 changes: 33 additions & 12 deletions webservice/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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, Session

from odoo.tests.common import tagged

Expand All @@ -28,22 +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 urlparse(r.url).netloc in ("localhost.demo.odoo", "custom.url"):
return cls._super_send(s, r)
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
10 changes: 5 additions & 5 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,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 @@ -159,7 +159,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 @@ -175,7 +175,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 @@ -189,7 +189,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
48 changes: 23 additions & 25 deletions webservice/views/webservice_backend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
type="object"
name="button_authorize"
string="OAuth Authorize"
attrs="{'invisible': ['|', ('auth_type', '!=', 'oauth2'), ('oauth2_flow', '!=', 'web_application')]}"
invisible="auth_type != 'oauth2' or oauth2_flow != 'web_application'"
/>
</header>
<sheet>
Expand All @@ -37,65 +37,63 @@
<field name="auth_type" />
<field
name="username"
attrs="{
'invisible': [('auth_type', '!=', 'user_pwd')],
'required': [('auth_type', '=', 'user_pwd')],
}"
invisible="auth_type != 'user_pwd'"
required="auth_type == 'user_pwd'"
/>
<field
name="password"
attrs="{
'invisible': [('auth_type', '!=', 'user_pwd')],
'required': [('auth_type', '=', 'user_pwd')],
}"
invisible="auth_type != 'user_pwd'"
required="auth_type == 'user_pwd'"
password="True"
/>
<field
name="api_key"
attrs="{
'invisible': [('auth_type', '!=', 'api_key')],
'required': [('auth_type', '=', 'api_key')],
}"
invisible="auth_type != 'api_key'"
required="auth_type == 'api_key'"
password="True"
/>
<field
name="api_key_header"
attrs="{
'invisible': [('auth_type', '!=', 'api_key')],
'required': [('auth_type', '=', 'api_key')],
}"
invisible="auth_type != 'api_key'"
required="auth_type == 'api_key'"
/>
<field
name="oauth2_flow"
attrs="{'invisible': [('auth_type', '!=', 'oauth2')], 'required': [('auth_type', '=', 'oauth2')]}"
invisible="auth_type != 'oauth2'"
required="auth_type == 'oauth2'"
/>
<field
name="redirect_url"
attrs="{'invisible': [('auth_type', '!=', 'oauth2'), ('oauth2_flow', '!=', 'web_application')]}"
invisible="auth_type != 'oauth2' and oauth2_flow != 'web_application'"
/>
<field
name="oauth2_clientid"
attrs="{'invisible': [('auth_type', '!=', 'oauth2')], 'required': [('auth_type', '=', 'oauth2')]}"
invisible="auth_type != 'oauth2'"
required="auth_type == 'oauth2'"
/>
<field
name="oauth2_client_secret"
attrs="{'invisible': [('auth_type', '!=', 'oauth2')], 'required': [('auth_type', '=', 'oauth2')]}"
invisible="auth_type != 'oauth2'"
required="auth_type == 'oauth2'"
/>
<field
name="oauth2_scope"
attrs="{'invisible': [('auth_type', '!=', 'oauth2'), ('oauth2_flow', '!=', 'web_application')], 'required': [('auth_type', '=', 'oauth2'), ('oauth2_flow', '=', 'authorization_code')]}"
invisible="auth_type != 'oauth2' and oauth2_flow != 'web_application'"
required="auth_type == 'oauth2' and oauth2_flow == 'authorization_code'"
/>
<field
name="oauth2_authorization_url"
attrs="{'invisible': [('auth_type', '!=', 'oauth2'), ('oauth2_flow', '!=', 'web_application')], 'required': [('auth_type', '=', 'oauth2'), ('oauth2_flow', '=', 'authorization_code')]}"
invisible="auth_type != 'oauth2' and oauth2_flow != 'web_application'"
required="auth_type == 'oauth2' and oauth2_flow == 'authorization_code'"
/>
<field
name="oauth2_token_url"
attrs="{'invisible': [('auth_type', '!=', 'oauth2')], 'required': [('auth_type', '=', 'oauth2')]}"
invisible="auth_type != 'oauth2'"
required="auth_type == 'oauth2'"
/>
<field
name="oauth2_audience"
attrs="{'invisible': [('auth_type', '!=', 'oauth2')]}"
invisible="auth_type != 'oauth2'"
/>
</group>
</sheet>
Expand Down

0 comments on commit 6f28230

Please sign in to comment.