|
| 1 | +"""PATCH remove operation checkers for SCIM compliance testing.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from scim2_client import SCIMClientError |
| 6 | +from scim2_models import Mutability |
| 7 | +from scim2_models import PatchOp |
| 8 | +from scim2_models import PatchOperation |
| 9 | +from scim2_models import Required |
| 10 | +from scim2_models import Resource |
| 11 | + |
| 12 | +from ..urns import get_annotation_by_urn |
| 13 | +from ..urns import get_value_by_urn |
| 14 | +from ..urns import iter_all_urns |
| 15 | +from ..utils import CheckContext |
| 16 | +from ..utils import CheckResult |
| 17 | +from ..utils import Status |
| 18 | +from ..utils import checker |
| 19 | + |
| 20 | + |
| 21 | +@checker("patch:remove") |
| 22 | +def check_remove_attribute( |
| 23 | + context: CheckContext, model: type[Resource[Any]] |
| 24 | +) -> list[CheckResult]: |
| 25 | + """Test PATCH remove operation on all attributes (simple, complex, and extensions). |
| 26 | +
|
| 27 | + Creates a resource with initial values, then iterates over ALL possible URNs |
| 28 | + (base model, extensions, and sub-attributes) to test PATCH remove operations |
| 29 | + systematically. Uses a unified approach that handles all attribute types consistently. |
| 30 | +
|
| 31 | + **Tested Behavior:** |
| 32 | + - Removing attribute values (simple, complex, and extension attributes) |
| 33 | + - Server accepts the PATCH request with correct URN paths for extensions |
| 34 | + - Response contains the resource with removed attributes (null/missing) |
| 35 | +
|
| 36 | + **Status:** |
| 37 | + - :attr:`~scim2_tester.Status.SUCCESS`: Attribute successfully removed |
| 38 | + - :attr:`~scim2_tester.Status.ERROR`: Failed to remove attribute or attribute still exists |
| 39 | + - :attr:`~scim2_tester.Status.SKIPPED`: No removable attributes found |
| 40 | +
|
| 41 | + .. pull-quote:: :rfc:`RFC 7644 Section 3.5.2.2 - Remove Operation <7644#section-3.5.2.2>` |
| 42 | +
|
| 43 | + "The 'remove' operation removes the value at the target location specified |
| 44 | + by the required attribute 'path'. The operation performs the following |
| 45 | + functions, depending on the target location specified by 'path'." |
| 46 | + """ |
| 47 | + results = [] |
| 48 | + all_urns = list( |
| 49 | + iter_all_urns( |
| 50 | + context, |
| 51 | + model, |
| 52 | + required=[Required.false], |
| 53 | + mutability=[Mutability.read_write, Mutability.write_only], |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + if not all_urns: |
| 58 | + return [ |
| 59 | + CheckResult( |
| 60 | + status=Status.SKIPPED, |
| 61 | + reason=f"No removable attributes found for {model.__name__}", |
| 62 | + resource_type=model.__name__, |
| 63 | + ) |
| 64 | + ] |
| 65 | + |
| 66 | + full_resource = context.resource_manager.create_and_register(model, fill_all=True) |
| 67 | + |
| 68 | + for urn, source_model in all_urns: |
| 69 | + initial_value = get_value_by_urn(full_resource, urn) |
| 70 | + if initial_value is None: |
| 71 | + continue |
| 72 | + |
| 73 | + remove_op = PatchOp[type(full_resource)]( |
| 74 | + operations=[ |
| 75 | + PatchOperation( |
| 76 | + op=PatchOperation.Op.remove, |
| 77 | + path=urn, |
| 78 | + ) |
| 79 | + ] |
| 80 | + ) |
| 81 | + |
| 82 | + try: |
| 83 | + updated_resource = context.client.modify( |
| 84 | + resource_model=type(full_resource), |
| 85 | + id=full_resource.id, |
| 86 | + patch_op=remove_op, |
| 87 | + ) |
| 88 | + except SCIMClientError as exc: |
| 89 | + results.append( |
| 90 | + CheckResult( |
| 91 | + status=Status.ERROR, |
| 92 | + reason=f"Failed to remove attribute '{urn}': {exc}", |
| 93 | + resource_type=model.__name__, |
| 94 | + data={ |
| 95 | + "urn": urn, |
| 96 | + "error": exc, |
| 97 | + "initial_value": initial_value, |
| 98 | + }, |
| 99 | + ) |
| 100 | + ) |
| 101 | + continue |
| 102 | + |
| 103 | + actual_value = get_value_by_urn(updated_resource, urn) |
| 104 | + |
| 105 | + if ( |
| 106 | + get_annotation_by_urn(Mutability, urn, source_model) |
| 107 | + == Mutability.write_only |
| 108 | + or actual_value is None |
| 109 | + ): |
| 110 | + results.append( |
| 111 | + CheckResult( |
| 112 | + status=Status.SUCCESS, |
| 113 | + reason=f"Successfully removed attribute '{urn}'", |
| 114 | + resource_type=model.__name__, |
| 115 | + data={ |
| 116 | + "urn": urn, |
| 117 | + "initial_value": initial_value, |
| 118 | + }, |
| 119 | + ) |
| 120 | + ) |
| 121 | + else: |
| 122 | + results.append( |
| 123 | + CheckResult( |
| 124 | + status=Status.ERROR, |
| 125 | + reason=f"Attribute '{urn}' was not removed", |
| 126 | + resource_type=model.__name__, |
| 127 | + data={ |
| 128 | + "urn": urn, |
| 129 | + "initial_value": initial_value, |
| 130 | + "actual_value": actual_value, |
| 131 | + }, |
| 132 | + ) |
| 133 | + ) |
| 134 | + |
| 135 | + return results |
0 commit comments