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
SilvioC2C committed Sep 12, 2024
1 parent b7f79b9 commit 6dabd31
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
2 changes: 1 addition & 1 deletion webservice/models/webservice_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ def _server_env_fields(self):
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()
res = super()._compute_server_env()
self.filtered(lambda r: r.auth_type != "oauth2").oauth2_flow = None
return res
115 changes: 87 additions & 28 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import json
import os
import time
from unittest import mock
from urllib.parse import quote

import responses
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError

from odoo.tests.common import Form

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

Expand Down Expand Up @@ -217,32 +220,88 @@ def test_fetch_token_from_auth(self):
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"
def test_oauth2_flow_compute_with_server_env(self):
"""Check the ``compute`` method when updating server envs"""
ws = self.webservice
url = self.url
for auth_type, oauth2_flow in [
(tp, fl)
for tp in ws._fields["auth_type"].get_values(ws.env)
for fl in ws._fields["oauth2_flow"].get_values(ws.env)
]:
# Update env with current ``auth_type`` and ``oauth2_flow``
with mock.patch.dict(
os.environ,
{
"SERVER_ENV_CONFIG": f"""
[webservice_backend.test_oauth2_web]
auth_type = {auth_type}
oauth2_flow = {oauth2_flow}
oauth2_clientid = some_client_id
oauth2_client_secret = shh_secret
oauth2_token_url = {url}oauth2/token
oauth2_audience = {url}
oauth2_authorization_url = {url}/authorize
""",
},
):
server_env_mixin.serv_config = server_env._load_config() # Reload vars
ws.invalidate_recordset() # Avoid reading from cache
if auth_type == "oauth2":
self.assertEqual(ws.oauth2_flow, oauth2_flow)
else:
self.assertFalse(ws.oauth2_flow)

def test_oauth2_flow_compute_with_ui(self):
"""Check the ``compute`` method when updating WS from UI"""
ws = self.webservice
url = self.url
form_xmlid = "webservice.webservice_backend_form_view"
for auth_type, oauth2_flow in [
(tp, fl)
for tp in ws._fields["auth_type"].get_values(ws.env)
for fl in ws._fields["oauth2_flow"].get_values(ws.env)
]:
next_ws_id = ws.sudo().search([], order="id desc", limit=1).id + 1
# Create a new WS with each ``auth_type/oauth2_flow`` couple through UI
with Form(ws.browse(), form_xmlid) as ws_form:
# Common fields
ws_form.name = "WebService Test UI"
ws_form.tech_name = f"webservice_test_ui_{next_ws_id}"
ws_form.protocol = "http"
ws_form.url = url
ws_form.content_type = "application/xml"
ws_form.auth_type = auth_type
# Auth type specific fields
if auth_type == "api_key":
ws_form.api_key = "Test Api Key"
ws_form.api_key_header = "Test Api Key Header"
if auth_type == "oauth2":
ws_form.oauth2_flow = oauth2_flow
ws_form.oauth2_clientid = "Test Client ID"
ws_form.oauth2_client_secret = "Test Client Secret"
ws_form.oauth2_token_url = f"{url}oauth2/token"
if auth_type == "user_pwd":
ws_form.username = "Test Username"
ws_form.password = "Test Password"
ws = ws_form.save()
# Check that ``oauth2_flow`` is the expected one after creation only if the
# ``auth_type`` is "oauth2", else it should be False
self.assertEqual(
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
)
# Change WS's ``auth_type`` through UI
with Form(ws, form_xmlid) as ws_form:
new_auth_type = "none" if ws.auth_type == "oauth2" else "oauth2"
ws_form.auth_type = new_auth_type
if new_auth_type == "oauth2":
ws_form.oauth2_flow = oauth2_flow
ws_form.oauth2_clientid = "Test Client ID"
ws_form.oauth2_client_secret = "Test Client Secret"
ws_form.oauth2_token_url = f"{url}oauth2/token"
ws = ws_form.save()
# Check that ``oauth2_flow`` is the expected one after update only if the
# ``auth_type`` is "oauth2", else it should be False
self.assertEqual(
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
)
)
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 6dabd31

Please sign in to comment.