From 1cdbeeca55eb275520f2bc31a95018001071e5a5 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Tue, 28 May 2019 08:54:15 +0000 Subject: [PATCH] [IMP] test_mail: Add multi company test from _redirect_to_record --- addons/test_mail/__manifest__.py | 1 + addons/test_mail/models/test_mail_models.py | 10 ++++ addons/test_mail/security/ir.model.access.csv | 3 +- .../test_mail/security/test_mail_security.xml | 13 +++++ addons/test_mail/tests/__init__.py | 1 + addons/test_mail/tests/test_multi_company.py | 57 +++++++++++++++++++ 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 addons/test_mail/security/test_mail_security.xml create mode 100644 addons/test_mail/tests/test_multi_company.py diff --git a/addons/test_mail/__manifest__.py b/addons/test_mail/__manifest__.py index 4f76d51cac9..9f716d8786f 100644 --- a/addons/test_mail/__manifest__.py +++ b/addons/test_mail/__manifest__.py @@ -12,6 +12,7 @@ tests independently to functional aspects of other models. """, 'depends': ['test_performance', 'mail', 'mail_bot'], 'data': [ 'security/ir.model.access.csv', + 'security/test_mail_security.xml', 'data/data.xml', ], 'demo': [ diff --git a/addons/test_mail/models/test_mail_models.py b/addons/test_mail/models/test_mail_models.py index b9ff40ebc7a..c7ee1d2e9bf 100644 --- a/addons/test_mail/models/test_mail_models.py +++ b/addons/test_mail/models/test_mail_models.py @@ -148,3 +148,13 @@ class MailCC(models.Model): _inherit = ['mail.thread.cc'] name = fields.Char() + + +class MailMultiCompany(models.Model): + """ This model can be used in multi company tests""" + _name = 'mail.test.multi.company' + _description = "Test Multi Company Mail" + _inherit = 'mail.thread' + + name = fields.Char() + company_id = fields.Many2one('res.company') diff --git a/addons/test_mail/security/ir.model.access.csv b/addons/test_mail/security/ir.model.access.csv index b0d62e69a4a..90b56e67703 100644 --- a/addons/test_mail/security/ir.model.access.csv +++ b/addons/test_mail/security/ir.model.access.csv @@ -14,4 +14,5 @@ access_mail_test_portal,mail.test.portal,model_mail_test,base.group_portal,1,1,0 access_mail_test_user,mail.test.user,model_mail_test,base.group_user,1,1,1,1 access_mail_test_cc_portal,mail.test.cc.portal,model_mail_test_cc,base.group_portal,1,0,0,0 access_mail_test_cc_user,mail.test.cc.user,model_mail_test_cc,base.group_user,1,1,1,1 - +access_mail_test_multi_company_user,mail.test.multi.company.user,model_mail_test_multi_company,base.group_user,1,1,1,1 +access_mail_test_multi_company_portal,mail.test.multi.company.portal,model_mail_test_multi_company,base.group_portal,1,0,0,0 diff --git a/addons/test_mail/security/test_mail_security.xml b/addons/test_mail/security/test_mail_security.xml new file mode 100644 index 00000000000..762df7cf167 --- /dev/null +++ b/addons/test_mail/security/test_mail_security.xml @@ -0,0 +1,13 @@ + + + + + + Mail Test Multi Company + + + ['|', ('company_id', '=', False), ('company_id', 'in', company_ids)] + + + + diff --git a/addons/test_mail/tests/__init__.py b/addons/test_mail/tests/__init__.py index 71c0b6a0a41..00691b6ffca 100644 --- a/addons/test_mail/tests/__init__.py +++ b/addons/test_mail/tests/__init__.py @@ -19,3 +19,4 @@ from . import test_discuss from . import test_performance from . import test_res_users from . import test_odoobot +from . import test_multi_company diff --git a/addons/test_mail/tests/test_multi_company.py b/addons/test_mail/tests/test_multi_company.py new file mode 100644 index 00000000000..9272362d745 --- /dev/null +++ b/addons/test_mail/tests/test_multi_company.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from werkzeug.urls import url_decode, url_parse +from odoo.tests.common import HttpCase + + +class TestMultiCompany(HttpCase): + + def test_redirect_to_records(self): + + self.authenticate('admin', 'admin') + + self.company_A = self.env['res.company'].create({ + 'name': 'Company A', + 'user_ids': [(4, self.ref('base.user_admin'))], + }) + + self.company_B = self.env['res.company'].create({ + 'name': 'Company B', + }) + + self.multi_company_record = self.env['mail.test.multi.company'].create({ + 'name': 'Multi Company Record', + 'company_id': self.company_A.id, + }) + + # Test Case 1 + # Logged into company 1, try accessing record in company A + # _redirect_to_record should add company A in allowed_company_ids + response = self.url_open('/mail/view?model=%s&res_id=%s' % ( + self.multi_company_record._name, + self.multi_company_record.id), timeout=15) + + self.assertEqual(response.status_code, 200) + + fragment = url_parse(response.url).fragment + cids = url_decode(fragment)['cids'] + + self.assertEqual(cids, '1,%s' % (self.company_A.id)) + + # Test Case 2 + # Logged into company 1, try accessing record in company B + # _redirect_to_record should redirect to messaging as the user + # doesn't have any access for this company + self.multi_company_record.company_id = self.company_B + + response = self.url_open('/mail/view?model=%s&res_id=%s' % ( + self.multi_company_record._name, + self.multi_company_record.id), timeout=15) + + self.assertEqual(response.status_code, 200) + + fragment = url_parse(response.url).fragment + action = url_decode(fragment)['action'] + + self.assertEqual(action, 'mail.action_discuss')