test_mailing_mailing_schedule_date.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime
  4. from freezegun import freeze_time
  5. from odoo.addons.base.tests.test_ir_cron import CronMixinCase
  6. from odoo.addons.mass_mailing.tests.common import MassMailCommon
  7. from odoo.tests import users, Form
  8. from odoo.tools import mute_logger
  9. class TestMailingScheduleDateWizard(MassMailCommon, CronMixinCase):
  10. @mute_logger('odoo.addons.mail.models.mail_mail')
  11. @users('user_marketing')
  12. def test_mailing_next_departure(self):
  13. # test if mailing.mailing.next_departure is correctly set taking into account
  14. # presence of implicitly created cron triggers (since odoo v15). These should
  15. # launch cron job before its schedule nextcall datetime (if scheduled_date < nextcall)
  16. cron_job = self.env.ref('mass_mailing.ir_cron_mass_mailing_queue').sudo()
  17. cron_job.write({'nextcall' : datetime(2023, 2, 18, 9, 0)})
  18. cron_job_id = cron_job.id
  19. # case where user click on "Send" button (action_launch)
  20. with freeze_time(datetime(2023, 2, 17, 9, 0)):
  21. with self.capture_triggers(cron_job_id) as capt:
  22. mailing = self.env['mailing.mailing'].create({
  23. 'name': 'mailing',
  24. 'subject': 'some subject',
  25. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  26. 'state' : 'draft'
  27. })
  28. mailing.action_launch()
  29. capt.records.ensure_one()
  30. # assert that the schedule_date and schedule_type fields are correct and that the mailing is put in queue
  31. self.assertEqual(mailing.next_departure, datetime(2023, 2, 17, 9, 0))
  32. self.assertIsNot(mailing.schedule_date, cron_job.nextcall)
  33. self.assertEqual(mailing.schedule_type, 'now')
  34. self.assertEqual(mailing.state, 'in_queue')
  35. self.assertEqual(capt.records.call_at, datetime(2023, 2, 17, 9, 0)) #verify that cron.trigger exists
  36. # case where client uses schedule wizard to chose a date between now and cron.job nextcall
  37. with freeze_time(datetime(2023, 2, 17, 9, 0)):
  38. with self.capture_triggers(cron_job_id) as capt:
  39. mailing = self.env['mailing.mailing'].create({
  40. 'name': 'mailing',
  41. 'subject': 'some subject',
  42. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  43. 'state' : 'draft',
  44. 'schedule_date' : datetime(2023, 2, 17, 11, 0),
  45. 'schedule_type' : 'scheduled'
  46. })
  47. mailing.action_schedule()
  48. capt.records.ensure_one()
  49. self.assertEqual(mailing.schedule_date, datetime(2023, 2, 17, 11, 0))
  50. self.assertEqual(mailing.next_departure, datetime(2023, 2, 17, 11, 0))
  51. self.assertEqual(mailing.schedule_type, 'scheduled')
  52. self.assertEqual(mailing.state, 'in_queue')
  53. self.assertEqual(capt.records.call_at, datetime(2023, 2, 17, 11, 0)) #verify that cron.trigger exists
  54. # case where client uses schedule wizard to chose a date after cron.job nextcall
  55. # which means mails will get send after that date (datetime(2023, 2, 18, 9, 0))
  56. with freeze_time(datetime(2023, 2, 17, 9, 0)):
  57. with self.capture_triggers(cron_job_id) as capt:
  58. mailing = self.env['mailing.mailing'].create({
  59. 'name': 'mailing',
  60. 'subject': 'some subject',
  61. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  62. 'state' : 'draft',
  63. 'schedule_date' : datetime(2024, 2, 17, 11, 0),
  64. 'schedule_type' : 'scheduled'
  65. })
  66. mailing.action_schedule()
  67. capt.records.ensure_one()
  68. self.assertEqual(mailing.schedule_date, datetime(2024, 2, 17, 11, 0))
  69. self.assertEqual(mailing.next_departure, datetime(2024, 2, 17, 11, 0))
  70. self.assertEqual(mailing.schedule_type, 'scheduled')
  71. self.assertEqual(mailing.state, 'in_queue')
  72. self.assertEqual(capt.records.call_at, datetime(2024, 2, 17, 11, 0)) #verify that cron.trigger exists
  73. # case where client uses schedule wizard to chose a date in the past
  74. with freeze_time(datetime(2023, 2, 17, 9, 0)):
  75. with self.capture_triggers(cron_job_id) as capt:
  76. mailing = self.env['mailing.mailing'].create({
  77. 'name': 'mailing',
  78. 'subject': 'some subject',
  79. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  80. 'state' : 'draft',
  81. 'schedule_date' : datetime(2024, 2, 17, 11, 0),
  82. 'schedule_type' : 'scheduled'
  83. })
  84. # create a schedule date wizard
  85. # Have to use wizard for this case to simulate schedule date in the past
  86. # Otherwise "state" doesn't get update from draft to'in_queue'
  87. # in test env vs production env (see mailing.mailing.schedule.date wizard)
  88. wizard_form = Form(
  89. self.env['mailing.mailing.schedule.date'].with_context(default_mass_mailing_id=mailing.id))
  90. # set a schedule date
  91. wizard_form.schedule_date = datetime(2022, 2, 17, 11, 0)
  92. wizard = wizard_form.save()
  93. wizard.action_schedule_date()
  94. capt.records.ensure_one()
  95. self.assertEqual(mailing.schedule_date, datetime(2022, 2, 17, 11, 0))
  96. self.assertEqual(mailing.next_departure, datetime(2023, 2, 17, 9, 0)) #now
  97. self.assertEqual(mailing.schedule_type, 'scheduled')
  98. self.assertEqual(mailing.state, 'in_queue')
  99. self.assertEqual(capt.records.call_at, datetime(2022, 2, 17, 11, 0)) #verify that cron.trigger exists
  100. def test_mailing_schedule_date(self):
  101. mailing = self.env['mailing.mailing'].create({
  102. 'name': 'mailing',
  103. 'subject': 'some subject',
  104. 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
  105. })
  106. # create a schedule date wizard
  107. wizard_form = Form(
  108. self.env['mailing.mailing.schedule.date'].with_context(default_mass_mailing_id=mailing.id))
  109. # set a schedule date
  110. wizard_form.schedule_date = datetime(2021, 4, 30, 9, 0)
  111. wizard = wizard_form.save()
  112. wizard.action_schedule_date()
  113. # assert that the schedule_date and schedule_type fields are correct and that the mailing is put in queue
  114. self.assertEqual(mailing.schedule_date, datetime(2021, 4, 30, 9, 0))
  115. self.assertEqual(mailing.schedule_type, 'scheduled')
  116. self.assertEqual(mailing.state, 'in_queue')