authentik.sources.scim.tests.test_patch
1from copy import deepcopy 2from unittest.mock import Mock, patch 3 4from rest_framework.test import APITestCase 5 6from authentik.providers.scim.clients.schema import PatchOp, PatchOperation 7from authentik.sources.scim.constants import SCIM_URN_USER_ENTERPRISE 8from authentik.sources.scim.patch.parser import SCIMPathParser 9from authentik.sources.scim.patch.processor import SCIMPatchProcessor 10 11 12class TestSCIMPatchProcessor(APITestCase): 13 14 def setUp(self): 15 """Set up test fixtures""" 16 self.processor = SCIMPatchProcessor() 17 self.sample_data = { 18 "userName": "john.doe", 19 "name": {"givenName": "John", "familyName": "Doe"}, 20 "emails": [ 21 {"value": "john@example.com", "type": "work", "primary": True}, 22 {"value": "john.personal@example.com", "type": "personal"}, 23 ], 24 "active": True, 25 } 26 27 def test_data(self): 28 user_data = { 29 "id": "user123", 30 "userName": "john.doe", 31 "name": {"formatted": "John Doe", "familyName": "Doe", "givenName": "John"}, 32 "emails": [ 33 {"value": "john.doe@example.com", "type": "work", "primary": True}, 34 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 35 ], 36 "phoneNumbers": [ 37 {"value": "+1-555-123-4567", "type": "work", "primary": True}, 38 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 39 ], 40 "addresses": [ 41 { 42 "streetAddress": "123 Work St", 43 "city": "Work City", 44 "type": "work", 45 "primary": True, 46 }, 47 { 48 "streetAddress": "456 Home Ave", 49 "city": "Home City", 50 "type": "home", 51 "primary": False, 52 }, 53 { 54 "streetAddress": "789 Other Rd", 55 "city": "Other City", 56 "type": "work", 57 "primary": False, 58 }, 59 ], 60 } 61 62 # Create processor 63 processor = SCIMPatchProcessor() 64 65 # Example patch operations 66 patches = [ 67 # Replace primary phone number 68 PatchOperation( 69 op=PatchOp.replace, 70 path="phoneNumbers[primary eq true].value", 71 value="+1-555-999-0000", 72 ), 73 # Add new email 74 PatchOperation( 75 op=PatchOp.add, 76 path="emails", 77 value={"value": "john.new@example.com", "type": "home", "primary": False}, 78 ), 79 # Update user's given name 80 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 81 # Remove work email 82 PatchOperation(op=PatchOp.remove, path='emails[type eq "work"]'), 83 # Add with empty path, simple object 84 PatchOperation(op=PatchOp.add, path=None, value={"foo": "bar"}), 85 # Empty path with complex object 86 PatchOperation(op=PatchOp.add, path=None, value={"name.formatted": "formatted"}), 87 ] 88 result = processor.apply_patches(user_data, patches) 89 self.assertEqual( 90 result, 91 { 92 "id": "user123", 93 "userName": "john.doe", 94 "name": {"formatted": "formatted", "familyName": "Doe", "givenName": "Johnny"}, 95 "emails": [ 96 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 97 {"value": "john.new@example.com", "type": "home", "primary": False}, 98 ], 99 "phoneNumbers": [ 100 {"value": "+1-555-999-0000", "type": "work", "primary": True}, 101 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 102 ], 103 "addresses": [ 104 { 105 "streetAddress": "123 Work St", 106 "city": "Work City", 107 "type": "work", 108 "primary": True, 109 }, 110 { 111 "streetAddress": "456 Home Ave", 112 "city": "Home City", 113 "type": "home", 114 "primary": False, 115 }, 116 { 117 "streetAddress": "789 Other Rd", 118 "city": "Other City", 119 "type": "work", 120 "primary": False, 121 }, 122 ], 123 "foo": "bar", 124 }, 125 ) 126 127 def test_parse(self): 128 test_paths = [ 129 { 130 "filter": "userName", 131 "components": [{"attribute": "userName", "filter": None, "sub_attribute": None}], 132 }, 133 { 134 "filter": "name.givenName", 135 "components": [{"attribute": "name", "filter": None, "sub_attribute": "givenName"}], 136 }, 137 { 138 "filter": "emails[primary eq true].value", 139 "components": [ 140 { 141 "attribute": "emails", 142 "filter": { 143 "type": "comparison", 144 "attribute": "primary", 145 "operator": "eq", 146 "value": True, 147 }, 148 "sub_attribute": "value", 149 } 150 ], 151 }, 152 { 153 "filter": 'phoneNumbers[type eq "work"].value', 154 "components": [ 155 { 156 "attribute": "phoneNumbers", 157 "filter": { 158 "type": "comparison", 159 "attribute": "type", 160 "operator": "eq", 161 "value": "work", 162 }, 163 "sub_attribute": "value", 164 } 165 ], 166 }, 167 { 168 "filter": 'addresses[type eq "work" and primary eq true].streetAddress', 169 "components": [ 170 { 171 "attribute": "addresses", 172 "filter": { 173 "type": "logical", 174 "operator": "and", 175 "left": { 176 "type": "comparison", 177 "attribute": "type", 178 "operator": "eq", 179 "value": "work", 180 }, 181 "right": { 182 "type": "comparison", 183 "attribute": "primary", 184 "operator": "eq", 185 "value": True, 186 }, 187 }, 188 "sub_attribute": "streetAddress", 189 } 190 ], 191 }, 192 { 193 "filter": f"{SCIM_URN_USER_ENTERPRISE}:manager", 194 "components": [ 195 { 196 "attribute": SCIM_URN_USER_ENTERPRISE, 197 "filter": None, 198 "sub_attribute": "manager", 199 } 200 ], 201 }, 202 ] 203 204 for path in test_paths: 205 with self.subTest(path=path["filter"]): 206 parser = SCIMPathParser() 207 components = parser.parse_path(path["filter"]) 208 self.assertEqual(components, path["components"]) 209 210 def test_init(self): 211 """Test processor initialization""" 212 processor = SCIMPatchProcessor() 213 self.assertIsNotNone(processor.parser) 214 215 def test_apply_patches_empty_list(self): 216 """Test applying empty patch list returns unchanged data""" 217 result = self.processor.apply_patches(self.sample_data, []) 218 self.assertEqual(result, self.sample_data) 219 # Ensure original data is not modified 220 self.assertIsNot(result, self.sample_data) 221 222 def test_apply_patches_does_not_mutate_input(self): 223 """apply_patches must not mutate its input 224 225 The SCIM source views decide whether to persist a patched resource by 226 comparing the patched data against the stored attributes, so a nested 227 mutation that leaks into the input dict is silently dropped: the 228 comparison can no longer see a difference. The input must stay 229 untouched (regression test for #26896). 230 """ 231 data = { 232 "userName": "john.doe", 233 "name": {"givenName": "John", "familyName": "Doe"}, 234 "emails": [ 235 {"value": "john@example.com", "type": "work", "primary": True}, 236 ], 237 } 238 before = deepcopy(data) 239 patches = [ 240 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 241 PatchOperation( 242 op=PatchOp.replace, 243 path='emails[type eq "work"].value', 244 value="john.new@example.com", 245 ), 246 PatchOperation(op=PatchOp.remove, path="name.familyName"), 247 ] 248 249 result = self.processor.apply_patches(data, patches) 250 251 # the patched data carries the new values 252 self.assertEqual(result["name"]["givenName"], "Johnny") 253 self.assertNotIn("familyName", result["name"]) 254 self.assertEqual(result["emails"][0]["value"], "john.new@example.com") 255 # the input dict is not modified, and the change remains visible to the caller 256 self.assertEqual(data, before) 257 self.assertNotEqual(result, data) 258 259 def test_apply_patches_with_validation(self): 260 """Test that patches are validated using PatchOperation.model_validate""" 261 with patch("authentik.sources.scim.patch.processor.PatchOperation") as mock_patch_op: 262 mock_patch_op.model_validate.return_value = Mock( 263 path="userName", op=PatchOp.replace, value="jane.doe" 264 ) 265 266 patches = [{"op": "replace", "path": "userName", "value": "jane.doe"}] 267 self.processor.apply_patches(self.sample_data, patches) 268 269 mock_patch_op.model_validate.assert_called_once() 270 271 # Test ADD operations 272 def test_apply_add_simple_attribute(self): 273 """Test adding a simple attribute""" 274 with patch.object(self.processor.parser, "parse_path") as mock_parse: 275 mock_parse.return_value = [ 276 {"attribute": "title", "filter": None, "sub_attribute": None} 277 ] 278 279 patches = [PatchOperation(op=PatchOp.add, path="title", value="Manager")] 280 result = self.processor.apply_patches(self.sample_data, patches) 281 282 self.assertEqual(result["title"], "Manager") 283 284 def test_apply_add_sub_attribute(self): 285 """Test adding a sub-attribute""" 286 with patch.object(self.processor.parser, "parse_path") as mock_parse: 287 mock_parse.return_value = [ 288 {"attribute": "name", "filter": None, "sub_attribute": "middleName"} 289 ] 290 291 patches = [PatchOperation(op=PatchOp.add, path="name.middleName", value="William")] 292 result = self.processor.apply_patches(self.sample_data, patches) 293 294 self.assertEqual(result["name"]["middleName"], "William") 295 296 def test_apply_add_sub_attribute_new_parent(self): 297 """Test adding a sub-attribute when parent doesn't exist""" 298 with patch.object(self.processor.parser, "parse_path") as mock_parse: 299 mock_parse.return_value = [ 300 {"attribute": "address", "filter": None, "sub_attribute": "street"} 301 ] 302 303 patches = [PatchOperation(op=PatchOp.add, path="address.street", value="123 Main St")] 304 result = self.processor.apply_patches(self.sample_data, patches) 305 306 self.assertEqual(result["address"]["street"], "123 Main St") 307 308 def test_apply_add_enterprise_manager(self): 309 """Test adding enterprise manager attribute (special case)""" 310 with patch.object(self.processor.parser, "parse_path") as mock_parse: 311 mock_parse.return_value = [ 312 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 313 ] 314 315 patches = [ 316 PatchOperation( 317 op=PatchOp.add, path=f"{SCIM_URN_USER_ENTERPRISE}.manager", value="mgr123" 318 ) 319 ] 320 result = self.processor.apply_patches(self.sample_data, patches) 321 322 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "mgr123"}) 323 324 def test_apply_add_to_existing_array(self): 325 """Test adding to an existing array attribute""" 326 with patch.object(self.processor.parser, "parse_path") as mock_parse: 327 mock_parse.return_value = [ 328 {"attribute": "emails", "filter": None, "sub_attribute": None} 329 ] 330 331 new_email = {"value": "john.work@example.com", "type": "work"} 332 patches = [PatchOperation(op=PatchOp.add, path="emails", value=new_email)] 333 result = self.processor.apply_patches(self.sample_data, patches) 334 335 self.assertEqual(len(result["emails"]), 3) 336 self.assertIn(new_email, result["emails"]) 337 338 def test_apply_add_new_attribute_as_value(self): 339 """Test adding a new attribute that gets set as value (not array)""" 340 with patch.object(self.processor.parser, "parse_path") as mock_parse: 341 mock_parse.return_value = [ 342 {"attribute": "department", "filter": None, "sub_attribute": None} 343 ] 344 345 patches = [PatchOperation(op=PatchOp.add, path="department", value="Engineering")] 346 result = self.processor.apply_patches(self.sample_data, patches) 347 348 self.assertEqual(result["department"], "Engineering") 349 350 def test_apply_add_complex_path(self): 351 """Test adding with complex path (filters)""" 352 with patch.object(self.processor.parser, "parse_path") as mock_parse: 353 mock_parse.return_value = [ 354 { 355 "attribute": "emails", 356 "filter": {"type": "comparison"}, 357 "sub_attribute": "verified", 358 } 359 ] 360 361 patches = [ 362 PatchOperation(op=PatchOp.add, path='emails[type eq "work"].verified', value=True) 363 ] 364 365 with patch.object(self.processor, "_navigate_and_modify") as mock_navigate: 366 self.processor.apply_patches(self.sample_data, patches) 367 mock_navigate.assert_called_once() 368 369 # Test REMOVE operations 370 def test_apply_remove_simple_attribute(self): 371 """Test removing a simple attribute""" 372 with patch.object(self.processor.parser, "parse_path") as mock_parse: 373 mock_parse.return_value = [ 374 {"attribute": "active", "filter": None, "sub_attribute": None} 375 ] 376 377 patches = [PatchOperation(op=PatchOp.remove, path="active")] 378 result = self.processor.apply_patches(self.sample_data, patches) 379 380 self.assertNotIn("active", result) 381 382 def test_apply_remove_sub_attribute(self): 383 """Test removing a sub-attribute""" 384 with patch.object(self.processor.parser, "parse_path") as mock_parse: 385 mock_parse.return_value = [ 386 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 387 ] 388 389 patches = [PatchOperation(op=PatchOp.remove, path="name.givenName")] 390 result = self.processor.apply_patches(self.sample_data, patches) 391 392 self.assertNotIn("givenName", result["name"]) 393 self.assertIn("familyName", result["name"]) # Other sub-attributes remain 394 395 def test_apply_remove_sub_attribute_nonexistent_parent(self): 396 """Test removing sub-attribute when parent doesn't exist""" 397 with patch.object(self.processor.parser, "parse_path") as mock_parse: 398 mock_parse.return_value = [ 399 {"attribute": "nonexistent", "filter": None, "sub_attribute": "field"} 400 ] 401 402 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent.field")] 403 result = self.processor.apply_patches(self.sample_data, patches) 404 405 # Should not raise error and data should be unchanged 406 self.assertEqual(result, self.sample_data) 407 408 def test_apply_remove_nonexistent_attribute(self): 409 """Test removing a non-existent attribute (should not raise error)""" 410 with patch.object(self.processor.parser, "parse_path") as mock_parse: 411 mock_parse.return_value = [ 412 {"attribute": "nonexistent", "filter": None, "sub_attribute": None} 413 ] 414 415 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent")] 416 result = self.processor.apply_patches(self.sample_data, patches) 417 418 # Should not raise error and data should be unchanged 419 self.assertEqual(result, self.sample_data) 420 421 # Test REPLACE operations 422 def test_apply_replace_simple_attribute(self): 423 """Test replacing a simple attribute""" 424 with patch.object(self.processor.parser, "parse_path") as mock_parse: 425 mock_parse.return_value = [ 426 {"attribute": "userName", "filter": None, "sub_attribute": None} 427 ] 428 429 patches = [PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe")] 430 result = self.processor.apply_patches(self.sample_data, patches) 431 432 self.assertEqual(result["userName"], "jane.doe") 433 434 def test_apply_replace_sub_attribute(self): 435 """Test replacing a sub-attribute""" 436 with patch.object(self.processor.parser, "parse_path") as mock_parse: 437 mock_parse.return_value = [ 438 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 439 ] 440 441 patches = [PatchOperation(op=PatchOp.replace, path="name.givenName", value="Jane")] 442 result = self.processor.apply_patches(self.sample_data, patches) 443 444 self.assertEqual(result["name"]["givenName"], "Jane") 445 446 def test_apply_replace_sub_attribute_new_parent(self): 447 """Test replacing sub-attribute when parent doesn't exist""" 448 with patch.object(self.processor.parser, "parse_path") as mock_parse: 449 mock_parse.return_value = [ 450 {"attribute": "address", "filter": None, "sub_attribute": "city"} 451 ] 452 453 patches = [PatchOperation(op=PatchOp.replace, path="address.city", value="New York")] 454 result = self.processor.apply_patches(self.sample_data, patches) 455 456 self.assertEqual(result["address"]["city"], "New York") 457 458 def test_apply_replace_enterprise_manager(self): 459 """Test replacing enterprise manager attribute (special case)""" 460 with patch.object(self.processor.parser, "parse_path") as mock_parse: 461 mock_parse.return_value = [ 462 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 463 ] 464 465 patches = [ 466 PatchOperation( 467 op=PatchOp.replace, 468 path=f"{SCIM_URN_USER_ENTERPRISE}.manager", 469 value="newmgr456", 470 ) 471 ] 472 result = self.processor.apply_patches(self.sample_data, patches) 473 474 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "newmgr456"}) 475 476 # Test bulk operations (path is None) 477 def test_apply_bulk_add_operation(self): 478 """Test bulk add operation when path is None""" 479 patches = [ 480 PatchOperation( 481 op=PatchOp.add, path=None, value={"title": "Manager", "department": "IT"} 482 ) 483 ] 484 485 with patch.object(self.processor, "_apply_add") as mock_add: 486 self.processor.apply_patches(self.sample_data, patches) 487 self.assertEqual(mock_add.call_count, 2) 488 489 def test_apply_bulk_remove_operation(self): 490 """Test bulk remove operation when path is None""" 491 patches = [ 492 PatchOperation(op=PatchOp.remove, path=None, value={"active": None, "userName": None}) 493 ] 494 495 with patch.object(self.processor, "_apply_remove") as mock_remove: 496 self.processor.apply_patches(self.sample_data, patches) 497 self.assertEqual(mock_remove.call_count, 2) 498 499 def test_apply_bulk_replace_operation(self): 500 """Test bulk replace operation when path is None""" 501 patches = [ 502 PatchOperation( 503 op=PatchOp.replace, path=None, value={"userName": "jane.doe", "active": False} 504 ) 505 ] 506 507 with patch.object(self.processor, "_apply_replace") as mock_replace: 508 self.processor.apply_patches(self.sample_data, patches) 509 self.assertEqual(mock_replace.call_count, 2) 510 511 def test_apply_bulk_operation_invalid_value(self): 512 """Test bulk operation with non-dict value (should be ignored)""" 513 patches = [PatchOperation(op=PatchOp.add, path=None, value="invalid")] 514 result = self.processor.apply_patches(self.sample_data, patches) 515 516 self.assertEqual(result, self.sample_data) 517 518 # Test _navigate_and_modify method 519 def test_navigate_and_modify_with_filter_add_new_item(self): 520 """Test navigating with filter and adding new item""" 521 components = [ 522 { 523 "attribute": "emails", 524 "filter": { 525 "type": "comparison", 526 "attribute": "type", 527 "operator": "eq", 528 "value": "home", 529 }, 530 "sub_attribute": None, 531 } 532 ] 533 534 new_email = {"value": "home@example.com", "type": "home"} 535 data_copy = self.sample_data.copy() 536 data_copy["emails"] = self.sample_data["emails"].copy() 537 538 self.processor._navigate_and_modify(data_copy, components, new_email, "add") 539 540 # Should add new email with type "home" 541 home_emails = [email for email in data_copy["emails"] if email.get("type") == "home"] 542 self.assertEqual(len(home_emails), 1) 543 544 def test_navigate_and_modify_with_filter_modify_existing(self): 545 """Test navigating with filter and modifying existing item""" 546 components = [ 547 { 548 "attribute": "emails", 549 "filter": { 550 "type": "comparison", 551 "attribute": "type", 552 "operator": "eq", 553 "value": "work", 554 }, 555 "sub_attribute": "verified", 556 } 557 ] 558 559 data_copy = self.sample_data.copy() 560 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 561 562 self.processor._navigate_and_modify(data_copy, components, True, "add") 563 564 # Should add verified field to work email 565 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 566 self.assertTrue(work_email["verified"]) 567 568 def test_navigate_and_modify_remove_item(self): 569 """Test removing entire item with filter""" 570 components = [ 571 { 572 "attribute": "emails", 573 "filter": { 574 "type": "comparison", 575 "attribute": "type", 576 "operator": "eq", 577 "value": "personal", 578 }, 579 "sub_attribute": None, 580 } 581 ] 582 583 data_copy = self.sample_data.copy() 584 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 585 original_count = len(data_copy["emails"]) 586 587 self.processor._navigate_and_modify(data_copy, components, None, "remove") 588 589 # Should remove personal email 590 self.assertEqual(len(data_copy["emails"]), original_count - 1) 591 personal_emails = [ 592 email for email in data_copy["emails"] if email.get("type") == "personal" 593 ] 594 self.assertEqual(len(personal_emails), 0) 595 596 def test_navigate_and_modify_nonexistent_attribute_add(self): 597 """Test navigating to non-existent attribute for add operation""" 598 components = [ 599 { 600 "attribute": "phones", 601 "filter": { 602 "type": "comparison", 603 "attribute": "type", 604 "operator": "eq", 605 "value": "mobile", 606 }, 607 "sub_attribute": None, 608 } 609 ] 610 611 data_copy = self.sample_data.copy() 612 self.processor._navigate_and_modify( 613 data_copy, components, {"value": "123-456-7890", "type": "mobile"}, "add" 614 ) 615 616 # Should create new phones array 617 self.assertIn("phones", data_copy) 618 self.assertEqual(len(data_copy["phones"]), 1) 619 620 def test_navigate_and_modify_nonexistent_attribute_remove(self): 621 """Test navigating to non-existent attribute for remove operation""" 622 components = [ 623 { 624 "attribute": "phones", 625 "filter": { 626 "type": "comparison", 627 "attribute": "type", 628 "operator": "eq", 629 "value": "mobile", 630 }, 631 "sub_attribute": None, 632 } 633 ] 634 635 data_copy = self.sample_data.copy() 636 self.processor._navigate_and_modify(data_copy, components, None, "remove") 637 638 # Should not create attribute or raise error 639 self.assertNotIn("phones", data_copy) 640 641 # Test filter matching methods 642 def test_matches_filter_no_filter(self): 643 """Test matching with no filter (should return True)""" 644 item = {"type": "work"} 645 result = self.processor._matches_filter(item, None) 646 self.assertTrue(result) 647 648 def test_matches_filter_empty_filter(self): 649 """Test matching with empty filter (should return True)""" 650 item = {"type": "work"} 651 result = self.processor._matches_filter(item, {}) 652 self.assertTrue(result) 653 654 def test_matches_filter_unknown_type(self): 655 """Test matching with unknown filter type""" 656 item = {"type": "work"} 657 filter_expr = {"type": "unknown"} 658 result = self.processor._matches_filter(item, filter_expr) 659 self.assertFalse(result) 660 661 def test_matches_comparison_eq(self): 662 """Test comparison filter with eq operator""" 663 item = {"type": "work", "primary": True} 664 filter_expr = {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"} 665 666 result = self.processor._matches_comparison(item, filter_expr) 667 self.assertTrue(result) 668 669 def test_matches_comparison_eq_false(self): 670 """Test comparison filter with eq operator (false case)""" 671 item = {"type": "work"} 672 filter_expr = { 673 "type": "comparison", 674 "attribute": "type", 675 "operator": "eq", 676 "value": "personal", 677 } 678 679 result = self.processor._matches_comparison(item, filter_expr) 680 self.assertFalse(result) 681 682 def test_matches_comparison_ne(self): 683 """Test comparison filter with ne operator""" 684 item = {"type": "work"} 685 filter_expr = { 686 "type": "comparison", 687 "attribute": "type", 688 "operator": "ne", 689 "value": "personal", 690 } 691 692 result = self.processor._matches_comparison(item, filter_expr) 693 self.assertTrue(result) 694 695 def test_matches_comparison_co(self): 696 """Test comparison filter with co (contains) operator""" 697 item = {"value": "john@example.com"} 698 filter_expr = { 699 "type": "comparison", 700 "attribute": "value", 701 "operator": "co", 702 "value": "example", 703 } 704 705 result = self.processor._matches_comparison(item, filter_expr) 706 self.assertTrue(result) 707 708 def test_matches_comparison_sw(self): 709 """Test comparison filter with sw (starts with) operator""" 710 item = {"value": "john@example.com"} 711 filter_expr = { 712 "type": "comparison", 713 "attribute": "value", 714 "operator": "sw", 715 "value": "john", 716 } 717 718 result = self.processor._matches_comparison(item, filter_expr) 719 self.assertTrue(result) 720 721 def test_matches_comparison_ew(self): 722 """Test comparison filter with ew (ends with) operator""" 723 item = {"value": "john@example.com"} 724 filter_expr = { 725 "type": "comparison", 726 "attribute": "value", 727 "operator": "ew", 728 "value": ".com", 729 } 730 731 result = self.processor._matches_comparison(item, filter_expr) 732 self.assertTrue(result) 733 734 def test_matches_comparison_gt(self): 735 """Test comparison filter with gt (greater than) operator""" 736 item = {"priority": 10} 737 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "gt", "value": 5} 738 739 result = self.processor._matches_comparison(item, filter_expr) 740 self.assertTrue(result) 741 742 def test_matches_comparison_lt(self): 743 """Test comparison filter with lt (less than) operator""" 744 item = {"priority": 3} 745 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "lt", "value": 5} 746 747 result = self.processor._matches_comparison(item, filter_expr) 748 self.assertTrue(result) 749 750 def test_matches_comparison_ge(self): 751 """Test comparison filter with ge (greater than or equal) operator""" 752 item = {"priority": 5} 753 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "ge", "value": 5} 754 755 result = self.processor._matches_comparison(item, filter_expr) 756 self.assertTrue(result) 757 758 def test_matches_comparison_le(self): 759 """Test comparison filter with le (less than or equal) operator""" 760 item = {"priority": 5} 761 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "le", "value": 5} 762 763 result = self.processor._matches_comparison(item, filter_expr) 764 self.assertTrue(result) 765 766 def test_matches_comparison_pr(self): 767 """Test comparison filter with pr (present) operator""" 768 item = {"value": "john@example.com"} 769 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 770 771 result = self.processor._matches_comparison(item, filter_expr) 772 self.assertTrue(result) 773 774 def test_matches_comparison_pr_false(self): 775 """Test comparison filter with pr operator (false case)""" 776 item = {"value": None} 777 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 778 779 result = self.processor._matches_comparison(item, filter_expr) 780 self.assertFalse(result) 781 782 def test_matches_comparison_missing_attribute(self): 783 """Test comparison filter with missing attribute""" 784 item = {"type": "work"} 785 filter_expr = { 786 "type": "comparison", 787 "attribute": "missing", 788 "operator": "eq", 789 "value": "test", 790 } 791 792 result = self.processor._matches_comparison(item, filter_expr) 793 self.assertFalse(result) 794 795 def test_matches_comparison_unknown_operator(self): 796 """Test comparison filter with unknown operator""" 797 item = {"type": "work"} 798 filter_expr = { 799 "type": "comparison", 800 "attribute": "type", 801 "operator": "unknown", 802 "value": "work", 803 } 804 805 result = self.processor._matches_comparison(item, filter_expr) 806 self.assertFalse(result) 807 808 def test_matches_logical_and_true(self): 809 """Test logical AND filter (true case)""" 810 item = {"type": "work", "primary": True} 811 filter_expr = { 812 "type": "logical", 813 "operator": "and", 814 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 815 "right": { 816 "type": "comparison", 817 "attribute": "primary", 818 "operator": "eq", 819 "value": True, 820 }, 821 } 822 823 result = self.processor._matches_logical(item, filter_expr) 824 self.assertTrue(result) 825 826 def test_matches_logical_and_false(self): 827 """Test logical AND filter (false case)""" 828 item = {"type": "work", "primary": False} 829 filter_expr = { 830 "type": "logical", 831 "operator": "and", 832 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 833 "right": { 834 "type": "comparison", 835 "attribute": "primary", 836 "operator": "eq", 837 "value": True, 838 }, 839 } 840 841 result = self.processor._matches_logical(item, filter_expr) 842 self.assertFalse(result) 843 844 def test_matches_logical_or_true(self): 845 """Test logical OR filter (true case)""" 846 item = {"type": "personal", "primary": True} 847 filter_expr = { 848 "type": "logical", 849 "operator": "or", 850 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 851 "right": { 852 "type": "comparison", 853 "attribute": "primary", 854 "operator": "eq", 855 "value": True, 856 }, 857 } 858 859 result = self.processor._matches_logical(item, filter_expr) 860 self.assertTrue(result) 861 862 def test_matches_logical_or_false(self): 863 """Test logical OR filter (false case)""" 864 item = {"type": "personal", "primary": False} 865 filter_expr = { 866 "type": "logical", 867 "operator": "or", 868 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 869 "right": { 870 "type": "comparison", 871 "attribute": "primary", 872 "operator": "eq", 873 "value": True, 874 }, 875 } 876 877 result = self.processor._matches_logical(item, filter_expr) 878 self.assertFalse(result) 879 880 def test_matches_logical_not_true(self): 881 """Test logical NOT filter (true case)""" 882 item = {"type": "personal"} 883 filter_expr = { 884 "type": "logical", 885 "operator": "not", 886 "operand": { 887 "type": "comparison", 888 "attribute": "type", 889 "operator": "eq", 890 "value": "work", 891 }, 892 } 893 894 result = self.processor._matches_logical(item, filter_expr) 895 self.assertTrue(result) 896 897 def test_matches_logical_not_false(self): 898 """Test logical NOT filter (false case)""" 899 item = {"type": "work"} 900 filter_expr = { 901 "type": "logical", 902 "operator": "not", 903 "operand": { 904 "type": "comparison", 905 "attribute": "type", 906 "operator": "eq", 907 "value": "work", 908 }, 909 } 910 911 result = self.processor._matches_logical(item, filter_expr) 912 self.assertFalse(result) 913 914 def test_matches_logical_unknown_operator(self): 915 """Test logical filter with unknown operator""" 916 item = {"type": "work"} 917 filter_expr = { 918 "type": "logical", 919 "operator": "unknown", 920 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 921 } 922 923 result = self.processor._matches_logical(item, filter_expr) 924 self.assertFalse(result) 925 926 def test_multiple_patches_applied_sequentially(self): 927 """Test that multiple patches are applied in sequence""" 928 patches = [ 929 PatchOperation(op=PatchOp.add, path="title", value="Manager"), 930 PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe"), 931 PatchOperation(op=PatchOp.remove, path="active"), 932 ] 933 934 with patch.object(self.processor.parser, "parse_path") as mock_parse: 935 mock_parse.side_effect = [ 936 [{"attribute": "title", "filter": None, "sub_attribute": None}], 937 [{"attribute": "userName", "filter": None, "sub_attribute": None}], 938 [{"attribute": "active", "filter": None, "sub_attribute": None}], 939 ] 940 941 result = self.processor.apply_patches(self.sample_data, patches) 942 943 self.assertEqual(result["title"], "Manager") 944 self.assertEqual(result["userName"], "jane.doe") 945 self.assertNotIn("active", result) 946 947 def test_navigate_and_modify_simple_attribute_last_component_add(self): 948 """Test navigating to simple attribute as last component with add operation""" 949 components = [ 950 {"attribute": "profile", "filter": None, "sub_attribute": None}, 951 {"attribute": "title", "filter": None, "sub_attribute": None}, 952 ] 953 954 data_copy = self.sample_data.copy() 955 data_copy["profile"] = {} 956 957 self.processor._navigate_and_modify(data_copy, components, "Senior Manager", "add") 958 959 self.assertEqual(data_copy["profile"]["title"], "Senior Manager") 960 961 def test_navigate_and_modify_simple_attribute_last_component_replace(self): 962 """Test navigating to simple attribute as last component with replace operation""" 963 components = [ 964 {"attribute": "profile", "filter": None, "sub_attribute": None}, 965 {"attribute": "title", "filter": None, "sub_attribute": None}, 966 ] 967 968 data_copy = self.sample_data.copy() 969 data_copy["profile"] = {"title": "Manager"} 970 971 self.processor._navigate_and_modify(data_copy, components, "Director", "replace") 972 973 self.assertEqual(data_copy["profile"]["title"], "Director") 974 975 def test_navigate_and_modify_simple_attribute_last_component_remove(self): 976 """Test navigating to simple attribute as last component with remove operation""" 977 components = [ 978 {"attribute": "profile", "filter": None, "sub_attribute": None}, 979 {"attribute": "title", "filter": None, "sub_attribute": None}, 980 ] 981 982 data_copy = self.sample_data.copy() 983 data_copy["profile"] = {"title": "Manager", "department": "IT"} 984 985 self.processor._navigate_and_modify(data_copy, components, None, "remove") 986 987 self.assertNotIn("title", data_copy["profile"]) 988 self.assertIn("department", data_copy["profile"]) # Other attributes remain 989 990 def test_navigate_and_modify_sub_attribute_last_component_add(self): 991 """Test navigating to sub-attribute as last component with add operation""" 992 components = [ 993 {"attribute": "profile", "filter": None, "sub_attribute": None}, 994 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 995 ] 996 997 data_copy = self.sample_data.copy() 998 data_copy["profile"] = {"address": {}} 999 1000 self.processor._navigate_and_modify(data_copy, components, "123 Main St", "add") 1001 1002 self.assertEqual(data_copy["profile"]["address"]["street"], "123 Main St") 1003 1004 def test_navigate_and_modify_sub_attribute_last_component_replace(self): 1005 """Test navigating to sub-attribute as last component with replace operation""" 1006 components = [ 1007 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1008 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1009 ] 1010 1011 data_copy = self.sample_data.copy() 1012 data_copy["profile"] = {"address": {"street": "456 Oak Ave"}} 1013 1014 self.processor._navigate_and_modify(data_copy, components, "789 Pine Rd", "replace") 1015 1016 self.assertEqual(data_copy["profile"]["address"]["street"], "789 Pine Rd") 1017 1018 def test_navigate_and_modify_sub_attribute_last_component_remove(self): 1019 """Test navigating to sub-attribute as last component with remove operation""" 1020 components = [ 1021 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1022 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1023 ] 1024 1025 data_copy = self.sample_data.copy() 1026 data_copy["profile"] = {"address": {"street": "123 Main St", "city": "New York"}} 1027 1028 self.processor._navigate_and_modify(data_copy, components, None, "remove") 1029 1030 self.assertNotIn("street", data_copy["profile"]["address"]) 1031 self.assertIn("city", data_copy["profile"]["address"]) # Other sub-attributes remain 1032 1033 def test_navigate_and_modify_sub_attribute_parent_not_exists(self): 1034 """Test navigating to sub-attribute when parent attribute doesn't exist""" 1035 components = [ 1036 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1037 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1038 ] 1039 1040 data_copy = self.sample_data.copy() 1041 data_copy["profile"] = {} # address doesn't exist yet 1042 1043 self.processor._navigate_and_modify(data_copy, components, "123 Main St", "add") 1044 1045 self.assertEqual(data_copy["profile"]["address"]["street"], "123 Main St") 1046 1047 def test_navigate_and_modify_deeper_navigation(self): 1048 """Test navigating deeper through multiple levels without filters""" 1049 components = [ 1050 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1051 {"attribute": "department", "filter": None, "sub_attribute": None}, 1052 {"attribute": "team", "filter": None, "sub_attribute": None}, 1053 {"attribute": "name", "filter": None, "sub_attribute": None}, 1054 ] 1055 1056 data_copy = self.sample_data.copy() 1057 1058 self.processor._navigate_and_modify(data_copy, components, "Engineering Team Alpha", "add") 1059 1060 self.assertEqual( 1061 data_copy["organization"]["department"]["team"]["name"], "Engineering Team Alpha" 1062 ) 1063 1064 def test_navigate_and_modify_deeper_navigation_partial_path_exists(self): 1065 """Test navigating deeper when part of the path already exists""" 1066 components = [ 1067 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1068 {"attribute": "department", "filter": None, "sub_attribute": None}, 1069 {"attribute": "budget", "filter": None, "sub_attribute": None}, 1070 ] 1071 1072 data_copy = self.sample_data.copy() 1073 data_copy["organization"] = {"department": {"name": "IT"}} 1074 1075 self.processor._navigate_and_modify(data_copy, components, 100000, "add") 1076 1077 self.assertEqual(data_copy["organization"]["department"]["budget"], 100000) 1078 self.assertEqual( 1079 data_copy["organization"]["department"]["name"], "IT" 1080 ) # Existing data preserved 1081 1082 def test_navigate_and_modify_array_not_list_type(self): 1083 """Test navigation when expected array attribute is not a list""" 1084 components = [ 1085 { 1086 "attribute": "emails", 1087 "filter": { 1088 "type": "comparison", 1089 "attribute": "type", 1090 "operator": "eq", 1091 "value": "work", 1092 }, 1093 "sub_attribute": "verified", 1094 } 1095 ] 1096 1097 data_copy = self.sample_data.copy() 1098 data_copy["emails"] = "not_a_list" # Invalid type 1099 1100 # Should return early without error 1101 self.processor._navigate_and_modify(data_copy, components, True, "add") 1102 1103 # Data should remain unchanged 1104 self.assertEqual(data_copy["emails"], "not_a_list") 1105 1106 def test_navigate_and_modify_update_matching_item_with_dict_value(self): 1107 """Test updating matching item with dictionary value""" 1108 components = [ 1109 { 1110 "attribute": "emails", 1111 "filter": { 1112 "type": "comparison", 1113 "attribute": "type", 1114 "operator": "eq", 1115 "value": "work", 1116 }, 1117 "sub_attribute": None, 1118 } 1119 ] 1120 1121 data_copy = self.sample_data.copy() 1122 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1123 1124 update_data = {"verified": True, "lastChecked": "2023-01-01"} 1125 self.processor._navigate_and_modify(data_copy, components, update_data, "add") 1126 1127 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1128 self.assertTrue(work_email["verified"]) 1129 self.assertEqual(work_email["lastChecked"], "2023-01-01") 1130 # Original fields should still exist 1131 self.assertEqual(work_email["value"], "john@example.com") 1132 1133 def test_navigate_and_modify_update_matching_item_with_non_dict_value(self): 1134 """Test updating matching item with non-dictionary value (should be ignored)""" 1135 components = [ 1136 { 1137 "attribute": "emails", 1138 "filter": { 1139 "type": "comparison", 1140 "attribute": "type", 1141 "operator": "eq", 1142 "value": "work", 1143 }, 1144 "sub_attribute": None, 1145 } 1146 ] 1147 1148 data_copy = self.sample_data.copy() 1149 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1150 original_work_email = next( 1151 email for email in data_copy["emails"] if email.get("type") == "work" 1152 ).copy() 1153 1154 # Try to update with non-dict value 1155 self.processor._navigate_and_modify(data_copy, components, "string_value", "add") 1156 1157 # Email should remain unchanged 1158 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1159 self.assertEqual(work_email, original_work_email) 1160 1161 def test_navigate_and_modify_remove_entire_matching_item(self): 1162 """Test removing entire matching item from array""" 1163 components = [ 1164 { 1165 "attribute": "emails", 1166 "filter": { 1167 "type": "comparison", 1168 "attribute": "type", 1169 "operator": "eq", 1170 "value": "personal", 1171 }, 1172 "sub_attribute": None, 1173 } 1174 ] 1175 1176 data_copy = self.sample_data.copy() 1177 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1178 original_count = len(data_copy["emails"]) 1179 1180 self.processor._navigate_and_modify(data_copy, components, None, "remove") 1181 1182 # Should remove the personal email 1183 self.assertEqual(len(data_copy["emails"]), original_count - 1) 1184 personal_emails = [ 1185 email for email in data_copy["emails"] if email.get("type") == "personal" 1186 ] 1187 self.assertEqual(len(personal_emails), 0) 1188 1189 # Work email should still exist 1190 work_emails = [email for email in data_copy["emails"] if email.get("type") == "work"] 1191 self.assertEqual(len(work_emails), 1) 1192 1193 def test_navigate_and_modify_mixed_filters_and_simple_navigation(self): 1194 """Test navigation with mix of filtered and simple components""" 1195 # This test actually reveals a limitation in the current implementation 1196 # The _navigate_and_modify method doesn't properly handle navigation 1197 # after a filtered component. Let's test what actually happens. 1198 components = [ 1199 { 1200 "attribute": "emails", 1201 "filter": { 1202 "type": "comparison", 1203 "attribute": "type", 1204 "operator": "eq", 1205 "value": "work", 1206 }, 1207 "sub_attribute": "verified", # Changed to test sub_attribute on filtered item 1208 } 1209 ] 1210 1211 data_copy = self.sample_data.copy() 1212 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1213 1214 self.processor._navigate_and_modify(data_copy, components, True, "add") 1215 1216 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1217 self.assertTrue(work_email["verified"]) 1218 1219 def test_navigate_and_modify_simple_navigation_multiple_levels(self): 1220 """Test simple navigation through multiple levels without filters""" 1221 components = [ 1222 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1223 {"attribute": "settings", "filter": None, "sub_attribute": None}, 1224 {"attribute": "notifications", "filter": None, "sub_attribute": "email"}, 1225 ] 1226 1227 data_copy = self.sample_data.copy() 1228 1229 self.processor._navigate_and_modify(data_copy, components, True, "add") 1230 1231 self.assertTrue(data_copy["profile"]["settings"]["notifications"]["email"]) 1232 1233 def test_navigate_and_modify_filter_then_simple_attribute_workaround(self): 1234 """Test the actual behavior when we have filter followed by simple navigation""" 1235 # Based on the code, after processing a filter, the method doesn't continue 1236 # to navigate deeper. This test documents the current behavior. 1237 components = [ 1238 { 1239 "attribute": "emails", 1240 "filter": { 1241 "type": "comparison", 1242 "attribute": "type", 1243 "operator": "eq", 1244 "value": "work", 1245 }, 1246 "sub_attribute": None, 1247 } 1248 ] 1249 1250 data_copy = self.sample_data.copy() 1251 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1252 1253 # Update the work email with a dict containing nested data 1254 update_data = {"metadata": {"verified": True, "source": "manual"}} 1255 self.processor._navigate_and_modify(data_copy, components, update_data, "add") 1256 1257 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1258 self.assertTrue(work_email["metadata"]["verified"]) 1259 self.assertEqual(work_email["metadata"]["source"], "manual") 1260 1261 def test_navigate_and_modify_intermediate_navigation_missing_parent(self): 1262 """Test navigation when intermediate parent doesn't exist""" 1263 components = [ 1264 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1265 {"attribute": "department", "filter": None, "sub_attribute": None}, 1266 {"attribute": "name", "filter": None, "sub_attribute": None}, 1267 ] 1268 1269 data_copy = self.sample_data.copy() 1270 # organization doesn't exist initially 1271 1272 self.processor._navigate_and_modify(data_copy, components, "Engineering", "add") 1273 1274 self.assertEqual(data_copy["organization"]["department"]["name"], "Engineering") 1275 1276 def test_navigate_and_modify_intermediate_navigation_existing_path(self): 1277 """Test navigation when part of the path already exists""" 1278 components = [ 1279 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1280 {"attribute": "department", "filter": None, "sub_attribute": None}, 1281 {"attribute": "budget", "filter": None, "sub_attribute": None}, 1282 ] 1283 1284 data_copy = self.sample_data.copy() 1285 data_copy["organization"] = {"department": {"name": "IT", "head": "John"}} 1286 1287 self.processor._navigate_and_modify(data_copy, components, 500000, "add") 1288 1289 self.assertEqual(data_copy["organization"]["department"]["budget"], 500000) 1290 # Existing data should be preserved 1291 self.assertEqual(data_copy["organization"]["department"]["name"], "IT") 1292 self.assertEqual(data_copy["organization"]["department"]["head"], "John")
13class TestSCIMPatchProcessor(APITestCase): 14 15 def setUp(self): 16 """Set up test fixtures""" 17 self.processor = SCIMPatchProcessor() 18 self.sample_data = { 19 "userName": "john.doe", 20 "name": {"givenName": "John", "familyName": "Doe"}, 21 "emails": [ 22 {"value": "john@example.com", "type": "work", "primary": True}, 23 {"value": "john.personal@example.com", "type": "personal"}, 24 ], 25 "active": True, 26 } 27 28 def test_data(self): 29 user_data = { 30 "id": "user123", 31 "userName": "john.doe", 32 "name": {"formatted": "John Doe", "familyName": "Doe", "givenName": "John"}, 33 "emails": [ 34 {"value": "john.doe@example.com", "type": "work", "primary": True}, 35 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 36 ], 37 "phoneNumbers": [ 38 {"value": "+1-555-123-4567", "type": "work", "primary": True}, 39 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 40 ], 41 "addresses": [ 42 { 43 "streetAddress": "123 Work St", 44 "city": "Work City", 45 "type": "work", 46 "primary": True, 47 }, 48 { 49 "streetAddress": "456 Home Ave", 50 "city": "Home City", 51 "type": "home", 52 "primary": False, 53 }, 54 { 55 "streetAddress": "789 Other Rd", 56 "city": "Other City", 57 "type": "work", 58 "primary": False, 59 }, 60 ], 61 } 62 63 # Create processor 64 processor = SCIMPatchProcessor() 65 66 # Example patch operations 67 patches = [ 68 # Replace primary phone number 69 PatchOperation( 70 op=PatchOp.replace, 71 path="phoneNumbers[primary eq true].value", 72 value="+1-555-999-0000", 73 ), 74 # Add new email 75 PatchOperation( 76 op=PatchOp.add, 77 path="emails", 78 value={"value": "john.new@example.com", "type": "home", "primary": False}, 79 ), 80 # Update user's given name 81 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 82 # Remove work email 83 PatchOperation(op=PatchOp.remove, path='emails[type eq "work"]'), 84 # Add with empty path, simple object 85 PatchOperation(op=PatchOp.add, path=None, value={"foo": "bar"}), 86 # Empty path with complex object 87 PatchOperation(op=PatchOp.add, path=None, value={"name.formatted": "formatted"}), 88 ] 89 result = processor.apply_patches(user_data, patches) 90 self.assertEqual( 91 result, 92 { 93 "id": "user123", 94 "userName": "john.doe", 95 "name": {"formatted": "formatted", "familyName": "Doe", "givenName": "Johnny"}, 96 "emails": [ 97 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 98 {"value": "john.new@example.com", "type": "home", "primary": False}, 99 ], 100 "phoneNumbers": [ 101 {"value": "+1-555-999-0000", "type": "work", "primary": True}, 102 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 103 ], 104 "addresses": [ 105 { 106 "streetAddress": "123 Work St", 107 "city": "Work City", 108 "type": "work", 109 "primary": True, 110 }, 111 { 112 "streetAddress": "456 Home Ave", 113 "city": "Home City", 114 "type": "home", 115 "primary": False, 116 }, 117 { 118 "streetAddress": "789 Other Rd", 119 "city": "Other City", 120 "type": "work", 121 "primary": False, 122 }, 123 ], 124 "foo": "bar", 125 }, 126 ) 127 128 def test_parse(self): 129 test_paths = [ 130 { 131 "filter": "userName", 132 "components": [{"attribute": "userName", "filter": None, "sub_attribute": None}], 133 }, 134 { 135 "filter": "name.givenName", 136 "components": [{"attribute": "name", "filter": None, "sub_attribute": "givenName"}], 137 }, 138 { 139 "filter": "emails[primary eq true].value", 140 "components": [ 141 { 142 "attribute": "emails", 143 "filter": { 144 "type": "comparison", 145 "attribute": "primary", 146 "operator": "eq", 147 "value": True, 148 }, 149 "sub_attribute": "value", 150 } 151 ], 152 }, 153 { 154 "filter": 'phoneNumbers[type eq "work"].value', 155 "components": [ 156 { 157 "attribute": "phoneNumbers", 158 "filter": { 159 "type": "comparison", 160 "attribute": "type", 161 "operator": "eq", 162 "value": "work", 163 }, 164 "sub_attribute": "value", 165 } 166 ], 167 }, 168 { 169 "filter": 'addresses[type eq "work" and primary eq true].streetAddress', 170 "components": [ 171 { 172 "attribute": "addresses", 173 "filter": { 174 "type": "logical", 175 "operator": "and", 176 "left": { 177 "type": "comparison", 178 "attribute": "type", 179 "operator": "eq", 180 "value": "work", 181 }, 182 "right": { 183 "type": "comparison", 184 "attribute": "primary", 185 "operator": "eq", 186 "value": True, 187 }, 188 }, 189 "sub_attribute": "streetAddress", 190 } 191 ], 192 }, 193 { 194 "filter": f"{SCIM_URN_USER_ENTERPRISE}:manager", 195 "components": [ 196 { 197 "attribute": SCIM_URN_USER_ENTERPRISE, 198 "filter": None, 199 "sub_attribute": "manager", 200 } 201 ], 202 }, 203 ] 204 205 for path in test_paths: 206 with self.subTest(path=path["filter"]): 207 parser = SCIMPathParser() 208 components = parser.parse_path(path["filter"]) 209 self.assertEqual(components, path["components"]) 210 211 def test_init(self): 212 """Test processor initialization""" 213 processor = SCIMPatchProcessor() 214 self.assertIsNotNone(processor.parser) 215 216 def test_apply_patches_empty_list(self): 217 """Test applying empty patch list returns unchanged data""" 218 result = self.processor.apply_patches(self.sample_data, []) 219 self.assertEqual(result, self.sample_data) 220 # Ensure original data is not modified 221 self.assertIsNot(result, self.sample_data) 222 223 def test_apply_patches_does_not_mutate_input(self): 224 """apply_patches must not mutate its input 225 226 The SCIM source views decide whether to persist a patched resource by 227 comparing the patched data against the stored attributes, so a nested 228 mutation that leaks into the input dict is silently dropped: the 229 comparison can no longer see a difference. The input must stay 230 untouched (regression test for #26896). 231 """ 232 data = { 233 "userName": "john.doe", 234 "name": {"givenName": "John", "familyName": "Doe"}, 235 "emails": [ 236 {"value": "john@example.com", "type": "work", "primary": True}, 237 ], 238 } 239 before = deepcopy(data) 240 patches = [ 241 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 242 PatchOperation( 243 op=PatchOp.replace, 244 path='emails[type eq "work"].value', 245 value="john.new@example.com", 246 ), 247 PatchOperation(op=PatchOp.remove, path="name.familyName"), 248 ] 249 250 result = self.processor.apply_patches(data, patches) 251 252 # the patched data carries the new values 253 self.assertEqual(result["name"]["givenName"], "Johnny") 254 self.assertNotIn("familyName", result["name"]) 255 self.assertEqual(result["emails"][0]["value"], "john.new@example.com") 256 # the input dict is not modified, and the change remains visible to the caller 257 self.assertEqual(data, before) 258 self.assertNotEqual(result, data) 259 260 def test_apply_patches_with_validation(self): 261 """Test that patches are validated using PatchOperation.model_validate""" 262 with patch("authentik.sources.scim.patch.processor.PatchOperation") as mock_patch_op: 263 mock_patch_op.model_validate.return_value = Mock( 264 path="userName", op=PatchOp.replace, value="jane.doe" 265 ) 266 267 patches = [{"op": "replace", "path": "userName", "value": "jane.doe"}] 268 self.processor.apply_patches(self.sample_data, patches) 269 270 mock_patch_op.model_validate.assert_called_once() 271 272 # Test ADD operations 273 def test_apply_add_simple_attribute(self): 274 """Test adding a simple attribute""" 275 with patch.object(self.processor.parser, "parse_path") as mock_parse: 276 mock_parse.return_value = [ 277 {"attribute": "title", "filter": None, "sub_attribute": None} 278 ] 279 280 patches = [PatchOperation(op=PatchOp.add, path="title", value="Manager")] 281 result = self.processor.apply_patches(self.sample_data, patches) 282 283 self.assertEqual(result["title"], "Manager") 284 285 def test_apply_add_sub_attribute(self): 286 """Test adding a sub-attribute""" 287 with patch.object(self.processor.parser, "parse_path") as mock_parse: 288 mock_parse.return_value = [ 289 {"attribute": "name", "filter": None, "sub_attribute": "middleName"} 290 ] 291 292 patches = [PatchOperation(op=PatchOp.add, path="name.middleName", value="William")] 293 result = self.processor.apply_patches(self.sample_data, patches) 294 295 self.assertEqual(result["name"]["middleName"], "William") 296 297 def test_apply_add_sub_attribute_new_parent(self): 298 """Test adding a sub-attribute when parent doesn't exist""" 299 with patch.object(self.processor.parser, "parse_path") as mock_parse: 300 mock_parse.return_value = [ 301 {"attribute": "address", "filter": None, "sub_attribute": "street"} 302 ] 303 304 patches = [PatchOperation(op=PatchOp.add, path="address.street", value="123 Main St")] 305 result = self.processor.apply_patches(self.sample_data, patches) 306 307 self.assertEqual(result["address"]["street"], "123 Main St") 308 309 def test_apply_add_enterprise_manager(self): 310 """Test adding enterprise manager attribute (special case)""" 311 with patch.object(self.processor.parser, "parse_path") as mock_parse: 312 mock_parse.return_value = [ 313 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 314 ] 315 316 patches = [ 317 PatchOperation( 318 op=PatchOp.add, path=f"{SCIM_URN_USER_ENTERPRISE}.manager", value="mgr123" 319 ) 320 ] 321 result = self.processor.apply_patches(self.sample_data, patches) 322 323 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "mgr123"}) 324 325 def test_apply_add_to_existing_array(self): 326 """Test adding to an existing array attribute""" 327 with patch.object(self.processor.parser, "parse_path") as mock_parse: 328 mock_parse.return_value = [ 329 {"attribute": "emails", "filter": None, "sub_attribute": None} 330 ] 331 332 new_email = {"value": "john.work@example.com", "type": "work"} 333 patches = [PatchOperation(op=PatchOp.add, path="emails", value=new_email)] 334 result = self.processor.apply_patches(self.sample_data, patches) 335 336 self.assertEqual(len(result["emails"]), 3) 337 self.assertIn(new_email, result["emails"]) 338 339 def test_apply_add_new_attribute_as_value(self): 340 """Test adding a new attribute that gets set as value (not array)""" 341 with patch.object(self.processor.parser, "parse_path") as mock_parse: 342 mock_parse.return_value = [ 343 {"attribute": "department", "filter": None, "sub_attribute": None} 344 ] 345 346 patches = [PatchOperation(op=PatchOp.add, path="department", value="Engineering")] 347 result = self.processor.apply_patches(self.sample_data, patches) 348 349 self.assertEqual(result["department"], "Engineering") 350 351 def test_apply_add_complex_path(self): 352 """Test adding with complex path (filters)""" 353 with patch.object(self.processor.parser, "parse_path") as mock_parse: 354 mock_parse.return_value = [ 355 { 356 "attribute": "emails", 357 "filter": {"type": "comparison"}, 358 "sub_attribute": "verified", 359 } 360 ] 361 362 patches = [ 363 PatchOperation(op=PatchOp.add, path='emails[type eq "work"].verified', value=True) 364 ] 365 366 with patch.object(self.processor, "_navigate_and_modify") as mock_navigate: 367 self.processor.apply_patches(self.sample_data, patches) 368 mock_navigate.assert_called_once() 369 370 # Test REMOVE operations 371 def test_apply_remove_simple_attribute(self): 372 """Test removing a simple attribute""" 373 with patch.object(self.processor.parser, "parse_path") as mock_parse: 374 mock_parse.return_value = [ 375 {"attribute": "active", "filter": None, "sub_attribute": None} 376 ] 377 378 patches = [PatchOperation(op=PatchOp.remove, path="active")] 379 result = self.processor.apply_patches(self.sample_data, patches) 380 381 self.assertNotIn("active", result) 382 383 def test_apply_remove_sub_attribute(self): 384 """Test removing a sub-attribute""" 385 with patch.object(self.processor.parser, "parse_path") as mock_parse: 386 mock_parse.return_value = [ 387 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 388 ] 389 390 patches = [PatchOperation(op=PatchOp.remove, path="name.givenName")] 391 result = self.processor.apply_patches(self.sample_data, patches) 392 393 self.assertNotIn("givenName", result["name"]) 394 self.assertIn("familyName", result["name"]) # Other sub-attributes remain 395 396 def test_apply_remove_sub_attribute_nonexistent_parent(self): 397 """Test removing sub-attribute when parent doesn't exist""" 398 with patch.object(self.processor.parser, "parse_path") as mock_parse: 399 mock_parse.return_value = [ 400 {"attribute": "nonexistent", "filter": None, "sub_attribute": "field"} 401 ] 402 403 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent.field")] 404 result = self.processor.apply_patches(self.sample_data, patches) 405 406 # Should not raise error and data should be unchanged 407 self.assertEqual(result, self.sample_data) 408 409 def test_apply_remove_nonexistent_attribute(self): 410 """Test removing a non-existent attribute (should not raise error)""" 411 with patch.object(self.processor.parser, "parse_path") as mock_parse: 412 mock_parse.return_value = [ 413 {"attribute": "nonexistent", "filter": None, "sub_attribute": None} 414 ] 415 416 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent")] 417 result = self.processor.apply_patches(self.sample_data, patches) 418 419 # Should not raise error and data should be unchanged 420 self.assertEqual(result, self.sample_data) 421 422 # Test REPLACE operations 423 def test_apply_replace_simple_attribute(self): 424 """Test replacing a simple attribute""" 425 with patch.object(self.processor.parser, "parse_path") as mock_parse: 426 mock_parse.return_value = [ 427 {"attribute": "userName", "filter": None, "sub_attribute": None} 428 ] 429 430 patches = [PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe")] 431 result = self.processor.apply_patches(self.sample_data, patches) 432 433 self.assertEqual(result["userName"], "jane.doe") 434 435 def test_apply_replace_sub_attribute(self): 436 """Test replacing a sub-attribute""" 437 with patch.object(self.processor.parser, "parse_path") as mock_parse: 438 mock_parse.return_value = [ 439 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 440 ] 441 442 patches = [PatchOperation(op=PatchOp.replace, path="name.givenName", value="Jane")] 443 result = self.processor.apply_patches(self.sample_data, patches) 444 445 self.assertEqual(result["name"]["givenName"], "Jane") 446 447 def test_apply_replace_sub_attribute_new_parent(self): 448 """Test replacing sub-attribute when parent doesn't exist""" 449 with patch.object(self.processor.parser, "parse_path") as mock_parse: 450 mock_parse.return_value = [ 451 {"attribute": "address", "filter": None, "sub_attribute": "city"} 452 ] 453 454 patches = [PatchOperation(op=PatchOp.replace, path="address.city", value="New York")] 455 result = self.processor.apply_patches(self.sample_data, patches) 456 457 self.assertEqual(result["address"]["city"], "New York") 458 459 def test_apply_replace_enterprise_manager(self): 460 """Test replacing enterprise manager attribute (special case)""" 461 with patch.object(self.processor.parser, "parse_path") as mock_parse: 462 mock_parse.return_value = [ 463 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 464 ] 465 466 patches = [ 467 PatchOperation( 468 op=PatchOp.replace, 469 path=f"{SCIM_URN_USER_ENTERPRISE}.manager", 470 value="newmgr456", 471 ) 472 ] 473 result = self.processor.apply_patches(self.sample_data, patches) 474 475 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "newmgr456"}) 476 477 # Test bulk operations (path is None) 478 def test_apply_bulk_add_operation(self): 479 """Test bulk add operation when path is None""" 480 patches = [ 481 PatchOperation( 482 op=PatchOp.add, path=None, value={"title": "Manager", "department": "IT"} 483 ) 484 ] 485 486 with patch.object(self.processor, "_apply_add") as mock_add: 487 self.processor.apply_patches(self.sample_data, patches) 488 self.assertEqual(mock_add.call_count, 2) 489 490 def test_apply_bulk_remove_operation(self): 491 """Test bulk remove operation when path is None""" 492 patches = [ 493 PatchOperation(op=PatchOp.remove, path=None, value={"active": None, "userName": None}) 494 ] 495 496 with patch.object(self.processor, "_apply_remove") as mock_remove: 497 self.processor.apply_patches(self.sample_data, patches) 498 self.assertEqual(mock_remove.call_count, 2) 499 500 def test_apply_bulk_replace_operation(self): 501 """Test bulk replace operation when path is None""" 502 patches = [ 503 PatchOperation( 504 op=PatchOp.replace, path=None, value={"userName": "jane.doe", "active": False} 505 ) 506 ] 507 508 with patch.object(self.processor, "_apply_replace") as mock_replace: 509 self.processor.apply_patches(self.sample_data, patches) 510 self.assertEqual(mock_replace.call_count, 2) 511 512 def test_apply_bulk_operation_invalid_value(self): 513 """Test bulk operation with non-dict value (should be ignored)""" 514 patches = [PatchOperation(op=PatchOp.add, path=None, value="invalid")] 515 result = self.processor.apply_patches(self.sample_data, patches) 516 517 self.assertEqual(result, self.sample_data) 518 519 # Test _navigate_and_modify method 520 def test_navigate_and_modify_with_filter_add_new_item(self): 521 """Test navigating with filter and adding new item""" 522 components = [ 523 { 524 "attribute": "emails", 525 "filter": { 526 "type": "comparison", 527 "attribute": "type", 528 "operator": "eq", 529 "value": "home", 530 }, 531 "sub_attribute": None, 532 } 533 ] 534 535 new_email = {"value": "home@example.com", "type": "home"} 536 data_copy = self.sample_data.copy() 537 data_copy["emails"] = self.sample_data["emails"].copy() 538 539 self.processor._navigate_and_modify(data_copy, components, new_email, "add") 540 541 # Should add new email with type "home" 542 home_emails = [email for email in data_copy["emails"] if email.get("type") == "home"] 543 self.assertEqual(len(home_emails), 1) 544 545 def test_navigate_and_modify_with_filter_modify_existing(self): 546 """Test navigating with filter and modifying existing item""" 547 components = [ 548 { 549 "attribute": "emails", 550 "filter": { 551 "type": "comparison", 552 "attribute": "type", 553 "operator": "eq", 554 "value": "work", 555 }, 556 "sub_attribute": "verified", 557 } 558 ] 559 560 data_copy = self.sample_data.copy() 561 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 562 563 self.processor._navigate_and_modify(data_copy, components, True, "add") 564 565 # Should add verified field to work email 566 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 567 self.assertTrue(work_email["verified"]) 568 569 def test_navigate_and_modify_remove_item(self): 570 """Test removing entire item with filter""" 571 components = [ 572 { 573 "attribute": "emails", 574 "filter": { 575 "type": "comparison", 576 "attribute": "type", 577 "operator": "eq", 578 "value": "personal", 579 }, 580 "sub_attribute": None, 581 } 582 ] 583 584 data_copy = self.sample_data.copy() 585 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 586 original_count = len(data_copy["emails"]) 587 588 self.processor._navigate_and_modify(data_copy, components, None, "remove") 589 590 # Should remove personal email 591 self.assertEqual(len(data_copy["emails"]), original_count - 1) 592 personal_emails = [ 593 email for email in data_copy["emails"] if email.get("type") == "personal" 594 ] 595 self.assertEqual(len(personal_emails), 0) 596 597 def test_navigate_and_modify_nonexistent_attribute_add(self): 598 """Test navigating to non-existent attribute for add operation""" 599 components = [ 600 { 601 "attribute": "phones", 602 "filter": { 603 "type": "comparison", 604 "attribute": "type", 605 "operator": "eq", 606 "value": "mobile", 607 }, 608 "sub_attribute": None, 609 } 610 ] 611 612 data_copy = self.sample_data.copy() 613 self.processor._navigate_and_modify( 614 data_copy, components, {"value": "123-456-7890", "type": "mobile"}, "add" 615 ) 616 617 # Should create new phones array 618 self.assertIn("phones", data_copy) 619 self.assertEqual(len(data_copy["phones"]), 1) 620 621 def test_navigate_and_modify_nonexistent_attribute_remove(self): 622 """Test navigating to non-existent attribute for remove operation""" 623 components = [ 624 { 625 "attribute": "phones", 626 "filter": { 627 "type": "comparison", 628 "attribute": "type", 629 "operator": "eq", 630 "value": "mobile", 631 }, 632 "sub_attribute": None, 633 } 634 ] 635 636 data_copy = self.sample_data.copy() 637 self.processor._navigate_and_modify(data_copy, components, None, "remove") 638 639 # Should not create attribute or raise error 640 self.assertNotIn("phones", data_copy) 641 642 # Test filter matching methods 643 def test_matches_filter_no_filter(self): 644 """Test matching with no filter (should return True)""" 645 item = {"type": "work"} 646 result = self.processor._matches_filter(item, None) 647 self.assertTrue(result) 648 649 def test_matches_filter_empty_filter(self): 650 """Test matching with empty filter (should return True)""" 651 item = {"type": "work"} 652 result = self.processor._matches_filter(item, {}) 653 self.assertTrue(result) 654 655 def test_matches_filter_unknown_type(self): 656 """Test matching with unknown filter type""" 657 item = {"type": "work"} 658 filter_expr = {"type": "unknown"} 659 result = self.processor._matches_filter(item, filter_expr) 660 self.assertFalse(result) 661 662 def test_matches_comparison_eq(self): 663 """Test comparison filter with eq operator""" 664 item = {"type": "work", "primary": True} 665 filter_expr = {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"} 666 667 result = self.processor._matches_comparison(item, filter_expr) 668 self.assertTrue(result) 669 670 def test_matches_comparison_eq_false(self): 671 """Test comparison filter with eq operator (false case)""" 672 item = {"type": "work"} 673 filter_expr = { 674 "type": "comparison", 675 "attribute": "type", 676 "operator": "eq", 677 "value": "personal", 678 } 679 680 result = self.processor._matches_comparison(item, filter_expr) 681 self.assertFalse(result) 682 683 def test_matches_comparison_ne(self): 684 """Test comparison filter with ne operator""" 685 item = {"type": "work"} 686 filter_expr = { 687 "type": "comparison", 688 "attribute": "type", 689 "operator": "ne", 690 "value": "personal", 691 } 692 693 result = self.processor._matches_comparison(item, filter_expr) 694 self.assertTrue(result) 695 696 def test_matches_comparison_co(self): 697 """Test comparison filter with co (contains) operator""" 698 item = {"value": "john@example.com"} 699 filter_expr = { 700 "type": "comparison", 701 "attribute": "value", 702 "operator": "co", 703 "value": "example", 704 } 705 706 result = self.processor._matches_comparison(item, filter_expr) 707 self.assertTrue(result) 708 709 def test_matches_comparison_sw(self): 710 """Test comparison filter with sw (starts with) operator""" 711 item = {"value": "john@example.com"} 712 filter_expr = { 713 "type": "comparison", 714 "attribute": "value", 715 "operator": "sw", 716 "value": "john", 717 } 718 719 result = self.processor._matches_comparison(item, filter_expr) 720 self.assertTrue(result) 721 722 def test_matches_comparison_ew(self): 723 """Test comparison filter with ew (ends with) operator""" 724 item = {"value": "john@example.com"} 725 filter_expr = { 726 "type": "comparison", 727 "attribute": "value", 728 "operator": "ew", 729 "value": ".com", 730 } 731 732 result = self.processor._matches_comparison(item, filter_expr) 733 self.assertTrue(result) 734 735 def test_matches_comparison_gt(self): 736 """Test comparison filter with gt (greater than) operator""" 737 item = {"priority": 10} 738 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "gt", "value": 5} 739 740 result = self.processor._matches_comparison(item, filter_expr) 741 self.assertTrue(result) 742 743 def test_matches_comparison_lt(self): 744 """Test comparison filter with lt (less than) operator""" 745 item = {"priority": 3} 746 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "lt", "value": 5} 747 748 result = self.processor._matches_comparison(item, filter_expr) 749 self.assertTrue(result) 750 751 def test_matches_comparison_ge(self): 752 """Test comparison filter with ge (greater than or equal) operator""" 753 item = {"priority": 5} 754 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "ge", "value": 5} 755 756 result = self.processor._matches_comparison(item, filter_expr) 757 self.assertTrue(result) 758 759 def test_matches_comparison_le(self): 760 """Test comparison filter with le (less than or equal) operator""" 761 item = {"priority": 5} 762 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "le", "value": 5} 763 764 result = self.processor._matches_comparison(item, filter_expr) 765 self.assertTrue(result) 766 767 def test_matches_comparison_pr(self): 768 """Test comparison filter with pr (present) operator""" 769 item = {"value": "john@example.com"} 770 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 771 772 result = self.processor._matches_comparison(item, filter_expr) 773 self.assertTrue(result) 774 775 def test_matches_comparison_pr_false(self): 776 """Test comparison filter with pr operator (false case)""" 777 item = {"value": None} 778 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 779 780 result = self.processor._matches_comparison(item, filter_expr) 781 self.assertFalse(result) 782 783 def test_matches_comparison_missing_attribute(self): 784 """Test comparison filter with missing attribute""" 785 item = {"type": "work"} 786 filter_expr = { 787 "type": "comparison", 788 "attribute": "missing", 789 "operator": "eq", 790 "value": "test", 791 } 792 793 result = self.processor._matches_comparison(item, filter_expr) 794 self.assertFalse(result) 795 796 def test_matches_comparison_unknown_operator(self): 797 """Test comparison filter with unknown operator""" 798 item = {"type": "work"} 799 filter_expr = { 800 "type": "comparison", 801 "attribute": "type", 802 "operator": "unknown", 803 "value": "work", 804 } 805 806 result = self.processor._matches_comparison(item, filter_expr) 807 self.assertFalse(result) 808 809 def test_matches_logical_and_true(self): 810 """Test logical AND filter (true case)""" 811 item = {"type": "work", "primary": True} 812 filter_expr = { 813 "type": "logical", 814 "operator": "and", 815 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 816 "right": { 817 "type": "comparison", 818 "attribute": "primary", 819 "operator": "eq", 820 "value": True, 821 }, 822 } 823 824 result = self.processor._matches_logical(item, filter_expr) 825 self.assertTrue(result) 826 827 def test_matches_logical_and_false(self): 828 """Test logical AND filter (false case)""" 829 item = {"type": "work", "primary": False} 830 filter_expr = { 831 "type": "logical", 832 "operator": "and", 833 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 834 "right": { 835 "type": "comparison", 836 "attribute": "primary", 837 "operator": "eq", 838 "value": True, 839 }, 840 } 841 842 result = self.processor._matches_logical(item, filter_expr) 843 self.assertFalse(result) 844 845 def test_matches_logical_or_true(self): 846 """Test logical OR filter (true case)""" 847 item = {"type": "personal", "primary": True} 848 filter_expr = { 849 "type": "logical", 850 "operator": "or", 851 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 852 "right": { 853 "type": "comparison", 854 "attribute": "primary", 855 "operator": "eq", 856 "value": True, 857 }, 858 } 859 860 result = self.processor._matches_logical(item, filter_expr) 861 self.assertTrue(result) 862 863 def test_matches_logical_or_false(self): 864 """Test logical OR filter (false case)""" 865 item = {"type": "personal", "primary": False} 866 filter_expr = { 867 "type": "logical", 868 "operator": "or", 869 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 870 "right": { 871 "type": "comparison", 872 "attribute": "primary", 873 "operator": "eq", 874 "value": True, 875 }, 876 } 877 878 result = self.processor._matches_logical(item, filter_expr) 879 self.assertFalse(result) 880 881 def test_matches_logical_not_true(self): 882 """Test logical NOT filter (true case)""" 883 item = {"type": "personal"} 884 filter_expr = { 885 "type": "logical", 886 "operator": "not", 887 "operand": { 888 "type": "comparison", 889 "attribute": "type", 890 "operator": "eq", 891 "value": "work", 892 }, 893 } 894 895 result = self.processor._matches_logical(item, filter_expr) 896 self.assertTrue(result) 897 898 def test_matches_logical_not_false(self): 899 """Test logical NOT filter (false case)""" 900 item = {"type": "work"} 901 filter_expr = { 902 "type": "logical", 903 "operator": "not", 904 "operand": { 905 "type": "comparison", 906 "attribute": "type", 907 "operator": "eq", 908 "value": "work", 909 }, 910 } 911 912 result = self.processor._matches_logical(item, filter_expr) 913 self.assertFalse(result) 914 915 def test_matches_logical_unknown_operator(self): 916 """Test logical filter with unknown operator""" 917 item = {"type": "work"} 918 filter_expr = { 919 "type": "logical", 920 "operator": "unknown", 921 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 922 } 923 924 result = self.processor._matches_logical(item, filter_expr) 925 self.assertFalse(result) 926 927 def test_multiple_patches_applied_sequentially(self): 928 """Test that multiple patches are applied in sequence""" 929 patches = [ 930 PatchOperation(op=PatchOp.add, path="title", value="Manager"), 931 PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe"), 932 PatchOperation(op=PatchOp.remove, path="active"), 933 ] 934 935 with patch.object(self.processor.parser, "parse_path") as mock_parse: 936 mock_parse.side_effect = [ 937 [{"attribute": "title", "filter": None, "sub_attribute": None}], 938 [{"attribute": "userName", "filter": None, "sub_attribute": None}], 939 [{"attribute": "active", "filter": None, "sub_attribute": None}], 940 ] 941 942 result = self.processor.apply_patches(self.sample_data, patches) 943 944 self.assertEqual(result["title"], "Manager") 945 self.assertEqual(result["userName"], "jane.doe") 946 self.assertNotIn("active", result) 947 948 def test_navigate_and_modify_simple_attribute_last_component_add(self): 949 """Test navigating to simple attribute as last component with add operation""" 950 components = [ 951 {"attribute": "profile", "filter": None, "sub_attribute": None}, 952 {"attribute": "title", "filter": None, "sub_attribute": None}, 953 ] 954 955 data_copy = self.sample_data.copy() 956 data_copy["profile"] = {} 957 958 self.processor._navigate_and_modify(data_copy, components, "Senior Manager", "add") 959 960 self.assertEqual(data_copy["profile"]["title"], "Senior Manager") 961 962 def test_navigate_and_modify_simple_attribute_last_component_replace(self): 963 """Test navigating to simple attribute as last component with replace operation""" 964 components = [ 965 {"attribute": "profile", "filter": None, "sub_attribute": None}, 966 {"attribute": "title", "filter": None, "sub_attribute": None}, 967 ] 968 969 data_copy = self.sample_data.copy() 970 data_copy["profile"] = {"title": "Manager"} 971 972 self.processor._navigate_and_modify(data_copy, components, "Director", "replace") 973 974 self.assertEqual(data_copy["profile"]["title"], "Director") 975 976 def test_navigate_and_modify_simple_attribute_last_component_remove(self): 977 """Test navigating to simple attribute as last component with remove operation""" 978 components = [ 979 {"attribute": "profile", "filter": None, "sub_attribute": None}, 980 {"attribute": "title", "filter": None, "sub_attribute": None}, 981 ] 982 983 data_copy = self.sample_data.copy() 984 data_copy["profile"] = {"title": "Manager", "department": "IT"} 985 986 self.processor._navigate_and_modify(data_copy, components, None, "remove") 987 988 self.assertNotIn("title", data_copy["profile"]) 989 self.assertIn("department", data_copy["profile"]) # Other attributes remain 990 991 def test_navigate_and_modify_sub_attribute_last_component_add(self): 992 """Test navigating to sub-attribute as last component with add operation""" 993 components = [ 994 {"attribute": "profile", "filter": None, "sub_attribute": None}, 995 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 996 ] 997 998 data_copy = self.sample_data.copy() 999 data_copy["profile"] = {"address": {}} 1000 1001 self.processor._navigate_and_modify(data_copy, components, "123 Main St", "add") 1002 1003 self.assertEqual(data_copy["profile"]["address"]["street"], "123 Main St") 1004 1005 def test_navigate_and_modify_sub_attribute_last_component_replace(self): 1006 """Test navigating to sub-attribute as last component with replace operation""" 1007 components = [ 1008 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1009 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1010 ] 1011 1012 data_copy = self.sample_data.copy() 1013 data_copy["profile"] = {"address": {"street": "456 Oak Ave"}} 1014 1015 self.processor._navigate_and_modify(data_copy, components, "789 Pine Rd", "replace") 1016 1017 self.assertEqual(data_copy["profile"]["address"]["street"], "789 Pine Rd") 1018 1019 def test_navigate_and_modify_sub_attribute_last_component_remove(self): 1020 """Test navigating to sub-attribute as last component with remove operation""" 1021 components = [ 1022 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1023 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1024 ] 1025 1026 data_copy = self.sample_data.copy() 1027 data_copy["profile"] = {"address": {"street": "123 Main St", "city": "New York"}} 1028 1029 self.processor._navigate_and_modify(data_copy, components, None, "remove") 1030 1031 self.assertNotIn("street", data_copy["profile"]["address"]) 1032 self.assertIn("city", data_copy["profile"]["address"]) # Other sub-attributes remain 1033 1034 def test_navigate_and_modify_sub_attribute_parent_not_exists(self): 1035 """Test navigating to sub-attribute when parent attribute doesn't exist""" 1036 components = [ 1037 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1038 {"attribute": "address", "filter": None, "sub_attribute": "street"}, 1039 ] 1040 1041 data_copy = self.sample_data.copy() 1042 data_copy["profile"] = {} # address doesn't exist yet 1043 1044 self.processor._navigate_and_modify(data_copy, components, "123 Main St", "add") 1045 1046 self.assertEqual(data_copy["profile"]["address"]["street"], "123 Main St") 1047 1048 def test_navigate_and_modify_deeper_navigation(self): 1049 """Test navigating deeper through multiple levels without filters""" 1050 components = [ 1051 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1052 {"attribute": "department", "filter": None, "sub_attribute": None}, 1053 {"attribute": "team", "filter": None, "sub_attribute": None}, 1054 {"attribute": "name", "filter": None, "sub_attribute": None}, 1055 ] 1056 1057 data_copy = self.sample_data.copy() 1058 1059 self.processor._navigate_and_modify(data_copy, components, "Engineering Team Alpha", "add") 1060 1061 self.assertEqual( 1062 data_copy["organization"]["department"]["team"]["name"], "Engineering Team Alpha" 1063 ) 1064 1065 def test_navigate_and_modify_deeper_navigation_partial_path_exists(self): 1066 """Test navigating deeper when part of the path already exists""" 1067 components = [ 1068 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1069 {"attribute": "department", "filter": None, "sub_attribute": None}, 1070 {"attribute": "budget", "filter": None, "sub_attribute": None}, 1071 ] 1072 1073 data_copy = self.sample_data.copy() 1074 data_copy["organization"] = {"department": {"name": "IT"}} 1075 1076 self.processor._navigate_and_modify(data_copy, components, 100000, "add") 1077 1078 self.assertEqual(data_copy["organization"]["department"]["budget"], 100000) 1079 self.assertEqual( 1080 data_copy["organization"]["department"]["name"], "IT" 1081 ) # Existing data preserved 1082 1083 def test_navigate_and_modify_array_not_list_type(self): 1084 """Test navigation when expected array attribute is not a list""" 1085 components = [ 1086 { 1087 "attribute": "emails", 1088 "filter": { 1089 "type": "comparison", 1090 "attribute": "type", 1091 "operator": "eq", 1092 "value": "work", 1093 }, 1094 "sub_attribute": "verified", 1095 } 1096 ] 1097 1098 data_copy = self.sample_data.copy() 1099 data_copy["emails"] = "not_a_list" # Invalid type 1100 1101 # Should return early without error 1102 self.processor._navigate_and_modify(data_copy, components, True, "add") 1103 1104 # Data should remain unchanged 1105 self.assertEqual(data_copy["emails"], "not_a_list") 1106 1107 def test_navigate_and_modify_update_matching_item_with_dict_value(self): 1108 """Test updating matching item with dictionary value""" 1109 components = [ 1110 { 1111 "attribute": "emails", 1112 "filter": { 1113 "type": "comparison", 1114 "attribute": "type", 1115 "operator": "eq", 1116 "value": "work", 1117 }, 1118 "sub_attribute": None, 1119 } 1120 ] 1121 1122 data_copy = self.sample_data.copy() 1123 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1124 1125 update_data = {"verified": True, "lastChecked": "2023-01-01"} 1126 self.processor._navigate_and_modify(data_copy, components, update_data, "add") 1127 1128 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1129 self.assertTrue(work_email["verified"]) 1130 self.assertEqual(work_email["lastChecked"], "2023-01-01") 1131 # Original fields should still exist 1132 self.assertEqual(work_email["value"], "john@example.com") 1133 1134 def test_navigate_and_modify_update_matching_item_with_non_dict_value(self): 1135 """Test updating matching item with non-dictionary value (should be ignored)""" 1136 components = [ 1137 { 1138 "attribute": "emails", 1139 "filter": { 1140 "type": "comparison", 1141 "attribute": "type", 1142 "operator": "eq", 1143 "value": "work", 1144 }, 1145 "sub_attribute": None, 1146 } 1147 ] 1148 1149 data_copy = self.sample_data.copy() 1150 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1151 original_work_email = next( 1152 email for email in data_copy["emails"] if email.get("type") == "work" 1153 ).copy() 1154 1155 # Try to update with non-dict value 1156 self.processor._navigate_and_modify(data_copy, components, "string_value", "add") 1157 1158 # Email should remain unchanged 1159 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1160 self.assertEqual(work_email, original_work_email) 1161 1162 def test_navigate_and_modify_remove_entire_matching_item(self): 1163 """Test removing entire matching item from array""" 1164 components = [ 1165 { 1166 "attribute": "emails", 1167 "filter": { 1168 "type": "comparison", 1169 "attribute": "type", 1170 "operator": "eq", 1171 "value": "personal", 1172 }, 1173 "sub_attribute": None, 1174 } 1175 ] 1176 1177 data_copy = self.sample_data.copy() 1178 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1179 original_count = len(data_copy["emails"]) 1180 1181 self.processor._navigate_and_modify(data_copy, components, None, "remove") 1182 1183 # Should remove the personal email 1184 self.assertEqual(len(data_copy["emails"]), original_count - 1) 1185 personal_emails = [ 1186 email for email in data_copy["emails"] if email.get("type") == "personal" 1187 ] 1188 self.assertEqual(len(personal_emails), 0) 1189 1190 # Work email should still exist 1191 work_emails = [email for email in data_copy["emails"] if email.get("type") == "work"] 1192 self.assertEqual(len(work_emails), 1) 1193 1194 def test_navigate_and_modify_mixed_filters_and_simple_navigation(self): 1195 """Test navigation with mix of filtered and simple components""" 1196 # This test actually reveals a limitation in the current implementation 1197 # The _navigate_and_modify method doesn't properly handle navigation 1198 # after a filtered component. Let's test what actually happens. 1199 components = [ 1200 { 1201 "attribute": "emails", 1202 "filter": { 1203 "type": "comparison", 1204 "attribute": "type", 1205 "operator": "eq", 1206 "value": "work", 1207 }, 1208 "sub_attribute": "verified", # Changed to test sub_attribute on filtered item 1209 } 1210 ] 1211 1212 data_copy = self.sample_data.copy() 1213 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1214 1215 self.processor._navigate_and_modify(data_copy, components, True, "add") 1216 1217 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1218 self.assertTrue(work_email["verified"]) 1219 1220 def test_navigate_and_modify_simple_navigation_multiple_levels(self): 1221 """Test simple navigation through multiple levels without filters""" 1222 components = [ 1223 {"attribute": "profile", "filter": None, "sub_attribute": None}, 1224 {"attribute": "settings", "filter": None, "sub_attribute": None}, 1225 {"attribute": "notifications", "filter": None, "sub_attribute": "email"}, 1226 ] 1227 1228 data_copy = self.sample_data.copy() 1229 1230 self.processor._navigate_and_modify(data_copy, components, True, "add") 1231 1232 self.assertTrue(data_copy["profile"]["settings"]["notifications"]["email"]) 1233 1234 def test_navigate_and_modify_filter_then_simple_attribute_workaround(self): 1235 """Test the actual behavior when we have filter followed by simple navigation""" 1236 # Based on the code, after processing a filter, the method doesn't continue 1237 # to navigate deeper. This test documents the current behavior. 1238 components = [ 1239 { 1240 "attribute": "emails", 1241 "filter": { 1242 "type": "comparison", 1243 "attribute": "type", 1244 "operator": "eq", 1245 "value": "work", 1246 }, 1247 "sub_attribute": None, 1248 } 1249 ] 1250 1251 data_copy = self.sample_data.copy() 1252 data_copy["emails"] = [email.copy() for email in self.sample_data["emails"]] 1253 1254 # Update the work email with a dict containing nested data 1255 update_data = {"metadata": {"verified": True, "source": "manual"}} 1256 self.processor._navigate_and_modify(data_copy, components, update_data, "add") 1257 1258 work_email = next(email for email in data_copy["emails"] if email.get("type") == "work") 1259 self.assertTrue(work_email["metadata"]["verified"]) 1260 self.assertEqual(work_email["metadata"]["source"], "manual") 1261 1262 def test_navigate_and_modify_intermediate_navigation_missing_parent(self): 1263 """Test navigation when intermediate parent doesn't exist""" 1264 components = [ 1265 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1266 {"attribute": "department", "filter": None, "sub_attribute": None}, 1267 {"attribute": "name", "filter": None, "sub_attribute": None}, 1268 ] 1269 1270 data_copy = self.sample_data.copy() 1271 # organization doesn't exist initially 1272 1273 self.processor._navigate_and_modify(data_copy, components, "Engineering", "add") 1274 1275 self.assertEqual(data_copy["organization"]["department"]["name"], "Engineering") 1276 1277 def test_navigate_and_modify_intermediate_navigation_existing_path(self): 1278 """Test navigation when part of the path already exists""" 1279 components = [ 1280 {"attribute": "organization", "filter": None, "sub_attribute": None}, 1281 {"attribute": "department", "filter": None, "sub_attribute": None}, 1282 {"attribute": "budget", "filter": None, "sub_attribute": None}, 1283 ] 1284 1285 data_copy = self.sample_data.copy() 1286 data_copy["organization"] = {"department": {"name": "IT", "head": "John"}} 1287 1288 self.processor._navigate_and_modify(data_copy, components, 500000, "add") 1289 1290 self.assertEqual(data_copy["organization"]["department"]["budget"], 500000) 1291 # Existing data should be preserved 1292 self.assertEqual(data_copy["organization"]["department"]["name"], "IT") 1293 self.assertEqual(data_copy["organization"]["department"]["head"], "John")
Similar to TransactionTestCase, but use transaction.atomic() to achieve
test isolation.
In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).
On database backends with no transaction support, TestCase behaves as TransactionTestCase.
15 def setUp(self): 16 """Set up test fixtures""" 17 self.processor = SCIMPatchProcessor() 18 self.sample_data = { 19 "userName": "john.doe", 20 "name": {"givenName": "John", "familyName": "Doe"}, 21 "emails": [ 22 {"value": "john@example.com", "type": "work", "primary": True}, 23 {"value": "john.personal@example.com", "type": "personal"}, 24 ], 25 "active": True, 26 }
Set up test fixtures
28 def test_data(self): 29 user_data = { 30 "id": "user123", 31 "userName": "john.doe", 32 "name": {"formatted": "John Doe", "familyName": "Doe", "givenName": "John"}, 33 "emails": [ 34 {"value": "john.doe@example.com", "type": "work", "primary": True}, 35 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 36 ], 37 "phoneNumbers": [ 38 {"value": "+1-555-123-4567", "type": "work", "primary": True}, 39 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 40 ], 41 "addresses": [ 42 { 43 "streetAddress": "123 Work St", 44 "city": "Work City", 45 "type": "work", 46 "primary": True, 47 }, 48 { 49 "streetAddress": "456 Home Ave", 50 "city": "Home City", 51 "type": "home", 52 "primary": False, 53 }, 54 { 55 "streetAddress": "789 Other Rd", 56 "city": "Other City", 57 "type": "work", 58 "primary": False, 59 }, 60 ], 61 } 62 63 # Create processor 64 processor = SCIMPatchProcessor() 65 66 # Example patch operations 67 patches = [ 68 # Replace primary phone number 69 PatchOperation( 70 op=PatchOp.replace, 71 path="phoneNumbers[primary eq true].value", 72 value="+1-555-999-0000", 73 ), 74 # Add new email 75 PatchOperation( 76 op=PatchOp.add, 77 path="emails", 78 value={"value": "john.new@example.com", "type": "home", "primary": False}, 79 ), 80 # Update user's given name 81 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 82 # Remove work email 83 PatchOperation(op=PatchOp.remove, path='emails[type eq "work"]'), 84 # Add with empty path, simple object 85 PatchOperation(op=PatchOp.add, path=None, value={"foo": "bar"}), 86 # Empty path with complex object 87 PatchOperation(op=PatchOp.add, path=None, value={"name.formatted": "formatted"}), 88 ] 89 result = processor.apply_patches(user_data, patches) 90 self.assertEqual( 91 result, 92 { 93 "id": "user123", 94 "userName": "john.doe", 95 "name": {"formatted": "formatted", "familyName": "Doe", "givenName": "Johnny"}, 96 "emails": [ 97 {"value": "john.personal@example.com", "type": "personal", "primary": False}, 98 {"value": "john.new@example.com", "type": "home", "primary": False}, 99 ], 100 "phoneNumbers": [ 101 {"value": "+1-555-999-0000", "type": "work", "primary": True}, 102 {"value": "+1-555-987-6543", "type": "mobile", "primary": False}, 103 ], 104 "addresses": [ 105 { 106 "streetAddress": "123 Work St", 107 "city": "Work City", 108 "type": "work", 109 "primary": True, 110 }, 111 { 112 "streetAddress": "456 Home Ave", 113 "city": "Home City", 114 "type": "home", 115 "primary": False, 116 }, 117 { 118 "streetAddress": "789 Other Rd", 119 "city": "Other City", 120 "type": "work", 121 "primary": False, 122 }, 123 ], 124 "foo": "bar", 125 }, 126 )
128 def test_parse(self): 129 test_paths = [ 130 { 131 "filter": "userName", 132 "components": [{"attribute": "userName", "filter": None, "sub_attribute": None}], 133 }, 134 { 135 "filter": "name.givenName", 136 "components": [{"attribute": "name", "filter": None, "sub_attribute": "givenName"}], 137 }, 138 { 139 "filter": "emails[primary eq true].value", 140 "components": [ 141 { 142 "attribute": "emails", 143 "filter": { 144 "type": "comparison", 145 "attribute": "primary", 146 "operator": "eq", 147 "value": True, 148 }, 149 "sub_attribute": "value", 150 } 151 ], 152 }, 153 { 154 "filter": 'phoneNumbers[type eq "work"].value', 155 "components": [ 156 { 157 "attribute": "phoneNumbers", 158 "filter": { 159 "type": "comparison", 160 "attribute": "type", 161 "operator": "eq", 162 "value": "work", 163 }, 164 "sub_attribute": "value", 165 } 166 ], 167 }, 168 { 169 "filter": 'addresses[type eq "work" and primary eq true].streetAddress', 170 "components": [ 171 { 172 "attribute": "addresses", 173 "filter": { 174 "type": "logical", 175 "operator": "and", 176 "left": { 177 "type": "comparison", 178 "attribute": "type", 179 "operator": "eq", 180 "value": "work", 181 }, 182 "right": { 183 "type": "comparison", 184 "attribute": "primary", 185 "operator": "eq", 186 "value": True, 187 }, 188 }, 189 "sub_attribute": "streetAddress", 190 } 191 ], 192 }, 193 { 194 "filter": f"{SCIM_URN_USER_ENTERPRISE}:manager", 195 "components": [ 196 { 197 "attribute": SCIM_URN_USER_ENTERPRISE, 198 "filter": None, 199 "sub_attribute": "manager", 200 } 201 ], 202 }, 203 ] 204 205 for path in test_paths: 206 with self.subTest(path=path["filter"]): 207 parser = SCIMPathParser() 208 components = parser.parse_path(path["filter"]) 209 self.assertEqual(components, path["components"])
211 def test_init(self): 212 """Test processor initialization""" 213 processor = SCIMPatchProcessor() 214 self.assertIsNotNone(processor.parser)
Test processor initialization
216 def test_apply_patches_empty_list(self): 217 """Test applying empty patch list returns unchanged data""" 218 result = self.processor.apply_patches(self.sample_data, []) 219 self.assertEqual(result, self.sample_data) 220 # Ensure original data is not modified 221 self.assertIsNot(result, self.sample_data)
Test applying empty patch list returns unchanged data
223 def test_apply_patches_does_not_mutate_input(self): 224 """apply_patches must not mutate its input 225 226 The SCIM source views decide whether to persist a patched resource by 227 comparing the patched data against the stored attributes, so a nested 228 mutation that leaks into the input dict is silently dropped: the 229 comparison can no longer see a difference. The input must stay 230 untouched (regression test for #26896). 231 """ 232 data = { 233 "userName": "john.doe", 234 "name": {"givenName": "John", "familyName": "Doe"}, 235 "emails": [ 236 {"value": "john@example.com", "type": "work", "primary": True}, 237 ], 238 } 239 before = deepcopy(data) 240 patches = [ 241 PatchOperation(op=PatchOp.replace, path="name.givenName", value="Johnny"), 242 PatchOperation( 243 op=PatchOp.replace, 244 path='emails[type eq "work"].value', 245 value="john.new@example.com", 246 ), 247 PatchOperation(op=PatchOp.remove, path="name.familyName"), 248 ] 249 250 result = self.processor.apply_patches(data, patches) 251 252 # the patched data carries the new values 253 self.assertEqual(result["name"]["givenName"], "Johnny") 254 self.assertNotIn("familyName", result["name"]) 255 self.assertEqual(result["emails"][0]["value"], "john.new@example.com") 256 # the input dict is not modified, and the change remains visible to the caller 257 self.assertEqual(data, before) 258 self.assertNotEqual(result, data)
apply_patches must not mutate its input
The SCIM source views decide whether to persist a patched resource by comparing the patched data against the stored attributes, so a nested mutation that leaks into the input dict is silently dropped: the comparison can no longer see a difference. The input must stay untouched (regression test for #26896).
260 def test_apply_patches_with_validation(self): 261 """Test that patches are validated using PatchOperation.model_validate""" 262 with patch("authentik.sources.scim.patch.processor.PatchOperation") as mock_patch_op: 263 mock_patch_op.model_validate.return_value = Mock( 264 path="userName", op=PatchOp.replace, value="jane.doe" 265 ) 266 267 patches = [{"op": "replace", "path": "userName", "value": "jane.doe"}] 268 self.processor.apply_patches(self.sample_data, patches) 269 270 mock_patch_op.model_validate.assert_called_once()
Test that patches are validated using PatchOperation.model_validate
273 def test_apply_add_simple_attribute(self): 274 """Test adding a simple attribute""" 275 with patch.object(self.processor.parser, "parse_path") as mock_parse: 276 mock_parse.return_value = [ 277 {"attribute": "title", "filter": None, "sub_attribute": None} 278 ] 279 280 patches = [PatchOperation(op=PatchOp.add, path="title", value="Manager")] 281 result = self.processor.apply_patches(self.sample_data, patches) 282 283 self.assertEqual(result["title"], "Manager")
Test adding a simple attribute
285 def test_apply_add_sub_attribute(self): 286 """Test adding a sub-attribute""" 287 with patch.object(self.processor.parser, "parse_path") as mock_parse: 288 mock_parse.return_value = [ 289 {"attribute": "name", "filter": None, "sub_attribute": "middleName"} 290 ] 291 292 patches = [PatchOperation(op=PatchOp.add, path="name.middleName", value="William")] 293 result = self.processor.apply_patches(self.sample_data, patches) 294 295 self.assertEqual(result["name"]["middleName"], "William")
Test adding a sub-attribute
297 def test_apply_add_sub_attribute_new_parent(self): 298 """Test adding a sub-attribute when parent doesn't exist""" 299 with patch.object(self.processor.parser, "parse_path") as mock_parse: 300 mock_parse.return_value = [ 301 {"attribute": "address", "filter": None, "sub_attribute": "street"} 302 ] 303 304 patches = [PatchOperation(op=PatchOp.add, path="address.street", value="123 Main St")] 305 result = self.processor.apply_patches(self.sample_data, patches) 306 307 self.assertEqual(result["address"]["street"], "123 Main St")
Test adding a sub-attribute when parent doesn't exist
309 def test_apply_add_enterprise_manager(self): 310 """Test adding enterprise manager attribute (special case)""" 311 with patch.object(self.processor.parser, "parse_path") as mock_parse: 312 mock_parse.return_value = [ 313 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 314 ] 315 316 patches = [ 317 PatchOperation( 318 op=PatchOp.add, path=f"{SCIM_URN_USER_ENTERPRISE}.manager", value="mgr123" 319 ) 320 ] 321 result = self.processor.apply_patches(self.sample_data, patches) 322 323 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "mgr123"})
Test adding enterprise manager attribute (special case)
325 def test_apply_add_to_existing_array(self): 326 """Test adding to an existing array attribute""" 327 with patch.object(self.processor.parser, "parse_path") as mock_parse: 328 mock_parse.return_value = [ 329 {"attribute": "emails", "filter": None, "sub_attribute": None} 330 ] 331 332 new_email = {"value": "john.work@example.com", "type": "work"} 333 patches = [PatchOperation(op=PatchOp.add, path="emails", value=new_email)] 334 result = self.processor.apply_patches(self.sample_data, patches) 335 336 self.assertEqual(len(result["emails"]), 3) 337 self.assertIn(new_email, result["emails"])
Test adding to an existing array attribute
339 def test_apply_add_new_attribute_as_value(self): 340 """Test adding a new attribute that gets set as value (not array)""" 341 with patch.object(self.processor.parser, "parse_path") as mock_parse: 342 mock_parse.return_value = [ 343 {"attribute": "department", "filter": None, "sub_attribute": None} 344 ] 345 346 patches = [PatchOperation(op=PatchOp.add, path="department", value="Engineering")] 347 result = self.processor.apply_patches(self.sample_data, patches) 348 349 self.assertEqual(result["department"], "Engineering")
Test adding a new attribute that gets set as value (not array)
351 def test_apply_add_complex_path(self): 352 """Test adding with complex path (filters)""" 353 with patch.object(self.processor.parser, "parse_path") as mock_parse: 354 mock_parse.return_value = [ 355 { 356 "attribute": "emails", 357 "filter": {"type": "comparison"}, 358 "sub_attribute": "verified", 359 } 360 ] 361 362 patches = [ 363 PatchOperation(op=PatchOp.add, path='emails[type eq "work"].verified', value=True) 364 ] 365 366 with patch.object(self.processor, "_navigate_and_modify") as mock_navigate: 367 self.processor.apply_patches(self.sample_data, patches) 368 mock_navigate.assert_called_once()
Test adding with complex path (filters)
371 def test_apply_remove_simple_attribute(self): 372 """Test removing a simple attribute""" 373 with patch.object(self.processor.parser, "parse_path") as mock_parse: 374 mock_parse.return_value = [ 375 {"attribute": "active", "filter": None, "sub_attribute": None} 376 ] 377 378 patches = [PatchOperation(op=PatchOp.remove, path="active")] 379 result = self.processor.apply_patches(self.sample_data, patches) 380 381 self.assertNotIn("active", result)
Test removing a simple attribute
383 def test_apply_remove_sub_attribute(self): 384 """Test removing a sub-attribute""" 385 with patch.object(self.processor.parser, "parse_path") as mock_parse: 386 mock_parse.return_value = [ 387 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 388 ] 389 390 patches = [PatchOperation(op=PatchOp.remove, path="name.givenName")] 391 result = self.processor.apply_patches(self.sample_data, patches) 392 393 self.assertNotIn("givenName", result["name"]) 394 self.assertIn("familyName", result["name"]) # Other sub-attributes remain
Test removing a sub-attribute
396 def test_apply_remove_sub_attribute_nonexistent_parent(self): 397 """Test removing sub-attribute when parent doesn't exist""" 398 with patch.object(self.processor.parser, "parse_path") as mock_parse: 399 mock_parse.return_value = [ 400 {"attribute": "nonexistent", "filter": None, "sub_attribute": "field"} 401 ] 402 403 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent.field")] 404 result = self.processor.apply_patches(self.sample_data, patches) 405 406 # Should not raise error and data should be unchanged 407 self.assertEqual(result, self.sample_data)
Test removing sub-attribute when parent doesn't exist
409 def test_apply_remove_nonexistent_attribute(self): 410 """Test removing a non-existent attribute (should not raise error)""" 411 with patch.object(self.processor.parser, "parse_path") as mock_parse: 412 mock_parse.return_value = [ 413 {"attribute": "nonexistent", "filter": None, "sub_attribute": None} 414 ] 415 416 patches = [PatchOperation(op=PatchOp.remove, path="nonexistent")] 417 result = self.processor.apply_patches(self.sample_data, patches) 418 419 # Should not raise error and data should be unchanged 420 self.assertEqual(result, self.sample_data)
Test removing a non-existent attribute (should not raise error)
423 def test_apply_replace_simple_attribute(self): 424 """Test replacing a simple attribute""" 425 with patch.object(self.processor.parser, "parse_path") as mock_parse: 426 mock_parse.return_value = [ 427 {"attribute": "userName", "filter": None, "sub_attribute": None} 428 ] 429 430 patches = [PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe")] 431 result = self.processor.apply_patches(self.sample_data, patches) 432 433 self.assertEqual(result["userName"], "jane.doe")
Test replacing a simple attribute
435 def test_apply_replace_sub_attribute(self): 436 """Test replacing a sub-attribute""" 437 with patch.object(self.processor.parser, "parse_path") as mock_parse: 438 mock_parse.return_value = [ 439 {"attribute": "name", "filter": None, "sub_attribute": "givenName"} 440 ] 441 442 patches = [PatchOperation(op=PatchOp.replace, path="name.givenName", value="Jane")] 443 result = self.processor.apply_patches(self.sample_data, patches) 444 445 self.assertEqual(result["name"]["givenName"], "Jane")
Test replacing a sub-attribute
447 def test_apply_replace_sub_attribute_new_parent(self): 448 """Test replacing sub-attribute when parent doesn't exist""" 449 with patch.object(self.processor.parser, "parse_path") as mock_parse: 450 mock_parse.return_value = [ 451 {"attribute": "address", "filter": None, "sub_attribute": "city"} 452 ] 453 454 patches = [PatchOperation(op=PatchOp.replace, path="address.city", value="New York")] 455 result = self.processor.apply_patches(self.sample_data, patches) 456 457 self.assertEqual(result["address"]["city"], "New York")
Test replacing sub-attribute when parent doesn't exist
459 def test_apply_replace_enterprise_manager(self): 460 """Test replacing enterprise manager attribute (special case)""" 461 with patch.object(self.processor.parser, "parse_path") as mock_parse: 462 mock_parse.return_value = [ 463 {"attribute": SCIM_URN_USER_ENTERPRISE, "filter": None, "sub_attribute": "manager"} 464 ] 465 466 patches = [ 467 PatchOperation( 468 op=PatchOp.replace, 469 path=f"{SCIM_URN_USER_ENTERPRISE}.manager", 470 value="newmgr456", 471 ) 472 ] 473 result = self.processor.apply_patches(self.sample_data, patches) 474 475 self.assertEqual(result[SCIM_URN_USER_ENTERPRISE]["manager"], {"value": "newmgr456"})
Test replacing enterprise manager attribute (special case)
478 def test_apply_bulk_add_operation(self): 479 """Test bulk add operation when path is None""" 480 patches = [ 481 PatchOperation( 482 op=PatchOp.add, path=None, value={"title": "Manager", "department": "IT"} 483 ) 484 ] 485 486 with patch.object(self.processor, "_apply_add") as mock_add: 487 self.processor.apply_patches(self.sample_data, patches) 488 self.assertEqual(mock_add.call_count, 2)
Test bulk add operation when path is None
490 def test_apply_bulk_remove_operation(self): 491 """Test bulk remove operation when path is None""" 492 patches = [ 493 PatchOperation(op=PatchOp.remove, path=None, value={"active": None, "userName": None}) 494 ] 495 496 with patch.object(self.processor, "_apply_remove") as mock_remove: 497 self.processor.apply_patches(self.sample_data, patches) 498 self.assertEqual(mock_remove.call_count, 2)
Test bulk remove operation when path is None
500 def test_apply_bulk_replace_operation(self): 501 """Test bulk replace operation when path is None""" 502 patches = [ 503 PatchOperation( 504 op=PatchOp.replace, path=None, value={"userName": "jane.doe", "active": False} 505 ) 506 ] 507 508 with patch.object(self.processor, "_apply_replace") as mock_replace: 509 self.processor.apply_patches(self.sample_data, patches) 510 self.assertEqual(mock_replace.call_count, 2)
Test bulk replace operation when path is None
512 def test_apply_bulk_operation_invalid_value(self): 513 """Test bulk operation with non-dict value (should be ignored)""" 514 patches = [PatchOperation(op=PatchOp.add, path=None, value="invalid")] 515 result = self.processor.apply_patches(self.sample_data, patches) 516 517 self.assertEqual(result, self.sample_data)
Test bulk operation with non-dict value (should be ignored)
643 def test_matches_filter_no_filter(self): 644 """Test matching with no filter (should return True)""" 645 item = {"type": "work"} 646 result = self.processor._matches_filter(item, None) 647 self.assertTrue(result)
Test matching with no filter (should return True)
649 def test_matches_filter_empty_filter(self): 650 """Test matching with empty filter (should return True)""" 651 item = {"type": "work"} 652 result = self.processor._matches_filter(item, {}) 653 self.assertTrue(result)
Test matching with empty filter (should return True)
655 def test_matches_filter_unknown_type(self): 656 """Test matching with unknown filter type""" 657 item = {"type": "work"} 658 filter_expr = {"type": "unknown"} 659 result = self.processor._matches_filter(item, filter_expr) 660 self.assertFalse(result)
Test matching with unknown filter type
662 def test_matches_comparison_eq(self): 663 """Test comparison filter with eq operator""" 664 item = {"type": "work", "primary": True} 665 filter_expr = {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"} 666 667 result = self.processor._matches_comparison(item, filter_expr) 668 self.assertTrue(result)
Test comparison filter with eq operator
670 def test_matches_comparison_eq_false(self): 671 """Test comparison filter with eq operator (false case)""" 672 item = {"type": "work"} 673 filter_expr = { 674 "type": "comparison", 675 "attribute": "type", 676 "operator": "eq", 677 "value": "personal", 678 } 679 680 result = self.processor._matches_comparison(item, filter_expr) 681 self.assertFalse(result)
Test comparison filter with eq operator (false case)
683 def test_matches_comparison_ne(self): 684 """Test comparison filter with ne operator""" 685 item = {"type": "work"} 686 filter_expr = { 687 "type": "comparison", 688 "attribute": "type", 689 "operator": "ne", 690 "value": "personal", 691 } 692 693 result = self.processor._matches_comparison(item, filter_expr) 694 self.assertTrue(result)
Test comparison filter with ne operator
696 def test_matches_comparison_co(self): 697 """Test comparison filter with co (contains) operator""" 698 item = {"value": "john@example.com"} 699 filter_expr = { 700 "type": "comparison", 701 "attribute": "value", 702 "operator": "co", 703 "value": "example", 704 } 705 706 result = self.processor._matches_comparison(item, filter_expr) 707 self.assertTrue(result)
Test comparison filter with co (contains) operator
709 def test_matches_comparison_sw(self): 710 """Test comparison filter with sw (starts with) operator""" 711 item = {"value": "john@example.com"} 712 filter_expr = { 713 "type": "comparison", 714 "attribute": "value", 715 "operator": "sw", 716 "value": "john", 717 } 718 719 result = self.processor._matches_comparison(item, filter_expr) 720 self.assertTrue(result)
Test comparison filter with sw (starts with) operator
722 def test_matches_comparison_ew(self): 723 """Test comparison filter with ew (ends with) operator""" 724 item = {"value": "john@example.com"} 725 filter_expr = { 726 "type": "comparison", 727 "attribute": "value", 728 "operator": "ew", 729 "value": ".com", 730 } 731 732 result = self.processor._matches_comparison(item, filter_expr) 733 self.assertTrue(result)
Test comparison filter with ew (ends with) operator
735 def test_matches_comparison_gt(self): 736 """Test comparison filter with gt (greater than) operator""" 737 item = {"priority": 10} 738 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "gt", "value": 5} 739 740 result = self.processor._matches_comparison(item, filter_expr) 741 self.assertTrue(result)
Test comparison filter with gt (greater than) operator
743 def test_matches_comparison_lt(self): 744 """Test comparison filter with lt (less than) operator""" 745 item = {"priority": 3} 746 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "lt", "value": 5} 747 748 result = self.processor._matches_comparison(item, filter_expr) 749 self.assertTrue(result)
Test comparison filter with lt (less than) operator
751 def test_matches_comparison_ge(self): 752 """Test comparison filter with ge (greater than or equal) operator""" 753 item = {"priority": 5} 754 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "ge", "value": 5} 755 756 result = self.processor._matches_comparison(item, filter_expr) 757 self.assertTrue(result)
Test comparison filter with ge (greater than or equal) operator
759 def test_matches_comparison_le(self): 760 """Test comparison filter with le (less than or equal) operator""" 761 item = {"priority": 5} 762 filter_expr = {"type": "comparison", "attribute": "priority", "operator": "le", "value": 5} 763 764 result = self.processor._matches_comparison(item, filter_expr) 765 self.assertTrue(result)
Test comparison filter with le (less than or equal) operator
767 def test_matches_comparison_pr(self): 768 """Test comparison filter with pr (present) operator""" 769 item = {"value": "john@example.com"} 770 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 771 772 result = self.processor._matches_comparison(item, filter_expr) 773 self.assertTrue(result)
Test comparison filter with pr (present) operator
775 def test_matches_comparison_pr_false(self): 776 """Test comparison filter with pr operator (false case)""" 777 item = {"value": None} 778 filter_expr = {"type": "comparison", "attribute": "value", "operator": "pr", "value": None} 779 780 result = self.processor._matches_comparison(item, filter_expr) 781 self.assertFalse(result)
Test comparison filter with pr operator (false case)
783 def test_matches_comparison_missing_attribute(self): 784 """Test comparison filter with missing attribute""" 785 item = {"type": "work"} 786 filter_expr = { 787 "type": "comparison", 788 "attribute": "missing", 789 "operator": "eq", 790 "value": "test", 791 } 792 793 result = self.processor._matches_comparison(item, filter_expr) 794 self.assertFalse(result)
Test comparison filter with missing attribute
796 def test_matches_comparison_unknown_operator(self): 797 """Test comparison filter with unknown operator""" 798 item = {"type": "work"} 799 filter_expr = { 800 "type": "comparison", 801 "attribute": "type", 802 "operator": "unknown", 803 "value": "work", 804 } 805 806 result = self.processor._matches_comparison(item, filter_expr) 807 self.assertFalse(result)
Test comparison filter with unknown operator
809 def test_matches_logical_and_true(self): 810 """Test logical AND filter (true case)""" 811 item = {"type": "work", "primary": True} 812 filter_expr = { 813 "type": "logical", 814 "operator": "and", 815 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 816 "right": { 817 "type": "comparison", 818 "attribute": "primary", 819 "operator": "eq", 820 "value": True, 821 }, 822 } 823 824 result = self.processor._matches_logical(item, filter_expr) 825 self.assertTrue(result)
Test logical AND filter (true case)
827 def test_matches_logical_and_false(self): 828 """Test logical AND filter (false case)""" 829 item = {"type": "work", "primary": False} 830 filter_expr = { 831 "type": "logical", 832 "operator": "and", 833 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 834 "right": { 835 "type": "comparison", 836 "attribute": "primary", 837 "operator": "eq", 838 "value": True, 839 }, 840 } 841 842 result = self.processor._matches_logical(item, filter_expr) 843 self.assertFalse(result)
Test logical AND filter (false case)
845 def test_matches_logical_or_true(self): 846 """Test logical OR filter (true case)""" 847 item = {"type": "personal", "primary": True} 848 filter_expr = { 849 "type": "logical", 850 "operator": "or", 851 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 852 "right": { 853 "type": "comparison", 854 "attribute": "primary", 855 "operator": "eq", 856 "value": True, 857 }, 858 } 859 860 result = self.processor._matches_logical(item, filter_expr) 861 self.assertTrue(result)
Test logical OR filter (true case)
863 def test_matches_logical_or_false(self): 864 """Test logical OR filter (false case)""" 865 item = {"type": "personal", "primary": False} 866 filter_expr = { 867 "type": "logical", 868 "operator": "or", 869 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 870 "right": { 871 "type": "comparison", 872 "attribute": "primary", 873 "operator": "eq", 874 "value": True, 875 }, 876 } 877 878 result = self.processor._matches_logical(item, filter_expr) 879 self.assertFalse(result)
Test logical OR filter (false case)
881 def test_matches_logical_not_true(self): 882 """Test logical NOT filter (true case)""" 883 item = {"type": "personal"} 884 filter_expr = { 885 "type": "logical", 886 "operator": "not", 887 "operand": { 888 "type": "comparison", 889 "attribute": "type", 890 "operator": "eq", 891 "value": "work", 892 }, 893 } 894 895 result = self.processor._matches_logical(item, filter_expr) 896 self.assertTrue(result)
Test logical NOT filter (true case)
898 def test_matches_logical_not_false(self): 899 """Test logical NOT filter (false case)""" 900 item = {"type": "work"} 901 filter_expr = { 902 "type": "logical", 903 "operator": "not", 904 "operand": { 905 "type": "comparison", 906 "attribute": "type", 907 "operator": "eq", 908 "value": "work", 909 }, 910 } 911 912 result = self.processor._matches_logical(item, filter_expr) 913 self.assertFalse(result)
Test logical NOT filter (false case)
915 def test_matches_logical_unknown_operator(self): 916 """Test logical filter with unknown operator""" 917 item = {"type": "work"} 918 filter_expr = { 919 "type": "logical", 920 "operator": "unknown", 921 "left": {"type": "comparison", "attribute": "type", "operator": "eq", "value": "work"}, 922 } 923 924 result = self.processor._matches_logical(item, filter_expr) 925 self.assertFalse(result)
Test logical filter with unknown operator
927 def test_multiple_patches_applied_sequentially(self): 928 """Test that multiple patches are applied in sequence""" 929 patches = [ 930 PatchOperation(op=PatchOp.add, path="title", value="Manager"), 931 PatchOperation(op=PatchOp.replace, path="userName", value="jane.doe"), 932 PatchOperation(op=PatchOp.remove, path="active"), 933 ] 934 935 with patch.object(self.processor.parser, "parse_path") as mock_parse: 936 mock_parse.side_effect = [ 937 [{"attribute": "title", "filter": None, "sub_attribute": None}], 938 [{"attribute": "userName", "filter": None, "sub_attribute": None}], 939 [{"attribute": "active", "filter": None, "sub_attribute": None}], 940 ] 941 942 result = self.processor.apply_patches(self.sample_data, patches) 943 944 self.assertEqual(result["title"], "Manager") 945 self.assertEqual(result["userName"], "jane.doe") 946 self.assertNotIn("active", result)
Test that multiple patches are applied in sequence