diff --git a/doc/changelog.rst b/doc/changelog.rst index 87492e7..b3d745b 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,12 @@ Changelog ========= +[0.2.12] - Unreleased +--------------------- +Added +^^^^^ +- Implement :meth:`Attribute.get_attribute `. + [0.2.11] - 2024-12-08 --------------------- Added diff --git a/scim2_models/rfc7643/schema.py b/scim2_models/rfc7643/schema.py index 891a70e..2390f5e 100644 --- a/scim2_models/rfc7643/schema.py +++ b/scim2_models/rfc7643/schema.py @@ -238,6 +238,13 @@ def to_python(self) -> Optional[tuple[Any, Field]]: return annotation, field + def get_attribute(self, attribute_name: str) -> Optional["Attribute"]: + """Find an attribute by its name.""" + for sub_attribute in self.sub_attributes or []: + if sub_attribute.name == attribute_name: + return sub_attribute + return None + class Schema(Resource): schemas: Annotated[list[str], Required.true] = [ diff --git a/tests/test_schema.py b/tests/test_schema.py index e393bbc..8d2bba9 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -87,7 +87,7 @@ def test_uri_ids(): Schema(id="invalid\nuri") -def test_get_attribute(load_sample): +def test_get_schema_attribute(load_sample): """Test the Schema.get_attribute method.""" payload = load_sample("rfc7643-8.7.1-schema-user.json") schema = Schema.model_validate(payload) @@ -98,3 +98,18 @@ def test_get_attribute(load_sample): schema.get_attribute("userName").mutability = Mutability.read_only assert schema.attributes[0].mutability == Mutability.read_only + + +def test_get_attribute_attribute(load_sample): + """Test the Schema.get_attribute method.""" + payload = load_sample("rfc7643-8.7.1-schema-group.json") + schema = Schema.model_validate(payload) + attribute = schema.get_attribute("members") + + assert attribute.get_attribute("invalid") is None + + assert attribute.sub_attributes[0].name == "value" + assert attribute.sub_attributes[0].mutability == Mutability.immutable + attribute.get_attribute("value").mutability = Mutability.read_only + + assert attribute.sub_attributes[0].mutability == Mutability.read_only