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.

68 lines
2.6 KiB

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