You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
5.0 KiB

  1. # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  2. # Copyright 2017 Vicent Cubells - <vicent.cubells@tecnativa.com>
  3. # Copyright 2017 David Vidal - <david.vidal@tecnativa.com>
  4. # Copyright 2018 Tecnativa - Pedro M. Baeza
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. import mock
  7. from odoo.tools import mute_logger
  8. from odoo.tests.common import at_install, post_install, TransactionCase
  9. mock_send_email = ('odoo.addons.base.ir.ir_mail_server.'
  10. 'IrMailServer.send_email')
  11. @at_install(False)
  12. @post_install(True)
  13. class TestMassMailing(TransactionCase):
  14. def setUp(self, *args, **kwargs):
  15. super(TestMassMailing, self).setUp(*args, **kwargs)
  16. self.list = self.env['mail.mass_mailing.list'].create({
  17. 'name': 'Test mail tracking',
  18. })
  19. self.list.name = '%s #%s' % (self.list.name, self.list.id)
  20. self.contact_a = self.env['mail.mass_mailing.contact'].create({
  21. 'list_ids': [(6, 0, self.list.ids)],
  22. 'name': 'Test contact A',
  23. 'email': 'contact_a@example.com',
  24. })
  25. self.mailing = self.env['mail.mass_mailing'].create({
  26. 'name': 'Test subject',
  27. 'email_from': 'from@example.com',
  28. 'mailing_model_id': self.env.ref(
  29. 'mass_mailing.model_mail_mass_mailing_contact'
  30. ).id,
  31. 'mailing_domain': "[('list_ids', '=', %d), "
  32. "('opt_out', '=', False)]" % self.list.id,
  33. 'contact_list_ids': [(6, False, [self.list.id])],
  34. 'body_html': '<p>Test email body</p>',
  35. 'reply_to_mode': 'email',
  36. })
  37. @mute_logger('odoo.addons.mail.models.mail_mail')
  38. def test_smtp_error(self):
  39. with mock.patch(mock_send_email) as mock_func:
  40. mock_func.side_effect = Warning('Mock test error')
  41. self.mailing.send_mail()
  42. for stat in self.mailing.statistics_ids:
  43. if stat.mail_mail_id:
  44. stat.mail_mail_id.send()
  45. tracking = self.env['mail.tracking.email'].search([
  46. ('mail_id_int', '=', stat.mail_mail_id_int),
  47. ])
  48. for track in tracking:
  49. self.assertEqual('error', track.state)
  50. self.assertEqual('Warning', track.error_type)
  51. self.assertEqual('Mock test error',
  52. track.error_description)
  53. self.assertTrue(self.contact_a.email_bounced)
  54. def test_tracking_email_link(self):
  55. self.mailing.send_mail()
  56. for stat in self.mailing.statistics_ids:
  57. if stat.mail_mail_id:
  58. stat.mail_mail_id.send()
  59. tracking_email = self.env['mail.tracking.email'].search([
  60. ('mail_id_int', '=', stat.mail_mail_id_int),
  61. ])
  62. self.assertTrue(tracking_email)
  63. self.assertEqual(
  64. tracking_email.mass_mailing_id.id, self.mailing.id)
  65. self.assertEqual(tracking_email.mail_stats_id.id, stat.id)
  66. self.assertEqual(stat.mail_tracking_id.id, tracking_email.id)
  67. # And now open the email
  68. metadata = {
  69. 'ip': '127.0.0.1',
  70. 'user_agent': 'Odoo Test/1.0',
  71. 'os_family': 'linux',
  72. 'ua_family': 'odoo',
  73. }
  74. tracking_email.event_create('open', metadata)
  75. self.assertTrue(stat.opened)
  76. def _tracking_email_bounce(self, event_type, state):
  77. self.mailing.send_mail()
  78. for stat in self.mailing.statistics_ids:
  79. if stat.mail_mail_id:
  80. stat.mail_mail_id.send()
  81. tracking_email = self.env['mail.tracking.email'].search([
  82. ('mail_id_int', '=', stat.mail_mail_id_int),
  83. ])
  84. # And now mark the email as bounce
  85. metadata = {
  86. 'bounce_type': '499',
  87. 'bounce_description': 'Unable to connect to MX servers',
  88. }
  89. tracking_email.event_create(event_type, metadata)
  90. self.assertTrue(stat.bounced)
  91. def test_tracking_email_hard_bounce(self):
  92. self._tracking_email_bounce('hard_bounce', 'bounced')
  93. def test_tracking_email_soft_bounce(self):
  94. self._tracking_email_bounce('soft_bounce', 'soft-bounced')
  95. def test_tracking_email_reject(self):
  96. self._tracking_email_bounce('reject', 'rejected')
  97. def test_tracking_email_spam(self):
  98. self._tracking_email_bounce('spam', 'spam')
  99. def test_contact_tracking_emails(self):
  100. self._tracking_email_bounce('hard_bounce', 'bounced')
  101. self.assertTrue(self.contact_a.email_bounced)
  102. self.assertTrue(self.contact_a.email_score < 50.0)
  103. self.contact_a.email = 'other_contact_a@example.com'
  104. self.assertFalse(self.contact_a.email_bounced)
  105. self.assertTrue(self.contact_a.email_score == 50.0)
  106. self.contact_a.email = 'contact_a@example.com'
  107. self.assertTrue(self.contact_a.email_bounced)
  108. self.assertTrue(self.contact_a.email_score < 50.0)