summaryrefslogtreecommitdiff
path: root/addons/test_mail
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2020-03-03 13:35:05 +0000
committerThibault Delavallée <tde@odoo.com>2020-03-03 16:25:00 +0000
commit68a457e17491ac90e3b806178eefc4b7150d176b (patch)
tree34e99cbb522987a0195de643da606dd3e69c45e8 /addons/test_mail
parent1380bd9123b7cc79de1552e43449ad547cf02e39 (diff)
[FW][FIX] mail: correctly check all recipients when routing on aliases
/!\ This commit is a manual forward-port of two PR targetting originally 11.0: #40442 and its fix / partial revert #41042. /!\ Purpose of this commit is to check all recipients of incoming emails when trying to determine the route to apply. Notably * an incoming email should be considered as a write to catchall only if all recipients are catchall. Catchall + a valid alias should take the alias into account; * an incoming email sent to the bounce alias should be considered as a bounce even if another valid alias is in recipients and whatever the order. Indeed it indicates an issue and bounce is considered as more important; * forward to an alias linked to another model should check all recipients and not only the first one. Otherwise reply_alias, forward_alias is considered as a reply whereas it should be considered as a forward when considering all recipients; Tests are added according to those specifications. Please see original PRs for more details about the content, the comments done on it and the various discussions. PR #46764 Task ID 2121551 X-original-commit: df00bd2d3bddf9a2b56e8554380669025e146d21
Diffstat (limited to 'addons/test_mail')
-rw-r--r--addons/test_mail/tests/test_mail_gateway.py195
1 files changed, 191 insertions, 4 deletions
diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py
index d9da245a1e0..52e2e15eb5d 100644
--- a/addons/test_mail/tests/test_mail_gateway.py
+++ b/addons/test_mail/tests/test_mail_gateway.py
@@ -395,19 +395,206 @@ class TestMailgateway(TestMailCommon):
self.assertEqual(record.message_ids[0].author_id, self.user_employee.partner_id)
# --------------------------------------------------
+ # Alias routing management
+ # --------------------------------------------------
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_alias_no_domain(self):
+ """ Incoming email: write to alias even if no domain set: considered as valid alias """
+ self.env['ir.config_parameter'].set_param('mail.catchall.domain', '')
+
+ new_record = self.format_and_process(MAIL_TEMPLATE, self.partner_1.email_formatted, 'groups@another.domain.com', subject='Test Subject')
+ # Test: one group created
+ self.assertEqual(len(new_record), 1, 'message_process: a new mail.test.simple should have been created')
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_alias_forward_bypass_reply_first(self):
+ """ Incoming email: write to two "new thread" alias, one as a reply, one being another model -> consider as a forward """
+ self.assertEqual(len(self.test_record.message_ids), 1)
+
+ # test@.. will cause the creation of new mail.test
+ new_alias_2 = self.env['mail.alias'].create({
+ 'alias_name': 'test',
+ 'alias_user_id': False,
+ 'alias_model_id': self.env['ir.model']._get('mail.test').id,
+ 'alias_contact': 'everyone',
+ })
+ new_rec = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (new_alias_2.alias_name, self.alias_domain, self.alias.alias_name, self.alias_domain),
+ subject='Test Subject',
+ extra='In-Reply-To:\r\n\t%s\n' % self.fake_email.message_id,
+ target_model=new_alias_2.alias_model_id.model
+ )
+ # Forward created a new record in mail.test
+ self.assertEqual(len(new_rec), 1, 'message_process: a new mail.test should have been created')
+ self.assertEqual(new_rec._name, new_alias_2.alias_model_id.model)
+ # No new post on test_record, no new record in mail.test.simple either
+ self.assertEqual(len(self.test_record.message_ids), 1, 'message_process: should not post on replied record as forward should bypass it')
+ # TDE FIXME: if forward, all alias are applied, should probably not
+ # new_simple = self.env['mail.test.simple'].search([('name', '=', 'Test Subject')])
+ # self.assertEqual(len(new_simple), 0, 'message_process: a new mail.test should not have been created')
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_alias_forward_bypass_reply_second(self):
+ """ Incoming email: write to two "new thread" alias, one as a reply, one being another model -> consider as a forward """
+ self.assertEqual(len(self.test_record.message_ids), 1)
+
+ # test@.. will cause the creation of new mail.test
+ new_alias_2 = self.env['mail.alias'].create({
+ 'alias_name': 'test',
+ 'alias_user_id': False,
+ 'alias_model_id': self.env['ir.model']._get('mail.test').id,
+ 'alias_contact': 'everyone',
+ })
+ new_rec = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (self.alias.alias_name, self.alias_domain, new_alias_2.alias_name, self.alias_domain),
+ subject='Test Subject',
+ extra='In-Reply-To:\r\n\t%s\n' % self.fake_email.message_id,
+ target_model=new_alias_2.alias_model_id.model
+ )
+ # Forward created a new record in mail.test
+ self.assertEqual(len(new_rec), 1, 'message_process: a new mail.test should have been created')
+ self.assertEqual(new_rec._name, new_alias_2.alias_model_id.model)
+ # No new post on test_record, no new record in mail.test.simple either
+ self.assertEqual(len(self.test_record.message_ids), 1, 'message_process: should not post on replied record as forward should bypass it')
+ # TDE FIXME: if forward, all alias are applied, should probably not
+ # new_simple = self.env['mail.test.simple'].search([('name', '=', 'Test Subject')])
+ # self.assertEqual(len(new_simple), 0, 'message_process: a new mail.test should not have been created')
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_alias_forward_bypass_update_alias(self):
+ """ Incoming email: write to one "update", one "new thread" alias, one as a reply, one being another model -> consider as a forward """
+ self.assertEqual(len(self.test_record.message_ids), 1)
+ self.alias.write({
+ 'alias_force_thread_id': self.test_record.id,
+ })
+
+ # test@.. will cause the creation of new mail.test
+ new_alias_2 = self.env['mail.alias'].create({
+ 'alias_name': 'test',
+ 'alias_user_id': False,
+ 'alias_model_id': self.env['ir.model']._get('mail.test').id,
+ 'alias_contact': 'everyone',
+ })
+ new_rec = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (new_alias_2.alias_name, self.alias_domain, self.alias.alias_name, self.alias_domain),
+ subject='Test Subject',
+ extra='In-Reply-To:\r\n\t%s\n' % self.fake_email.message_id,
+ target_model=new_alias_2.alias_model_id.model
+ )
+ # Forward created a new record in mail.test
+ self.assertEqual(len(new_rec), 1, 'message_process: a new mail.test should have been created')
+ self.assertEqual(new_rec._name, new_alias_2.alias_model_id.model)
+ # TDE FIXME No new post on test_record, no new record in mail.test.simple either
+ # self.assertEqual(len(self.test_record.message_ids), 1, 'message_process: should not post on replied record as forward should bypass it')
+ # No new record on first alias model
+ new_simple = self.env['mail.test.gateway'].search([('name', '=', 'Test Subject')])
+ self.assertEqual(len(new_simple), 0, 'message_process: a new mail.test should not have been created')
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_alias_multiple_new(self):
+ """ Incoming email: write to two aliases creating records: both should be activated """
+ # test@.. will cause the creation of new mail.test
+ new_alias_2 = self.env['mail.alias'].create({
+ 'alias_name': 'test',
+ 'alias_user_id': False,
+ 'alias_model_id': self.env['ir.model']._get('mail.test').id,
+ 'alias_contact': 'everyone',
+ })
+ new_rec = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (self.alias.alias_name, self.alias_domain, new_alias_2.alias_name, self.alias_domain),
+ subject='Test Subject',
+ target_model=new_alias_2.alias_model_id.model
+ )
+ # New record in both mail.test (new_alias_2) and mail.test.simple (self.alias)
+ self.assertEqual(len(new_rec), 1, 'message_process: a new mail.test should have been created')
+ self.assertEqual(new_rec._name, new_alias_2.alias_model_id.model)
+ new_simple = self.env['mail.test.gateway'].search([('name', '=', 'Test Subject')])
+ self.assertEqual(len(new_simple), 1, 'message_process: a new mail.test should have been created')
+
+ # --------------------------------------------------
# Email Management
# --------------------------------------------------
- @mute_logger('odoo.addons.mail.models.mail_thread')
- def test_message_process_write_to_catchall(self):
- """ Writing directly to catchall should bounce """
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_bounce(self):
+ """Incoming email: bounce using bounce alias: no record creation """
+ with self.mock_mail_gateway():
+ new_recs = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s+%s-%s-%s@%s' % (
+ self.alias_bounce, self.fake_email.id,
+ self.fake_email.model, self.fake_email.res_id,
+ self.alias_domain
+ ),
+ subject='Should bounce',
+ )
+ self.assertFalse(new_recs)
+ self.assertNotSentEmail()
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_bounce_other_recipients(self):
+ """Incoming email: bounce processing: bounce should be computed even if not first recipient """
+ with self.mock_mail_gateway():
+ new_recs = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s+%s-%s-%s@%s' % (
+ self.alias.alias_name, self.alias_domain,
+ self.alias_bounce, self.fake_email.id,
+ self.fake_email.model, self.fake_email.res_id,
+ self.alias_domain
+ ),
+ subject='Should bounce',
+ )
+ self.assertFalse(new_recs)
+ self.assertNotSentEmail()
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.addons.mail.models.mail_mail', 'odoo.models.unlink')
+ def test_message_route_write_to_catchall(self):
+ """ Writing directly to catchall should bounce """
# Test: no group created, email bounced
with self.mock_mail_gateway():
- record = self.format_and_process(MAIL_TEMPLATE, self.partner_1.email_formatted, '"My Super Catchall" <catchall.test@test.com>', subject='Should Bounce')
+ record = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '"My Super Catchall" <%s@%s>' % (self.alias_catchall, self.alias_domain),
+ subject='Should Bounce')
self.assertFalse(record)
self.assertSentEmail('"MAILER-DAEMON" <bounce.test@test.com>', ['whatever-2a840@postmaster.twitter.com'], subject='Re: Should Bounce')
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_write_to_catchall_other_recipients_first(self):
+ """ Writing directly to catchall and a valid alias should take alias """
+ # Test: no group created, email bounced
+ with self.mock_mail_gateway():
+ record = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (self.alias_catchall, self.alias_domain, self.alias.alias_name, self.alias_domain),
+ subject='Catchall Not Blocking'
+ )
+ # Test: one group created
+ self.assertEqual(len(record), 1, 'message_process: a new mail.test should have been created')
+ # No bounce email
+ self.assertNotSentEmail()
+
+ @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
+ def test_message_route_write_to_catchall_other_recipients_second(self):
+ """ Writing directly to catchall and a valid alias should take alias """
+ # Test: no group created, email bounced
+ with self.mock_mail_gateway():
+ record = self.format_and_process(
+ MAIL_TEMPLATE, self.partner_1.email_formatted,
+ '%s@%s, %s@%s' % (self.alias.alias_name, self.alias_domain, self.alias_catchall, self.alias_domain),
+ subject='Catchall Not Blocking'
+ )
+ # Test: one group created
+ self.assertEqual(len(record), 1, 'message_process: a new mail.test should have been created')
+ # No bounce email
+ self.assertNotSentEmail()
+
@mute_logger('odoo.addons.mail.models.mail_thread')
def test_message_process_bounce_alias(self):
""" Writing to bounce alias is considered as a bounce even if not multipart/report bounce structure """