Skip to content

Commit

Permalink
fixup! [FIX] webservice: WARNING message in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yankinmax committed Aug 7, 2024
1 parent 3be2570 commit 7f5236e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 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
48 changes: 32 additions & 16 deletions webservice/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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=",
}
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 7f5236e

Please sign in to comment.