test_sms_schedule.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime, timedelta
  4. from odoo import fields
  5. from odoo.addons.event.tests.common import EventCase
  6. from odoo.addons.phone_validation.tools import phone_validation
  7. from odoo.addons.sms.tests.common import SMSCase
  8. from odoo.tests import users
  9. class TestSMSSchedule(EventCase, SMSCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. super(TestSMSSchedule, cls).setUpClass()
  13. cls.sms_template_sub = cls.env['sms.template'].create({
  14. 'name': 'Test subscription',
  15. 'model_id': cls.env.ref('event.model_event_registration').id,
  16. 'body': '{{ object.event_id.organizer_id.name }} registration confirmation.',
  17. 'lang': '{{ object.partner_id.lang }}'
  18. })
  19. cls.sms_template_rem = cls.env['sms.template'].create({
  20. 'name': 'Test reminder',
  21. 'model_id': cls.env.ref('event.model_event_registration').id,
  22. 'body': '{{ object.event_id.organizer_id.name }} reminder',
  23. 'lang': '{{ object.partner_id.lang }}'
  24. })
  25. cls.test_event = cls.env['event.event'].create({
  26. 'auto_confirm': True,
  27. 'date_begin': fields.Datetime.to_string(datetime.today() + timedelta(days=1)),
  28. 'date_end': fields.Datetime.to_string(datetime.today() + timedelta(days=15)),
  29. 'date_tz': 'Europe/Brussels',
  30. 'event_mail_ids': [
  31. (5, 0),
  32. (0, 0, { # right at subscription
  33. 'interval_unit': 'now',
  34. 'interval_type': 'after_sub',
  35. 'notification_type': 'sms',
  36. 'template_ref': 'sms.template,%i' % cls.sms_template_sub.id}),
  37. (0, 0, { # 3 days before event
  38. 'interval_nbr': 3,
  39. 'interval_unit': 'days',
  40. 'interval_type': 'before_event',
  41. 'notification_type': 'sms',
  42. 'template_ref': 'sms.template,%i' % cls.sms_template_rem.id}),
  43. ],
  44. 'name': 'TestEvent',
  45. })
  46. @users('user_eventmanager')
  47. def test_sms_schedule(self):
  48. test_event = self.env['event.event'].browse(self.test_event.ids)
  49. with self.mockSMSGateway():
  50. self._create_registrations(test_event, 3)
  51. # check subscription scheduler
  52. sub_scheduler = self.env['event.mail'].search([('event_id', '=', test_event.id), ('interval_type', '=', 'after_sub')])
  53. self.assertEqual(len(sub_scheduler), 1)
  54. self.assertEqual(sub_scheduler.scheduled_date, test_event.create_date.replace(microsecond=0), 'event: incorrect scheduled date for checking controller')
  55. # verify that subscription scheduler was auto-executed after each registration
  56. self.assertEqual(len(sub_scheduler.mail_registration_ids), 3)
  57. self.assertTrue(all(m.mail_sent is True for m in sub_scheduler.mail_registration_ids))
  58. self.assertEqual(sub_scheduler.mapped('mail_registration_ids.registration_id'), test_event.registration_ids)
  59. sanitized_numbers = []
  60. for registration in test_event.registration_ids:
  61. reg_sanitized_number = phone_validation.phone_format(registration.phone, 'BE', '32', force_format='E164')
  62. sanitized_numbers.append(reg_sanitized_number)
  63. self.assertSMSOutgoing(
  64. self.env['res.partner'], reg_sanitized_number,
  65. content='%s registration confirmation.' % test_event.organizer_id.name)
  66. self.assertTrue(sub_scheduler.mail_done)
  67. self.assertEqual(sub_scheduler.mail_count_done, 3)
  68. # clear notification queue to avoid conflicts when checking next notifications
  69. self.env['mail.notification'].sudo().search([('sms_number', 'in', sanitized_numbers)]).unlink()
  70. self.env['sms.sms'].sudo().search([('number', 'in', sanitized_numbers)]).unlink()
  71. # check before event scheduler
  72. before_scheduler = self.env['event.mail'].search([('event_id', '=', test_event.id), ('interval_type', '=', 'before_event')])
  73. self.assertEqual(len(before_scheduler), 1, 'event: wrong scheduler creation')
  74. self.assertEqual(before_scheduler.scheduled_date, test_event.date_begin + timedelta(days=-3))
  75. # execute event reminder scheduler explicitly
  76. with self.mockSMSGateway():
  77. before_scheduler.execute()
  78. # verify that subscription scheduler was auto-executed after each registration
  79. for registration in test_event.registration_ids:
  80. reg_sanitized_number = phone_validation.phone_format(registration.phone, 'BE', '32', force_format='E164')
  81. self.assertSMSOutgoing(
  82. self.env['res.partner'], reg_sanitized_number,
  83. content='%s reminder' % test_event.organizer_id.name)
  84. self.assertTrue(before_scheduler.mail_done)
  85. self.assertEqual(before_scheduler.mail_count_done, 3)