From 9146b68b3fd900b122eeb3115a9ff387ee4fc50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 22 Sep 2021 15:12:18 +0000 Subject: [PATCH] [IMP] test_mail: add test for reply_to computation on records Purpose is to assert current behavior as this may change in master. Two kind of tests are added: performance and multi company. Task-2661036 (Performance tests data cleanup) Prepares Task-36879 (MultiCompany Aliases) closes odoo/odoo#77845 Related: odoo/enterprise#21456 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/tests/common.py | 1 + addons/test_mail/tests/__init__.py | 1 + .../test_mail/tests/test_mail_multicompany.py | 92 +++++++++++++++++++ addons/test_mail/tests/test_performance.py | 26 ++++++ 4 files changed, 120 insertions(+) create mode 100644 addons/test_mail/tests/test_mail_multicompany.py diff --git a/addons/mail/tests/common.py b/addons/mail/tests/common.py index f81a9a48848..5371ff3d159 100644 --- a/addons/mail/tests/common.py +++ b/addons/mail/tests/common.py @@ -933,6 +933,7 @@ class MailCommon(common.TransactionCase, MailCase): cls.env, login='employee_c2', groups='base.group_user', company_id=cls.company_2.id, + company_ids=[(4, cls.company_2.id)], name='Enguerrand Employee C2', notification_type='inbox', signature='--\nEnguerrand' diff --git a/addons/test_mail/tests/__init__.py b/addons/test_mail/tests/__init__.py index 0400ce9280f..8d34da5957e 100644 --- a/addons/test_mail/tests/__init__.py +++ b/addons/test_mail/tests/__init__.py @@ -9,6 +9,7 @@ from . import test_mail_followers from . import test_mail_message from . import test_mail_mail from . import test_mail_gateway +from . import test_mail_multicompany from . import test_mail_thread_internals from . import test_mail_template from . import test_mail_template_preview diff --git a/addons/test_mail/tests/test_mail_multicompany.py b/addons/test_mail/tests/test_mail_multicompany.py new file mode 100644 index 00000000000..18082651e69 --- /dev/null +++ b/addons/test_mail/tests/test_mail_multicompany.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import socket + +from odoo.addons.test_mail.tests.common import TestMailCommon, TestRecipients +from odoo.tests import tagged +from odoo.tests.common import users +from odoo.tools import formataddr + + +@tagged('multi_company') +class TestMultiCompanySetup(TestMailCommon, TestRecipients): + + @classmethod + def setUpClass(cls): + super(TestMultiCompanySetup, cls).setUpClass() + cls._activate_multi_company() + + cls.test_model = cls.env['ir.model']._get('mail.test.gateway') + cls.email_from = '"Sylvie Lelitre" ' + + cls.test_record = cls.env['mail.test.gateway'].with_context(cls._test_context).create({ + 'name': 'Test', + 'email_from': 'ignasse@example.com', + }).with_context({}) + + cls.partner_1 = cls.env['res.partner'].with_context(cls._test_context).create({ + 'name': 'Valid Lelitre', + 'email': 'valid.lelitre@agrolait.com', + }) + # groups@.. will cause the creation of new mail.test.gateway + cls.alias = cls.env['mail.alias'].create({ + 'alias_name': 'groups', + 'alias_user_id': False, + 'alias_model_id': cls.test_model.id, + 'alias_contact': 'everyone'}) + + # Set a first message on public group to test update and hierarchy + cls.fake_email = cls.env['mail.message'].create({ + 'model': 'mail.test.gateway', + 'res_id': cls.test_record.id, + 'subject': 'Public Discussion', + 'message_type': 'email', + 'subtype_id': cls.env.ref('mail.mt_comment').id, + 'author_id': cls.partner_1.id, + 'message_id': '<123456-openerp-%s-mail.test.gateway@%s>' % (cls.test_record.id, socket.gethostname()), + }) + + cls._init_mail_gateway() + + @users('employee') + def test_notify_reply_to_computation(self): + test_record = self.env['mail.test.gateway'].browse(self.test_record.ids) + res = test_record._notify_get_reply_to() + self.assertEqual( + res[test_record.id], + formataddr(( + "%s %s" % (self.user_employee.company_id.name, test_record.name), + "%s@%s" % (self.alias_catchall, self.alias_domain))) + ) + + @users('employee_c2') + def test_notify_reply_to_computation_mc(self): + """ Test reply-to computation in multi company mode. Add notably tests + depending on user company_id / company_ids. """ + # Test1: no company_id field + test_record = self.env['mail.test.gateway'].browse(self.test_record.ids) + res = test_record._notify_get_reply_to() + self.assertEqual( + res[test_record.id], + formataddr(( + "%s %s" % (self.user_employee_c2.company_id.name, test_record.name), + "%s@%s" % (self.alias_catchall, self.alias_domain))) + ) + + # Test2: company_id field, MC environment + self.user_employee_c2.write({'company_ids': [(4, self.user_employee.company_id.id)]}) + test_records = self.env['mail.test.multi.company'].create([ + {'name': 'Test', + 'company_id': self.user_employee.company_id.id}, + {'name': 'Test', + 'company_id': self.user_employee_c2.company_id.id}, + ]) + res = test_records._notify_get_reply_to() + for test_record in test_records: + self.assertEqual( + res[test_record.id], + formataddr(( + "%s %s" % (self.user_employee_c2.company_id.name, test_record.name), + "%s@%s" % (self.alias_catchall, self.alias_domain))) + ) diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 53a6fac67e9..95df796b177 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -466,6 +466,32 @@ class TestMailAPIPerformance(BaseMailPerformance): record.write({'field_%s' % (i): 'Field With Cache %s' % (i) for i in range(3)}) record.flush() + @users('__system__', 'employee') + @warmup + def test_notification_reply_to_batch(self): + test_records_sudo = self.env['mail.test.container'].sudo().create([ + {'alias_name': 'alias.test.%s.%d' % (self.env.user.name, index), + 'customer_id': self.customer.id, + 'name': 'Test_%d' % index, + } for index in range(10) + ]) + + with self.assertQueryCount(__system__=1, employee=1): + test_records = self.env['mail.test.container'].browse(test_records_sudo.ids) + reply_to = test_records._notify_get_reply_to( + default=self.env.user.email_formatted + ) + + for record in test_records: + self.assertEqual( + reply_to[record.id], + formataddr( + ("%s %s" % (self.env.user.company_id.name, record.name), + "%s@%s" % (record.alias_name, self.alias_domain) + ) + ) + ) + @tagged('mail_performance') class TestMailComplexPerformance(BaseMailPerformance):