|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from odoo.fields import Command |
| 4 | +from odoo.tests.common import TransactionCase |
| 5 | + |
| 6 | +from odoo.addons.base.models.res_users import Groups |
| 7 | + |
| 8 | + |
| 9 | +class TestMultiCompany(TransactionCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + super().setUpClass() |
| 13 | + # Disarm any existing auditing rules. |
| 14 | + cls.env["auditlog.rule"].search([]).unlink() |
| 15 | + cls.env["auditlog.log"].search([]).unlink() |
| 16 | + # Set up a group with two share users from different companies |
| 17 | + cls.company1 = cls.env["res.company"].create({"name": "c1"}) |
| 18 | + cls.company2 = cls.env["res.company"].create({"name": "c2"}) |
| 19 | + cls.group = cls.env["res.groups"].create({"name": "g1", "share": True}) |
| 20 | + cls.user1 = cls.env["res.users"].create( |
| 21 | + { |
| 22 | + "name": "u1", |
| 23 | + "login": "u1", |
| 24 | + "groups_id": [Command.set(cls.group.ids)], |
| 25 | + "company_ids": [Command.set(cls.company1.ids)], |
| 26 | + "company_id": cls.company1.id, |
| 27 | + } |
| 28 | + ) |
| 29 | + cls.user2 = cls.env["res.users"].create( |
| 30 | + { |
| 31 | + "name": "u2", |
| 32 | + "login": "u2", |
| 33 | + "groups_id": [Command.set(cls.group.ids)], |
| 34 | + "company_ids": [Command.set(cls.company2.ids)], |
| 35 | + "company_id": cls.company2.id, |
| 36 | + } |
| 37 | + ) |
| 38 | + # We will test with a user that has access to only one of the companies |
| 39 | + cls.user_demo = cls.env.ref("base.user_demo") |
| 40 | + cls.user_demo.write( |
| 41 | + { |
| 42 | + "company_ids": [Command.set(cls.company2.ids)], |
| 43 | + "company_id": cls.company2.id, |
| 44 | + "groups_id": [Command.link(cls.env.ref("base.group_system").id)], |
| 45 | + } |
| 46 | + ) |
| 47 | + |
| 48 | + def test_group_set_users(self): # pylint: disable=missing-return |
| 49 | + """Writing x2many values does not wipe values from inaccessible companies.""" |
| 50 | + self.assertEqual( |
| 51 | + self.group.users, |
| 52 | + self.user1 + self.user2, |
| 53 | + ) |
| 54 | + self.group.invalidate_recordset() |
| 55 | + group_with_user = self.group.with_user(self.user_demo) |
| 56 | + self.assertEqual(group_with_user.users, self.user2) |
| 57 | + |
| 58 | + # The issue arises when `users` is missing from the cache and is first |
| 59 | + # read as the superuser when fetching the full values for the auditlog. |
| 60 | + # To emulate this, we want the field missing from the cache at the |
| 61 | + # moment of writing. To prevent various overrides from populating the |
| 62 | + # cache even earlier on when fetching other fields we preemptively fill |
| 63 | + # the cache of the other fields. |
| 64 | + # |
| 65 | + # All of this is undermined by res.users's own `write` method which |
| 66 | + # wipes the cache just in time, so we avoid this override with a patch. |
| 67 | + # |
| 68 | + # The issue is reproduceable on the product.template model without this |
| 69 | + # trickery but this module does not depend on the product module so the |
| 70 | + # model is not available. |
| 71 | + self.group.read() |
| 72 | + self.group.invalidate_recordset(["users"]) |
| 73 | + |
| 74 | + def write(self, vals): |
| 75 | + """Avoid the cache invalidation in this particular override. |
| 76 | +
|
| 77 | + With the faulty behaviour, values from all companies are already |
| 78 | + present in the cache at this point, leading to the deletion of the |
| 79 | + value from the company that is inaccessible to the current user. |
| 80 | + """ |
| 81 | + return super(Groups, self).write(vals) |
| 82 | + |
| 83 | + # Do the write. |
| 84 | + with patch.object(Groups, "write", side_effect=write, autospec=True): |
| 85 | + group_with_user.write({"users": [Command.set(self.user2.ids)]}) |
| 86 | + self.assertEqual(group_with_user.users, self.user2) |
| 87 | + # Ensure that the users of the other companies are still there. |
| 88 | + self.env.invalidate_all() |
| 89 | + self.assertEqual( |
| 90 | + self.group.users, |
| 91 | + self.user1 + self.user2, |
| 92 | + ) |
| 93 | + |
| 94 | + def test_group_set_users_with_auditlog(self): |
| 95 | + """Repeat the test above with an auditlog on the groups model""" |
| 96 | + rule = ( |
| 97 | + self.env["auditlog.rule"] |
| 98 | + .sudo() |
| 99 | + .create( |
| 100 | + { |
| 101 | + "name": "Test rule for groups", |
| 102 | + "model_id": self.env["ir.model"]._get("res.groups").id, |
| 103 | + "log_read": False, |
| 104 | + "log_create": False, |
| 105 | + "log_write": True, |
| 106 | + "log_unlink": False, |
| 107 | + "log_type": "full", |
| 108 | + "state": "subscribed", |
| 109 | + } |
| 110 | + ) |
| 111 | + ) |
| 112 | + try: |
| 113 | + self.test_group_set_users() |
| 114 | + finally: |
| 115 | + rule.unlink() |
0 commit comments