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.

179 lines
7.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.tests.common import TransactionCase
  5. from openerp.exceptions import Warning as UserError
  6. class TestMassMailingSending(TransactionCase):
  7. def setUp(self, *args, **kwargs):
  8. super(TestMassMailingSending, self).setUp(*args, **kwargs)
  9. self.list = self.env['mail.mass_mailing.list'].create({
  10. 'name': 'Test list',
  11. })
  12. # Define a lower batch size for testing purposes
  13. self.env['ir.config_parameter'].set_param(
  14. 'mail.mass_mailing.sending.batch_size', 5)
  15. self.contact_a = self.env['mail.mass_mailing.contact'].create({
  16. 'list_id': self.list.id,
  17. 'name': 'Test contact A',
  18. 'email': 'contact_a@example.org',
  19. })
  20. self.contact_b = self.env['mail.mass_mailing.contact'].create({
  21. 'list_id': self.list.id,
  22. 'name': 'Test contact B',
  23. 'email': 'contact_b@example.org',
  24. })
  25. for i in range(1, 6):
  26. self.env['mail.mass_mailing.contact'].create({
  27. 'list_id': self.list.id,
  28. 'name': 'Test contact %s' % i,
  29. 'email': 'contact_%s@example.org' % i,
  30. })
  31. self.mass_mailing = self.env['mail.mass_mailing'].create({
  32. 'name': 'Test mass mailing',
  33. 'email_from': 'from@example.org',
  34. 'mailing_model': 'mail.mass_mailing.contact',
  35. 'mailing_domain': [
  36. ('list_id', 'in', [self.list.id]),
  37. ('opt_out', '=', False),
  38. ],
  39. 'contact_list_ids': [(6, False, [self.list.id])],
  40. 'body_html': '<p>Hello world!</p>',
  41. 'reply_to_mode': 'email',
  42. })
  43. self.partner = self.env['res.partner'].create({
  44. 'name': 'Test partner',
  45. 'email': 'partner@example.org',
  46. })
  47. self.mass_mailing_short = self.env['mail.mass_mailing'].create({
  48. 'name': 'Test mass mailing short',
  49. 'email_from': 'from@example.org',
  50. 'mailing_model': 'res.partner',
  51. 'mailing_domain': [
  52. ('id', 'in', [self.partner.id]),
  53. ('opt_out', '=', False),
  54. ],
  55. 'body_html': '<p>Hello partner!</p>',
  56. 'reply_to_mode': 'email',
  57. })
  58. def test_cron_contacts(self):
  59. self.mass_mailing.with_context(sending_queue_enabled=True).send_mail()
  60. sendings = self.env['mail.mass_mailing.sending'].search([
  61. ('mass_mailing_id', '=', self.mass_mailing.id),
  62. ])
  63. stats = self.env['mail.mail.statistics'].search([
  64. ('mass_mailing_id', '=', self.mass_mailing.id),
  65. ])
  66. # Sending in 'enqueued' state and 0 email stats created
  67. self.assertEqual(1, len(sendings))
  68. self.assertEqual(0, len(stats))
  69. sending = sendings[0]
  70. self.assertEqual('enqueued', sending.state)
  71. self.assertEqual(7, sending.pending_count)
  72. self.assertEqual('sending', self.mass_mailing.state)
  73. self.assertEqual(7, self.mass_mailing.pending_count)
  74. # Create email stats
  75. sending.cron()
  76. stats = self.env['mail.mail.statistics'].search([
  77. ('mass_mailing_id', '=', self.mass_mailing.id),
  78. ])
  79. self.env.invalidate_all()
  80. # Sending in 'enqueued' state and 5 stats created, 2 pending, 5 sending
  81. self.assertEqual(5, len(stats))
  82. self.assertEqual('enqueued', sending.state)
  83. self.assertEqual(2, sending.pending_count)
  84. self.assertEqual(5, sending.sending_count)
  85. self.assertEqual('sending', self.mass_mailing.state)
  86. for stat in stats:
  87. if stat.mail_mail_id:
  88. stat.mail_mail_id.send()
  89. self.env.invalidate_all()
  90. # Check that 5 emails are already sent
  91. self.assertEqual(0, sending.sending_count)
  92. self.assertEqual(5, sending.sent_count)
  93. sending.cron()
  94. stats = self.env['mail.mail.statistics'].search([
  95. ('mass_mailing_id', '=', self.mass_mailing.id),
  96. ])
  97. self.env.invalidate_all()
  98. # Sending in 'sending' state and 7 stats created, 0 pending, 2 sending
  99. self.assertEqual(7, len(stats))
  100. self.assertEqual('sending', sending.state)
  101. self.assertEqual(0, sending.pending_count)
  102. self.assertEqual(2, sending.sending_count)
  103. self.assertEqual('sending', self.mass_mailing.state)
  104. for stat in stats:
  105. if stat.mail_mail_id:
  106. stat.mail_mail_id.send()
  107. self.env.invalidate_all()
  108. # Check that 7 emails are already sent
  109. self.assertEqual('sent', sending.state)
  110. self.assertEqual(0, sending.sending_count)
  111. self.assertEqual(7, sending.sent_count)
  112. self.assertEqual(0, sending.failed_count)
  113. self.assertEqual('done', self.mass_mailing.state)
  114. def test_cron_partners(self):
  115. self.mass_mailing_short.with_context(
  116. sending_queue_enabled=True).send_mail()
  117. sendings = self.env['mail.mass_mailing.sending'].search([
  118. ('mass_mailing_id', '=', self.mass_mailing_short.id),
  119. ])
  120. stats = self.env['mail.mail.statistics'].search([
  121. ('mass_mailing_id', '=', self.mass_mailing_short.id),
  122. ])
  123. # Sending in 'draft' state and 1 email stats created
  124. self.assertEqual(1, len(sendings))
  125. self.assertEqual(1, len(stats))
  126. sending = sendings[0]
  127. self.assertEqual('sending', sending.state)
  128. self.assertEqual(0, sending.pending_count)
  129. self.assertEqual('sending', self.mass_mailing_short.state)
  130. self.assertEqual(1, self.mass_mailing_short.pending_count)
  131. for stat in stats:
  132. if stat.mail_mail_id:
  133. stat.mail_mail_id.send()
  134. self.env.invalidate_all()
  135. # Check that 1 email are already sent
  136. self.assertEqual('sent', sending.state)
  137. self.assertEqual(0, sending.sending_count)
  138. self.assertEqual(1, sending.sent_count)
  139. self.assertEqual(0, sending.failed_count)
  140. self.assertEqual('done', self.mass_mailing_short.state)
  141. def test_concurrent(self):
  142. self.mass_mailing.with_context(sending_queue_enabled=True).send_mail()
  143. with self.assertRaises(UserError):
  144. self.mass_mailing.with_context(
  145. sending_queue_enabled=True).send_mail()
  146. def test_read_group(self):
  147. groups = self.env['mail.mass_mailing'].read_group(
  148. [('sent_date', '<', '1900-12-31')], ['state', 'name'], ['state'])
  149. self.assertTrue([
  150. x for x in groups if (
  151. x['state_count'] == 0 and x['state'][0] == 'sending')
  152. ])
  153. def test_no_recipients(self):
  154. empty_list = self.env['mail.mass_mailing.list'].create({
  155. 'name': 'Test list with no recipients',
  156. })
  157. mass_mailing = self.env['mail.mass_mailing'].create({
  158. 'name': 'Test mass mailing with no recipients',
  159. 'email_from': 'from@example.org',
  160. 'mailing_model': 'mail.mass_mailing.contact',
  161. 'mailing_domain': [
  162. ('list_id', 'in', [empty_list.id]),
  163. ('opt_out', '=', False),
  164. ],
  165. 'contact_list_ids': [(6, False, [empty_list.id])],
  166. 'body_html': '<p>Hello no one!</p>',
  167. 'reply_to_mode': 'email',
  168. })
  169. with self.assertRaises(UserError):
  170. mass_mailing.send_mail()