test_event_notifications.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from unittest.mock import patch
  4. from datetime import datetime, date
  5. from dateutil.relativedelta import relativedelta
  6. from odoo import fields
  7. from odoo.tests.common import TransactionCase, new_test_user
  8. from odoo.addons.base.tests.test_ir_cron import CronMixinCase
  9. from odoo.addons.mail.tests.common import MailCase
  10. class TestEventNotifications(TransactionCase, MailCase, CronMixinCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. super().setUpClass()
  14. cls.event = cls.env['calendar.event'].create({
  15. 'name': "Doom's day",
  16. 'start': datetime(2019, 10, 25, 8, 0),
  17. 'stop': datetime(2019, 10, 27, 18, 0),
  18. }).with_context(mail_notrack=True)
  19. cls.user = new_test_user(cls.env, 'xav', email='em@il.com', notification_type='inbox')
  20. cls.partner = cls.user.partner_id
  21. def test_message_invite(self):
  22. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  23. 'message_type': 'user_notification',
  24. 'subtype': 'mail.mt_note',
  25. }):
  26. self.event.partner_ids = self.partner
  27. def test_message_invite_allday(self):
  28. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  29. 'message_type': 'user_notification',
  30. 'subtype': 'mail.mt_note',
  31. }):
  32. self.env['calendar.event'].with_context(mail_create_nolog=True).create([{
  33. 'name': 'Meeting',
  34. 'allday': True,
  35. 'start_date': fields.Date.today() + relativedelta(days=7),
  36. 'stop_date': fields.Date.today() + relativedelta(days=8),
  37. 'partner_ids': [(4, self.partner.id)],
  38. }])
  39. def test_message_invite_self(self):
  40. with self.assertNoNotifications():
  41. self.event.with_user(self.user).partner_ids = self.partner
  42. def test_message_inactive_invite(self):
  43. self.event.active = False
  44. with self.assertNoNotifications():
  45. self.event.partner_ids = self.partner
  46. def test_message_set_inactive_invite(self):
  47. self.event.active = False
  48. with self.assertNoNotifications():
  49. self.event.write({
  50. 'partner_ids': [(4, self.partner.id)],
  51. 'active': False,
  52. })
  53. def test_message_datetime_changed(self):
  54. self.event.partner_ids = self.partner
  55. "Invitation to Presentation of the new Calendar"
  56. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  57. 'message_type': 'user_notification',
  58. 'subtype': 'mail.mt_note',
  59. }):
  60. self.event.start = fields.Datetime.now() + relativedelta(days=1)
  61. def test_message_date_changed(self):
  62. self.event.write({
  63. 'allday': True,
  64. 'start_date': fields.Date.today() + relativedelta(days=7),
  65. 'stop_date': fields.Date.today() + relativedelta(days=8),
  66. })
  67. self.event.partner_ids = self.partner
  68. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  69. 'message_type': 'user_notification',
  70. 'subtype': 'mail.mt_note',
  71. }):
  72. self.event.start_date += relativedelta(days=-1)
  73. def test_message_date_changed_past(self):
  74. self.event.write({
  75. 'allday': True,
  76. 'start_date': fields.Date.today(),
  77. 'stop_date': fields.Date.today() + relativedelta(days=1),
  78. })
  79. self.event.partner_ids = self.partner
  80. with self.assertNoNotifications():
  81. self.event.write({'start': date(2019, 1, 1)})
  82. def test_message_set_inactive_date_changed(self):
  83. self.event.write({
  84. 'allday': True,
  85. 'start_date': date(2019, 10, 15),
  86. 'stop_date': date(2019, 10, 15),
  87. })
  88. self.event.partner_ids = self.partner
  89. with self.assertNoNotifications():
  90. self.event.write({
  91. 'start_date': self.event.start_date - relativedelta(days=1),
  92. 'active': False,
  93. })
  94. def test_message_inactive_date_changed(self):
  95. self.event.write({
  96. 'allday': True,
  97. 'start_date': date(2019, 10, 15),
  98. 'stop_date': date(2019, 10, 15),
  99. 'active': False,
  100. })
  101. self.event.partner_ids = self.partner
  102. with self.assertNoNotifications():
  103. self.event.start_date += relativedelta(days=-1)
  104. def test_message_add_and_date_changed(self):
  105. self.event.partner_ids -= self.partner
  106. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  107. 'message_type': 'user_notification',
  108. 'subtype': 'mail.mt_note',
  109. }):
  110. self.event.write({
  111. 'start': self.event.start - relativedelta(days=1),
  112. 'partner_ids': [(4, self.partner.id)],
  113. })
  114. def test_bus_notif(self):
  115. alarm = self.env['calendar.alarm'].create({
  116. 'name': 'Alarm',
  117. 'alarm_type': 'notification',
  118. 'interval': 'minutes',
  119. 'duration': 30,
  120. })
  121. now = fields.Datetime.now()
  122. with patch.object(fields.Datetime, 'now', lambda: now):
  123. with self.assertBus([(self.env.cr.dbname, 'res.partner', self.partner.id)], [
  124. {
  125. "type": "calendar.alarm",
  126. "payload": [{
  127. "alarm_id": alarm.id,
  128. "event_id": self.event.id,
  129. "title": "Doom's day",
  130. "message": self.event.display_time,
  131. "timer": 20 * 60,
  132. "notify_at": fields.Datetime.to_string(now + relativedelta(minutes=20)),
  133. }],
  134. },
  135. ]):
  136. self.event.with_context(no_mail_to_attendees=True).write({
  137. 'start': now + relativedelta(minutes=50),
  138. 'stop': now + relativedelta(minutes=55),
  139. 'partner_ids': [(4, self.partner.id)],
  140. 'alarm_ids': [(4, alarm.id)]
  141. })
  142. def test_email_alarm(self):
  143. now = fields.Datetime.now()
  144. with self.capture_triggers('calendar.ir_cron_scheduler_alarm') as capt:
  145. alarm = self.env['calendar.alarm'].create({
  146. 'name': 'Alarm',
  147. 'alarm_type': 'email',
  148. 'interval': 'minutes',
  149. 'duration': 20,
  150. })
  151. self.event.write({
  152. 'name': 'test event',
  153. 'start': now + relativedelta(minutes=15),
  154. 'stop': now + relativedelta(minutes=18),
  155. 'partner_ids': [fields.Command.link(self.partner.id)],
  156. 'alarm_ids': [fields.Command.link(alarm.id)],
  157. })
  158. self.env.flush_all() # flush is required to make partner_ids be present in the event
  159. capt.records.ensure_one()
  160. self.assertLessEqual(capt.records.call_at, now)
  161. with patch.object(fields.Datetime, 'now', lambda: now):
  162. with self.assertSinglePostNotifications([{'partner': self.partner, 'type': 'inbox'}], {
  163. 'message_type': 'user_notification',
  164. 'subtype': 'mail.mt_note',
  165. }):
  166. self.env['calendar.alarm_manager'].with_context(lastcall=now - relativedelta(minutes=15))._send_reminder()