Skip to content
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
25 changes: 25 additions & 0 deletions webservice/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError

from odoo import exceptions
from odoo.tests import Form

from .common import CommonWebService, mock_cursor

Expand Down Expand Up @@ -375,6 +376,30 @@ def test_fetch_token_from_auth(self):
)
self.assertEqual("cool_token", token["access_token"])

def test_web_application_required_fields_with_ui(self):
"""The authorization URL and the scope are required for the web
application flow, the authorization redirect needs them."""
ws_form = Form(
self.webservice.browse(), "webservice.webservice_backend_form_view"
)
ws_form.name = "WebService Test UI"
ws_form.tech_name = "webservice_test_ui_web_application"
ws_form.protocol = "http"
ws_form.url = self.url
ws_form.auth_type = "oauth2"
ws_form.oauth2_flow = "web_application"
ws_form.oauth2_clientid = "Test Client ID"
ws_form.oauth2_client_secret = "Test Client Secret"
ws_form.oauth2_token_url = f"{self.url}oauth2/token"
with self.assertRaisesRegex(AssertionError, "oauth2_scope"):
ws_form.save()
ws_form.oauth2_scope = "Test Scope"
with self.assertRaisesRegex(AssertionError, "oauth2_authorization_url"):
ws_form.save()
ws_form.oauth2_authorization_url = f"{self.url}oauth2/auth"
ws = ws_form.save()
self.assertEqual(ws.oauth2_authorization_url, f"{self.url}oauth2/auth")


class TestWebServiceOauth2FlowReset(CommonWebService):
"""``oauth2_flow`` must be reset on any write, not only via the UI.
Expand Down
4 changes: 2 additions & 2 deletions webservice/views/webservice_backend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
<field
name="oauth2_scope"
invisible="auth_type != 'oauth2' or oauth2_flow != 'web_application'"
required="auth_type == 'oauth2' and oauth2_flow == 'authorization_code'"
required="auth_type == 'oauth2' and oauth2_flow == 'web_application'"
/>
<field
name="oauth2_authorization_url"
invisible="auth_type != 'oauth2' or oauth2_flow != 'web_application'"
required="auth_type == 'oauth2' and oauth2_flow == 'authorization_code'"
required="auth_type == 'oauth2' and oauth2_flow == 'web_application'"
/>
<field
name="oauth2_token_url"
Expand Down
6 changes: 6 additions & 0 deletions webservice_server_env/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def test_oauth2_flow_compute_with_ui(self):
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 oauth2_flow == "web_application":
ws_form.oauth2_scope = "Test Scope"
ws_form.oauth2_authorization_url = f"{url}oauth2/auth"
if auth_type == "user_pwd":
ws_form.username = "Test Username"
ws_form.password = "Test Password"
Expand All @@ -131,6 +134,9 @@ def test_oauth2_flow_compute_with_ui(self):
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 oauth2_flow == "web_application":
ws_form.oauth2_scope = "Test Scope"
ws_form.oauth2_authorization_url = f"{url}oauth2/auth"
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
Expand Down
Loading