-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_path.py
More file actions
1438 lines (1046 loc) · 44.5 KB
/
test_path.py
File metadata and controls
1438 lines (1046 loc) · 44.5 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for SCIM path validation utilities."""
from typing import Any
import pydantic
import pytest
from scim2_models import Email
from scim2_models import EnterpriseUser
from scim2_models import Group
from scim2_models import InvalidPathException
from scim2_models import Manager
from scim2_models import Mutability
from scim2_models import Name
from scim2_models import PathNotFoundException
from scim2_models import Required
from scim2_models import User
from scim2_models.base import BaseModel
from scim2_models.path import Path
def test_validate_scim_path_syntax_valid_paths():
"""Test that valid SCIM paths are accepted."""
valid_paths = [
"userName",
"name.familyName",
"emails.value",
"groups.display",
"urn:ietf:params:scim:schemas:core:2.0:User:userName",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber",
'emails[type eq "work"].value',
'groups[display eq "Admin"]',
"meta.lastModified",
"a", # Single character
"a.b", # Simple dotted
"a_b", # Underscore
"a-b", # Hyphen
]
for path in valid_paths:
Path(path)
def test_validate_scim_path_syntax_invalid_paths():
"""Test that invalid SCIM paths are rejected."""
invalid_paths = [
" ", # Whitespace only
"123invalid", # Starts with digit
"invalid..path", # Double dots
"invalid@path", # Invalid character
"urn:invalid", # Invalid URN format
"urn:too:short", # URN too short
"urn:ending:with:a:comma:", # URN ending with a comma
]
for path in invalid_paths:
with pytest.raises(ValueError):
Path(path)
def test_empty_path_is_valid():
"""Empty path represents the resource root."""
path = Path("")
assert str(path) == ""
def test_path_generic_type_is_accepted():
"""Path with or without type parameter accepts any valid syntax."""
class ModelWithType(BaseModel):
path: Path[User]
class ModelWithoutType(BaseModel):
path: Path
ModelWithType.model_validate({"path": "userName"})
ModelWithType.model_validate({"path": "anyAttribute"})
ModelWithoutType.model_validate({"path": "anyAttribute"})
def test_path_validation_from_path_instance():
"""Path field accepts another Path instance."""
class ModelWithPath(BaseModel):
path: Path[User]
source_path = Path("userName")
model = ModelWithPath.model_validate({"path": source_path})
assert str(model.path) == "userName"
def test_path_validation_rejects_invalid_type():
"""Path field rejects non-string non-Path values."""
class ModelWithPath(BaseModel):
path: Path[User]
with pytest.raises(pydantic.ValidationError):
ModelWithPath.model_validate({"path": 123})
# --- Path bound to model tests ---
def test_model_simple_attribute():
"""Model property returns the target model for simple attribute."""
path = Path[User]("userName")
assert path.model == User
def test_model_complex_attribute():
"""Model property returns the nested model for complex attribute."""
path = Path[User]("name.familyName")
assert path.model == Name
def test_model_extension_attribute():
"""Model property returns extension model for extension path."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert path.model == EnterpriseUser
def test_model_extension_complex_attribute():
"""Model property navigates into extension complex attributes."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.value"
)
assert path.model == Manager
def test_model_invalid_attribute():
"""Model property returns None for invalid attribute."""
path = Path[User]("invalidAttribute")
assert path.model is None
def test_model_unbound_path():
"""Model property returns None for unbound path."""
path = Path("userName")
assert path.model is None
def test_field_name_simple_attribute():
"""field_name property returns snake_case field name."""
path = Path[User]("userName")
assert path.field_name == "user_name"
def test_field_name_complex_attribute():
"""field_name property returns snake_case for nested attribute."""
path = Path[User]("name.familyName")
assert path.field_name == "family_name"
def test_field_name_extension_attribute():
"""field_name property works for extension attributes."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert path.field_name == "employee_number"
def test_field_name_invalid_attribute():
"""field_name property returns None for invalid attribute."""
path = Path[User]("invalidAttribute")
assert path.field_name is None
def test_field_name_unbound_path():
"""field_name property returns None for unbound path."""
path = Path("userName")
assert path.field_name is None
def test_field_name_schema_only():
"""field_name property returns None for schema-only path."""
path = Path[User]("urn:ietf:params:scim:schemas:core:2.0:User")
assert path.field_name is None
def test_urn_simple_attribute():
"""URN property returns fully qualified URN."""
path = Path[User]("userName")
assert path.urn == "urn:ietf:params:scim:schemas:core:2.0:User:userName"
def test_urn_complex_attribute():
"""URN property includes dotted path."""
path = Path[User]("name.familyName")
assert path.urn == "urn:ietf:params:scim:schemas:core:2.0:User:name.familyName"
def test_urn_already_qualified():
"""URN property preserves already qualified paths."""
path = Path[User]("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.urn == "urn:ietf:params:scim:schemas:core:2.0:User:userName"
def test_urn_extension_attribute():
"""URN property works for extension attributes."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert (
path.urn
== "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
def test_urn_invalid_attribute():
"""URN property returns None for invalid attribute."""
path = Path[User]("invalidAttribute")
assert path.urn is None
def test_urn_unbound_path():
"""URN property returns None for unbound path."""
path = Path("userName")
assert path.urn is None
def test_urn_schema_only():
"""URN property returns schema for schema-only path."""
path = Path[User]("urn:ietf:params:scim:schemas:core:2.0:User")
assert path.urn == "urn:ietf:params:scim:schemas:core:2.0:User"
def test_path_caching():
"""Path[Model] classes are cached."""
path_class_1 = Path[User]
path_class_2 = Path[User]
assert path_class_1 is path_class_2
def test_path_different_models():
"""Different models create different Path classes."""
path_class_user = Path[User]
path_class_group = Path[Group]
assert path_class_user is not path_class_group
# --- Path.get() tests ---
def test_get_simple_attribute():
"""Get a simple attribute value."""
user = User(user_name="john.doe")
path = Path("userName")
assert path.get(user) == "john.doe"
def test_get_complex_attribute():
"""Get a complex attribute sub-value."""
user = User(user_name="john", name=Name(family_name="Doe", given_name="John"))
path = Path("name.familyName")
assert path.get(user) == "Doe"
def test_get_with_schema_urn_prefix():
"""Get value using full schema URN prefix."""
user = User(user_name="john.doe")
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.get(user) == "john.doe"
def test_get_extension_attribute():
"""Get an extension attribute value."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert path.get(user) == "12345"
def test_get_extension_root():
"""Get the entire extension object."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
result = path.get(user)
assert isinstance(result, EnterpriseUser)
assert result.employee_number == "12345"
def test_get_none_when_attribute_missing():
"""Get returns None when attribute is not set."""
user = User(user_name="john")
path = Path("displayName")
assert path.get(user) is None
def test_get_none_when_parent_missing():
"""Get returns None when parent complex attribute is not set."""
user = User(user_name="john")
path = Path("name.familyName")
assert path.get(user) is None
def test_get_invalid_attribute():
"""Get raises PathNotFoundException for invalid attribute."""
user = User(user_name="john")
path = Path("invalidAttribute")
with pytest.raises(PathNotFoundException):
path.get(user)
def test_get_extension_complex_subattribute():
"""Get a sub-attribute from extension complex attribute."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(
manager=Manager(value="mgr-123", display_name="Boss")
)
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.value"
)
assert path.get(user) == "mgr-123"
def test_get_invalid_first_part_in_complex_path():
"""Get raises PathNotFoundException when first part of complex path is invalid."""
user = User(user_name="john", name=Name(family_name="Doe"))
path = Path("invalidAttr.subField")
with pytest.raises(PathNotFoundException):
path.get(user)
def test_get_main_schema_prefix_strips_correctly():
"""Get value with main schema prefix handles stripping."""
user = User(user_name="john", display_name="John Doe")
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:displayName")
assert path.get(user) == "John Doe"
def test_get_extension_attribute_uppercase_urn():
"""Get extension attribute with uppercase URN."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path(
"URN:IETF:PARAMS:SCIM:SCHEMAS:EXTENSION:ENTERPRISE:2.0:USER:employeeNumber"
)
assert path.get(user) == "12345"
# --- Path.set() tests ---
def test_set_simple_attribute():
"""Set a simple attribute value."""
user = User(user_name="john")
path = Path("displayName")
result = path.set(user, "John Doe")
assert result is True
assert user.display_name == "John Doe"
def test_set_complex_attribute():
"""Set a complex attribute sub-value, creating parent if needed."""
user = User(user_name="john")
path = Path("name.familyName")
result = path.set(user, "Doe")
assert result is True
assert user.name.family_name == "Doe"
def test_set_with_schema_urn_prefix():
"""Set value using full schema URN prefix."""
user = User(user_name="john")
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:displayName")
result = path.set(user, "John Doe")
assert result is True
assert user.display_name == "John Doe"
def test_set_extension_attribute():
"""Set an extension attribute value, creating extension if needed."""
user = User[EnterpriseUser](user_name="john")
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
result = path.set(user, "12345")
assert result is True
assert user[EnterpriseUser].employee_number == "12345"
def test_set_extension_root():
"""Set the entire extension object."""
user = User[EnterpriseUser](user_name="john")
path = Path("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
result = path.set(user, EnterpriseUser(employee_number="99999"))
assert result is True
assert user[EnterpriseUser].employee_number == "99999"
def test_set_extension_attribute_uppercase_urn():
"""Set extension attribute with uppercase URN."""
user = User[EnterpriseUser](user_name="john")
path = Path(
"URN:IETF:PARAMS:SCIM:SCHEMAS:EXTENSION:ENTERPRISE:2.0:USER:employeeNumber"
)
result = path.set(user, "12345")
assert result is True
assert user[EnterpriseUser].employee_number == "12345"
def test_set_multivalued_wraps_single_value():
"""Set wraps single value in list for multi-valued attributes."""
user = User(user_name="john")
path = Path("emails")
email = Email(value="john@example.com")
result = path.set(user, email)
assert result is True
assert user.emails == [email]
def test_set_invalid_attribute():
"""Set raises PathNotFoundException for invalid attribute."""
user = User(user_name="john")
path = Path("invalidAttribute")
with pytest.raises(PathNotFoundException):
path.set(user, "value")
def test_set_extension_complex_subattribute():
"""Set a sub-attribute on extension complex attribute."""
user = User[EnterpriseUser](user_name="john")
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.value"
)
result = path.set(user, "mgr-456")
assert result is True
assert user[EnterpriseUser].manager.value == "mgr-456"
def test_set_invalid_first_part_in_complex_path():
"""Set raises PathNotFoundException when first part of complex path is invalid."""
user = User(user_name="john")
path = Path("invalidAttr.subField")
with pytest.raises(PathNotFoundException):
path.set(user, "value")
def test_set_main_schema_prefix_strips_correctly():
"""Set value with main schema prefix handles stripping."""
user = User(user_name="john")
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:displayName")
result = path.set(user, "John Doe")
assert result is True
assert user.display_name == "John Doe"
def test_set_creates_intermediate_objects():
"""Set creates intermediate complex objects when needed."""
user = User(user_name="john")
assert user.name is None
path = Path("name.givenName")
result = path.set(user, "John")
assert result is True
assert user.name.given_name == "John"
def test_set_schema_only_path_merges_dict():
"""Set with schema-only path merges dict into resource."""
user = User(user_name="john")
path = Path("urn:ietf:params:scim:schemas:core:2.0:User")
result = path.set(user, {"displayName": "Test"})
assert result is True
assert user.display_name == "Test"
def test_set_cannot_navigate_into_list():
"""Set returns False when trying to navigate into a multi-valued attribute."""
user = User(user_name="john", emails=[Email(value="john@example.com")])
path = Path("emails.value")
result = path.set(user, "new@example.com")
assert result is False
def test_set_invalid_last_part_in_complex_path():
"""Set raises PathNotFoundException when last part of complex path is invalid."""
user = User(user_name="john", name=Name(family_name="Doe"))
path = Path("name.invalidField")
with pytest.raises(PathNotFoundException):
path.set(user, "value")
# --- Path.iter_paths() tests ---
def test_iter_paths_simple_attributes():
"""Iterate over simple attributes of a model."""
paths = list(Path[User].iter_paths(include_subattributes=False))
path_strings = [str(p) for p in paths]
assert "userName" in path_strings
assert "displayName" in path_strings
assert "name" in path_strings
assert "emails" in path_strings
def test_iter_paths_with_subattributes():
"""Iterate includes sub-attributes when enabled."""
paths = list(Path[User].iter_paths(include_subattributes=True))
path_strings = [str(p) for p in paths]
assert "name" in path_strings
assert "name.familyName" in path_strings
assert "name.givenName" in path_strings
assert "emails" in path_strings
assert "emails.value" in path_strings
assert "emails.type" in path_strings
def test_iter_paths_excludes_meta_id_schemas():
"""Iterate excludes meta, id, and schemas fields."""
paths = list(Path[User].iter_paths())
path_strings = [str(p) for p in paths]
assert "meta" not in path_strings
assert "id" not in path_strings
assert "schemas" not in path_strings
def test_iter_paths_includes_extensions():
"""Iterate includes extension model attributes."""
paths = list(Path[User[EnterpriseUser]].iter_paths(include_subattributes=False))
path_strings = [str(p) for p in paths]
assert "userName" in path_strings
assert (
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
in path_strings
)
assert (
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department"
in path_strings
)
def test_iter_paths_excludes_extensions():
"""Iterate excludes extension attributes when include_extensions=False."""
paths = list(
Path[User[EnterpriseUser]].iter_paths(
include_subattributes=False, include_extensions=False
)
)
path_strings = [str(p) for p in paths]
assert "userName" in path_strings
assert not any("enterprise" in p.lower() for p in path_strings)
def test_iter_paths_returns_bound_paths():
"""Iterate returns paths bound to the model."""
paths = list(Path[User[EnterpriseUser]].iter_paths(include_subattributes=False))
user_name_path = next(p for p in paths if str(p) == "userName")
assert user_name_path.model == User[EnterpriseUser]
ext_path = next(
p
for p in paths
if str(p)
== "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert ext_path.model == EnterpriseUser
def test_iter_paths_without_extensions():
"""Iterate over model without extensions."""
paths = list(Path[User].iter_paths(include_subattributes=False))
path_strings = [str(p) for p in paths]
assert "userName" in path_strings
assert "displayName" in path_strings
assert not any("enterprise" in p.lower() for p in path_strings)
def test_iter_paths_requires_bound_path():
"""Iterate raises TypeError if Path is not bound to a model."""
with pytest.raises(TypeError, match="iter_paths requires a bound Path type"):
list(Path.iter_paths())
# --- Path.set() with is_add=True tests ---
def test_set_add_to_empty_list():
"""Add a value to an empty multi-valued attribute."""
user = User(user_name="john")
email = Email(value="john@example.com")
path = Path("emails")
result = path.set(user, email, is_add=True)
assert result is True
assert user.emails == [email]
def test_set_add_to_existing_list():
"""Add a value to an existing multi-valued attribute."""
email1 = Email(value="john@example.com")
user = User(user_name="john", emails=[email1])
email2 = Email(value="john@work.com")
path = Path("emails")
result = path.set(user, email2, is_add=True)
assert result is True
assert len(user.emails) == 2
assert email1 in user.emails
assert email2 in user.emails
def test_set_add_duplicate_not_added():
"""Adding a duplicate value returns False."""
email = Email(value="john@example.com")
user = User(user_name="john", emails=[email])
path = Path("emails")
result = path.set(user, Email(value="john@example.com"), is_add=True)
assert result is False
assert len(user.emails) == 1
def test_set_add_multiple_values():
"""Add multiple values at once."""
user = User(user_name="john")
emails = [Email(value="a@example.com"), Email(value="b@example.com")]
path = Path("emails")
result = path.set(user, emails, is_add=True)
assert result is True
assert len(user.emails) == 2
def test_set_add_extension_attribute():
"""Add value to extension attribute with is_add (non-list, behaves like replace)."""
user = User[EnterpriseUser](user_name="john")
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
result = path.set(user, "12345", is_add=True)
assert result is True
assert user[EnterpriseUser].employee_number == "12345"
# --- Path.delete() tests ---
def test_delete_simple_attribute():
"""Delete a simple attribute value."""
user = User(user_name="john", display_name="John Doe")
path = Path("displayName")
result = path.delete(user)
assert result is True
assert user.display_name is None
def test_delete_attribute_already_none():
"""Delete returns False when attribute is already None."""
user = User(user_name="john")
path = Path("displayName")
result = path.delete(user)
assert result is False
def test_delete_from_list_specific_value():
"""Delete a specific value from a multi-valued attribute."""
email1 = Email(value="john@example.com")
email2 = Email(value="john@work.com")
user = User(user_name="john", emails=[email1, email2])
path = Path("emails")
result = path.delete(user, Email(value="john@example.com"))
assert result is True
assert len(user.emails) == 1
assert user.emails[0].value == "john@work.com"
def test_delete_from_list_value_not_found():
"""Delete returns False when value not in list."""
email = Email(value="john@example.com")
user = User(user_name="john", emails=[email])
path = Path("emails")
result = path.delete(user, Email(value="other@example.com"))
assert result is False
assert len(user.emails) == 1
def test_delete_last_item_from_list():
"""Delete last item from list sets attribute to None."""
email = Email(value="john@example.com")
user = User(user_name="john", emails=[email])
path = Path("emails")
result = path.delete(user, email)
assert result is True
assert user.emails is None
def test_delete_extension_attribute():
"""Delete an extension attribute."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
result = path.delete(user)
assert result is True
assert user[EnterpriseUser].employee_number is None
def test_delete_extension_root():
"""Delete entire extension."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
result = path.delete(user)
assert result is True
assert user[EnterpriseUser] is None
def test_delete_extension_attribute_uppercase_urn():
"""Delete extension attribute with uppercase URN."""
user = User[EnterpriseUser](user_name="john")
user[EnterpriseUser] = EnterpriseUser(employee_number="12345")
path = Path(
"URN:IETF:PARAMS:SCIM:SCHEMAS:EXTENSION:ENTERPRISE:2.0:USER:employeeNumber"
)
result = path.delete(user)
assert result is True
assert user[EnterpriseUser].employee_number is None
def test_delete_complex_subattribute():
"""Delete a sub-attribute of a complex attribute."""
user = User(user_name="john", name=Name(family_name="Doe", given_name="John"))
path = Path("name.familyName")
result = path.delete(user)
assert result is True
assert user.name.family_name is None
assert user.name.given_name == "John"
def test_delete_invalid_path():
"""Delete raises PathNotFoundException for invalid path."""
user = User(user_name="john")
path = Path("invalidAttribute")
with pytest.raises(PathNotFoundException):
path.delete(user)
# --- Path component properties (schema, attr, parts) tests ---
def test_schema_simple_path():
"""Simple path has no schema."""
path = Path("userName")
assert path.schema is None
def test_schema_dotted_path():
"""Dotted path without URN has no schema."""
path = Path("name.familyName")
assert path.schema is None
def test_schema_urn_path():
"""URN path returns schema portion."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.schema == "urn:ietf:params:scim:schemas:core:2.0:User"
def test_schema_extension_path():
"""Extension path returns extension schema."""
path = Path(
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
)
assert path.schema == "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
def test_schema_schema_only_path():
"""Schema-only path returns the full schema."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User")
assert path.schema == "urn:ietf:params:scim:schemas:core:2.0"
def test_attr_simple_path():
"""Simple path attr is the path itself."""
path = Path("userName")
assert path.attr == "userName"
def test_attr_dotted_path():
"""Dotted path attr is the full dotted path."""
path = Path("name.familyName")
assert path.attr == "name.familyName"
def test_attr_urn_path():
"""URN path attr is the attribute after the schema."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.attr == "userName"
def test_attr_urn_dotted_path():
"""URN path with dotted attribute."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:name.familyName")
assert path.attr == "name.familyName"
def test_attr_schema_only_path():
"""Schema-only path has empty attr."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User")
assert path.attr == "User"
def test_attr_empty_path():
"""Empty path has empty attr."""
path = Path("")
assert path.attr == ""
def test_parts_simple_path():
"""Simple path has single part."""
path = Path("userName")
assert path.parts == ("userName",)
def test_parts_dotted_path():
"""Dotted path splits into parts."""
path = Path("name.familyName")
assert path.parts == ("name", "familyName")
def test_parts_urn_path():
"""URN path parts are from attr portion only."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.parts == ("userName",)
def test_parts_urn_dotted_path():
"""URN path with dotted attribute splits correctly."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:name.familyName")
assert path.parts == ("name", "familyName")
def test_parts_empty_path():
"""Empty path has no parts."""
path = Path("")
assert path.parts == ()
def test_parts_deeply_nested():
"""Deeply nested path splits all parts."""
path = Path("a.b.c.d")
assert path.parts == ("a", "b", "c", "d")
# --- Path prefix methods (is_prefix_of, has_prefix) tests ---
def test_is_prefix_of_simple_dot():
"""Path is prefix of dotted sub-path."""
path = Path("emails")
assert path.is_prefix_of("emails.value") is True
def test_is_prefix_of_not_equal():
"""Equal paths are not prefixes of each other."""
path = Path("emails")
assert path.is_prefix_of("emails") is False
def test_is_prefix_of_not_prefix():
"""Unrelated paths are not prefixes."""
path = Path("emails")
assert path.is_prefix_of("userName") is False
def test_is_prefix_of_partial_match_not_prefix():
"""Partial string match without separator is not a prefix."""
path = Path("email")
assert path.is_prefix_of("emails") is False
def test_is_prefix_of_schema_to_attribute():
"""Schema URN is prefix of full attribute path."""
schema = Path("urn:ietf:params:scim:schemas:core:2.0:User")
assert (
schema.is_prefix_of("urn:ietf:params:scim:schemas:core:2.0:User:userName")
is True
)
def test_is_prefix_of_case_insensitive():
"""Prefix matching is case-insensitive."""
path = Path("Emails")
assert path.is_prefix_of("emails.value") is True
def test_is_prefix_of_accepts_path_object():
"""is_prefix_of accepts Path objects."""
path1 = Path("emails")
path2 = Path("emails.value")
assert path1.is_prefix_of(path2) is True
def test_has_prefix_simple_dot():
"""Path has prefix when it starts with prefix + dot."""
path = Path("emails.value")
assert path.has_prefix("emails") is True
def test_has_prefix_not_equal():
"""Equal paths don't have each other as prefix."""
path = Path("emails")
assert path.has_prefix("emails") is False
def test_has_prefix_not_prefix():
"""Unrelated paths don't have prefix relationship."""
path = Path("userName")
assert path.has_prefix("emails") is False
def test_has_prefix_partial_match_not_prefix():
"""Partial string match without separator is not a prefix."""
path = Path("emails")
assert path.has_prefix("email") is False
def test_has_prefix_schema_prefix():
"""Full path has schema as prefix."""
path = Path("urn:ietf:params:scim:schemas:core:2.0:User:userName")
assert path.has_prefix("urn:ietf:params:scim:schemas:core:2.0:User") is True
def test_has_prefix_case_insensitive():
"""Prefix matching is case-insensitive."""
path = Path("emails.value")
assert path.has_prefix("EMAILS") is True
def test_has_prefix_accepts_path_object():
"""has_prefix accepts Path objects."""
path1 = Path("emails.value")
path2 = Path("emails")
assert path1.has_prefix(path2) is True
def test_prefix_symmetry():
"""is_prefix_of and has_prefix are symmetric."""
parent = Path("emails")
child = Path("emails.value")
assert parent.is_prefix_of(child) is True
assert child.has_prefix(parent) is True
assert child.is_prefix_of(parent) is False
assert parent.has_prefix(child) is False
# --- Bound path resolution edge cases ---
def test_model_extension_schema_only_path():
"""Model property returns extension for schema-only extension path."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
)
assert path.model == EnterpriseUser
assert path.field_name is None
def test_model_urn_unknown_extension():
"""Model property returns None for URN not matching any extension."""
path = Path[User[EnterpriseUser]](
"urn:ietf:params:scim:schemas:extension:unknown:2.0:User:field"
)
assert path.model is None
def test_model_dotted_path_invalid_intermediate():
"""Model property returns None when intermediate part is invalid."""
path = Path[User]("invalidAttr.familyName")
assert path.model is None
def test_model_dotted_path_intermediate_not_complex():
"""Model property returns None when intermediate type is not a complex attribute."""
path = Path[User]("userName.subField")
assert path.model is None
def test_urn_empty_path_non_resource():
"""URN property handles empty path on non-Resource model."""
path = Path[Name]("")
assert path.urn is None
# --- get() edge cases ---
def test_get_extension_attribute_when_extension_is_none():
"""Get returns None when extension object doesn't exist."""
user = User[EnterpriseUser](user_name="john")