|
4 | 4 | import os |
5 | 5 | from unittest import mock |
6 | 6 |
|
| 7 | +from odoo.tests import Form |
| 8 | + |
7 | 9 | from odoo.addons.server_environment import server_env |
8 | 10 | from odoo.addons.server_environment.models import server_env_mixin |
9 | 11 | from odoo.addons.webservice.tests.common import CommonWebService |
@@ -75,3 +77,57 @@ def test_oauth2_flow_compute_with_server_env(self): |
75 | 77 | self.assertEqual(ws.oauth2_flow, oauth2_flow) |
76 | 78 | else: |
77 | 79 | 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