|
| 1 | +############################################################################## |
| 2 | +# |
| 3 | +# Copyright (C) 2024 ADHOC SA (http://www.adhoc.com.ar) |
| 4 | +# All Rights Reserved. |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Affero General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Affero General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +############################################################################## |
| 20 | +from odoo.exceptions import ValidationError |
| 21 | +from odoo.tests import TransactionCase, tagged |
| 22 | + |
| 23 | + |
| 24 | +@tagged("-at_install", "post_install") |
| 25 | +class TestCompanyCrossCheck(TransactionCase): |
| 26 | + """End-to-end coverage for the cross-company protection on |
| 27 | + company_dependent Many2one fields, through load(). |
| 28 | +
|
| 29 | + The check lives exclusively in load() — the entry point for UI imports |
| 30 | + and module CSV data — so write()/create() are intentionally unchecked. |
| 31 | +
|
| 32 | + Uses a transient ``company.dependent.tester`` model (registered only for |
| 33 | + this run, rolled back by TransactionCase) so the protection is exercised |
| 34 | + without depending on ``account``. |
| 35 | + """ |
| 36 | + |
| 37 | + MODEL = "company.dependent.tester" |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def setUpClass(cls): |
| 41 | + super().setUpClass() |
| 42 | + cls._register_test_model() |
| 43 | + |
| 44 | + cls.company_a = cls.env.company |
| 45 | + cls.company_b = cls.env["res.company"].create({"name": "E2E Company B"}) |
| 46 | + cls.test_actor = cls.env.ref("base.user_admin") |
| 47 | + |
| 48 | + cls.partner_a = cls.env["res.partner"].create({"name": "E2E Partner A", "company_id": cls.company_a.id}) |
| 49 | + cls.partner_b = cls.env["res.partner"].create({"name": "E2E Partner B", "company_id": cls.company_b.id}) |
| 50 | + cls.partner_shared = cls.env["res.partner"].create({"name": "E2E Shared Partner", "company_id": False}) |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def _register_test_model(cls): |
| 54 | + from odoo.orm.model_classes import add_to_registry |
| 55 | + |
| 56 | + from . import company_dependent_models |
| 57 | + |
| 58 | + add_to_registry(cls.registry, company_dependent_models.CompanyDependentTester) |
| 59 | + cls.registry._setup_models__(cls.env.cr, [cls.MODEL]) |
| 60 | + cls.registry.init_models(cls.env.cr, [cls.MODEL], {"models_to_check": True}) |
| 61 | + cls.addClassCleanup(cls.registry.__delitem__, cls.MODEL) |
| 62 | + # Group-less ACL so the non-superuser test actor can operate the model. |
| 63 | + cls.env["ir.model"]._reflect_models([cls.MODEL]) |
| 64 | + cls.env["ir.model.access"].create( |
| 65 | + { |
| 66 | + "name": "company_dependent_tester_test", |
| 67 | + "model_id": cls.env["ir.model"]._get(cls.MODEL).id, |
| 68 | + "perm_read": True, |
| 69 | + "perm_write": True, |
| 70 | + "perm_create": True, |
| 71 | + "perm_unlink": True, |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + def _env_a(self): |
| 76 | + ctx = dict(self.env.context, allowed_company_ids=[self.company_a.id]) |
| 77 | + return self.env(user=self.test_actor, context=ctx) |
| 78 | + |
| 79 | + def _load(self, env, partner): |
| 80 | + """Helper: import a single row via load() into company.dependent.tester.""" |
| 81 | + return env[self.MODEL].load( |
| 82 | + ["name", "partner_id/.id"], |
| 83 | + [["Tester", str(partner.id)]], |
| 84 | + ) |
| 85 | + |
| 86 | + def test_load_cross_company_raises(self): |
| 87 | + """load() with a cross-company value raises ValidationError.""" |
| 88 | + with self.assertRaises(ValidationError): |
| 89 | + self._load(self._env_a(), self.partner_b) |
| 90 | + |
| 91 | + def test_load_own_company_passes(self): |
| 92 | + """load() with an own-company value succeeds and returns an id.""" |
| 93 | + result = self._load(self._env_a(), self.partner_a) |
| 94 | + self.assertTrue(result.get("ids"), "Expected a created record id") |
| 95 | + |
| 96 | + def test_load_company_id_false_never_blocked(self): |
| 97 | + """load() with a shared value (company_id=False) is never blocked.""" |
| 98 | + result = self._load(self._env_a(), self.partner_shared) |
| 99 | + self.assertTrue(result.get("ids")) |
| 100 | + |
| 101 | + def test_load_non_company_dependent_field_ignored(self): |
| 102 | + """load() on a regular (non company_dependent) Many2one is never checked.""" |
| 103 | + env = self._env_a() |
| 104 | + result = env[self.MODEL].load( |
| 105 | + ["name", "partner_regular_id/.id"], |
| 106 | + [["Tester", str(self.partner_b.id)]], |
| 107 | + ) |
| 108 | + self.assertTrue(result.get("ids")) |
| 109 | + |
| 110 | + def test_sibling_branch_domain_behavior_documented(self): |
| 111 | + """Lock the sibling-branch behavior, whatever _check_company_domain does. |
| 112 | +
|
| 113 | + Standard Odoo excludes a sibling branch's records from the domain, so |
| 114 | + the guard raises. A branch-aware override would make them visible and |
| 115 | + the guard would pass. The test asserts whichever holds, catching a shift. |
| 116 | + """ |
| 117 | + branch_1 = self.env["res.company"].create({"name": "Sibling Branch 1", "parent_id": self.company_a.id}) |
| 118 | + branch_2 = self.env["res.company"].create({"name": "Sibling Branch 2", "parent_id": self.company_a.id}) |
| 119 | + partner_of_branch_2 = self.env["res.partner"].create({"name": "Partner of Branch 2", "company_id": branch_2.id}) |
| 120 | + self.test_actor.sudo().write({"company_ids": [(4, branch_1.id), (4, branch_2.id)]}) |
| 121 | + ctx = dict(self.env.context, allowed_company_ids=[branch_1.id]) |
| 122 | + env_branch_1 = self.env(user=self.test_actor, context=ctx) |
| 123 | + |
| 124 | + company_domain = self.env["res.partner"]._check_company_domain(branch_1) |
| 125 | + sibling_accessible = bool( |
| 126 | + self.env["res.partner"] |
| 127 | + .sudo() |
| 128 | + .with_context(active_test=False) |
| 129 | + .search(company_domain + [("id", "=", partner_of_branch_2.id)], limit=1) |
| 130 | + ) |
| 131 | + |
| 132 | + if sibling_accessible: |
| 133 | + result = self._load(env_branch_1, partner_of_branch_2) |
| 134 | + self.assertTrue(result.get("ids")) |
| 135 | + else: |
| 136 | + with self.assertRaises(ValidationError): |
| 137 | + self._load(env_branch_1, partner_of_branch_2) |
0 commit comments