Files
odoo_source/addons/bus/tests/test_websocket_controller.py
T
tsm-odoo b5896c9d03 [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>
2023-07-31 23:21:10 +02:00

71 lines
2.6 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import json
from odoo.tests import common, JsonRpcException
class TestWebsocketController(common.HttpCase):
def test_websocket_peek(self):
result = self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': True,
})
# Response containing channels/notifications is retrieved and is
# conform to excpectations.
self.assertIsNotNone(result)
channels = result.get('channels')
self.assertIsNotNone(channels)
self.assertIsInstance(channels, list)
notifications = result.get('notifications')
self.assertIsNotNone(notifications)
self.assertIsInstance(notifications, list)
result = self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': False,
})
# Reponse is received as long as the session is valid.
self.assertIsNotNone(result)
def test_websocket_peek_session_expired_login(self):
session = self.authenticate(None, None)
# first rpc should be fine
self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': True,
})
self.authenticate('admin', 'admin')
# rpc with outdated session should lead to error.
headers = {'Cookie': f'session_id={session.sid};'}
with self.assertRaises(JsonRpcException, msg='odoo.http.SessionExpiredException'):
self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': False,
}, headers=headers)
def test_websocket_peek_session_expired_logout(self):
session = self.authenticate('demo', 'demo')
# first rpc should be fine
self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': True,
})
self.url_open('/web/session/logout')
# rpc with outdated session should lead to error.
headers = {'Cookie': f'session_id={session.sid};'}
with self.assertRaises(JsonRpcException, msg='odoo.http.SessionExpiredException'):
self.make_jsonrpc_request('/websocket/peek_notifications', {
'channels': [],
'last': 0,
'is_first_poll': False,
}, headers=headers)