def test_notify_with_action(self): bus_bus = self.env['bus.bus'] domain = [ ('channel', '=', json_dump(self.env.user.notify_info_channel_name)) ] existing = bus_bus.search(domain) self.env.user.notify_info( message='message', title='title', sticky=True, action={ "type": "ir.actions.act_window", "view_mode": "form", }, action_link_name="Open" ) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) # the action should be transformed by Odoo (clean_action) expected = ('{"message":"message","sticky":true,"title":"title",' '"show_reload":false,"action":' '{"type": "ir.actions.act_window", "view_mode":"form",' '"flags":{},"views":[[false, "form"]]},' '"action_link_name":"Open"}') self.assertDictEqual( json.loads(expected), json.loads(news.message) )
def assertBusNotification(self, channels, message_dicts=None, init=True): """ Check for bus notifications. Basic check is about used channels. Verifying content is optional. :param channels: list of channel :param messages: if given, list of message making a valid pair (channel, message) to be found in bus.bus """ if init: self.assertEqual(len(self.env['bus.bus'].search([])), len(channels)) notifications = self.env['bus.bus'].search([('channel', 'in', [json_dump(channel) for channel in channels])]) self.assertEqual(len(notifications), len(channels)) if message_dicts: notif_messages = [json.loads(n.message) for n in notifications] for expected in message_dicts: found = False for returned in notif_messages: for key, val in expected.items(): if key not in returned: continue if isinstance(returned[key], list): if set(returned[key]) != set(val): continue else: if returned[key] != val: continue found = True break if found: break if not found: raise AssertionError("Bus notification content %s not found" % (repr(expected)))
def assertBusNotifications(self, channels, message_items=None, check_unique=True): """ Check bus notifications content. Mandatory and basic check is about channels being notified. Content check is optional. EXPECTED :param channels: list of expected bus channels, like [ (self.cr.dbname, 'mail.channel', self.channel_1.id), (self.cr.dbname, 'res.partner', self.partner_employee_2.id) ] :param message_items: if given, list of expected message making a valid pair (channel, message) to be found in bus.bus, like [ {'type': 'message_notification_update', 'elements': {self.msg.id: { 'message_id': self.msg.id, 'message_type': 'sms', 'notifications': {...}, ... }} }, {...}] """ bus_notifs = self.env['bus.bus'].sudo().search([ ('channel', 'in', [json_dump(channel) for channel in channels]) ]) if check_unique: self.assertEqual(len(bus_notifs), len(channels)) self.assertEqual(set(bus_notifs.mapped('channel')), set([json_dump(channel) for channel in channels])) notif_messages = [n.message for n in bus_notifs] for expected in message_items or []: for notification in notif_messages: if json_dump(expected) == notification: break else: raise AssertionError( 'No notification was found with the expected value.\nExpected:\n%s\nReturned:\n%s' % (json_dump(expected), '\n'.join( [n for n in notif_messages]))) return bus_notifs
def test_notify_warning(self): bus_bus = self.env['bus.bus'] domain = [('channel', '=', json_dump(self.env.user.notify_warning_channel_name))] existing = bus_bus.search(domain) test_msg = {'message': 'message', 'title': 'title', 'sticky': True} self.env.user.notify_warning(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) self.assertEqual(test_msg, json.loads(news.message))
def test_notify_default(self): bus_bus = self.env["bus.bus"] domain = [("channel", "=", json_dump(self.env.user.notify_default_channel_name))] existing = bus_bus.search(domain) test_msg = {"message": "message", "title": "title", "sticky": True} self.env.user.notify_default(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": DEFAULT}) self.assertDictEqual(test_msg, json.loads(news.message))
def assertBusNotification(self, channels, message_items=None, init=True): """ Check for bus notifications. Basic check is about used channels. Verifying content is optional. :param channels: list of channel :param message_items: if given, list of message making a valid pair (channel, message) to be found in bus.bus """ def check_content(returned_value, expected_value): if isinstance(expected_value, list): done = [] for expected_item in expected_value: for returned_item in returned_value: if check_content(returned_item, expected_item): done.append(expected_item) break else: return False return len(done) == len(expected_value) elif isinstance(expected_value, dict): return all(k in returned_value for k in expected_value.keys()) and all( check_content(returned_value[key], val) for key, val in expected_value.items()) else: return returned_value == expected_value if init: self.assertEqual(len(self.env['bus.bus'].search([])), len(channels)) notifications = self.env['bus.bus'].search([ ('channel', 'in', [json_dump(channel) for channel in channels]) ]) notif_messages = [json.loads(n.message) for n in notifications] self.assertEqual(len(notifications), len(channels)) for expected in message_items or []: for notification in notif_messages: found_keys, not_found_keys = [], [] if not all(k in notification for k in expected.keys()): continue for expected_key, expected_value in expected.items(): done = check_content(notification[expected_key], expected_value) if done: found_keys.append(expected_key) else: not_found_keys.append(expected_key) if set(found_keys) == set(expected.keys()): break else: raise AssertionError( 'Keys %s not found (expected: %s - returned: %s)' % (not_found_keys, repr(expected), repr(notif_messages)))
def test_notify_warning(self): bus_bus = self.env['bus.bus'] domain = [('channel', '=', json_dump(self.env.user.notify_warning_channel_name))] existing = bus_bus.search(domain) self.env.user.notify_warning(message='message', title='title', sticky=True) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) self.assertEqual('{"message":"message","sticky":true,"title":"title"}', news.message)
def test_notify_warning(self): bus_bus = self.env['bus.bus'] domain = [ ('channel', '=', json_dump(self.env.user.notify_warning_channel_name)) ] existing = bus_bus.search(domain) self.env.user.notify_warning( message='message', title='title', sticky=True) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) self.assertEqual( '{"message":"message","sticky":true,"title":"title"}', news.message)
def test_notify_warning(self): bus_bus = self.env['bus.bus'] domain = [ ('channel', '=', json_dump(self.env.user.notify_warning_channel_name)) ] existing = bus_bus.search(domain) self.env.user.notify_warning( message='message', title='title', sticky=True, show_reload=True, foo="bar" ) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) expected = ('{"message":"message","sticky":true,"title":"title",' '"show_reload":true,"action":null,' '"action_link_name":null,"foo":"bar"}') self.assertDictEqual( json.loads(expected), json.loads(news.message) )
def assertBusNotifications(self, channels, message_items=None, check_unique=True): """ Check bus notifications content. Mandatory and basic check is about channels being notified. Content check is optional. EXPECTED :param channels: list of expected bus channels, like [ (self.cr.dbname, 'mail.channel', self.channel_1.id), (self.cr.dbname, 'res.partner', self.partner_employee_2.id) ] :param message_items: if given, list of expected message making a valid pair (channel, message) to be found in bus.bus, like [ {'type': 'sms_update', 'elements': [{ 'message_id': self.msg.id, 'failure_type': 'sms', 'notifications': {'%s' % self.partner_1.id: ['sent', self.partner_1.name], '%s' % self.partner_2.id: ['sent', self.partner_2.name]} }] }, {...}] """ def check_content(returned_value, expected_value): if isinstance(expected_value, list): done = [] for expected_item in expected_value: for returned_item in returned_value: if check_content(returned_item, expected_item): done.append(expected_item) break else: return False return len(done) == len(expected_value) elif isinstance(expected_value, dict): return all(k in returned_value for k in expected_value.keys()) and all( check_content(returned_value[key], val) for key, val in expected_value.items() ) else: return returned_value == expected_value bus_notifs = self.env['bus.bus'].sudo().search([('channel', 'in', [json_dump(channel) for channel in channels])]) if check_unique: self.assertEqual(len(bus_notifs), len(channels)) self.assertEqual(set(bus_notifs.mapped('channel')), set([json_dump(channel) for channel in channels])) notif_messages = [json.loads(n.message) for n in bus_notifs] for expected in message_items or []: for notification in notif_messages: found_keys, not_found_keys = [], [] if not all(k in notification for k in expected.keys()): continue for expected_key, expected_value in expected.items(): done = check_content(notification[expected_key], expected_value) if done: found_keys.append(expected_key) else: not_found_keys.append(expected_key) if set(found_keys) == set(expected.keys()): break else: raise AssertionError('Keys %s not found (expected: %s - returned: %s)' % (not_found_keys, repr(expected), repr(notif_messages))) return bus_notifs