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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
from datetime import datetime
from dateutil.relativedelta import relativedelta
from pytz import UTC
from odoo.addons.microsoft_calendar.utils.microsoft_event import MicrosoftEvent
from odoo.addons.microsoft_calendar.tests.common import TestCommon, patch_api
class TestMicrosoftEvent(TestCommon):
@patch_api
def setUp(self):
super().setUp()
self.create_events_for_tests()
def test_already_mapped_events(self):
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": self.simple_event.id,
"iCalUId": event_uid,
"id": event_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[event_id]["_odoo_id"], self.simple_event.id)
def test_map_an_event_using_global_id(self):
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": event_uid,
"id": event_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[event_id]["_odoo_id"], self.simple_event.id)
def test_map_an_event_using_instance_id(self):
"""
Here, the Odoo event has an uid but the Outlook event has not.
"""
# arrange
event_id = self.simple_event.microsoft_id
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": False,
"id": event_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[event_id]["_odoo_id"], self.simple_event.id)
def test_map_an_event_without_uid_using_instance_id(self):
"""
Here, the Odoo event has no uid but the Outlook event has one.
"""
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
self.simple_event.ms_universal_event_id = False
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": event_uid,
"id": event_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[event_id]["_odoo_id"], self.simple_event.id)
self.assertEqual(self.simple_event.ms_universal_event_id, event_uid)
def test_map_an_event_without_uid_using_instance_id_2(self):
"""
Here, both Odoo event and Outlook event have no uid.
"""
# arrange
event_id = self.simple_event.microsoft_id
self.simple_event.ms_universal_event_id = False
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": False,
"id": event_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[event_id]["_odoo_id"], self.simple_event.id)
self.assertEqual(self.simple_event.ms_universal_event_id, False)
def test_map_a_recurrence_using_global_id(self):
# arrange
rec_id = self.recurrence.microsoft_id
rec_uid = self.recurrence.ms_universal_event_id
events = MicrosoftEvent([{
"type": "seriesMaster",
"_odoo_id": False,
"iCalUId": rec_uid,
"id": rec_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[rec_id]["_odoo_id"], self.recurrence.id)
def test_map_a_recurrence_using_instance_id(self):
# arrange
rec_id = self.recurrence.microsoft_id
events = MicrosoftEvent([{
"type": "seriesMaster",
"_odoo_id": False,
"iCalUId": False,
"id": rec_id,
}])
# act
mapped = events._load_odoo_ids_from_db(self.env)
# assert
self.assertEqual(len(mapped._events), 1)
self.assertEqual(mapped._events[rec_id]["_odoo_id"], self.recurrence.id)
def test_try_to_map_mixed_of_single_events_and_recurrences(self):
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
rec_id = self.recurrence.microsoft_id
rec_uid = self.recurrence.ms_universal_event_id
events = MicrosoftEvent([
{
"type": "seriesMaster",
"_odoo_id": False,
"iCalUId": rec_uid,
"id": rec_id,
},
{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": event_uid,
"id": event_id,
},
])
# act & assert
with self.assertRaises(TypeError):
events._load_odoo_ids_from_db(self.env)
def test_match_event_only(self):
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": event_uid,
"id": event_id,
}])
# act
matched = events.match_with_odoo_events(self.env)
# assert
self.assertEqual(len(matched._events), 1)
self.assertEqual(matched._events[event_id]["_odoo_id"], self.simple_event.id)
def test_match_recurrence_only(self):
# arrange
rec_id = self.recurrence.microsoft_id
rec_uid = self.recurrence.ms_universal_event_id
events = MicrosoftEvent([{
"type": "seriesMaster",
"_odoo_id": False,
"iCalUId": rec_uid,
"id": rec_id,
}])
# act
matched = events.match_with_odoo_events(self.env)
# assert
self.assertEqual(len(matched._events), 1)
self.assertEqual(matched._events[rec_id]["_odoo_id"], self.recurrence.id)
def test_match_not_typed_recurrence(self):
"""
When a recurrence is deleted, Outlook returns the id of the deleted recurrence
without the type of event, so it's not directly possible to know that it's a
recurrence.
"""
# arrange
rec_id = self.recurrence.microsoft_id
rec_uid = self.recurrence.ms_universal_event_id
events = MicrosoftEvent([{
"@removed": {
"reason": "deleted",
},
"_odoo_id": False,
"iCalUId": rec_uid,
"id": rec_id,
}])
# act
matched = events.match_with_odoo_events(self.env)
# assert
self.assertEqual(len(matched._events), 1)
self.assertEqual(matched._events[rec_id]["_odoo_id"], self.recurrence.id)
def test_match_mix_of_events_and_recurrences(self):
# arrange
event_id = self.simple_event.microsoft_id
event_uid = self.simple_event.ms_universal_event_id
rec_id = self.recurrence.microsoft_id
rec_uid = self.recurrence.ms_universal_event_id
events = MicrosoftEvent([
{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": event_uid,
"id": event_id,
},
{
"@removed": {
"reason": "deleted",
},
"_odoo_id": False,
"iCalUId": rec_uid,
"id": rec_id,
}
])
# act
matched = events.match_with_odoo_events(self.env)
# assert
self.assertEqual(len(matched._events), 2)
self.assertEqual(matched._events[event_id]["_odoo_id"], self.simple_event.id)
self.assertEqual(matched._events[rec_id]["_odoo_id"], self.recurrence.id)
def test_ignore_not_found_items(self):
# arrange
events = MicrosoftEvent([{
"type": "singleInstance",
"_odoo_id": False,
"iCalUId": "UNKNOWN_EVENT",
"id": "UNKNOWN_EVENT",
}])
# act
matched = events.match_with_odoo_events(self.env)
# assert
self.assertEqual(len(matched._events), 0)
def test_search_set_ms_universal_event_id(self):
not_synced_events = self.env['calendar.event'].search([('ms_universal_event_id', '=', False)])
synced_events = self.env['calendar.event'].search([('ms_universal_event_id', '!=', False)])
self.assertIn(self.simple_event, synced_events)
self.assertNotIn(self.simple_event, not_synced_events)
self.simple_event.ms_universal_event_id = False
not_synced_events = self.env['calendar.event'].search([('ms_universal_event_id', '=', False)])
synced_events = self.env['calendar.event'].search([('ms_universal_event_id', '!=', False)])
self.assertNotIn(self.simple_event, synced_events)
self.assertIn(self.simple_event, not_synced_events)
def test_microsoft_event_readonly(self):
event = MicrosoftEvent()
with self.assertRaises(TypeError):
event._events['foo'] = 'bar'
with self.assertRaises(AttributeError):
event._events.update({'foo': 'bar'})
with self.assertRaises(TypeError):
dict.update(event._events, {'foo': 'bar'})
def test_performance_check(self):
# Test what happens when microsoft returns a lot of data
# This test does not aim to check what we do with the data but it ensure that we are able to process it.
# Other tests take care of how we update odoo records with the api result.
start_date = datetime(2023, 9, 25, 17, 25)
record_count = 10000
single_event_data = [{
'@odata.type': '#microsoft.graph.event',
'@odata.etag': f'W/"AAAAAA{x}"',
'type': 'singleInstance',
'createdDateTime': (start_date + relativedelta(minutes=x)).isoformat(),
'lastModifiedDateTime': (datetime.now().astimezone(UTC) + relativedelta(days=3)).isoformat(),
'changeKey': f'ZS2uEVAVyU6BMZ3m6cH{x}mtgAADI/Dig==',
'categories': [],
'originalStartTimeZone': 'Romance Standard Time',
'originalEndTimeZone': 'Romance Standard Time',
'id': f'AA{x}',
'subject': f"Subject of {x}",
'bodyPreview': f"Body of {x}",
'start': {'dateTime': (start_date + relativedelta(minutes=x)).isoformat(), 'timeZone': 'UTC'},
'end': {'dateTime': (start_date + relativedelta(minutes=x)).isoformat(), 'timeZone': 'UTC'},
'isOrganizer': True,
'organizer': {'emailAddress': {'name': f'outlook_{x}@outlook.com', 'address': f'outlook_{x}@outlook.com'}},
} for x in range(record_count)]
events = MicrosoftEvent(single_event_data)
mapped = events._load_odoo_ids_from_db(self.env)
self.assertFalse(mapped, "No odoo record should correspond to the microsoft values")
recurring_event_data = [{
'@odata.type': '#microsoft.graph.event',
'@odata.etag': f'W/"{x}IaZKQ=="',
'createdDateTime': (start_date + relativedelta(minutes=(2*x))).isoformat(),
'lastModifiedDateTime': (datetime.now().astimezone(UTC) + relativedelta(days=3)).isoformat(),
'changeKey': 'ZS2uEVAVyU6BMZ3m6cHmtgAADIaZKQ==',
'categories': [],
'originalStartTimeZone': 'Romance Standard Time',
'originalEndTimeZone': 'Romance Standard Time',
'iCalUId': f'XX{x}',
'id': f'AAA{x}',
'reminderMinutesBeforeStart': 15,
'isReminderOn': True,
'hasAttachments': False,
'subject': f'My recurrent event {x}',
'bodyPreview': '', 'importance':
'normal', 'sensitivity': 'normal',
'isAllDay': False, 'isCancelled': False,
'isOrganizer': True, 'IsRoomRequested': False,
'AutoRoomBookingStatus': 'None',
'responseRequested': True,
'seriesMasterId': None,
'showAs': 'busy',
'type': 'seriesMaster',
'webLink': f'https://outlook.live.com/owa/?itemid={x}&exvsurl=1&path=/calendar/item',
'onlineMeetingUrl': None,
'isOnlineMeeting': False,
'onlineMeetingProvider': 'unknown', 'AllowNewTimeProposals': True,
'IsDraft': False,
'responseStatus': {'response': 'organizer', 'time': '0001-01-01T00:00:00Z'},
'body': {'contentType': 'html', 'content': ''},
'start': {'dateTime': '2020-05-03T14:30:00.0000000', 'timeZone': 'UTC'},
'end': {'dateTime': '2020-05-03T16:00:00.0000000', 'timeZone': 'UTC'},
'location': {'displayName': '',
'locationType': 'default',
'uniqueIdType': 'unknown',
'address': {},
'coordinates': {}},
'locations': [],
'recurrence': {'pattern':
{'type': 'daily',
'interval': 1,
'month': 0,
'dayOfMonth': 0,
'firstDayOfWeek': 'sunday',
'index': 'first'},
'range': {'type': 'endDate',
'startDate': '2020-05-03',
'endDate': '2020-05-05',
'recurrenceTimeZone': 'Romance Standard Time',
'numberOfOccurrences': 0}
},
'attendees': [],
'organizer': {'emailAddress': {'name': f'outlook_{x}@outlook.com',
'address': f'outlook_{x}@outlook.com'}}
} for x in range(record_count)]
recurrences = MicrosoftEvent(recurring_event_data)
mapped = recurrences._load_odoo_ids_from_db(self.env)
self.assertFalse(mapped, "No odoo record should correspond to the microsoft values")
|