|
| 1 | +# Copyright 2025 Tecnativa - Víctor Martínez |
| 2 | +# Copyright 2025 Tecnativa - David Bañón |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | +from datetime import datetime, timedelta |
| 5 | + |
| 6 | +from odoo import Command |
| 7 | +from odoo.tests import new_test_user, tagged, users |
| 8 | + |
| 9 | +from odoo.addons.base.tests.common import BaseCommon |
| 10 | + |
| 11 | + |
| 12 | +@tagged("-at_install", "post_install") |
| 13 | +class TestAnnouncement(BaseCommon): |
| 14 | + @classmethod |
| 15 | + def setUpClass(cls): |
| 16 | + super().setUpClass() |
| 17 | + cls.general_announcement = cls.env["announcement"].create( |
| 18 | + { |
| 19 | + "name": "Test announcement", |
| 20 | + "content": "<p>Test content for test announcement</p>", |
| 21 | + "is_general_announcement": True, |
| 22 | + "notification_date": datetime.now() + timedelta(days=-1), |
| 23 | + "notification_expiry_date": datetime.now() + timedelta(days=+1), |
| 24 | + "active": True, |
| 25 | + } |
| 26 | + ) |
| 27 | + cls.expired_announcement = cls.env["announcement"].create( |
| 28 | + { |
| 29 | + "name": "Test expired announcement", |
| 30 | + "content": "<p>Its gone</p>", |
| 31 | + "is_general_announcement": True, |
| 32 | + "notification_date": datetime.now() + timedelta(days=-2), |
| 33 | + "notification_expiry_date": datetime.now() + timedelta(days=-1), |
| 34 | + "active": True, |
| 35 | + } |
| 36 | + ) |
| 37 | + cls.user = new_test_user(cls.env, login="test-user") |
| 38 | + cls.user_system = new_test_user( |
| 39 | + cls.env, login="test-user-system", groups="base.group_system" |
| 40 | + ) |
| 41 | + cls.admin_announcement = cls.env["announcement"].create( |
| 42 | + { |
| 43 | + "name": "Test admin only announcement", |
| 44 | + "content": "<p>Test content for admins only</p>", |
| 45 | + "announcement_type": "user_group", |
| 46 | + "user_group_ids": cls.env.ref("base.group_system"), |
| 47 | + "notification_date": datetime.now() + timedelta(days=-1), |
| 48 | + "notification_expiry_date": datetime.now() + timedelta(days=+1), |
| 49 | + "active": True, |
| 50 | + } |
| 51 | + ) |
| 52 | + cls.custom_announcement = cls.env["announcement"].create( |
| 53 | + { |
| 54 | + "name": "Test custom only announcement", |
| 55 | + "content": "<p>Test content for custom users</p>", |
| 56 | + "announcement_type": "specific_users", |
| 57 | + "specific_user_ids": [ |
| 58 | + Command.link(cls.user.id), |
| 59 | + Command.link(cls.user_system.id), |
| 60 | + ], |
| 61 | + "notification_date": datetime.now() + timedelta(days=-1), |
| 62 | + "notification_expiry_date": datetime.now() + timedelta(days=+1), |
| 63 | + "active": True, |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + @users("test-user-system") |
| 68 | + def test_announcements_user_system(self): |
| 69 | + res = self.env.user.get_announcements() |
| 70 | + announcement_ids = [announcement["id"] for announcement in res["data"]] |
| 71 | + self.assertIn(self.general_announcement.id, announcement_ids) |
| 72 | + self.assertIn(self.admin_announcement.id, announcement_ids) |
| 73 | + self.assertIn(self.custom_announcement.id, announcement_ids) |
| 74 | + self.assertNotIn(self.expired_announcement.id, announcement_ids) |
| 75 | + |
| 76 | + @users("test-user") |
| 77 | + def test_announcements_user(self): |
| 78 | + res = self.env.user.get_announcements() |
| 79 | + announcement_ids = [announcement["id"] for announcement in res["data"]] |
| 80 | + self.assertIn(self.general_announcement.id, announcement_ids) |
| 81 | + self.assertIn(self.custom_announcement.id, announcement_ids) |
| 82 | + self.assertNotIn(self.admin_announcement.id, announcement_ids) |
| 83 | + self.assertNotIn(self.expired_announcement.id, announcement_ids) |
| 84 | + |
| 85 | + def test_custom_anouncement_write(self): |
| 86 | + self.assertIn(self.custom_announcement, self.user.unread_announcement_ids) |
| 87 | + self.custom_announcement.write( |
| 88 | + {"specific_user_ids": [Command.unlink(self.user.id)]} |
| 89 | + ) |
| 90 | + self.assertNotIn(self.custom_announcement, self.user.unread_announcement_ids) |
| 91 | + self.custom_announcement.write( |
| 92 | + {"specific_user_ids": [Command.link(self.user.id)]} |
| 93 | + ) |
| 94 | + self.assertIn(self.custom_announcement, self.user.unread_announcement_ids) |
0 commit comments