From 3ffa1a061162b32a395fc2d3f9754da8295e01e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 26 Sep 2023 10:00:58 +0200 Subject: [PATCH] [IMP] test_mail: various improvements in tests Extract some test preparation, cleanup and improvements before gradually adding features that impact tests. Add tests about 'copy()' behavior in 'mail.alias.mixin' as it will be impacted by multi-company enabled aliases. Task-3547653 (Mail: Add error type for wrong email_from) Prepares Task-36879 (Mail: Support MultiCompany Aliases) Part-of: odoo/odoo#138202 --- addons/mail/tests/common.py | 17 +++++++++----- addons/test_mail/tests/test_mail_alias.py | 16 ++++++++++--- addons/test_mail/tests/test_mail_gateway.py | 26 +++++++-------------- addons/test_mail/tests/test_message_post.py | 4 ++-- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/addons/mail/tests/common.py b/addons/mail/tests/common.py index 1311678506e..1d79a301d87 100644 --- a/addons/mail/tests/common.py +++ b/addons/mail/tests/common.py @@ -6,6 +6,7 @@ import email import email.policy import time +from ast import literal_eval from collections import defaultdict from contextlib import contextmanager from functools import partial @@ -398,12 +399,16 @@ class MockEmail(common.BaseCase, MockSmtplibCase): self.assertTrue(bool(mail)) if content: self.assertIn(content, mail.body_html) - for fname, fvalue in (fields_values or {}).items(): - with self.subTest(fname=fname, fvalue=fvalue): - self.assertEqual( - mail[fname], fvalue, - 'Mail: expected %s for %s, got %s' % (fvalue, fname, mail[fname]) - ) + for fname, expected_fvalue in (fields_values or {}).items(): + with self.subTest(fname=fname, expected_fvalue=expected_fvalue): + if fname == 'headers': + fvalue = literal_eval(mail[fname]) + self.assertDictEqual(fvalue, expected_fvalue) + else: + self.assertEqual( + mail[fname], expected_fvalue, + 'Mail: expected %s for %s, got %s' % (expected_fvalue, fname, mail[fname]) + ) if status == 'sent': if email_to_recipients: recipients = email_to_recipients # already formatted diff --git a/addons/test_mail/tests/test_mail_alias.py b/addons/test_mail/tests/test_mail_alias.py index 5e6c7603998..5f4e2002827 100644 --- a/addons/test_mail/tests/test_mail_alias.py +++ b/addons/test_mail/tests/test_mail_alias.py @@ -293,6 +293,7 @@ class TestMailAliasMixin(TestMailAliasCommon): @users('employee') @mute_logger('odoo.addons.base.models.ir_model') def test_alias_mixin(self): + """ Various base checks on alias mixin behavior """ record = self.env['mail.test.container'].create({ 'name': 'Test Record', 'alias_name': 'alias.test', @@ -380,8 +381,10 @@ class TestMailAliasMixin(TestMailAliasCommon): self.assertFalse(list(filter(None, existing_aliases.mapped('alias_name')))) @users('employee') - def test_alias_mixin_copy_content(self): - self.assertFalse(self.env.user.has_group('base.group_system'), 'Test user should not have Administrator access') + def test_copy_content(self): + self.assertFalse( + self.env.user.has_group('base.group_system'), + 'Test user should not have Administrator access') record = self.env['mail.test.container'].create({ 'name': 'Test Record', @@ -389,9 +392,16 @@ class TestMailAliasMixin(TestMailAliasCommon): 'alias_contact': 'followers', 'alias_bounced_content': False, }) + record_alias = record.alias_id self.assertFalse(record.alias_bounced_content) record_copy = record.copy() + record_alias_copy = record_copy.alias_id + self.assertNotEqual(record_alias, record_alias_copy) + self.assertEqual(record_alias.alias_force_thread_id, record.id) + self.assertEqual(record_alias_copy.alias_force_thread_id, record_copy.id) self.assertFalse(record_copy.alias_bounced_content) + self.assertEqual(record_copy.alias_contact, record.alias_contact) + self.assertFalse(record_copy.alias_name, 'Copy should not duplicate name') new_content = '

Bounced Content

' record_copy.write({'alias_bounced_content': new_content}) @@ -400,7 +410,7 @@ class TestMailAliasMixin(TestMailAliasCommon): self.assertEqual(record_copy2.alias_bounced_content, new_content) @users('erp_manager') - def test_alias_mixin_mc(self): + def test_multi_company_setup(self): """ Test company change does not impact anything at alias domain level """ record = self.env['mail.test.container.mc'].create({ 'name': 'Test Record', diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index 344ebbfbf7e..2bbe767d4f3 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -929,35 +929,27 @@ class TestMailgateway(MailCommon): """ Incoming email: check that if domains are set in the optional system parameter `mail.catchall.domain.allowed` only incoming emails from these domains will generate records.""" - MailTestGatewayModel = self.env['mail.test.gateway'] - MailTestContainerModel = self.env['mail.test.container'] - # test@.. will cause the creation of new mail.test new_alias_2 = self.env['mail.alias'].create({ 'alias_contact': 'everyone', - 'alias_model_id': self.env['ir.model']._get('mail.test.container').id, + 'alias_model_id': self.env['ir.model']._get_id('mail.test.container.mc'), 'alias_name': 'test', 'alias_user_id': False, }) - allowed_domain = 'hello.com' - for (alias_right_part, allowed_domain), (gateway_created, container_created) in zip( + test_domain = 'hello.com' + for (alias_right_part, allowed_domain), container_created in zip( [ # Test with 'mail.catchall.domain.allowed' not set in system parameters # and with a domain not allowed ('bonjour.com', ""), # Test with 'mail.catchall.domain.allowed' set in system parameters # and with a domain not allowed - ('bonjour.com', allowed_domain), + ('bonjour.com', test_domain), # Test with 'mail.catchall.domain.allowed' set in system parameters # and with a domain allowed - (allowed_domain, allowed_domain), - ], [ - (True, True), - (True, False), - (True, True), - ] - ): + (test_domain, test_domain), + ], [True, False, True]): with self.subTest(alias_right_part=alias_right_part, allowed_domain=allowed_domain): self.env['ir.config_parameter'].set_param('mail.catchall.domain.allowed', allowed_domain) @@ -970,9 +962,9 @@ class TestMailgateway(MailCommon): target_model=self.alias.alias_model_id.model ) - res_alias_1 = MailTestGatewayModel.search([('name', '=', subject)]) - res_alias_2 = MailTestContainerModel.search([('name', '=', subject)]) - self.assertEqual(bool(res_alias_1), gateway_created) + res_alias_1 = self.env['mail.test.gateway'].search([('name', '=', subject)]) + res_alias_2 = self.env['mail.test.container.mc'].search([('name', '=', subject)]) + self.assertTrue(bool(res_alias_1), 'First alias should always be respected') self.assertEqual(bool(res_alias_2), container_created) # -------------------------------------------------- diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index d4c396cd876..8147c4bf832 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -969,9 +969,9 @@ class TestMessagePost(TestMessagePostCommon, CronMixinCase): }, }, 'mail_mail_values': { - 'headers': repr({ + 'headers': { 'X-Odoo-Objects': f'{record._name}-{record.id}', - }), + }, }, 'message_values': { 'author_id': self.user_erp_manager.partner_id,