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.

102 lines
3.7 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.tests import common
  6. from odoo.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. show_reload=True, foo="bar"
  19. )
  20. news = bus_bus.search(domain) - existing
  21. self.assertEqual(1, len(news))
  22. expected = ('{"message":"message","sticky":true,"title":"title",'
  23. '"show_reload":true,"action":null,'
  24. '"action_link_name":null,"foo":"bar"}')
  25. self.assertDictEqual(
  26. json.loads(expected),
  27. json.loads(news.message)
  28. )
  29. def test_notify_warning(self):
  30. bus_bus = self.env['bus.bus']
  31. domain = [
  32. ('channel', '=',
  33. json_dump(self.env.user.notify_warning_channel_name))
  34. ]
  35. existing = bus_bus.search(domain)
  36. self.env.user.notify_warning(
  37. message='message', title='title', sticky=True,
  38. show_reload=True, foo="bar"
  39. )
  40. news = bus_bus.search(domain) - existing
  41. self.assertEqual(1, len(news))
  42. expected = ('{"message":"message","sticky":true,"title":"title",'
  43. '"show_reload":true,"action":null,'
  44. '"action_link_name":null,"foo":"bar"}')
  45. self.assertDictEqual(
  46. json.loads(expected),
  47. json.loads(news.message)
  48. )
  49. def test_notify_with_action(self):
  50. bus_bus = self.env['bus.bus']
  51. domain = [
  52. ('channel', '=',
  53. json_dump(self.env.user.notify_info_channel_name))
  54. ]
  55. existing = bus_bus.search(domain)
  56. self.env.user.notify_info(
  57. message='message', title='title', sticky=True,
  58. action={
  59. "type": "ir.actions.act_window",
  60. "view_mode": "form",
  61. },
  62. action_link_name="Open"
  63. )
  64. news = bus_bus.search(domain) - existing
  65. self.assertEqual(1, len(news))
  66. # the action should be transformed by Odoo (clean_action)
  67. expected = ('{"message":"message","sticky":true,"title":"title",'
  68. '"show_reload":false,"action":'
  69. '{"type": "ir.actions.act_window", "view_mode":"form",'
  70. '"flags":{},"views":[[false, "form"]]},'
  71. '"action_link_name":"Open"}')
  72. self.assertDictEqual(
  73. json.loads(expected),
  74. json.loads(news.message)
  75. )
  76. def test_notify_many(self):
  77. # check that the notification of a list of users is done with
  78. # a single call to the bus
  79. with mock.patch('odoo.addons.bus.models.bus.ImBus.sendmany'
  80. ) as mockedSendMany:
  81. users = self.env.user.search([(1, "=", 1)])
  82. self.assertTrue(len(users) > 1)
  83. users.notify_warning('message')
  84. self.assertEqual(1, mockedSendMany.call_count)
  85. # call_args is a tuple with args (tuple) & kwargs (dict). We check
  86. # positional arguments (args), hence the [0].
  87. pos_call_args = mockedSendMany.call_args[0]
  88. # Ensure the first positional argument is a list with as many
  89. # elements as we had users.
  90. first_pos_call_args = pos_call_args[0]
  91. self.assertIsInstance(first_pos_call_args, list)
  92. self.assertEqual(len(users), len(first_pos_call_args))