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.

86 lines
3.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. self.assertEqual(stat.tracking_state, 'opened')
  69. def test_contact_tracking_emails(self):
  70. self.mailing.send_mail()
  71. for stat in self.mailing.statistics_ids:
  72. if stat.mail_mail_id:
  73. stat.mail_mail_id.send()
  74. self.assertEqual(len(self.contact_a.tracking_email_ids), 1)
  75. self.contact_a.email = 'other_contact_a@example.com'
  76. self.assertEqual(len(self.contact_a.tracking_email_ids), 0)
  77. self.contact_a.email = 'contact_a@example.com'
  78. self.assertEqual(len(self.contact_a.tracking_email_ids), 1)