Skip to content

Commit f2787ee

Browse files
J-Buazmeuk
andauthored
fix: correctly set values for Reference objects in parent object (#37)
`value` and `ref` values in `MultiValuedComplexAttribute` are coherent (the id in `value` is extracted from `ref`) Co-authored-by: Éloi Rivard <eloi@yaal.coop>
1 parent 753ec05 commit f2787ee

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
Fixed
88
^^^^^
99
- Sort results in get_all_available_tags, so it can be used with pytest-xdist.
10+
- `generate_random_value` generate coherent `ref` and `value` values for complex attributes. :pr:`30` :pr:`37`
1011

1112
[0.2.0] - 2025-08-12
1213
--------------------

scim2_tester/filling.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from scim2_models import ComplexAttribute
1212
from scim2_models import Extension
1313
from scim2_models import ExternalReference
14+
from scim2_models import MultiValuedComplexAttribute
1415
from scim2_models import Mutability
1516
from scim2_models import Reference
1617
from scim2_models import Resource
@@ -123,6 +124,9 @@ def generate_random_value(
123124
elif isclass(field_type) and issubclass(field_type, Enum):
124125
value = random.choice(list(field_type))
125126

127+
elif isclass(field_type) and issubclass(field_type, MultiValuedComplexAttribute):
128+
value = fill_mvca_with_random_values(context, field_type()) # type: ignore[arg-type]
129+
126130
elif isclass(field_type) and issubclass(field_type, ComplexAttribute):
127131
value = fill_with_random_values(context, field_type()) # type: ignore[arg-type]
128132

@@ -172,3 +176,24 @@ def fill_with_random_values(
172176
set_value_by_urn(obj, urn, value)
173177

174178
return obj
179+
180+
181+
def fill_mvca_with_random_values(
182+
context: "CheckContext",
183+
obj: MultiValuedComplexAttribute,
184+
) -> Resource[Any] | None:
185+
"""Fill a MultiValuedComplexAttribute with random values.
186+
187+
For SCIM reference fields, correctly sets the value field to match
188+
the ID extracted from the reference URL.
189+
"""
190+
fill_with_random_values(context, obj)
191+
ref_type = type(obj).get_field_root_type("ref")
192+
if (
193+
get_origin(ref_type) is Reference
194+
and get_args(ref_type)
195+
and get_args(ref_type)[0] not in (URIReference, ExternalReference, Any)
196+
and (ref := getattr(obj, "ref", None))
197+
):
198+
obj.value = ref.rsplit("/", 1)[-1]
199+
return obj

tests/test_filling.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ def test_model_resolution_from_reference_type(testing_context):
3333
assert result != Group
3434

3535

36+
def test_generate_random_value_for_reference(testing_context, httpserver):
37+
"""Validates random value generation for reference fields."""
38+
group = Group()
39+
40+
# Mock HTTP response for user creation
41+
user_data = {
42+
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
43+
"id": "test-user-id",
44+
"userName": "test-user",
45+
"meta": {
46+
"resourceType": "User",
47+
"location": f"http://localhost:{httpserver.port}/Users/test-user-id",
48+
},
49+
}
50+
httpserver.expect_request("/Users", method="POST").respond_with_json(
51+
user_data, status=201
52+
)
53+
54+
result = fill_with_random_values(testing_context, group)
55+
56+
assert len(result.members) == 1
57+
assert result.members[0].value == result.members[0].ref.rsplit("/", 1)[-1]
58+
59+
3660
def test_fill_with_empty_field_list(testing_context):
3761
"""Confirms no fields are modified when empty field list provided."""
3862
user = User(user_name="test")

0 commit comments

Comments
 (0)