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.

118 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 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_id': self.list.id,
  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': 'mail.mass_mailing.contact',
  29. 'mailing_domain': "[('list_id', 'in', [%d]), "
  30. "('opt_out', '=', False)]" % self.list.id,
  31. 'contact_list_ids': [(6, False, [self.list.id])],
  32. 'body_html': '<p>Test email body</p>',
  33. 'reply_to_mode': 'email',
  34. })
  35. @mute_logger('odoo.addons.mail.models.mail_mail')
  36. def test_smtp_error(self):
  37. with mock.patch(mock_send_email) as mock_func:
  38. mock_func.side_effect = Warning('Mock test error')
  39. self.mailing.send_mail()
  40. for stat in self.mailing.statistics_ids:
  41. if stat.mail_mail_id:
  42. stat.mail_mail_id.send()
  43. tracking = self.env['mail.tracking.email'].search([
  44. ('mail_id_int', '=', stat.mail_mail_id_int),
  45. ])
  46. for track in tracking:
  47. self.assertEqual('error', track.state)
  48. self.assertEqual('Warning', track.error_type)
  49. self.assertEqual('Mock test error',
  50. track.error_description)
  51. self.assertTrue(self.contact_a.email_bounced)
  52. def test_tracking_email_link(self):
  53. self.mailing.send_mail()
  54. for stat in self.mailing.statistics_ids:
  55. if stat.mail_mail_id:
  56. stat.mail_mail_id.send()
  57. tracking_email = self.env['mail.tracking.email'].search([
  58. ('mail_id_int', '=', stat.mail_mail_id_int),
  59. ])
  60. self.assertTrue(tracking_email)
  61. self.assertEqual(
  62. tracking_email.mass_mailing_id.id, self.mailing.id)
  63. self.assertEqual(tracking_email.mail_stats_id.id, stat.id)
  64. self.assertEqual(stat.mail_tracking_id.id, tracking_email.id)
  65. # And now open the email
  66. metadata = {
  67. 'ip': '127.0.0.1',
  68. 'user_agent': 'Odoo Test/1.0',
  69. 'os_family': 'linux',
  70. 'ua_family': 'odoo',
  71. }
  72. tracking_email.event_create('open', metadata)
  73. self.assertTrue(stat.opened)
  74. def _tracking_email_bounce(self, event_type, state):
  75. self.mailing.send_mail()
  76. for stat in self.mailing.statistics_ids:
  77. if stat.mail_mail_id:
  78. stat.mail_mail_id.send()
  79. tracking_email = self.env['mail.tracking.email'].search([
  80. ('mail_id_int', '=', stat.mail_mail_id_int),
  81. ])
  82. # And now mark the email as bounce
  83. metadata = {
  84. 'bounce_type': '499',
  85. 'bounce_description': 'Unable to connect to MX servers',
  86. }
  87. tracking_email.event_create(event_type, metadata)
  88. self.assertTrue(stat.bounced)
  89. def test_tracking_email_hard_bounce(self):
  90. self._tracking_email_bounce('hard_bounce', 'bounced')
  91. def test_tracking_email_soft_bounce(self):
  92. self._tracking_email_bounce('soft_bounce', 'soft-bounced')
  93. def test_tracking_email_reject(self):
  94. self._tracking_email_bounce('reject', 'rejected')
  95. def test_tracking_email_spam(self):
  96. self._tracking_email_bounce('spam', 'spam')
  97. def test_contact_tracking_emails(self):
  98. self._tracking_email_bounce('hard_bounce', 'bounced')
  99. self.assertTrue(self.contact_a.email_bounced)
  100. self.assertTrue(self.contact_a.email_score < 50.0)
  101. self.contact_a.email = 'other_contact_a@example.com'
  102. self.assertFalse(self.contact_a.email_bounced)
  103. self.assertTrue(self.contact_a.email_score == 50.0)
  104. self.contact_a.email = 'contact_a@example.com'
  105. self.assertTrue(self.contact_a.email_bounced)
  106. self.assertTrue(self.contact_a.email_score < 50.0)