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.

113 lines
4.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import json
  5. from odoo import exceptions
  6. from odoo.tests import common
  7. from odoo.addons.bus.models.bus import json_dump
  8. import mock
  9. class TestResUsers(common.TransactionCase):
  10. def test_notify_info(self):
  11. bus_bus = self.env['bus.bus']
  12. domain = [
  13. ('channel', '=',
  14. json_dump(self.env.user.notify_info_channel_name))
  15. ]
  16. existing = bus_bus.search(domain)
  17. self.env.user.notify_info(
  18. message='message', title='title', sticky=True,
  19. show_reload=True, foo="bar"
  20. )
  21. news = bus_bus.search(domain) - existing
  22. self.assertEqual(1, len(news))
  23. expected = ('{"message":"message","sticky":true,"title":"title",'
  24. '"show_reload":true,"action":null,'
  25. '"action_link_name":null,"foo":"bar"}')
  26. self.assertDictEqual(
  27. json.loads(expected),
  28. json.loads(news.message)
  29. )
  30. def test_notify_warning(self):
  31. bus_bus = self.env['bus.bus']
  32. domain = [
  33. ('channel', '=',
  34. json_dump(self.env.user.notify_warning_channel_name))
  35. ]
  36. existing = bus_bus.search(domain)
  37. self.env.user.notify_warning(
  38. message='message', title='title', sticky=True,
  39. show_reload=True, foo="bar"
  40. )
  41. news = bus_bus.search(domain) - existing
  42. self.assertEqual(1, len(news))
  43. expected = ('{"message":"message","sticky":true,"title":"title",'
  44. '"show_reload":true,"action":null,'
  45. '"action_link_name":null,"foo":"bar"}')
  46. self.assertDictEqual(
  47. json.loads(expected),
  48. json.loads(news.message)
  49. )
  50. def test_notify_with_action(self):
  51. bus_bus = self.env['bus.bus']
  52. domain = [
  53. ('channel', '=',
  54. json_dump(self.env.user.notify_info_channel_name))
  55. ]
  56. existing = bus_bus.search(domain)
  57. self.env.user.notify_info(
  58. message='message', title='title', sticky=True,
  59. action={
  60. "type": "ir.actions.act_window",
  61. "view_mode": "form",
  62. },
  63. action_link_name="Open"
  64. )
  65. news = bus_bus.search(domain) - existing
  66. self.assertEqual(1, len(news))
  67. # the action should be transformed by Odoo (clean_action)
  68. expected = ('{"message":"message","sticky":true,"title":"title",'
  69. '"show_reload":false,"action":'
  70. '{"type": "ir.actions.act_window", "view_mode":"form",'
  71. '"flags":{},"views":[[false, "form"]]},'
  72. '"action_link_name":"Open"}')
  73. self.assertDictEqual(
  74. json.loads(expected),
  75. json.loads(news.message)
  76. )
  77. def test_notify_many(self):
  78. # check that the notification of a list of users is done with
  79. # a single call to the bus
  80. with mock.patch('odoo.addons.bus.models.bus.ImBus.sendmany'
  81. ) as mockedSendMany:
  82. users = self.env.user.search([(1, "=", 1)])
  83. self.assertTrue(len(users) > 1)
  84. users.notify_warning('message')
  85. self.assertEqual(1, mockedSendMany.call_count)
  86. # call_args is a tuple with args (tuple) & kwargs (dict). We check
  87. # positional arguments (args), hence the [0].
  88. pos_call_args = mockedSendMany.call_args[0]
  89. # Ensure the first positional argument is a list with as many
  90. # elements as we had users.
  91. first_pos_call_args = pos_call_args[0]
  92. self.assertIsInstance(first_pos_call_args, list)
  93. self.assertEqual(len(users), len(first_pos_call_args))
  94. def test_notify_other_user(self):
  95. other_user = self.env.ref('base.user_demo')
  96. other_user_model = self.env['res.users'].sudo(other_user)
  97. with self.assertRaises(exceptions.UserError):
  98. other_user_model.browse(self.env.uid).notify_info('hello')
  99. def test_notify_admin_allowed_other_user(self):
  100. other_user = self.env.ref('base.user_demo')
  101. other_user.notify_info('hello')