Skip to content

Commit

Permalink
[FW][IMP] test_mail: add test for from and reply-to computation in co…
Browse files Browse the repository at this point in the history
…mposer mass mode

FWD-PORT: updated to new test files, and tests are more in-depth since v14

When sending a mass mail through the composer, if the field ``reply_to`` had to
fall back to being ``email_from``, reply_to would take the value of the template
syntax instead of the rendered value.

This is notably the case when mass-mailing invoices through the accounting app.
Resulting in reply_to fields such as: '{{user.email}}'

On some mail clients (including mailhog), this could also result in template
syntax being shown as part of the subject or sender field.

Task-2816845

X-original-commit: 2f029b6
Part-of: odoo#98192
  • Loading branch information
reth-odoo authored and tde-banana-odoo committed Aug 16, 2022
1 parent cddf06d commit 5341581
Showing 1 changed file with 96 additions and 8 deletions.
104 changes: 96 additions & 8 deletions addons/test_mail/tests/test_mail_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from odoo.exceptions import AccessError
from odoo.tests import tagged
from odoo.tests.common import users, Form
from odoo.tools import mute_logger
from odoo.tools import mute_logger, formataddr

@tagged('mail_composer')
class TestMailComposer(TestMailCommon, TestRecipients):
Expand Down Expand Up @@ -417,6 +417,7 @@ def test_mail_composer_parent(self):
self.assertEqual(composer.partner_ids, self.partner_1 + self.partner_2)

@users('user_rendering_restricted')
@mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail', 'odoo.models.unlink')
def test_mail_composer_rights_attachments(self):
""" Ensure a user without write access to a template can send an email"""
template_1 = self.template.copy({
Expand Down Expand Up @@ -452,6 +453,7 @@ def test_mail_composer_rights_attachments(self):
sorted(self.test_record.message_ids[0].attachment_ids.mapped('name')),
sorted(template_1_attachment_name))

@mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail', 'odoo.models.unlink')
def test_mail_composer_rights_portal(self):
portal_user = self._create_portal_user()

Expand Down Expand Up @@ -502,6 +504,7 @@ class TestComposerResultsComment(TestMailComposer):
notification and emails generated during this process. """

@users('employee')
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_mail_composer_document_based(self):
""" Tests a document-based mass mailing with the same address mails
This should be allowed and not considered as duplicate in this context
Expand Down Expand Up @@ -621,6 +624,7 @@ def test_mail_composer_wtpl_complete(self):
default_template_id=self.template.id)
))
composer = composer_form.save()
self.assertFalse(composer.reply_to_force_new, 'Mail: thread-enabled models should use auto thread by default')
with self.mock_mail_gateway(mail_unlink_sent=False), self.mock_mail_app():
composer._action_send_mail()

Expand Down Expand Up @@ -704,6 +708,7 @@ def test_mail_composer_wtpl(self):
default_template_id=self.template.id)
))
composer = composer_form.save()
self.assertFalse(composer.reply_to_force_new, 'Mail: thread-enabled models should use auto thread by default')
with self.mock_mail_gateway(mail_unlink_sent=True):
composer._action_send_mail()

Expand Down Expand Up @@ -777,7 +782,6 @@ def test_mail_composer_wtpl_complete(self):
[mail for mail in self._mails if '%s-%s' % (record.id, record._name) in mail['message_id']]
for record in self.test_records
]
_mails_record2 = [mail for mail in self._mails if '%s-%s' % (self.test_records[1].id, self.test_records._name) in mail['message_id']]

for record, _mails in zip(self.test_records, _mails_records):
# message copy is kept
Expand All @@ -790,14 +794,60 @@ def test_mail_composer_wtpl_complete(self):
mail_message=message,
author=self.partner_employee,
email_values={
'attachments_info': [
{'name': '00.txt', 'raw': b'Att00', 'type': 'text/plain'},
{'name': '01.txt', 'raw': b'Att01', 'type': 'text/plain'},
{'name': 'TestReport for %s.html' % record.name, 'type': 'text/plain'},
],
'body_content': 'TemplateBody %s' % record.name,
'email_from': self.partner_employee.email_formatted,
'reply_to': formataddr((
f'{self.env.user.company_id.name} {record.name}',
f'{self.alias_catchall}@{self.alias_domain}'
)),
'subject': 'TemplateSubject %s' % record.name,
# 'attachments': [
# ('00.txt', b'Att00', 'text/plain'),
# ('01.txt', b'Att01', 'text/plain'),
# ('report.test_mail.mail_test_ticket_test_template.html', b'My second attachment', 'text/plain')
# ]
}
},
fields_values={
'email_from': self.partner_employee.email_formatted,
'reply_to': formataddr((
f'{self.env.user.company_id.name} {record.name}',
f'{self.alias_catchall}@{self.alias_domain}'
)),
},
)

# test without catchall filling reply-to
composer_form = Form(self.env['mail.compose.message'].with_context(
self._get_web_context(self.test_records, add_web=True,
default_template_id=self.template.id)
))
composer = composer_form.save()
with self.mock_mail_gateway(mail_unlink_sent=True):
# remove alias so that _notify_get_reply_to will return the default value instead of alias
self.env['ir.config_parameter'].sudo().set_param("mail.catchall.domain", None)
composer.action_send_mail()

# hack to use assertEmails: filtering on from/to only is not sufficient to distinguish emails
_mails_records = [
[mail for mail in self._mails if '%s-%s' % (record.id, record._name) in mail['message_id']]
for record in self.test_records
]

for record, _mails in zip(self.test_records, _mails_records):
# template is sent only to partners (email_to are transformed)
self._mails = _mails
self.assertMailMail(record.customer_id + new_partners + self.partner_admin,
'sent',
mail_message=record.message_ids[0],
author=self.partner_employee,
email_values={
'email_from': self.partner_employee.email_formatted,
# 'reply_to': self.partner_employee.email_formatted, FIXME: currently failing (unrendered jinja)
},
fields_values={
'email_from': self.partner_employee.email_formatted,
# 'reply_to': self.partner_employee.email_formatted, # FIXME: currently failing (unrendered jinja)
},
)

@users('employee')
Expand Down Expand Up @@ -919,3 +969,41 @@ def test_mail_composer_wtpl_recipients(self):
composer = composer_form.save()
with self.mock_mail_gateway(mail_unlink_sent=False), self.assertRaises(ValueError):
composer._action_send_mail()

@users('employee')
@mute_logger('odoo.models.unlink', 'odoo.addons.mail.models.mail_mail')
def test_mail_composer_wtpl_reply_to_force_new(self):
""" Test no auto thread behavior, notably with reply-to. """
# launch composer in mass mode
composer_form = Form(self.env['mail.compose.message'].with_context(
self._get_web_context(self.test_records, add_web=True,
default_template_id=self.template.id)
))
composer_form.reply_to_force_new = True
composer_form.reply_to = "{{ '\"' + object.name + '\" <%s>' % 'dynamic.reply.to@test.com' }}"
composer = composer_form.save()
with self.mock_mail_gateway(mail_unlink_sent=False):
composer.action_send_mail()

for record in self.test_records:
self.assertMailMail(record.customer_id,
'sent',
mail_message=record.message_ids[0],
author=self.partner_employee,
email_values={
'body_content': 'TemplateBody %s' % record.name,
'email_from': self.partner_employee.email_formatted,
'reply_to': formataddr((
f'{record.name}',
'dynamic.reply.to@test.com'
)),
'subject': 'TemplateSubject %s' % record.name,
},
fields_values={
'email_from': self.partner_employee.email_formatted,
'reply_to': formataddr((
f'{record.name}',
'dynamic.reply.to@test.com'
)),
},
)

0 comments on commit 5341581

Please sign in to comment.