[IMP] test_mail: improve notification and post tests

Add tests for notification layouts. Purpose of this commit is to test rendering
of all current email notification layouts, ensuring they effectively render
well whatever changes done in mail thread mixin code about template preparation
or rendering. They should also correctly render on simple chatter models which
is currently not the case.

A test ensuring message_post returns an ID when called by RPC should be
called post install. That way we test overrides that frequently break
this method API, and not only the base one.

Task-2621326
PR odoo/odoo#76441

X-original-commit: d7343dc8917505914058b698c59e6520dbacffd5
Part-of: odoo/odoo#76459
This commit is contained in:
Thibault Delavallée
2021-09-14 07:11:02 +00:00
parent 60fce9f656
commit 2973ecd720
+72 -10
View File
@@ -5,6 +5,7 @@ import base64
from unittest.mock import patch
from odoo import tools
from odoo.addons.test_mail.data.test_mail_data import MAIL_TEMPLATE_PLAINTEXT
from odoo.addons.test_mail.models.test_mail_models import MailTestSimple
from odoo.addons.test_mail.tests.common import TestMailCommon, TestRecipients
@@ -22,19 +23,53 @@ class TestMessagePost(TestMailCommon, TestRecipients):
def setUpClass(cls):
super(TestMessagePost, cls).setUpClass()
cls._create_portal_user()
cls.test_record = cls.env['mail.test.simple'].with_context(cls._test_context).create({'name': 'Test', 'email_from': 'ignasse@example.com'})
cls.test_record = cls.env['mail.test.simple'].with_context(cls._test_context).create({
'name': 'Test',
'email_from': 'ignasse@example.com'
})
cls._reset_mail_context(cls.test_record)
cls.test_message = cls.env['mail.message'].create({
'author_id': cls.partner_employee.id,
'body': '<p>Notify Body <span>Woop Woop</span></p>',
'email_from': cls.partner_employee.email_formatted,
'is_internal': False,
'message_id': tools.generate_tracking_message_id('dummy-generate'),
'message_type': 'comment',
'model': cls.test_record._name,
'record_name': False,
'reply_to': 'wrong.alias@test.example.com',
'subtype_id': cls.env['ir.model.data']._xmlid_to_res_id('mail.mt_comment'),
'subject': 'Notify Test',
})
cls.user_admin.write({'notification_type': 'email'})
# This method should be run inside a post_install class to ensure that all
# message_post overrides are tested.
def test_message_post_return(self):
test_channel = self.env['mail.channel'].create({
'name': 'Test',
})
# Use call_kw as shortcut to simulate a RPC call.
messageId = call_kw(self.env['mail.channel'], 'message_post', [test_channel.id], {'body': 'test'})
self.assertTrue(isinstance(messageId, int))
@users('employee')
def test_notify_email_layouts(self):
self.user_employee.write({'notification_type': 'email'})
test_record = self.env['mail.test.simple'].browse(self.test_record.ids)
test_message = self.env['mail.message'].browse(self.test_message.ids)
recipients_data = self._generate_notify_recipients(self.partner_1 + self.partner_2 + self.partner_employee)
for email_xmlid in ['mail.message_notification_email',
'mail.mail_notification_borders',
'mail.mail_notification_light',
'mail.mail_notification_paynow']:
test_message.write({'email_layout_xmlid': email_xmlid})
with self.mock_mail_gateway():
test_record._notify_record_by_email(
test_message,
recipients_data,
force_send=False
)
self.assertEqual(len(self._new_mails), 2, 'Should have 2 emails: one for customers, one for internal users')
# check customer email
customer_email = self._new_mails.filtered(lambda mail: mail.recipient_ids == self.partner_1 + self.partner_2)
self.assertTrue(customer_email)
# check internal user email
user_email = self._new_mails.filtered(lambda mail: mail.recipient_ids == self.partner_employee)
self.assertTrue(user_email)
@users('employee')
def test_notify_prepare_template_context_company_value(self):
@@ -432,3 +467,30 @@ class TestMessagePost(TestMailCommon, TestRecipients):
subject='About %s' % test_record.name,
body_content=test_record.name,
attachments=[])
@tagged('mail_post', 'post_install', '-at_install')
class TestMessagePostGlobal(TestMailCommon, TestRecipients):
@classmethod
def setUpClass(cls):
super(TestMessagePostGlobal, cls).setUpClass()
cls.test_record = cls.env['mail.test.simple'].with_context(cls._test_context).create({
'email_from': 'ignasse@example.com',
'name': 'Test',
})
cls._reset_mail_context(cls.test_record)
cls.user_admin.write({'notification_type': 'email'})
@users('employee')
def test_message_post_return(self):
""" Ensures calling message_post through RPC always return an ID. """
test_record = self.env['mail.test.simple'].browse(self.test_record.ids)
# Use call_kw as shortcut to simulate a RPC call.
message_id = call_kw(self.env['mail.test.simple'],
'message_post',
[test_record.id],
{'body': 'test'}
)
self.assertTrue(isinstance(message_id, int))