[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
This commit is contained in:
Thibault Delavallée
2023-10-10 14:07:16 +00:00
parent ac187363ea
commit 3ffa1a0611
4 changed files with 35 additions and 28 deletions
+11 -6
View File
@@ -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
+13 -3
View File
@@ -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 = '<p>Bounced Content</p>'
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',
+9 -17
View File
@@ -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)
# --------------------------------------------------
+2 -2
View File
@@ -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,