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.

112 lines
4.7 KiB

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