summaryrefslogtreecommitdiff
path: root/odoo/addons/test_new_api/tests/test_company_checks.py
diff options
context:
space:
mode:
authorVictor Feyens <vfe@odoo.com>2019-10-14 10:11:17 +0000
committerVictor Feyens <vfe@odoo.com>2019-10-24 17:12:16 +0000
commitf0d5c0b32d4bb9069874c5f2b0130e274be811da (patch)
tree895df3328cf7a150da9939462adf0a79c9346f51 /odoo/addons/test_new_api/tests/test_company_checks.py
parentf76c5a8bbd9a4f639c59b7f602b165c8d38ff360 (diff)
[IMP] base: test multi-company behavior.
Diffstat (limited to 'odoo/addons/test_new_api/tests/test_company_checks.py')
-rw-r--r--odoo/addons/test_new_api/tests/test_company_checks.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/odoo/addons/test_new_api/tests/test_company_checks.py b/odoo/addons/test_new_api/tests/test_company_checks.py
index 72540028824..94645fe1665 100644
--- a/odoo/addons/test_new_api/tests/test_company_checks.py
+++ b/odoo/addons/test_new_api/tests/test_company_checks.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-from odoo.exceptions import UserError
+from odoo.exceptions import UserError, AccessError
from odoo.tests import common
@@ -71,3 +71,52 @@ class TestCompanyCheck(common.TransactionCase):
'parent_id': self.parent_company_b_id.id,
'company_id': self.company_b.id,
})
+
+ def test_company_environment(self):
+ """ Check the company context on the environment is verified. """
+
+ self.company_c = self.env['res.company'].create({
+ 'name': 'Company C'
+ })
+
+ user = self.env['res.users'].create({
+ 'name': 'Test',
+ 'login': 'test',
+ 'company_id': self.company_a.id,
+ 'company_ids': (self.company_a | self.company_c).ids,
+ })
+
+ user = user.with_user(user).with_context(allowed_company_ids=[])
+
+ # When accessing company/companies, check raises error if unauthorized/unexisting company.
+ with self.assertRaises(AccessError):
+ user.with_context(allowed_company_ids=[self.company_a.id, self.company_b.id, self.company_c.id]).env.companies
+
+ with self.assertRaises(AccessError):
+ user.with_context(allowed_company_ids=[self.company_b.id]).env.company
+
+ with self.assertRaises(AccessError):
+ # crap in company context is not allowed.
+ user.with_context(allowed_company_ids=['company_qsdf', 'company564654']).env.companies
+
+ # In sudo mode, context check is bypassed.
+ companies = (self.company_a | self.company_b)
+ self.assertEqual(
+ user.sudo().with_context(allowed_company_ids=companies.ids).env.companies,
+ companies
+ )
+
+ self.assertEqual(
+ user.sudo().with_context(
+ allowed_company_ids=[self.company_b.id, 'abc']).env.company,
+ self.company_b
+ )
+ """
+ wrong_env = user.sudo().with_context(
+ allowed_company_ids=[self.company_a.id, self.company_b.id, 'abc'])
+ wrong_env.env.companies.mapped('name')
+ # Wrong SQL query due to wrong company id.
+ """
+ # Fallbacks when no allowed_company_ids context key
+ self.assertEqual(user.env.company, user.company_id)
+ self.assertEqual(user.env.companies, user.company_ids)