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.

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