[IMP] test_mass_mailing: add tests for replies to mailings depending on update mode

Purpose of this commit is to add tests related to answers through mail gateway
when having reply-to mode of mailing set to either updating existing threads
(aka replying on mailed records), either creating new threads (aka using the
reply-to alias configuration).

LINKS

Task ID-2117639
COM PR odoo/odoo#40931
This commit is contained in:
Thibault Delavallée
2021-04-26 13:53:03 +00:00
parent b36b74fa57
commit 4ef24369c8
2 changed files with 82 additions and 3 deletions
+28 -3
View File
@@ -128,7 +128,8 @@ class MockEmail(common.BaseCase):
""" Deprecated, remove in 14.4 """
return self.gateway_mail_reply_wrecord(template, record, use_in_reply_to=use_in_reply_to)
def gateway_mail_reply_wrecord(self, template, record, use_in_reply_to=True):
def gateway_mail_reply_wrecord(self, template, record, use_in_reply_to=True,
target_model=None, target_field=None):
""" Simulate a reply through the mail gateway. Usage: giving a record,
find an email sent to him and use its message-ID to simulate a reply.
@@ -148,8 +149,32 @@ class MockEmail(common.BaseCase):
subject='Re: %s' % mail_mail.subject,
extra=extra,
msg_id='<123456.%s.%d@test.example.com>' % (record._name, record.id),
target_model=record._name,
target_field=record._rec_name,
target_model=target_model or record._name,
target_field=target_field or record._rec_name,
)
def gateway_mail_reply_wemail(self, template, email_to, use_in_reply_to=True,
target_model=None, target_field=None):
""" Simulate a reply through the mail gateway. Usage: giving a record,
find an email sent to him and use its message-ID to simulate a reply.
Some noise is added in References just to test some robustness. """
sent_mail = self._find_sent_mail_wemail(email_to)
if use_in_reply_to:
extra = 'In-Reply-To:\r\n\t%s\n' % sent_mail['message_id']
else:
disturbing_other_msg_id = '<123456.654321@another.host.com>'
extra = 'References:\r\n\t%s\n\r%s' % (sent_mail['message_id'], disturbing_other_msg_id)
return self.format_and_process(
template,
sent_mail['email_to'],
sent_mail['reply_to'],
subject='Re: %s' % sent_mail['subject'],
extra=extra,
target_model=target_model,
target_field=target_field or 'name',
)
def from_string(self, text):
@@ -119,6 +119,60 @@ class TestMassMailing(TestMassMailCommon):
self.assertMailingStatistics(mailing, expected=5, delivered=4, sent=5, opened=1, clicked=1, bounced=1)
self.assertEqual(recipients[1].message_bounce, 1)
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_mailing_reply_to_mode_new(self):
mailing = self.env['mailing.mailing'].browse(self.mailing_bl.ids)
recipients = self._create_mailing_test_records(model='mailing.test.blacklist', count=5)
self.assertEqual(len(recipients), 5)
initial_messages = recipients.message_ids
mailing.write({
'mailing_domain': [('id', 'in', recipients.ids)],
'keep_archives': False,
'reply_to_mode': 'email',
'reply_to': self.test_alias.display_name,
})
with self.mock_mail_gateway(mail_unlink_sent=True):
mailing.action_send_mail()
answer_rec = self.gateway_mail_reply_wemail(MAIL_TEMPLATE, recipients[0].email_normalized, target_model=self.test_alias.alias_model_id.model)
self.assertTrue(bool(answer_rec))
self.assertEqual(answer_rec.name, 'Re: %s' % mailing.subject)
self.assertEqual(
answer_rec.message_ids.subject, 'Re: %s' % mailing.subject,
'Answer should be logged')
self.assertEqual(recipients.message_ids, initial_messages)
self.assertMailingStatistics(mailing, expected=5, delivered=5, sent=5, opened=1, replied=1)
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_mailing_reply_to_mode_update(self):
mailing = self.env['mailing.mailing'].browse(self.mailing_bl.ids)
recipients = self._create_mailing_test_records(model='mailing.test.blacklist', count=5)
self.assertEqual(len(recipients), 5)
mailing.write({
'mailing_domain': [('id', 'in', recipients.ids)],
'keep_archives': False,
'reply_to_mode': 'thread',
'reply_to': self.test_alias.display_name,
})
with self.mock_mail_gateway(mail_unlink_sent=True):
mailing.action_send_mail()
answer_rec = self.gateway_mail_reply_wemail(MAIL_TEMPLATE, recipients[0].email_normalized, target_model=self.test_alias.alias_model_id.model)
self.assertFalse(bool(answer_rec))
self.assertEqual(
recipients[0].message_ids[1].subject, mailing.subject,
'Should have keep a log (to enable thread-based answer)')
self.assertEqual(
recipients[0].message_ids[0].subject, 'Re: %s' % mailing.subject,
'Answer should be logged')
self.assertMailingStatistics(mailing, expected=5, delivered=5, sent=5, opened=1, replied=1)
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_thread')
def test_mailing_trace_utm(self):