Skip to content

Commit

Permalink
[FIX] webservice: WARNING message in logs
Browse files Browse the repository at this point in the history
The use of a compute method on ``oauth2_flow`` when this field is touched by
the server environment mixin causes it to be defined twice as computed,
with differents settings, and this ultimately causes a warning message
in the logs:

```
WARNING odoo odoo.modules.registry: webservice.backend: inconsistent 'compute_sudo' for computed fields: protocol, url, auth_type, username, password, api_key, api_key_header, oauth2_flow, oauth2_clientid, oauth2_client_secret, oauth2_token_url, oauth2_authorization_url, oauth2_audience, oauth2_scope, content_type
```

We fix this by overriding method ``compute_server_env()``
  • Loading branch information
gurneyalex authored and SilvioC2C committed Sep 6, 2024
1 parent 4f49a82 commit b7f79b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
15 changes: 7 additions & 8 deletions webservice/models/webservice_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class WebserviceBackend(models.Model):
("web_application", "Web Application (Authorization Code Grant)"),
],
readonly=False,
store=True,
compute="_compute_oauth2_flow",
)
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 @@ -122,12 +120,6 @@ def _get_adapter_protocol(self):
protocol += f"+{self.auth_type}-{self.oauth2_flow}"
return protocol

@api.depends("auth_type")
def _compute_oauth2_flow(self):
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 @@ -177,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
33 changes: 33 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,33 @@ 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 b7f79b9

Please sign in to comment.