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