[IMP] *: add generic make_jsonrpc method in tests
*: bus, crm_livechat, crm_mail_plugin, im_livechat, payment, payment_demo, pos_online_payment, test_discuss_full, test_mail_full, tests, website_livechat, website_sale. Several python tests have their own way to make jsonrpc requests. This PR adds a generic `make_jsonrpc_request` method to the `HttpCase` in order to provide a generic way to do so. At the same time, calls to `_open_livechat_channel` are removed in favor of jsonrpc request to `get_session`. This makes the tests more realistics (some incoherences were present like passing `country_id` to the open channel method while the user country id is not set...) Finally, this will ease the diff in the PR introducing livechat visitors as mail guests. part of task-3332628 closes odoo/odoo#130036 Related: odoo/enterprise#44840 Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
@@ -1610,6 +1610,12 @@ class Transport(xmlrpclib.Transport):
|
||||
return super().request(*args, **kwargs)
|
||||
|
||||
|
||||
class JsonRpcException(Exception):
|
||||
def __init__(self, code, message):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
|
||||
|
||||
class HttpCase(TransactionCase):
|
||||
""" Transactional HTTP TestCase with url_open and Chrome headless helpers. """
|
||||
registry_test_mode = True
|
||||
@@ -1812,6 +1818,34 @@ class HttpCase(TransactionCase):
|
||||
return sup.profile(description=request.httprequest.full_path)
|
||||
return profiler.Nested(_profiler, patch('odoo.http.Request._get_profiler_context_manager', route_profiler))
|
||||
|
||||
def make_jsonrpc_request(self, route, params=None, headers=None):
|
||||
"""Make a JSON-RPC request to the server.
|
||||
|
||||
:param str route: the route to request
|
||||
:param dict params: the parameters to send
|
||||
:raises requests.HTTPError: if one occurred
|
||||
:raises JsonRpcException: if the response contains an error
|
||||
:return: The 'result' key from the response if any.
|
||||
"""
|
||||
data = json.dumps({
|
||||
'id': 0,
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'call',
|
||||
'params': params,
|
||||
}).encode()
|
||||
headers = headers or {}
|
||||
headers['Content-Type'] = 'application/json'
|
||||
response = self.url_open(route, data, headers=headers)
|
||||
response.raise_for_status()
|
||||
decoded_response = response.json()
|
||||
if 'result' in decoded_response:
|
||||
return decoded_response['result']
|
||||
if 'error' in decoded_response:
|
||||
raise JsonRpcException(
|
||||
code=decoded_response['error']['code'],
|
||||
message=decoded_response['error']['data']['name']
|
||||
)
|
||||
|
||||
|
||||
def no_retry(arg):
|
||||
"""Disable auto retry on decorated test method or test class"""
|
||||
|
||||
Reference in New Issue
Block a user