1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
# -*- coding: utf-8 -*-
from unittest.mock import patch, ANY, call
from datetime import timedelta
from odoo import fields
from odoo.exceptions import UserError
from odoo.addons.microsoft_calendar.utils.microsoft_calendar import MicrosoftCalendarService
from odoo.addons.microsoft_calendar.utils.microsoft_event import MicrosoftEvent
from odoo.addons.microsoft_calendar.models.res_users import User
from odoo.addons.microsoft_calendar.tests.common import (
TestCommon,
mock_get_token,
_modified_date_in_the_future,
patch_api
)
@patch.object(User, '_get_microsoft_calendar_token', mock_get_token)
class TestDeleteEvents(TestCommon):
@patch_api
def setUp(self):
super(TestDeleteEvents, self).setUp()
self.create_events_for_tests()
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_simple_event_from_odoo_organizer_calendar(self, mock_delete):
event_id = self.simple_event.microsoft_id
self.simple_event.with_user(self.organizer_user).unlink()
self.call_post_commit_hooks()
self.simple_event.invalidate_recordset()
self.assertFalse(self.simple_event.exists())
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_simple_event_from_odoo_attendee_calendar(self, mock_delete):
event_id = self.simple_event.microsoft_id
self.simple_event.with_user(self.attendee_user).unlink()
self.call_post_commit_hooks()
self.simple_event.invalidate_recordset()
self.assertFalse(self.simple_event.exists())
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_archive_simple_event_from_odoo_organizer_calendar(self, mock_delete):
event_id = self.simple_event.microsoft_id
self.simple_event.with_user(self.organizer_user).write({'active': False})
self.call_post_commit_hooks()
self.simple_event.invalidate_recordset()
self.assertTrue(self.simple_event.exists())
self.assertFalse(self.simple_event.active)
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_archive_simple_event_from_odoo_attendee_calendar(self, mock_delete):
event_id = self.simple_event.microsoft_id
self.simple_event.with_user(self.attendee_user).write({'active': False})
self.call_post_commit_hooks()
self.simple_event.invalidate_recordset()
self.assertTrue(self.simple_event.exists())
self.assertFalse(self.simple_event.active)
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_archive_several_events_at_once(self, mock_delete):
"""
Archive several events at once should not produce any exception.
"""
# arrange
several_simple_events = self.several_events.filtered(lambda ev: not ev.recurrency and ev.microsoft_id)
# act
several_simple_events.action_archive()
self.call_post_commit_hooks()
several_simple_events.invalidate_recordset()
# assert
self.assertFalse(all(e.active for e in several_simple_events))
mock_delete.assert_has_calls([
call(e.microsoft_id, token=ANY, timeout=ANY)
for e in several_simple_events
])
@patch.object(MicrosoftCalendarService, 'get_events')
def test_cancel_simple_event_from_outlook_organizer_calendar(self, mock_get_events):
"""
In his Outlook calendar, the organizer cannot delete the event, he can only cancel it.
"""
event_id = self.simple_event.microsoft_id
mock_get_events.return_value = (
MicrosoftEvent([{
"id": event_id,
"@removed": {"reason": "deleted"}
}]),
None
)
self.organizer_user.with_user(self.organizer_user).sudo()._sync_microsoft_calendar()
self.assertFalse(self.simple_event.exists())
def test_delete_simple_event_from_outlook_attendee_calendar(self):
"""
If an attendee deletes an event from its Outlook calendar, during the sync, Odoo will be notified that
this event has been deleted BUT only with the attendees's calendar event id and not with the global one
(called iCalUId). That means, it's not possible to match this deleted event with an Odoo event.
LIMITATION:
Unfortunately, there is no magic solution:
1) keep the list of calendar events ids linked to a unique iCalUId but all Odoo users may not have synced
their Odoo calendar, leading to missing ids in the list => bad solution.
2) call the microsoft API to get the iCalUId matching the received event id => as the event has already
been deleted, this call may return an error.
"""
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_one_event_from_recurrence_from_odoo_calendar(self, mock_delete):
if not self.sync_odoo_recurrences_with_outlook_feature():
return
# arrange
idx = 2
event_id = self.recurrent_events[idx].microsoft_id
# act
self.recurrent_events[idx].with_user(self.organizer_user).unlink()
self.call_post_commit_hooks()
# assert
self.assertFalse(self.recurrent_events[idx].exists())
self.assertEqual(len(self.recurrence.calendar_event_ids), self.recurrent_events_count - 1)
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_first_event_from_recurrence_from_odoo_calendar(self, mock_delete):
if not self.sync_odoo_recurrences_with_outlook_feature():
return
# arrange
idx = 0
event_id = self.recurrent_events[idx].microsoft_id
# act
self.recurrent_events[idx].with_user(self.organizer_user).unlink()
self.call_post_commit_hooks()
# assert
self.assertFalse(self.recurrent_events[idx].exists())
self.assertEqual(len(self.recurrence.calendar_event_ids), self.recurrent_events_count - 1)
self.assertEqual(self.recurrence.base_event_id, self.recurrent_events[1])
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'get_events')
def test_delete_one_event_from_recurrence_from_outlook_calendar(self, mock_get_events):
"""
When a single event is removed from a recurrence, Outlook returns the recurrence and
events which still exist.
"""
# arrange
idx = 3
rec_values = [
dict(
event,
lastModifiedDateTime=_modified_date_in_the_future(self.recurrence)
)
for i, event in enumerate(self.recurrent_event_from_outlook_organizer)
if i != (idx + 1) # + 1 because recurrent_event_from_outlook_organizer contains the recurrence itself as first item
]
event_to_remove = self.recurrent_events[idx]
mock_get_events.return_value = (MicrosoftEvent(rec_values), None)
# act
self.organizer_user.with_user(self.organizer_user).sudo()._sync_microsoft_calendar()
# assert
self.assertFalse(event_to_remove.exists())
self.assertEqual(len(self.recurrence.calendar_event_ids), self.recurrent_events_count - 1)
@patch.object(MicrosoftCalendarService, 'get_events')
def test_delete_first_event_from_recurrence_from_outlook_calendar(self, mock_get_events):
# arrange
rec_values = [
dict(
event,
lastModifiedDateTime=_modified_date_in_the_future(self.recurrence)
)
for i, event in enumerate(self.recurrent_event_from_outlook_organizer)
if i != 1
]
event_to_remove = self.recurrent_events[0]
next_base_event = self.recurrent_events[1]
mock_get_events.return_value = (MicrosoftEvent(rec_values), None)
# act
self.organizer_user.with_user(self.organizer_user).sudo()._sync_microsoft_calendar()
# assert
self.assertFalse(event_to_remove.exists())
self.assertEqual(len(self.recurrence.calendar_event_ids), self.recurrent_events_count - 1)
self.assertEqual(self.recurrence.base_event_id, next_base_event)
@patch.object(MicrosoftCalendarService, 'get_events')
def test_delete_one_event_and_future_from_recurrence_from_outlook_calendar(self, mock_get_events):
if not self.sync_odoo_recurrences_with_outlook_feature():
return
# arrange
idx = range(4, self.recurrent_events_count)
rec_values = [
dict(
event,
lastModifiedDateTime=_modified_date_in_the_future(self.recurrence)
)
for i, event in enumerate(self.recurrent_event_from_outlook_organizer)
if i not in [x + 1 for x in idx]
]
event_to_remove = [e for i, e in enumerate(self.recurrent_events) if i in idx]
mock_get_events.return_value = (MicrosoftEvent(rec_values), None)
# act
self.organizer_user.with_user(self.organizer_user).sudo()._sync_microsoft_calendar()
# assert
for e in event_to_remove:
self.assertFalse(e.exists())
self.assertEqual(len(self.recurrence.calendar_event_ids), self.recurrent_events_count - len(idx))
@patch.object(MicrosoftCalendarService, 'get_events')
def test_delete_first_event_and_future_from_recurrence_from_outlook_calendar(self, mock_get_events):
"""
In Outlook, deleting the first event and future ones is the same than removing all the recurrence.
"""
# arrange
mock_get_events.return_value = (
MicrosoftEvent([{
"id": self.recurrence.microsoft_id,
"@removed": {"reason": "deleted"}
}]),
None
)
# act
self.organizer_user.with_context(dont_notify=True).with_user(self.organizer_user).sudo()._sync_microsoft_calendar()
# assert
self.assertFalse(self.recurrence.exists())
self.assertFalse(self.recurrence.calendar_event_ids.exists())
@patch.object(MicrosoftCalendarService, 'get_events')
def test_delete_all_events_from_recurrence_from_outlook_calendar(self, mock_get_events):
"""
Same than test_delete_first_event_and_future_from_recurrence_from_outlook_calendar.
"""
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_single_event_from_recurrence_from_odoo_calendar(self, mock_delete):
"""
Deletes the base_event of a recurrence and checks if the event was archived and the recurrence was updated.
"""
if not self.sync_odoo_recurrences_with_outlook_feature():
return
# arrange
idx = 0
event_id = self.recurrent_events[idx].microsoft_id
# act
self.recurrent_events[idx].with_user(self.organizer_user).action_mass_archive('self_only')
self.call_post_commit_hooks()
# assert that event is not active anymore and that a new base_event was select for the recurrence
self.assertFalse(self.recurrent_events[idx].active)
self.assertNotEqual(self.id, self.recurrent_events[idx].recurrence_id.base_event_id.id)
self.assertTrue(self.id not in [rec.id for rec in self.recurrent_events[idx].recurrence_id.calendar_event_ids])
mock_delete.assert_called_once_with(
event_id,
token=mock_get_token(self.organizer_user),
timeout=ANY
)
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_synced_event_with_sync_config_paused(self, mock_delete):
"""
Deletes an event with the Outlook Calendar synchronization paused, the event must be archived completely.
"""
# Set user synchronization configuration as active and pause it.
self.organizer_user.microsoft_synchronization_stopped = False
self.organizer_user.pause_microsoft_synchronization()
# Try to delete a simple event in Odoo Calendar.
self.simple_event.with_user(self.organizer_user).unlink()
self.call_post_commit_hooks()
self.simple_event.invalidate_recordset()
# Ensure that synchronization is paused, delete wasn't called and record doesn't exist anymore.
self.assertFalse(self.organizer_user.microsoft_synchronization_stopped)
self.assertEqual(self.organizer_user._get_microsoft_sync_status(), "sync_paused")
self.assertFalse(self.simple_event.exists(), "Event must be deleted from Odoo even though sync configuration is off")
mock_delete.assert_not_called()
@patch.object(MicrosoftCalendarService, 'delete')
def test_delete_recurrence_previously_synced(self, mock_delete):
# Arrange: select recurrent event and update token validity to simulate an active sync environment.
idx = 0
self.organizer_user.microsoft_calendar_token_validity = fields.Datetime.now() + timedelta(hours=1)
# Act: try to delete a recurrent event that was already synced.
with self.assertRaises(UserError):
self.recurrent_events[idx].with_user(self.organizer_user).action_mass_archive('all_events')
self.call_post_commit_hooks()
# Ensure that event remains undeleted after deletion attempt and delete method wasn't called.
self.assertTrue(self.recurrent_events[idx].with_user(self.organizer_user)._check_microsoft_sync_status())
self.assertTrue(self.recurrent_events[idx].active)
mock_delete.assert_not_called()
def test_forbid_recurrence_unlinking_list_view(self):
# Forbid recurrence unlinking from list view with sync on.
self.assertTrue(self.env['calendar.event'].with_user(self.organizer_user)._check_microsoft_sync_status())
with self.assertRaises(UserError):
self.recurrent_events.unlink()
# Allow recurrence unlinking when update comes from Microsoft (dont_notify=True).
self.recurrent_events[2:].with_context(dont_notify=True).unlink()
self.assertTrue(all(not event.exists() for event in self.recurrent_events[2:]), "Recurrent event must be deleted after unlink from Microsoft.")
# Allow unlinking recurrence when sync is off for the current user.
self.organizer_user.microsoft_synchronization_stopped = True
self.assertFalse(self.env['calendar.event'].with_user(self.organizer_user)._check_microsoft_sync_status())
self.recurrent_events[1].with_user(self.organizer_user).unlink()
self.assertFalse(self.recurrent_events[1].exists(), "Recurrent event must be deleted after unlink with sync off.")
|