-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathtest_extended_properties.py
443 lines (384 loc) · 18.1 KB
/
test_extended_properties.py
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from exchangelib.extended_properties import ExtendedProperty, Flag
from exchangelib.folders import Inbox
from exchangelib.items import BaseItem, CalendarItem, Message
from exchangelib.properties import Mailbox
from exchangelib.util import to_xml
from .common import get_random_int, get_random_url
from .test_items.test_basics import BaseItemTest
class ExtendedPropertyTest(BaseItemTest):
TEST_FOLDER = "inbox"
FOLDER_CLASS = Inbox
ITEM_CLASS = Message
def test_register(self):
# Tests that we can register and de-register custom extended properties
class TestProp(ExtendedProperty):
property_set_id = "deadbeaf-cafe-cafe-cafe-deadbeefcafe"
property_name = "Test Property"
property_type = "Integer"
attr_name = "dead_beef"
# Before register
self.assertNotIn(attr_name, {f.name for f in self.ITEM_CLASS.supported_fields(self.account.version)})
with self.assertRaises(ValueError):
self.ITEM_CLASS.deregister(attr_name) # Not registered yet
with self.assertRaises(ValueError):
self.ITEM_CLASS.deregister("subject") # Not an extended property
# Test that we can clean an item before and after registry
item = self.ITEM_CLASS()
item.clean(version=self.account.version)
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=TestProp)
item.clean(version=self.account.version)
try:
# After register
self.assertEqual(TestProp.python_type(), int)
self.assertIn(attr_name, {f.name for f in self.ITEM_CLASS.supported_fields(self.account.version)})
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.dead_beef
self.assertIsInstance(prop_val, int)
item.save()
item.refresh()
self.assertEqual(prop_val, item.dead_beef)
new_prop_val = get_random_int(0, 256)
item.dead_beef = new_prop_val
item.save()
item.refresh()
self.assertEqual(new_prop_val, item.dead_beef)
# Test deregister
with self.assertRaises(ValueError) as e:
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=TestProp)
self.assertEqual(e.exception.args[0], "'attr_name' 'dead_beef' is already registered")
with self.assertRaises(TypeError) as e:
self.ITEM_CLASS.register(attr_name="XXX", attr_cls=Mailbox)
self.assertEqual(
e.exception.args[0],
"'attr_cls' <class 'exchangelib.properties.Mailbox'> must be a subclass of type "
"<class 'exchangelib.extended_properties.ExtendedProperty'>",
)
with self.assertRaises(ValueError) as e:
BaseItem.register(attr_name=attr_name, attr_cls=Mailbox)
self.assertEqual(
e.exception.args[0],
"Class <class 'exchangelib.items.base.BaseItem'> is missing INSERT_AFTER_FIELD value",
)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
self.assertNotIn(attr_name, {f.name for f in self.ITEM_CLASS.supported_fields(self.account.version)})
def test_extended_property_arraytype(self):
# Tests array type extended properties
class TestArayProp(ExtendedProperty):
property_set_id = "deadcafe-beef-beef-beef-deadcafebeef"
property_name = "Test Array Property"
property_type = "IntegerArray"
attr_name = "dead_beef_array"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=TestArayProp)
try:
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.dead_beef_array
self.assertIsInstance(prop_val, list)
item.save()
item.refresh()
self.assertEqual(prop_val, item.dead_beef_array)
new_prop_val = self.random_val(self.ITEM_CLASS.get_field_by_fieldname(attr_name))
item.dead_beef_array = new_prop_val
item.save()
item.refresh()
self.assertEqual(new_prop_val, item.dead_beef_array)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
def test_extended_property_with_tag(self):
attr_name = "my_flag"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=Flag)
try:
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.my_flag
self.assertIsInstance(prop_val, int)
item.save()
item.refresh()
self.assertEqual(prop_val, item.my_flag)
new_prop_val = self.random_val(self.ITEM_CLASS.get_field_by_fieldname(attr_name))
item.my_flag = new_prop_val
item.save()
item.refresh()
self.assertEqual(new_prop_val, item.my_flag)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
def test_extended_property_with_invalid_tag(self):
class InvalidProp(ExtendedProperty):
property_tag = "0x8000"
property_type = "Integer"
with self.assertRaises(ValueError):
InvalidProp("Foo").clean() # property_tag is in protected range
def test_extended_property_with_string_tag(self):
attr_name = "my_flag"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=Flag)
try:
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.my_flag
self.assertIsInstance(prop_val, int)
item.save()
item.refresh()
self.assertEqual(prop_val, item.my_flag)
new_prop_val = self.random_val(self.ITEM_CLASS.get_field_by_fieldname(attr_name))
item.my_flag = new_prop_val
item.save()
item.refresh()
self.assertEqual(new_prop_val, item.my_flag)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
def test_extended_distinguished_property(self):
if self.ITEM_CLASS == CalendarItem:
# MyMeeting is an extended prop version of the 'CalendarItem.uid' field. They don't work together.
self.skipTest("This extendedproperty doesn't work on CalendarItems")
class MyMeeting(ExtendedProperty):
distinguished_property_set_id = "Meeting"
property_type = "Binary"
property_id = 3
attr_name = "my_meeting"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=MyMeeting)
try:
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.my_meeting
self.assertIsInstance(prop_val, bytes)
item.save()
item = self.get_item_by_id((item.id, item.changekey))
self.assertEqual(prop_val, item.my_meeting, (prop_val, item.my_meeting))
new_prop_val = self.random_val(self.ITEM_CLASS.get_field_by_fieldname(attr_name))
item.my_meeting = new_prop_val
item.save()
item = self.get_item_by_id((item.id, item.changekey))
self.assertEqual(new_prop_val, item.my_meeting)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
def test_extended_property_binary_array(self):
class MyMeetingArray(ExtendedProperty):
property_set_id = "00062004-0000-0000-C000-000000000046"
property_type = "BinaryArray"
property_id = 32852
attr_name = "my_meeting_array"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=MyMeetingArray)
try:
# Test item creation, refresh, and update
item = self.get_test_item(folder=self.test_folder)
prop_val = item.my_meeting_array
self.assertIsInstance(prop_val, list)
item.save()
item = self.get_item_by_id((item.id, item.changekey))
self.assertEqual(prop_val, item.my_meeting_array)
new_prop_val = self.random_val(self.ITEM_CLASS.get_field_by_fieldname(attr_name))
item.my_meeting_array = new_prop_val
item.save()
item = self.get_item_by_id((item.id, item.changekey))
self.assertEqual(new_prop_val, item.my_meeting_array)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
def test_extended_property_validation(self):
# Must not have property_set_id or property_tag
class TestProp1(ExtendedProperty):
distinguished_property_set_id = "XXX"
property_set_id = "YYY"
with self.assertRaises(ValueError) as e:
TestProp1.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'distinguished_property_set_id' is set, 'property_set_id' and 'property_tag' must be None",
)
# Must have property_id or property_name
class TestProp2(ExtendedProperty):
distinguished_property_set_id = "XXX"
with self.assertRaises(ValueError) as e:
TestProp2.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'distinguished_property_set_id' is set, 'property_id' or 'property_name' must also be set",
)
# distinguished_property_set_id must have a valid value
class TestProp3(ExtendedProperty):
distinguished_property_set_id = "XXX"
property_id = "YYY"
with self.assertRaises(ValueError) as e:
TestProp3.validate_cls()
self.assertEqual(
e.exception.args[0],
f"'distinguished_property_set_id' 'XXX' must be one of {sorted(ExtendedProperty.DISTINGUISHED_SETS)}",
)
# Must not have distinguished_property_set_id or property_tag
class TestProp4(ExtendedProperty):
property_set_id = "XXX"
property_tag = "YYY"
with self.assertRaises(ValueError) as e:
TestProp4.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'property_set_id' is set, 'distinguished_property_set_id' and 'property_tag' must be None",
)
# Must have property_id or property_name
class TestProp5(ExtendedProperty):
property_set_id = "XXX"
with self.assertRaises(ValueError) as e:
TestProp5.validate_cls()
self.assertEqual(
e.exception.args[0], "When 'property_set_id' is set, 'property_id' or 'property_name' must also be set"
)
# property_tag is only compatible with property_type
class TestProp6(ExtendedProperty):
property_tag = "XXX"
property_set_id = "YYY"
with self.assertRaises(ValueError) as e:
TestProp6.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'property_set_id' is set, 'distinguished_property_set_id' and 'property_tag' must be None",
)
# property_tag must be an integer or string that can be converted to int
class TestProp7(ExtendedProperty):
property_tag = "XXX"
with self.assertRaises(ValueError) as e:
TestProp7.validate_cls()
self.assertEqual(e.exception.args[0], "invalid literal for int() with base 16: 'XXX'")
# property_tag must not be in the reserved range
class TestProp8(ExtendedProperty):
property_tag = 0x8001
with self.assertRaises(ValueError) as e:
TestProp8.validate_cls()
self.assertEqual(e.exception.args[0], "'property_tag' value '0x8001' is reserved for custom properties")
# Must not have property_id or property_tag
class TestProp9(ExtendedProperty):
property_name = "XXX"
property_id = "YYY"
with self.assertRaises(ValueError) as e:
TestProp9.validate_cls()
self.assertEqual(
e.exception.args[0], "When 'property_name' is set, 'property_id' and 'property_tag' must be None"
)
# Must have distinguished_property_set_id or property_set_id
class TestProp10(ExtendedProperty):
property_name = "XXX"
with self.assertRaises(ValueError) as e:
TestProp10.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'property_name' is set, 'distinguished_property_set_id' or 'property_set_id' must also be set",
)
# Must not have property_name or property_tag
class TestProp11(ExtendedProperty):
property_id = "XXX"
property_name = "YYY"
with self.assertRaises(ValueError) as e:
TestProp11.validate_cls() # This actually hits the check on property_name values
self.assertEqual(
e.exception.args[0], "When 'property_name' is set, 'property_id' and 'property_tag' must be None"
)
# Must have distinguished_property_set_id or property_set_id
class TestProp12(ExtendedProperty):
property_id = "XXX"
with self.assertRaises(ValueError) as e:
TestProp12.validate_cls()
self.assertEqual(
e.exception.args[0],
"When 'property_id' is set, 'distinguished_property_set_id' or 'property_set_id' must also be set",
)
# property_type must be a valid value
class TestProp13(ExtendedProperty):
property_id = "XXX"
property_set_id = "YYY"
property_type = "ZZZ"
with self.assertRaises(ValueError) as e:
TestProp13.validate_cls()
self.assertEqual(
e.exception.args[0], f"'property_type' 'ZZZ' must be one of {sorted(ExtendedProperty.PROPERTY_TYPES)}"
)
# property_tag and property_id are mutually exclusive
class TestProp14(ExtendedProperty):
property_tag = "XXX"
property_id = "YYY"
with self.assertRaises(ValueError) as e:
# We cannot reach this exception directly with validate_cls()
TestProp14._validate_property_tag()
self.assertEqual(e.exception.args[0], "When 'property_tag' is set, only 'property_type' must be set")
with self.assertRaises(ValueError) as e:
# We cannot reach this exception directly with validate_cls()
TestProp14._validate_property_id()
self.assertEqual(
e.exception.args[0], "When 'property_id' is set, 'property_name' and 'property_tag' must be None"
)
def test_multiple_extended_properties(self):
class ExternalSharingUrl(ExtendedProperty):
property_set_id = "F52A8693-C34D-4980-9E20-9D4C1EABB6A7"
property_name = "ExternalSharingUrl"
property_type = "String"
class ExternalSharingFolderId(ExtendedProperty):
property_set_id = "F52A8693-C34D-4980-9E20-9D4C1EABB6A7"
property_name = "ExternalSharingLocalFolderId"
property_type = "Binary"
try:
self.ITEM_CLASS.register("sharing_url", ExternalSharingUrl)
self.ITEM_CLASS.register("sharing_folder_id", ExternalSharingFolderId)
url, folder_id = get_random_url(), self.test_folder.id.encode("utf-8")
m = self.get_test_item()
m.sharing_url, m.sharing_folder_id = url, folder_id
m.save()
m = self.test_folder.get(sharing_url=url)
self.assertEqual(m.sharing_url, url)
self.assertEqual(m.sharing_folder_id, folder_id)
finally:
self.ITEM_CLASS.deregister("sharing_url")
self.ITEM_CLASS.deregister("sharing_folder_id")
def test_via_queryset(self):
class TestProp(ExtendedProperty):
property_set_id = "deadbeaf-cafe-cafe-cafe-deadbeefcafe"
property_name = "Test Property"
property_type = "Integer"
class TestArayProp(ExtendedProperty):
property_set_id = "deadcafe-beef-beef-beef-deadcafebeef"
property_name = "Test Array Property"
property_type = "IntegerArray"
attr_name = "dead_beef"
array_attr_name = "dead_beef_array"
self.ITEM_CLASS.register(attr_name=attr_name, attr_cls=TestProp)
self.ITEM_CLASS.register(attr_name=array_attr_name, attr_cls=TestArayProp)
try:
item = self.get_test_item(folder=self.test_folder).save()
self.assertEqual(self.test_folder.filter(**{attr_name: getattr(item, attr_name)}).count(), 1)
self.assertEqual(
# Does not work in O365
# self.test_folder.filter(**{f"{array_attr_name}__contains": getattr(item, array_attr_name)}).count(), 1
self.test_folder.filter(**{f"{array_attr_name}__in": getattr(item, array_attr_name)}).count(),
1,
)
finally:
self.ITEM_CLASS.deregister(attr_name=attr_name)
self.ITEM_CLASS.deregister(attr_name=array_attr_name)
def test_from_xml(self):
# Test that empty and no-op XML Value elements for string props both return empty strings
class TestProp(ExtendedProperty):
property_set_id = "deadbeaf-cafe-cafe-cafe-deadbeefcafe"
property_name = "Test Property"
property_type = "String"
elem = to_xml(
b"""\
<ExtendedProperty xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
<ExtendedFieldURI/>
<Value>XXX</Value>
</ExtendedProperty>"""
)
self.assertEqual(TestProp.from_xml(elem, account=None), "XXX")
elem = to_xml(
b"""\
<ExtendedProperty xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
<ExtendedFieldURI/>
<Value></Value>
</ExtendedProperty>"""
)
self.assertEqual(TestProp.from_xml(elem, account=None), "")
elem = to_xml(
b"""\
<ExtendedProperty xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
<ExtendedFieldURI/>
<Value/>
</ExtendedProperty>"""
)
self.assertEqual(TestProp.from_xml(elem, account=None), "")