Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] webservice: WARNING message in logs #47

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be just super()

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")
Loading