Skip to content

Commit

Permalink
fixup! fixup! [FIX] webservice: WARNING message in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SilvioC2C committed Sep 5, 2024
1 parent cb396e1 commit a4bc9dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
16 changes: 7 additions & 9 deletions webservice/models/webservice_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class WebserviceBackend(models.Model):
("web_application", "Web Application (Authorization Code Grant)"),
],
readonly=False,
store=True,
)
oauth2_clientid = fields.Char(string="Client ID", auth_type="oauth2")
oauth2_client_secret = fields.Char(string="Client Secret", auth_type="oauth2")
Expand Down Expand Up @@ -121,14 +120,6 @@ def _get_adapter_protocol(self):
protocol += f"+{self.auth_type}-{self.oauth2_flow}"
return protocol

@api.onchange("auth_type")
def _onchange_oauth2_auth_type(self):
# reset the oauth2_flow when auth_type is not oauth2
# using a compute method interferes with the server environment mixin
for rec in self:
if rec.auth_type != "oauth2":
rec.oauth2_flow = False

@api.depends("auth_type", "oauth2_flow")
def _compute_redirect_url(self):
get_param = self.env["ir.config_parameter"].sudo().get_param
Expand Down Expand Up @@ -178,3 +169,10 @@ def _server_env_fields(self):
}
webservice_fields.update(base_fields)
return webservice_fields

def _compute_server_env(self):
# OVERRIDE: reset ``oauth2_flow`` when ``auth_type`` is not "oauth2", even if
# defined otherwise in server env vars
res = super(WebserviceBackend, self)._compute_server_env()
self.filtered(lambda r: r.auth_type != "oauth2").oauth2_flow = None
return res
31 changes: 31 additions & 0 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import responses
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError

from odoo.addons.server_environment import server_env
from odoo.addons.server_environment.models import server_env_mixin

from .common import CommonWebService, mock_cursor


Expand Down Expand Up @@ -213,3 +216,31 @@ def test_fetch_token_from_auth(self):
adapter = self.webservice._get_adapter()
token = adapter._fetch_token_from_authorization(code)
self.assertEqual("cool_token", token["access_token"])

def test_oauth2_flow_compute(self):
# Test with current configuration
env = os.environ
self.assertIn("auth_type = oauth2\n", env["SERVER_ENV_CONFIG"])
self.assertIn("oauth2_flow = web_application\n", env["SERVER_ENV_CONFIG"])
server_env_mixin.serv_config = server_env._load_config() # Reload env vars
self.webservice.invalidate_recordset() # Force recomputation when reading
self.assertEqual(self.webservice.oauth2_flow, "web_application")

# Update configuration: ``auth_type`` is changed to ``none``
env["SERVER_ENV_CONFIG"] = env["SERVER_ENV_CONFIG"].replace(
"auth_type = oauth2", "auth_type = none"
)
server_env_mixin.serv_config = server_env._load_config() # Reload env vars
self.webservice.invalidate_recordset() # Clear cache => read forces compute
self.assertFalse(self.webservice.oauth2_flow)

# Update configuration: ``auth_type`` is reverted to ``oauth2``,
# and ``oauth2_flow`` is updated to ``backend_application``
env["SERVER_ENV_CONFIG"] = env["SERVER_ENV_CONFIG"].replace(
"auth_type = none", "auth_type = oauth2"
).replace(
"oauth2_flow = web_application", "oauth2_flow = backend_application"
)
server_env_mixin.serv_config = server_env._load_config() # Reload env vars
self.webservice.invalidate_recordset() # Clear cache => read forces compute
self.assertEqual(self.webservice.oauth2_flow, "backend_application")

0 comments on commit a4bc9dc

Please sign in to comment.