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.

125 lines
4.6 KiB

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