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.

116 lines
4.9 KiB

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