Skip to content

Commit 97e1ea9

Browse files
committed
[IMP] webservice_server_env: move oauth2 UI test and add web dep
1 parent 8ab8423 commit 97e1ea9

3 files changed

Lines changed: 57 additions & 57 deletions

File tree

webservice/tests/test_oauth2.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import responses
99
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
1010

11-
from odoo.tests import Form
12-
1311
from .common import CommonWebService, mock_cursor
1412

1513

@@ -217,57 +215,3 @@ def test_fetch_token_from_auth(self):
217215
json.loads(responses.calls[0].response.content.decode())["access_token"],
218216
)
219217
self.assertEqual("cool_token", token["access_token"])
220-
221-
def test_oauth2_flow_compute_with_ui(self):
222-
"""Check the ``compute`` method when updating WS from UI"""
223-
ws = self.webservice
224-
url = self.url
225-
form_xmlid = "webservice.webservice_backend_form_view"
226-
for auth_type, oauth2_flow in [
227-
(tp, fl)
228-
for tp in ws._fields["auth_type"].get_values(ws.env)
229-
for fl in ws._fields["oauth2_flow"].get_values(ws.env)
230-
]:
231-
next_ws_id = ws.sudo().search([], order="id desc", limit=1).id + 1
232-
# Create a new WS with each ``auth_type/oauth2_flow`` couple through UI
233-
with Form(ws.browse(), form_xmlid) as ws_form:
234-
# Common fields
235-
ws_form.name = "WebService Test UI"
236-
ws_form.tech_name = f"webservice_test_ui_{next_ws_id}"
237-
ws_form.protocol = "http"
238-
ws_form.url = url
239-
ws_form.content_type = "application/xml"
240-
ws_form.auth_type = auth_type
241-
# Auth type specific fields
242-
if auth_type == "api_key":
243-
ws_form.api_key = "Test Api Key"
244-
ws_form.api_key_header = "Test Api Key Header"
245-
if auth_type == "oauth2":
246-
ws_form.oauth2_flow = oauth2_flow
247-
ws_form.oauth2_clientid = "Test Client ID"
248-
ws_form.oauth2_client_secret = "Test Client Secret"
249-
ws_form.oauth2_token_url = f"{url}oauth2/token"
250-
if auth_type == "user_pwd":
251-
ws_form.username = "Test Username"
252-
ws_form.password = "Test Password"
253-
ws = ws_form.save()
254-
# Check that ``oauth2_flow`` is the expected one after creation only if the
255-
# ``auth_type`` is "oauth2", else it should be False
256-
self.assertEqual(
257-
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
258-
)
259-
# Change WS's ``auth_type`` through UI
260-
with Form(ws, form_xmlid) as ws_form:
261-
new_auth_type = "none" if ws.auth_type == "oauth2" else "oauth2"
262-
ws_form.auth_type = new_auth_type
263-
if new_auth_type == "oauth2":
264-
ws_form.oauth2_flow = oauth2_flow
265-
ws_form.oauth2_clientid = "Test Client ID"
266-
ws_form.oauth2_client_secret = "Test Client Secret"
267-
ws_form.oauth2_token_url = f"{url}oauth2/token"
268-
ws = ws_form.save()
269-
# Check that ``oauth2_flow`` is the expected one after update only if the
270-
# ``auth_type`` is "oauth2", else it should be False
271-
self.assertEqual(
272-
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
273-
)

webservice_server_env/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"maintainers": ["etobella"],
1414
"author": "Creu Blanca, Camptocamp, Odoo Community Association (OCA)",
1515
"website": "https://github.com/OCA/web-api",
16-
"depends": ["webservice", "server_environment"],
16+
"depends": ["web", "webservice", "server_environment"],
1717
"auto_install": True,
1818
}

webservice_server_env/tests/test_oauth2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os
55
from unittest import mock
66

7+
from odoo.tests import Form
8+
79
from odoo.addons.server_environment import server_env
810
from odoo.addons.server_environment.models import server_env_mixin
911
from odoo.addons.webservice.tests.common import CommonWebService
@@ -75,3 +77,57 @@ def test_oauth2_flow_compute_with_server_env(self):
7577
self.assertEqual(ws.oauth2_flow, oauth2_flow)
7678
else:
7779
self.assertFalse(ws.oauth2_flow)
80+
81+
def test_oauth2_flow_compute_with_ui(self):
82+
"""Check the ``compute`` method when updating WS from UI"""
83+
ws = self.webservice
84+
url = self.url
85+
form_xmlid = "webservice.webservice_backend_form_view"
86+
for auth_type, oauth2_flow in [
87+
(tp, fl)
88+
for tp in ws._fields["auth_type"].get_values(ws.env)
89+
for fl in ws._fields["oauth2_flow"].get_values(ws.env)
90+
]:
91+
next_ws_id = ws.sudo().search([], order="id desc", limit=1).id + 1
92+
# Create a new WS with each ``auth_type/oauth2_flow`` couple through UI
93+
with Form(ws.browse(), form_xmlid) as ws_form:
94+
# Common fields
95+
ws_form.name = "WebService Test UI"
96+
ws_form.tech_name = f"webservice_test_ui_{next_ws_id}"
97+
ws_form.protocol = "http"
98+
ws_form.url = url
99+
ws_form.content_type = "application/xml"
100+
ws_form.auth_type = auth_type
101+
# Auth type specific fields
102+
if auth_type == "api_key":
103+
ws_form.api_key = "Test Api Key"
104+
ws_form.api_key_header = "Test Api Key Header"
105+
if auth_type == "oauth2":
106+
ws_form.oauth2_flow = oauth2_flow
107+
ws_form.oauth2_clientid = "Test Client ID"
108+
ws_form.oauth2_client_secret = "Test Client Secret"
109+
ws_form.oauth2_token_url = f"{url}oauth2/token"
110+
if auth_type == "user_pwd":
111+
ws_form.username = "Test Username"
112+
ws_form.password = "Test Password"
113+
ws = ws_form.save()
114+
# Check that ``oauth2_flow`` is the expected one after creation only if the
115+
# ``auth_type`` is "oauth2", else it should be False
116+
self.assertEqual(
117+
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
118+
)
119+
# Change WS's ``auth_type`` through UI
120+
with Form(ws, form_xmlid) as ws_form:
121+
new_auth_type = "none" if ws.auth_type == "oauth2" else "oauth2"
122+
ws_form.auth_type = new_auth_type
123+
if new_auth_type == "oauth2":
124+
ws_form.oauth2_flow = oauth2_flow
125+
ws_form.oauth2_clientid = "Test Client ID"
126+
ws_form.oauth2_client_secret = "Test Client Secret"
127+
ws_form.oauth2_token_url = f"{url}oauth2/token"
128+
ws = ws_form.save()
129+
# Check that ``oauth2_flow`` is the expected one after update only if the
130+
# ``auth_type`` is "oauth2", else it should be False
131+
self.assertEqual(
132+
ws.oauth2_flow, oauth2_flow if ws.auth_type == "oauth2" else False
133+
)

0 commit comments

Comments
 (0)