You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.2 KiB

  1. # Copyright 2016 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.tests import common
  4. from odoo.addons.bus.models.bus import json_dump
  5. import json
  6. import mock
  7. class TestResUsers(common.TransactionCase):
  8. def test_notify_info(self):
  9. bus_bus = self.env['bus.bus']
  10. domain = [
  11. ('channel', '=',
  12. json_dump(self.env.user.notify_info_channel_name))
  13. ]
  14. existing = bus_bus.search(domain)
  15. test_msg = {'message': 'message', 'title': 'title', 'sticky': True}
  16. self.env.user.notify_info(**test_msg)
  17. news = bus_bus.search(domain) - existing
  18. self.assertEqual(1, len(news))
  19. self.assertEqual(test_msg, json.loads(news.message))
  20. def test_notify_warning(self):
  21. bus_bus = self.env['bus.bus']
  22. domain = [
  23. ('channel', '=',
  24. json_dump(self.env.user.notify_warning_channel_name))
  25. ]
  26. existing = bus_bus.search(domain)
  27. test_msg = {'message': 'message', 'title': 'title', 'sticky': True}
  28. self.env.user.notify_warning(**test_msg)
  29. news = bus_bus.search(domain) - existing
  30. self.assertEqual(1, len(news))
  31. self.assertEqual(test_msg, json.loads(news.message))
  32. def test_notify_many(self):
  33. # check that the notification of a list of users is done with
  34. # a single call to the bus
  35. with mock.patch('odoo.addons.bus.models.bus.ImBus.sendmany'
  36. ) as mockedSendMany:
  37. users = self.env.user.search([(1, "=", 1)])
  38. self.assertTrue(len(users) > 1)
  39. users.notify_warning('message')
  40. self.assertEqual(1, mockedSendMany.call_count)
  41. # call_args is a tuple with args (tuple) & kwargs (dict). We check
  42. # positional arguments (args), hence the [0].
  43. pos_call_args = mockedSendMany.call_args[0]
  44. # Ensure the first positional argument is a list with as many
  45. # elements as we had users.
  46. first_pos_call_args = pos_call_args[0]
  47. self.assertIsInstance(first_pos_call_args, list)
  48. self.assertEqual(len(users), len(first_pos_call_args))