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.

113 lines
4.4 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. from openerp.tests.common import TransactionCase
  5. from openerp.exceptions import Warning as UserError
  6. # One test case per method
  7. class TestMassMailing(TransactionCase):
  8. def setUp(self):
  9. super(TestMassMailing, self).setUp()
  10. self.list = self.env['mail.mass_mailing.list'].create({
  11. 'name': 'Test mail tracking',
  12. })
  13. self.contact_a = self.env['mail.mass_mailing.contact'].create({
  14. 'list_id': self.list.id,
  15. 'name': 'Test contact A',
  16. 'email': 'contact_a@example.com',
  17. })
  18. self.mailing = self.env['mail.mass_mailing'].create({
  19. 'name': 'Test subject',
  20. 'email_from': 'from@example.com',
  21. 'mailing_model': 'mail.mass_mailing.contact',
  22. 'mailing_domain': "[('list_id', 'in', [%d]), "
  23. "('opt_out', '=', False)]" % self.list.id,
  24. 'contact_list_ids': [(6, False, [self.list.id])],
  25. 'body_html': '<p>Test email body</p>',
  26. 'reply_to_mode': 'email',
  27. })
  28. def resend_mass_mailing(self, first, second):
  29. self.mailing.send_mail()
  30. self.assertEqual(len(self.mailing.statistics_ids), first)
  31. self.env['mail.mass_mailing.contact'].create({
  32. 'list_id': self.list.id,
  33. 'name': 'Test contact B',
  34. 'email': 'contact_b@example.com',
  35. })
  36. self.mailing.send_mail()
  37. self.assertEqual(len(self.mailing.statistics_ids), second)
  38. def test_avoid_resend_enable(self):
  39. self.mailing.avoid_resend = True
  40. self.resend_mass_mailing(1, 2)
  41. with self.assertRaises(UserError):
  42. self.mailing.send_mail()
  43. def test_avoid_resend_disable(self):
  44. self.mailing.avoid_resend = False
  45. self.resend_mass_mailing(1, 3)
  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.mailing.send_mail()
  93. for stat in self.mailing.statistics_ids:
  94. if stat.mail_mail_id:
  95. stat.mail_mail_id.send()
  96. self.assertEqual(len(self.contact_a.tracking_email_ids), 1)
  97. self.contact_a.email = 'other_contact_a@example.com'
  98. self.assertEqual(len(self.contact_a.tracking_email_ids), 0)
  99. self.contact_a.email = 'contact_a@example.com'
  100. self.assertEqual(len(self.contact_a.tracking_email_ids), 1)