test_mailing_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import lxml.html
  4. from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon
  5. from odoo.fields import Command
  6. from odoo.tests.common import users, tagged
  7. from odoo.tools import mute_logger
  8. @tagged('mailing_manage')
  9. class TestMailingTest(TestMassMailCommon):
  10. @users('user_marketing')
  11. @mute_logger('odoo.addons.mail.models.mail_render_mixin')
  12. def test_mailing_test_button(self):
  13. mailing = self.env['mailing.mailing'].create({
  14. 'name': 'TestButton',
  15. 'subject': 'Subject {{ object.name }}',
  16. 'preview': 'Preview {{ object.name }}',
  17. 'state': 'draft',
  18. 'mailing_type': 'mail',
  19. 'body_html': '<p>Hello <t t-out="object.name"/></p>',
  20. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  21. })
  22. mailing_test = self.env['mailing.mailing.test'].create({
  23. 'email_to': 'test@test.com',
  24. 'mass_mailing_id': mailing.id,
  25. })
  26. with self.mock_mail_gateway():
  27. mailing_test.send_mail_test()
  28. # not great but matches send_mail_test, maybe that should be a method
  29. # on mailing_test?
  30. record = self.env[mailing.mailing_model_real].search([], limit=1)
  31. first_child = lxml.html.fromstring(self._mails.pop()['body']).xpath('//body/*[1]')[0]
  32. self.assertEqual(first_child.tag, 'div')
  33. self.assertIn('display:none', first_child.get('style'),
  34. "the preview node should be hidden")
  35. self.assertEqual(first_child.text.strip(), "Preview " + record.name,
  36. "the preview node should contain the preview text")
  37. # Test if bad inline_template in the subject raises an error
  38. mailing.write({'subject': 'Subject {{ object.name_id.id }}'})
  39. with self.mock_mail_gateway(), self.assertRaises(Exception):
  40. mailing_test.send_mail_test()
  41. # Test if bad inline_template in the body raises an error
  42. mailing.write({
  43. 'subject': 'Subject {{ object.name }}',
  44. 'body_html': '<p>Hello <t t-out="object.name_id.id"/></p>',
  45. })
  46. with self.mock_mail_gateway(), self.assertRaises(Exception):
  47. mailing_test.send_mail_test()
  48. # Test if bad inline_template in the preview raises an error
  49. mailing.write({
  50. 'body_html': '<p>Hello <t t-out="object.name"/></p>',
  51. 'preview': 'Preview {{ object.name_id.id }}',
  52. })
  53. with self.mock_mail_gateway(), self.assertRaises(Exception):
  54. mailing_test.send_mail_test()
  55. def test_mailing_test_equals_reality(self):
  56. """
  57. Check that both test and real emails will format the qweb and inline placeholders correctly in body and subject.
  58. """
  59. contact_list = self.env['mailing.list'].create({
  60. 'name': 'Testers',
  61. 'contact_ids': [Command.create({
  62. 'name': 'Mitchell Admin',
  63. 'email': 'real@real.com',
  64. })],
  65. })
  66. mailing = self.env['mailing.mailing'].create({
  67. 'name': 'TestButton',
  68. 'subject': 'Subject {{ object.name }} <t t-out="object.name"/>',
  69. 'state': 'draft',
  70. 'mailing_type': 'mail',
  71. 'body_html': '<p>Hello {{ object.name }} <t t-out="object.name"/></p>',
  72. 'mailing_model_id': self.env['ir.model']._get('mailing.list').id,
  73. 'contact_list_ids': [contact_list.id],
  74. })
  75. mailing_test = self.env['mailing.mailing.test'].create({
  76. 'email_to': 'test@test.com',
  77. 'mass_mailing_id': mailing.id,
  78. })
  79. with self.mock_mail_gateway():
  80. mailing_test.send_mail_test()
  81. expected_subject = 'Subject Mitchell Admin <t t-out="object.name"/>'
  82. expected_body = 'Hello {{ object.name }} Mitchell Admin'
  83. self.assertSentEmail(self.env.user.partner_id, ['test@test.com'],
  84. subject=expected_subject,
  85. body_content=expected_body)
  86. with self.mock_mail_gateway():
  87. # send the mailing
  88. mailing.action_launch()
  89. self.env.ref('mass_mailing.ir_cron_mass_mailing_queue').method_direct_trigger()
  90. self.assertSentEmail(self.env.user.partner_id, ['real@real.com'],
  91. subject=expected_subject,
  92. body_content=expected_body)