[REF] test_mail: update test message post to new common

PURPOSE

Currently tools and asserts for mail tests are located inside test_mail
module. It makes difficult to re-use them in apps tests or force them to
write custom quick and dirty tools and asserts.

SPECIFICATIONS

Update tests to new tools / asserts / helpers / classes defined in mail
and test_mail. Notably

  * ``BaseFunctionalTest`` class is replaced by the new ``TestMailCommon``
    pimped one;
  * ``assertNotifications`` is replaced by ``assertSinglePostNotifications``
    (shortcut for a simple message_post) or ``assertPostNotifications``
    (complete asserts involving several messages and notifications);
  * correctly invoke ``mock_mail_gateway`` when mocking email sending;
  * replace manual check of sent emails or ``assertEmails`` by
    ``assertSentEmails``;
  * remove custom mocks / checks and replace them by now standard mocks
    and assertions (if any);

In this commit we update: test message post

LINKS

Task ID 2068986
PR #38070
This commit is contained in:
Thibault Delavallée
2019-11-20 16:01:03 +00:00
parent 2ef5fc80ab
commit 363bdc16c7
+63 -59
View File
@@ -5,46 +5,44 @@ import base64
from unittest.mock import patch
from odoo.addons.test_mail.tests.common import BaseFunctionalTest, MockEmails, TestRecipients
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
from odoo.exceptions import AccessError
from odoo.tools import mute_logger, formataddr
class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
class TestMessagePost(TestMailCommon, TestRecipients):
@classmethod
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'})
# configure mailing
cls.alias_domain = 'schlouby.fr'
cls.alias_catchall = 'test+catchall'
cls.env['ir.config_parameter'].set_param('mail.catchall.domain', cls.alias_domain)
cls.env['ir.config_parameter'].set_param('mail.catchall.alias', cls.alias_catchall)
cls._reset_mail_context(cls.test_record)
cls.user_admin.write({'notification_type': 'email'})
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_needaction(self):
(self.user_employee | self.user_admin).write({'notification_type': 'inbox'})
with self.assertNotifications(partner_employee=(1, 'inbox', 'unread'), partner_admin=(0, '', '')):
with self.assertSinglePostNotifications([{'partner': self.partner_employee, 'type': 'inbox'}], {'content': 'Body'}):
self.test_record.message_post(
body='Test', message_type='comment', subtype='mail.mt_comment',
body='Body', message_type='comment', subtype='mail.mt_comment',
partner_ids=[self.user_employee.partner_id.id])
self.test_record.message_subscribe([self.partner_1.id])
with self.assertNotifications(partner_employee=(1, 'inbox', 'unread'), partner_admin=(0, '', ''), partner_1=(1, 'email', 'read')):
with self.assertSinglePostNotifications([
{'partner': self.partner_employee, 'type': 'inbox'},
{'partner': self.partner_1, 'type': 'email'}], {'content': 'NewBody'}):
self.test_record.message_post(
body='Test', message_type='comment', subtype='mail.mt_comment',
body='NewBody', message_type='comment', subtype='mail.mt_comment',
partner_ids=[self.user_employee.partner_id.id])
with self.assertNotifications(partner_admin=(0, '', ''), partner_1=(1, 'email', 'read'), partner_portal=(1, 'email', 'read')):
with self.assertSinglePostNotifications([
{'partner': self.partner_1, 'type': 'email'},
{'partner': self.partner_portal, 'type': 'email'}], {'content': 'ToPortal'}):
self.test_record.message_post(
body='Test', message_type='comment', subtype='mail.mt_comment',
body='ToPortal', message_type='comment', subtype='mail.mt_comment',
partner_ids=[self.partner_portal.id])
def test_post_inactive_follower(self):
@@ -52,28 +50,33 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
# Even if it shouldn't be the case, we want to be sure that odoobot is not notified
(self.user_employee | self.user_admin).write({'notification_type': 'inbox'})
self.test_record._message_subscribe(self.user_employee.partner_id.ids)
with self.assertNotifications(partner_employee=(1, 'inbox', 'unread')):
with self.assertSinglePostNotifications([{'partner': self.partner_employee, 'type': 'inbox'}], {'content': 'Test'}):
self.test_record.message_post(
body='Test', message_type='comment', subtype='mail.mt_comment')
self.user_employee.active = False
# at this point, partner is still active and would receive an email notification
self.user_employee.partner_id._write({'active': False})
with self.assertNotifications(partner_employee=(0, '', '')):
with self.assertPostNotifications([{'content': 'Test', 'notif': []}]):
self.test_record.message_post(
body='Test', message_type='comment', subtype='mail.mt_comment')
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_notifications(self):
_body, _body_alt, _subject = '<p>Test Body</p>', 'Test Body', 'Test Subject'
_body, _subject = '<p>Test Body</p>', 'Test Subject'
# subscribe second employee to the group to test notifications
self.test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id])
msg = self.test_record.with_user(self.user_employee).message_post(
body=_body, subject=_subject,
message_type='comment', subtype='mt_comment',
partner_ids=[self.partner_1.id, self.partner_2.id]
)
with self.assertSinglePostNotifications([
{'partner': self.partner_1, 'type': 'email'},
{'partner': self.partner_2, 'type': 'email'},
{'partner': self.partner_admin, 'type': 'email'}], {'content': _body}, mail_unlink_sent=True):
msg = self.test_record.with_user(self.user_employee).message_post(
body=_body, subject=_subject,
message_type='comment', subtype='mt_comment',
partner_ids=[self.partner_1.id, self.partner_2.id]
)
# message content
self.assertEqual(msg.subject, _subject)
@@ -86,14 +89,6 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
self.assertFalse(self.env['mail.mail'].search([('mail_message_id', '=', msg.id)]),
'message_post: mail.mail notifications should have been auto-deleted')
# notification emails
self.assertEmails(
self.user_employee.partner_id,
[[self.partner_1], [self.partner_2], [self.user_admin.partner_id]],
reply_to=msg.reply_to, subject=_subject,
body_content=_body, body_alt_content=_body_alt,
references=False)
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_notifications_keep_emails(self):
self.test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id])
@@ -128,13 +123,14 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
'datas': 'bWlncmF0aW9uIHRlc3Q=',
'res_model': 'mail.compose.message', 'res_id': 0})
msg = self.test_record.with_user(self.user_employee).message_post(
body='Test', subject='Test',
message_type='comment', subtype='mt_comment',
attachment_ids=[_attach_1.id, _attach_2.id],
partner_ids=[self.partner_1.id],
attachments=_attachments,
)
with self.mock_mail_gateway():
msg = self.test_record.with_user(self.user_employee).message_post(
body='Test', subject='Test',
message_type='comment', subtype='mt_comment',
attachment_ids=[_attach_1.id, _attach_2.id],
partner_ids=[self.partner_1.id],
attachments=_attachments,
)
# message attachments
self.assertEqual(len(msg.attachment_ids), 4)
@@ -146,7 +142,7 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
'message_post: mail.message attachments duplicated')
# notification email attachments
self.assertEmails(self.user_employee.partner_id, [[self.partner_1]])
self.assertSentEmail(self.user_employee.partner_id, [self.partner_1])
# self.assertEqual(len(self._mails), 1)
self.assertEqual(len(self._mails[0]['attachments']), 4)
self.assertIn(('List1', b'My first attachment', 'application/octet-stream'), self._mails[0]['attachments'])
@@ -156,25 +152,27 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_answer(self):
parent_msg = self.test_record.with_user(self.user_employee).message_post(
body='<p>Test</p>', subject='Test Subject',
message_type='comment', subtype='mt_comment')
with self.mock_mail_gateway():
parent_msg = self.test_record.with_user(self.user_employee).message_post(
body='<p>Test</p>', subject='Test Subject',
message_type='comment', subtype='mt_comment')
self.assertEqual(parent_msg.partner_ids, self.env['res.partner'])
self.assertEmails(self.user_employee.partner_id, [])
self.assertNotSentEmail()
msg = self.test_record.with_user(self.user_employee).message_post(
body='<p>Test Answer</p>',
message_type='comment', subtype='mt_comment',
partner_ids=[self.partner_1.id],
parent_id=parent_msg.id)
with self.assertPostNotifications([{'content': '<p>Test Answer</p>', 'notif': [{'partner': self.partner_1, 'type': 'email'}]}]):
msg = self.test_record.with_user(self.user_employee).message_post(
body='<p>Test Answer</p>',
message_type='comment', subtype='mt_comment',
partner_ids=[self.partner_1.id],
parent_id=parent_msg.id)
self.assertEqual(msg.parent_id.id, parent_msg.id)
self.assertEqual(msg.partner_ids, self.partner_1)
self.assertEqual(parent_msg.partner_ids, self.env['res.partner'])
# check notification emails: references
self.assertEmails(self.user_employee.partner_id, [[self.partner_1]], ref_content='openerp-%d-mail.test.simple' % self.test_record.id)
self.assertSentEmail(self.user_employee.partner_id, [self.partner_1], ref_content='openerp-%d-mail.test.simple' % self.test_record.id)
# self.assertTrue(all('openerp-%d-mail.test.simple' % self.test_record.id in m['references'] for m in self._mails))
new_msg = self.test_record.with_user(self.user_employee).message_post(
@@ -187,14 +185,17 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_portal_ok(self):
with patch.object(MailTestSimple, 'check_access_rights', return_value=True):
self.test_record.message_subscribe((self.partner_1 | self.user_employee.partner_id).ids)
self.test_record.message_subscribe((self.partner_1 | self.user_employee.partner_id).ids)
with self.assertPostNotifications([{'content': '<p>Test</p>', 'notif': [
{'partner': self.partner_employee, 'type': 'inbox'},
{'partner': self.partner_1, 'type': 'email'}]}
]), patch.object(MailTestSimple, 'check_access_rights', return_value=True):
new_msg = self.test_record.with_user(self.user_portal).message_post(
body='<p>Test</p>', subject='Subject',
message_type='comment', subtype='mt_comment')
self.assertEqual(new_msg.sudo().notified_partner_ids, (self.partner_1 | self.user_employee.partner_id))
self.assertEmails(self.user_portal.partner_id, [[self.partner_1], [self.user_employee.partner_id]])
def test_post_portal_crash(self):
with self.assertRaises(AccessError):
@@ -233,6 +234,7 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
self.assertEqual(new_note.email_from, formataddr((self.user_employee.name, self.user_employee.email)))
self.assertEqual(new_note.notified_partner_ids, self.env['res.partner'])
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_notify(self):
self.user_employee.write({'notification_type': 'inbox'})
new_notification = self.test_record.message_notify(
@@ -281,12 +283,14 @@ class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails):
# Force the attachments of the template to be in the natural order.
self.email_template.invalidate_cache(['attachment_ids'], ids=self.email_template.ids)
test_record.with_user(self.user_employee).message_post_with_template(self.email_template.id, composition_mode='comment')
with self.mock_mail_gateway():
test_record.with_user(self.user_employee).message_post_with_template(self.email_template.id, composition_mode='comment')
new_partners = self.env['res.partner'].search([('email', 'in', [email_1, email_2])])
self.assertEmails(
self.user_employee.partner_id,
[[self.partner_1], [self.partner_2], [new_partners[0]], [new_partners[1]], [self.partner_admin]],
subject='About %s' % test_record.name,
body_content=test_record.name,
attachments=[('first.txt', b'My first attachment', 'text/plain'), ('second.txt', b'My second attachment', 'text/plain')])
for r in [self.partner_1, self.partner_2, new_partners[0], new_partners[1], self.partner_admin]:
self.assertSentEmail(
self.user_employee.partner_id,
[r],
subject='About %s' % test_record.name,
body_content=test_record.name,
attachments=[('first.txt', b'My first attachment', 'text/plain'), ('second.txt', b'My second attachment', 'text/plain')])