diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-16 16:43:59 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-16 16:43:59 +0700 |
| commit | ad3677ba03880180873f27ac18ba841b81b2db14 (patch) | |
| tree | 27583b6e5e9c52286e2ceb2761e5c1e9fe0f5e47 /web_notify/tests | |
| parent | e6e43d9e597551be2f8a1bad3fe400b60c102361 (diff) | |
Add web_notify addons
Diffstat (limited to 'web_notify/tests')
| -rw-r--r-- | web_notify/tests/__init__.py | 3 | ||||
| -rw-r--r-- | web_notify/tests/test_res_users.py | 105 |
2 files changed, 108 insertions, 0 deletions
diff --git a/web_notify/tests/__init__.py b/web_notify/tests/__init__.py new file mode 100644 index 0000000..f759b96 --- /dev/null +++ b/web_notify/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_res_users diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py new file mode 100644 index 0000000..ba11546 --- /dev/null +++ b/web_notify/tests/test_res_users.py @@ -0,0 +1,105 @@ +# Copyright 2016 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import json +from unittest import mock + +from odoo import exceptions +from odoo.tests import common + +from odoo.addons.bus.models.bus import json_dump + +from ..models.res_users import DANGER, DEFAULT, INFO, SUCCESS, WARNING + + +class TestResUsers(common.TransactionCase): + def test_notify_success(self): + bus_bus = self.env["bus.bus"] + domain = [ + ("channel", "=", json_dump(self.env.user.notify_success_channel_name)) + ] + existing = bus_bus.search(domain) + test_msg = {"message": "message", "title": "title", "sticky": True} + self.env.user.notify_success(**test_msg) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + test_msg.update({"type": SUCCESS}) + self.assertDictEqual(test_msg, json.loads(news.message)) + + def test_notify_danger(self): + bus_bus = self.env["bus.bus"] + domain = [("channel", "=", json_dump(self.env.user.notify_danger_channel_name))] + existing = bus_bus.search(domain) + test_msg = {"message": "message", "title": "title", "sticky": True} + self.env.user.notify_danger(**test_msg) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + test_msg.update({"type": DANGER}) + self.assertDictEqual(test_msg, json.loads(news.message)) + + def test_notify_warning(self): + bus_bus = self.env["bus.bus"] + domain = [ + ("channel", "=", json_dump(self.env.user.notify_warning_channel_name)) + ] + existing = bus_bus.search(domain) + test_msg = {"message": "message", "title": "title", "sticky": True} + self.env.user.notify_warning(**test_msg) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + test_msg.update({"type": WARNING}) + self.assertDictEqual(test_msg, json.loads(news.message)) + + def test_notify_info(self): + bus_bus = self.env["bus.bus"] + domain = [("channel", "=", json_dump(self.env.user.notify_info_channel_name))] + existing = bus_bus.search(domain) + test_msg = {"message": "message", "title": "title", "sticky": True} + self.env.user.notify_info(**test_msg) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + test_msg.update({"type": INFO}) + self.assertDictEqual(test_msg, json.loads(news.message)) + + def test_notify_default(self): + bus_bus = self.env["bus.bus"] + domain = [ + ("channel", "=", json_dump(self.env.user.notify_default_channel_name)) + ] + existing = bus_bus.search(domain) + test_msg = {"message": "message", "title": "title", "sticky": True} + self.env.user.notify_default(**test_msg) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + test_msg.update({"type": DEFAULT}) + self.assertDictEqual(test_msg, json.loads(news.message)) + + def test_notify_many(self): + # check that the notification of a list of users is done with + # a single call to the bus + with mock.patch("odoo.addons.bus.models.bus.ImBus.sendmany") as mockedSendMany: + users = self.env.user.search([(1, "=", 1)]) + self.assertTrue(len(users) > 1) + users.notify_warning(message="message") + + self.assertEqual(1, mockedSendMany.call_count) + + # call_args is a tuple with args (tuple) & kwargs (dict). We check + # positional arguments (args), hence the [0]. + pos_call_args = mockedSendMany.call_args[0] + + # Ensure the first positional argument is a list with as many + # elements as we had users. + first_pos_call_args = pos_call_args[0] + self.assertIsInstance(first_pos_call_args, list) + self.assertEqual(len(users), len(first_pos_call_args)) + + def test_notify_other_user(self): + other_user = self.env.ref("base.user_demo") + other_user_model = self.env["res.users"].with_user(other_user) + with self.assertRaises(exceptions.UserError): + other_user_model.browse(self.env.uid).notify_info(message="hello") + + def test_notify_admin_allowed_other_user(self): + other_user = self.env.ref("base.user_demo") + other_user.notify_info(message="hello") |
