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.

131 lines
5.3 KiB

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