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.

62 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import exceptions
  5. from openerp.tests import common
  6. from openerp.addons.bus.models.bus import json_dump
  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. self.env.user.notify_info(
  17. message='message', title='title', sticky=True)
  18. news = bus_bus.search(domain) - existing
  19. self.assertEqual(1, len(news))
  20. self.assertEqual(
  21. '{"message":"message","sticky":true,"title":"title"}',
  22. news.message)
  23. def test_notify_warning(self):
  24. bus_bus = self.env['bus.bus']
  25. domain = [
  26. ('channel', '=',
  27. json_dump(self.env.user.notify_warning_channel_name))
  28. ]
  29. existing = bus_bus.search(domain)
  30. self.env.user.notify_warning(
  31. message='message', title='title', sticky=True)
  32. news = bus_bus.search(domain) - existing
  33. self.assertEqual(1, len(news))
  34. self.assertEqual(
  35. '{"message":"message","sticky":true,"title":"title"}',
  36. news.message)
  37. def test_notify_many(self):
  38. # check that the notification of a list of users is done with
  39. # a single call to the bus
  40. with mock.patch('openerp.addons.bus.models.bus.ImBus.sendmany'
  41. ) as mockedSendMany:
  42. users = self.env.user.search([])
  43. self.assertTrue(len(users) > 1)
  44. users.notify_warning('message')
  45. self.assertEqual(1, mockedSendMany.call_count)
  46. self.assertEqual(len(users), len(mockedSendMany.call_args[0][0]))
  47. def test_notify_other_user(self):
  48. other_user = self.env.ref('base.user_demo')
  49. other_user_model = self.env['res.users'].sudo(other_user)
  50. with self.assertRaises(exceptions.UserError):
  51. other_user_model.browse(self.env.uid).notify_info('hello')
  52. def test_notify_admin_allowed_other_user(self):
  53. other_user = self.env.ref('base.user_demo')
  54. other_user.notify_info('hello')