Skip to content

Commit

Permalink
feat: implement ResourceType.from_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Nov 30, 2024
1 parent 3ab3833 commit 041a038
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

[0.2.7] - Unreleased
--------------------

Added
^^^^^
- Implement :meth:`ResourceType.from_resource`.

[0.2.6] - 2024-11-29
--------------------

Expand Down
21 changes: 21 additions & 0 deletions scim2_models/rfc7643/resource_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional

from pydantic import Field
from typing_extensions import Self

from ..base import CaseExact
from ..base import ComplexAttribute
Expand Down Expand Up @@ -73,3 +74,23 @@ class ResourceType(Resource):
Optional[list[SchemaExtension]], Mutability.read_only, Required.true
] = None
"""A list of URIs of the resource type's schema extensions."""

@classmethod
def from_resource(cls, resource_model: type[Resource]) -> Self:
"""Build a naive ResourceType from a resource model."""
schema = resource_model.model_fields["schemas"].default[0]
name = schema.split(":")[-1]
extensions = resource_model.__pydantic_generic_metadata__["args"]
return ResourceType(
id=name,
name=name,
description=name,
endpoint=f"/{name}s",
schema_=schema,
schema_extensions=[
SchemaExtension(
schema_=extension.model_fields["schemas"].default[0], required=False
)
for extension in extensions
],
)
26 changes: 26 additions & 0 deletions tests/test_resource_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from scim2_models import EnterpriseUser
from scim2_models import Reference
from scim2_models import ResourceType
from scim2_models import User


def test_user_resource_type(load_sample):
Expand Down Expand Up @@ -35,3 +37,27 @@ def test_group_resource_type(load_sample):
assert obj.meta.resource_type == "ResourceType"

assert obj.model_dump(exclude_unset=True) == payload


def test_from_simple_resource():
user_rt = ResourceType.from_resource(User)
assert user_rt.id == "User"
assert user_rt.name == "User"
assert user_rt.description == "User"
assert user_rt.endpoint == "/Users"
assert user_rt.schema_ == "urn:ietf:params:scim:schemas:core:2.0:User"
assert not user_rt.schema_extensions


def test_from_resource_with_extensions():
enterprise_user_rt = ResourceType.from_resource(User[EnterpriseUser])
assert enterprise_user_rt.id == "User"
assert enterprise_user_rt.name == "User"
assert enterprise_user_rt.description == "User"
assert enterprise_user_rt.endpoint == "/Users"
assert enterprise_user_rt.schema_ == "urn:ietf:params:scim:schemas:core:2.0:User"
assert (
enterprise_user_rt.schema_extensions[0].schema_
== "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
)
assert not enterprise_user_rt.schema_extensions[0].required

0 comments on commit 041a038

Please sign in to comment.