"""A test suite checking that a storage follows the :class:`~scim2_server.storage.ScimStorage` contract.
Subclass :class:`ScimStorageContract` in the tests of a storage, and give it a
``storage`` fixture returning a new, empty storage::
from scim2_server.testing import ScimStorageContract
class TestMyStorage(ScimStorageContract):
@pytest.fixture
def storage(self):
return MyStorage()
For an :class:`~scim2_server.storage.AsyncScimStorage`, subclass
:class:`AsyncScimStorageContract` and give it an ``async_storage`` fixture
instead.
The suite needs the ``testing`` extra, which installs pytest.
"""
import asyncio
from collections.abc import Coroutine
from collections.abc import Generator
from collections.abc import Iterator
from contextlib import contextmanager
from typing import Annotated
from typing import Any
from typing import TypeVar
from typing import Union
from typing import cast
import pytest
from pydantic import ValidationError
from scim2_models import URI
from scim2_models import URN
from scim2_models import CaseExact
from scim2_models import ComplexAttribute
from scim2_models import Context
from scim2_models import InvalidFilterException
from scim2_models import InvalidPathException
from scim2_models import NotFoundException
from scim2_models import PreconditionFailedException
from scim2_models import Reference
from scim2_models import Resource
from scim2_models import ResourceType
from scim2_models import ResponseParameters
from scim2_models import ScimFilter
from scim2_models import ScimProvider
from scim2_models import SearchRequest
from scim2_models import Uniqueness
from scim2_models import UniquenessException
from scim2_server.storage import AsyncScimStorage
from scim2_server.storage import ScimStorage
from scim2_server.storage import SearchPage
from scim2_server.storage import async_search_page
from scim2_server.storage import projection
from scim2_server.storage import search_page
from scim2_server.utils import load_default_provider
from scim2_server.utils import parametrize
T = TypeVar("T")
ENTERPRISE = "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
SEARCH_USERS: list[dict[str, Any]] = [
{
"userName": "alice",
"displayName": "Alice",
"title": "Boss",
"name": {"givenName": "Alice", "familyName": "Liddell"},
"active": True,
"emails": [
{"value": "alice@home.example", "type": "home"},
{"value": "alice@work.example", "type": "work", "primary": True},
],
ENTERPRISE: {"employeeNumber": "100"},
},
{
"userName": "Bob",
"active": False,
"emails": [{"value": "bob@work.example", "type": "work"}],
},
{
"userName": "carol",
"title": "",
"name": {"familyName": "smith"},
ENTERPRISE: {"employeeNumber": "2"},
},
{
"userName": "dave_%",
"title": "50% off",
"emails": [{"value": "dave@home.example"}],
},
]
"""The users the filter and sort tests search, with the case, missing values and
``LIKE`` wildcards a translation into a query tends to get wrong."""
SEARCH_FILTERS = [
'userName eq "ALICE"',
'userName ne "alice"',
'userName sw "dave_"',
'userName ew "_%"',
'userName co "a"',
'userName gt "b"',
'userName le "carol"',
'title eq "boss"',
'title ne "Boss"',
'not (title eq "Boss")',
"title eq null",
"title ne null",
"title gt null",
"title pr",
'title co "%"',
'title lt "c"',
"displayName pr",
"name pr",
"name.givenName pr",
'name.familyName eq "SMITH"',
"active eq true",
"active eq false",
"not (active eq true)",
"active ne true",
'meta.created co "20"',
"active pr",
"emails pr",
'emails eq "alice@work.example"',
'emails ne "alice@work.example"',
'emails co "WORK"',
'emails.type eq "work"',
"emails.type pr",
"emails.primary eq true",
'emails[type eq "work" and value co "alice"]',
'emails[type eq "home" and primary eq true]',
'emails[value ne "alice@work.example"]',
'emails[not (type eq "work")]',
"emails[type pr]",
'meta.created gt "2000-01-01T00:00:00Z"',
'meta.lastModified lt "2000-01-01T00:00:00Z"',
f'{ENTERPRISE}:employeeNumber eq "100"',
f'{ENTERPRISE}:employeeNumber gt "10"',
'title eq "Boss" or userName eq "Bob"',
'title eq "Boss" and active eq false',
]
SEARCH_SORTS = [
"userName",
"title",
"displayName",
"name.familyName",
"active",
"emails",
"emails.type",
"meta.created",
f"{ENTERPRISE}:employeeNumber",
]
[docs]
class MailboxAddress(ComplexAttribute):
"""An address of a :class:`Mailbox`. No two mailboxes share an address, whatever its case."""
value: Annotated[str | None, Uniqueness.server] = None
type: str | None = None
[docs]
class MailboxOwner(ComplexAttribute):
"""The owner of a :class:`Mailbox`. No two mailboxes share an owner, whatever its case."""
value: Annotated[str | None, Uniqueness.server] = None
display: str | None = None
[docs]
class Mailbox(Resource[Any]):
"""A resource holding unique values below its attributes.
The tests of the uniqueness of sub-attributes and of multi-valued
attributes create mailboxes. No two mailboxes share an alias, but two
aliases differing by their case are different.
"""
__schema__ = URN("urn:example:scim:schemas:2.0:Mailbox")
display_name: str | None = None
addresses: list[MailboxAddress] | None = None
aliases: Annotated[list[str] | None, Uniqueness.server, CaseExact.true] = None
owner: MailboxOwner | None = None
MAILBOX_TYPE = ResourceType(
id="Mailbox",
name="Mailbox",
endpoint=Reference[URI]("/Mailboxes"),
schema_=Reference[URI](str(Mailbox.__schema__)),
)
"""The resource type of the :class:`Mailbox` resources, served at ``/Mailboxes``."""
[docs]
class ScimStorageContract:
"""The rules every :class:`~scim2_server.storage.ScimStorage` follows.
The suite uses the ``User`` and ``Group`` resource types of the
``provider`` fixture, which serves the default resource types of
:rfc:`RFC 7643 <7643>` unless overridden. The tests of a feature that the
configuration of the provider does not announce, such as sorting, are
skipped. So are the tests of the cursors, unless the storage
:attr:`~scim2_server.storage.ScimStorage.supports_cursors`.
"""
supports_root_search: bool = True
"""Whether the storage searches several resource types at once.
Set it to :data:`False` for a storage raising
:class:`~scim2_models.NotImplementedException` in that case.
"""
stores_created: bool = True
"""Whether the storage stores ``meta.created``.
Set it to :data:`False` for a storage that leaves ``meta.created`` empty.
"""
stores_last_modified: bool = True
"""Whether the storage stores ``meta.lastModified``.
Set it to :data:`False` for a storage that leaves ``meta.lastModified``
empty.
"""
stores_password: bool = True
"""Whether the storage stores the ``password`` of the users.
Set it to :data:`False` only for a storage that does not keep the
password at all. The tests of the write-only and never returned
attributes are then skipped. A storage that keeps a hash of the password
stores it: its :meth:`~scim2_server.storage.ScimStorage.get` returns the
hash.
"""
stores_mailboxes: bool = False
"""Whether the storage stores the :class:`Mailbox` resources.
Set it to :data:`True` to check the uniqueness of sub-attributes and of
multi-valued attributes. The storage then serves the ``Mailbox``
resource type, :data:`MAILBOX_TYPE`. The ``provider`` fixture serves it
too.
"""
@pytest.fixture
def provider(self) -> ScimProvider:
"""Return the description of the service the storage serves."""
provider = load_default_provider()
if not self.stores_mailboxes:
return provider
return ScimProvider(
models=[*provider.models, Mailbox],
resource_types=[*provider.resource_types, MAILBOX_TYPE],
config=provider.config,
policy=provider.policy,
)
@pytest.fixture
def user_type(self, provider: ScimProvider) -> ResourceType:
return next(rt for rt in provider.resource_types if rt.id == "User")
@pytest.fixture
def group_type(self, provider: ScimProvider) -> ResourceType:
return next(rt for rt in provider.resource_types if rt.id == "Group")
@pytest.fixture
def mailbox_type(self, provider: ScimProvider) -> ResourceType:
self.require(self.stores_mailboxes, "mailboxes")
return next(rt for rt in provider.resource_types if rt.id == "Mailbox")
@pytest.fixture
def user_model(
self, provider: ScimProvider, user_type: ResourceType
) -> type[Resource[Any]]:
return cast(type[Resource[Any]], provider.model_for(user_type))
@pytest.fixture
def group_model(
self, provider: ScimProvider, group_type: ResourceType
) -> type[Resource[Any]]:
return cast(type[Resource[Any]], provider.model_for(group_type))
@staticmethod
def search_request(
models: list[type[Resource[Any]]], **parameters: Any
) -> SearchRequest[Any]:
"""Build a search request on the given models, as the server builds it."""
return parametrize(SearchRequest, Union[tuple(models)])(**parameters) # noqa: UP007
@staticmethod
def search(
storage: Any,
resource_types: list[ResourceType],
search_request: SearchRequest[Any],
position: Any = None,
) -> Any:
"""Search a storage as the server does, whether its search method accepts a position or not."""
return search_page(storage, resource_types, search_request, position)
@staticmethod
def require(supported: bool, feature: str) -> None:
"""Skip the current test when the service does not support a feature."""
if not supported:
pytest.skip(f"The service does not support {feature}")
@staticmethod
def announces(provider: ScimProvider, capability: str) -> bool:
"""Tell whether the configuration of the service announces a capability, such as ``"sort"``."""
feature = getattr(provider.config, capability, None)
return bool(feature and feature.supported)
def create_users(
self,
storage: ScimStorage,
user_type: ResourceType,
user_model: Any,
*user_names: str,
) -> list[Any]:
return [
storage.create(user_type, user_model(user_name=user_name))
for user_name in user_names
]
def create_group(
self,
storage: ScimStorage,
group_type: ResourceType,
group_model: Any,
member: Any,
) -> Any:
return storage.create(
group_type,
group_model(display_name="admins", members=[{"value": member.id}]),
)
def test_create_fills_the_identifier_and_the_meta(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A created resource gets an id, its resource type and a version, but no location."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
assert user.id
assert user.meta.resource_type == user_type.name
assert user.meta.version
assert user.meta.location is None
def test_create_fills_the_stored_dates(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A created resource gets the dates the storage stores, and only them.
Per :rfc:`RFC 7643 §3.1 <7643#section-3.1>`, ``lastModified`` is
``created`` until the resource changes.
"""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
assert (user.meta.created is not None) is self.stores_created
assert (user.meta.last_modified is not None) is self.stores_last_modified
if self.stores_created and self.stores_last_modified:
assert user.meta.last_modified == user.meta.created
def test_create_does_not_change_the_given_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""The resource given to create keeps having no id and no meta."""
given = user_model(user_name="bjensen")
storage.create(user_type, given)
assert given.id is None
assert given.meta is None
def test_created_resources_get_distinct_identifiers(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Two created resources never share an id."""
first, second = self.create_users(
storage, user_type, user_model, "bjensen", "jsmith"
)
assert first.id != second.id
def test_create_returns_a_copy(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Changing a created resource does not change the stored one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
user.user_name = "changed"
assert storage.get(user_type, user.id).user_name == "bjensen"
def test_get_returns_the_stored_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A stored resource is read back with its values and its meta."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
stored = storage.get(user_type, user.id)
assert stored.user_name == "bjensen"
assert stored.meta.created == user.meta.created
assert stored.meta.last_modified == user.meta.last_modified
assert stored.meta.version == user.meta.version
def test_get_returns_a_copy(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Changing a read resource does not change the stored one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.get(user_type, user.id).user_name = "changed"
assert storage.get(user_type, user.id).user_name == "bjensen"
def test_get_an_unknown_resource(
self, storage: Any, user_type: ResourceType
) -> None:
"""Reading a resource that does not exist raises a 404."""
with pytest.raises(NotFoundException):
storage.get(user_type, "unknown")
def test_get_a_resource_of_another_type(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
) -> None:
"""A resource is only found under its own resource type."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
with pytest.raises(NotFoundException):
storage.get(group_type, user.id)
@pytest.mark.parametrize(
"parameters",
[
{"excludedAttributes": "members"},
{"excludedAttributes": "members.display"},
{"attributes": "displayName"},
{"attributes": "members.value"},
],
)
def test_get_keeps_what_the_response_returns(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
parameters: dict[str, str],
) -> None:
"""A resource read with response parameters gives the same response as the whole resource."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
group = storage.create(
group_type,
group_model.model_validate(
{
"displayName": "Admins",
"members": [{"value": user.id, "display": "bjensen"}],
}
),
)
response_parameters = parametrize(
ResponseParameters, group_model
).model_validate(parameters)
whole = storage.get(group_type, group.id)
projected = storage.get(
group_type, group.id, **projection(storage, response_parameters)
)
assert projected.model_dump(
scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
response_parameters=response_parameters,
) == whole.model_dump(
scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
response_parameters=response_parameters,
)
def test_update_replaces_the_stored_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""An update stores the new state, and changes the version."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
user.display_name = "Barbara"
updated = storage.update(user_type, user)
assert updated.display_name == "Barbara"
assert storage.get(user_type, user.id).display_name == "Barbara"
assert updated.meta.version != user.meta.version
assert updated.meta.location is None
def test_update_keeps_the_creation_date(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""An update keeps the creation date, and does not move the modification date back."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
user.display_name = "Barbara"
updated = storage.update(user_type, user)
assert updated.meta.created == user.meta.created
assert (updated.meta.last_modified is not None) is self.stores_last_modified
if self.stores_last_modified:
assert updated.meta.last_modified >= user.meta.last_modified
def test_update_does_not_change_the_given_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""The resource given to update keeps its former version."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
version = user.meta.version
storage.update(user_type, user)
assert user.meta.version == version
def test_update_returns_a_copy(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Changing an updated resource does not change the stored one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.update(user_type, user).user_name = "changed"
assert storage.get(user_type, user.id).user_name == "bjensen"
def create_user_with_password(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> Any:
"""Create a user with a password, and read it back, as the server does before a PUT or a PATCH."""
self.require(self.stores_password, "the password")
user = storage.create(
user_type, user_model(user_name="bjensen", password="secret")
)
return storage.get(user_type, user.id)
def test_get_returns_the_write_only_values(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A whole resource holds its password, in the form the storage keeps it."""
user = self.create_user_with_password(storage, user_type, user_model)
assert user.password is not None
def test_update_keeps_the_write_only_values_read_back(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""The password read back and given to update unchanged stays as it is.
It is what a PUT without password gives, per :rfc:`RFC 7644 §3.5.1
<7644#section-3.5.1>`.
"""
user = self.create_user_with_password(storage, user_type, user_model)
user.display_name = "Barbara"
storage.update(user_type, user)
assert storage.get(user_type, user.id).password == user.password
def test_update_clears_a_write_only_value(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A PATCH removing the password clears it."""
user = self.create_user_with_password(storage, user_type, user_model)
user.password = None
storage.update(user_type, user)
assert storage.get(user_type, user.id).password is None
def test_update_changes_a_write_only_value(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A new password replaces the stored one."""
user = self.create_user_with_password(storage, user_type, user_model)
former = user.password
user.password = "other"
storage.update(user_type, user)
assert storage.get(user_type, user.id).password not in (None, former)
def test_update_an_unknown_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Updating a resource that does not exist raises a 404."""
with pytest.raises(NotFoundException):
storage.update(user_type, user_model(id="unknown", user_name="bjensen"))
def test_update_with_the_expected_version(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""An update succeeds when the stored version is the expected one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
user.display_name = "Barbara"
storage.update(user_type, user, expected_version=user.meta.version)
assert storage.get(user_type, user.id).display_name == "Barbara"
def test_update_with_an_outdated_version(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""An update raises a 412 and stores nothing when the stored version changed."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
outdated = user.meta.version
user.display_name = "Babs"
storage.update(user_type, user)
user.display_name = "Barbara"
with pytest.raises(PreconditionFailedException):
storage.update(user_type, user, expected_version=outdated)
assert storage.get(user_type, user.id).display_name == "Babs"
def test_delete_removes_the_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A deleted resource cannot be read anymore."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.delete(user_type, user.id)
with pytest.raises(NotFoundException):
storage.get(user_type, user.id)
def test_delete_an_unknown_resource(
self, storage: Any, user_type: ResourceType
) -> None:
"""Deleting a resource that does not exist raises a 404."""
with pytest.raises(NotFoundException):
storage.delete(user_type, "unknown")
def test_delete_with_the_expected_version(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A deletion succeeds when the stored version is the expected one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.delete(user_type, user.id, expected_version=user.meta.version)
with pytest.raises(NotFoundException):
storage.get(user_type, user.id)
def test_delete_with_an_outdated_version(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A deletion raises a 412 and keeps the resource when the stored version changed."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
outdated = user.meta.version
user.display_name = "Babs"
storage.update(user_type, user)
with pytest.raises(PreconditionFailedException):
storage.delete(user_type, user.id, expected_version=outdated)
assert storage.get(user_type, user.id).user_name == "bjensen"
def test_create_with_a_taken_unique_value(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A userName already taken, whatever its case, raises a 409 on creation.
The detail tells the attribute, as
:func:`~scim2_server.storage.uniqueness_conflict` writes it, and
never the value.
"""
self.create_users(storage, user_type, user_model, "bjensen")
with pytest.raises(UniquenessException) as conflict:
storage.create(user_type, user_model(user_name="BJensen"))
assert conflict.value.detail == "Uniqueness conflict with attribute userName"
def test_update_with_a_taken_unique_value(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A userName already taken by another resource raises a 409 on update."""
_, user = self.create_users(storage, user_type, user_model, "bjensen", "jsmith")
user.user_name = "bjensen"
with pytest.raises(UniquenessException):
storage.update(user_type, user)
assert storage.get(user_type, user.id).user_name == "jsmith"
def test_update_frees_the_former_unique_values(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A userName a resource gave up can be taken by another resource."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
user.user_name = "babs"
storage.update(user_type, user)
self.create_users(storage, user_type, user_model, "bjensen")
def test_delete_frees_the_unique_values(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""The userName of a deleted resource can be taken by a new resource."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.delete(user_type, user.id)
self.create_users(storage, user_type, user_model, "bjensen")
def test_create_with_a_taken_unique_value_in_a_list(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A unique sub-attribute value already taken, whatever its case, raises a 409 on creation.
Each value of a multi-valued attribute counts on its own.
"""
storage.create(
mailbox_type,
Mailbox(addresses=[MailboxAddress(value="bjensen@example.com")]),
)
with pytest.raises(UniquenessException):
storage.create(
mailbox_type,
Mailbox(
addresses=[
MailboxAddress(value="babs@example.com"),
MailboxAddress(value="BJensen@example.com"),
]
),
)
def test_update_with_a_taken_unique_value_in_a_list(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A unique sub-attribute value taken by another resource raises a 409 on update, telling the sub-attribute."""
storage.create(
mailbox_type,
Mailbox(addresses=[MailboxAddress(value="bjensen@example.com")]),
)
mailbox = storage.create(
mailbox_type, Mailbox(addresses=[MailboxAddress(value="babs@example.com")])
)
mailbox.addresses = [
MailboxAddress(value="babs@example.com"),
MailboxAddress(value="bjensen@example.com"),
]
with pytest.raises(UniquenessException) as conflict:
storage.update(mailbox_type, mailbox)
assert (
conflict.value.detail
== "Uniqueness conflict with attribute addresses.value"
)
stored = storage.get(mailbox_type, mailbox.id)
assert [address.value for address in stored.addresses] == ["babs@example.com"]
def test_create_with_a_taken_value_of_a_multi_valued_attribute(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A value of a unique multi-valued attribute taken by another resource raises a 409."""
storage.create(mailbox_type, Mailbox(aliases=["babs", "bjensen"]))
with pytest.raises(UniquenessException):
storage.create(mailbox_type, Mailbox(aliases=["bjensen"]))
def test_create_with_a_taken_unique_value_in_a_complex_attribute(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A unique sub-attribute of a single complex attribute already taken, whatever its case, raises a 409."""
storage.create(mailbox_type, Mailbox(owner=MailboxOwner(value="bjensen")))
with pytest.raises(UniquenessException):
storage.create(mailbox_type, Mailbox(owner=MailboxOwner(value="BJensen")))
def test_case_exact_unique_values_differ_by_their_case(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A caseExact unique value is not taken by the same value in another case."""
storage.create(mailbox_type, Mailbox(aliases=["babs"]))
storage.create(mailbox_type, Mailbox(aliases=["Babs"]))
with pytest.raises(UniquenessException):
storage.create(mailbox_type, Mailbox(aliases=["babs"]))
def test_missing_unique_values_do_not_clash(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""Entries and complex attributes without their unique value do not clash."""
for _ in range(2):
storage.create(
mailbox_type,
Mailbox(
addresses=[MailboxAddress(type="work")],
owner=MailboxOwner(display="Babs"),
),
)
def test_update_keeps_and_swaps_its_unique_values(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A resource keeping its unique values, or swapping them between its entries, is written."""
mailbox = storage.create(
mailbox_type,
Mailbox(
display_name="Babs",
addresses=[
MailboxAddress(value="babs@example.com", type="work"),
MailboxAddress(value="bjensen@example.com", type="home"),
],
aliases=["babs"],
owner=MailboxOwner(value="bjensen"),
),
)
mailbox.display_name = "Barbara"
mailbox = storage.update(mailbox_type, mailbox)
mailbox.addresses = [
MailboxAddress(value="bjensen@example.com", type="work"),
MailboxAddress(value="babs@example.com", type="home"),
]
updated = storage.update(mailbox_type, mailbox)
assert updated.display_name == "Barbara"
assert {(address.type, address.value) for address in updated.addresses} == {
("work", "bjensen@example.com"),
("home", "babs@example.com"),
}
def test_writes_free_the_unique_values_in_a_list(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""Unique values a resource gave up, or held before its deletion, can be taken by another resource."""
mailbox = storage.create(
mailbox_type,
Mailbox(
addresses=[
MailboxAddress(value="babs@example.com"),
MailboxAddress(value="bjensen@example.com"),
],
aliases=["babs"],
),
)
mailbox.addresses = [MailboxAddress(value="bjensen@example.com")]
mailbox.aliases = None
mailbox = storage.update(mailbox_type, mailbox)
storage.create(
mailbox_type,
Mailbox(
addresses=[MailboxAddress(value="babs@example.com")], aliases=["babs"]
),
)
storage.delete(mailbox_type, mailbox.id)
storage.create(
mailbox_type,
Mailbox(addresses=[MailboxAddress(value="bjensen@example.com")]),
)
def test_unique_values_of_different_attributes_do_not_clash(
self, storage: Any, mailbox_type: ResourceType
) -> None:
"""A unique value only clashes with the values of its own attribute."""
storage.create(
mailbox_type, Mailbox(addresses=[MailboxAddress(value="bjensen")])
)
storage.create(mailbox_type, Mailbox(aliases=["bjensen"]))
storage.create(mailbox_type, Mailbox(owner=MailboxOwner(value="bjensen")))
def test_search_a_resource_type(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
) -> None:
"""A search on a resource type returns its resources only, and counts them."""
users = self.create_users(storage, user_type, user_model, "alice", "bob")
self.create_group(storage, group_type, group_model, users[0])
page = self.search(storage, [user_type], self.search_request([user_model]))
assert page.total == 2
assert sorted(r.user_name for r in page.resources) == ["alice", "bob"]
def test_search_returns_copies(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Changing a found resource does not change the stored one."""
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
(found,) = self.search(
storage, [user_type], self.search_request([user_model])
).resources
found.user_name = "changed"
assert storage.get(user_type, user.id).user_name == "bjensen"
def test_search_pages_the_results(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A page holds count resources from startIndex, and the total counts them all."""
self.create_users(storage, user_type, user_model, "alice", "bob", "carol")
page = self.search(
storage,
[user_type],
self.search_request([user_model], start_index=2, count=1),
)
assert page.total == 3
assert len(page.resources) == 1
def test_search_without_count_returns_every_resource(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A search without count is not paged."""
self.create_users(storage, user_type, user_model, "alice", "bob", "carol")
page = self.search(storage, [user_type], self.search_request([user_model]))
assert page.total == 3
assert len(page.resources) == 3
def test_search_with_a_filter(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""A filter keeps the matching resources, and the total counts them only."""
self.require(self.announces(provider, "filter"), "filtering")
self.create_users(storage, user_type, user_model, "alice", "bob", "carol")
page = self.search(
storage,
[user_type],
self.search_request([user_model], filter='userName eq "bob"', count=10),
)
assert page.total == 1
assert [r.user_name for r in page.resources] == ["bob"]
def test_search_sorted(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""A sorted search orders the resources before paging them."""
self.require(self.announces(provider, "sort"), "sorting")
self.create_users(storage, user_type, user_model, "bob", "carol", "alice")
page = self.search(
storage,
[user_type],
self.search_request(
[user_model], sort_by="userName", sort_order="descending", count=2
),
)
assert [r.user_name for r in page.resources] == ["carol", "bob"]
def test_search_at_the_root(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
) -> None:
"""A search on several resource types returns their resources as one collection."""
self.require(self.supports_root_search, "searching at the root")
(user,) = self.create_users(storage, user_type, user_model, "alice")
self.create_group(storage, group_type, group_model, user)
page = self.search(
storage,
[user_type, group_type],
self.search_request([user_model, group_model]),
)
assert page.total == 2
assert {r.meta.resource_type for r in page.resources} == {
user_type.name,
group_type.name,
}
def test_search_at_the_root_on_an_attribute_some_types_lack(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
) -> None:
"""An attribute a resource type does not declare matches none of its resources."""
self.require(self.supports_root_search, "searching at the root")
self.require(self.announces(provider, "filter"), "filtering")
(user,) = self.create_users(storage, user_type, user_model, "alice")
self.create_group(storage, group_type, group_model, user)
page = self.search(
storage,
[user_type, group_type],
self.search_request([user_model, group_model], filter="userName pr"),
)
assert page.total == 1
assert page.resources[0].user_name == "alice"
[docs]
def search_users(self) -> list[dict[str, Any]]:
"""Return the users the filter and sort tests search.
Override it to leave out the attributes the storage does not store.
"""
return SEARCH_USERS
def search_request_or_skip(
self, models: list[type[Resource[Any]]], **parameters: Any
) -> SearchRequest[Any]:
"""Build a search request, and skip the current test when the models do not declare an attribute of the filter or the sort."""
try:
return self.search_request(models, **parameters)
except ValidationError as exception:
errors = exception.errors()
if any(
error["type"] not in ("scim_invalidFilter", "scim_invalidPath")
for error in errors
):
raise
pytest.skip(f"The models refuse the search: {errors[0]['msg']}")
def search_or_skip(
self,
storage: Any,
resource_types: list[ResourceType],
search_request: SearchRequest[Any],
) -> Any:
"""Search a storage, and skip the current test when the storage refuses the filter or the sort."""
try:
return self.search(storage, resource_types, search_request)
except (InvalidFilterException, InvalidPathException) as exception:
pytest.skip(f"The storage refuses the search: {exception.detail}")
@pytest.mark.parametrize("scim_filter", SEARCH_FILTERS)
def test_search_filters_as_the_filter_evaluator(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
scim_filter: str,
) -> None:
"""A filter keeps the resources that ScimFilter.match keeps, and the total counts them."""
self.require(self.announces(provider, "filter"), "filtering")
for user in self.search_users():
storage.create(user_type, user_model.model_validate(user))
stored = self.search(storage, [user_type], self.search_request([user_model]))
search_request = self.search_request_or_skip([user_model], filter=scim_filter)
page = self.search_or_skip(storage, [user_type], search_request)
evaluator = cast(ScimFilter[Any], search_request.filter)
expected = [r.id for r in stored.resources if evaluator.match(r)]
assert page.total == len(expected)
assert sorted(r.id for r in page.resources) == sorted(expected)
@pytest.mark.parametrize("sort_order", ["ascending", "descending"])
@pytest.mark.parametrize("sort_by", SEARCH_SORTS)
def test_search_sorts_as_search_request_sort(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
sort_by: str,
sort_order: str,
) -> None:
"""A sorted search orders the resources as SearchRequest.sort does.
Resources with the same sort value may come in any order.
"""
self.require(self.announces(provider, "sort"), "sorting")
for user in self.search_users():
storage.create(user_type, user_model.model_validate(user))
stored = self.search(storage, [user_type], self.search_request([user_model]))
search_request = self.search_request_or_skip(
[user_model], sort_by=sort_by, sort_order=sort_order
)
page = self.search_or_skip(storage, [user_type], search_request)
expected = search_request.sort(stored.resources)
assert sorted(r.id for r in page.resources) == sorted(r.id for r in expected)
assert [search_request.sort_value(r) for r in page.resources] == [
search_request.sort_value(r) for r in expected
]
def walk(
self,
storage: Any,
resource_types: list[ResourceType],
search_request: SearchRequest[Any],
) -> list[Any]:
"""Return every page of a search paged with a cursor, from the first one."""
self.require(storage.supports_cursors, "cursor pagination")
pages = [self.search(storage, resource_types, search_request)]
while pages[-1].next is not None:
pages.append(
self.search(storage, resource_types, search_request, pages[-1].next)
)
return pages
def walk_back(
self,
storage: Any,
resource_types: list[ResourceType],
search_request: SearchRequest[Any],
page: Any,
) -> list[Any]:
"""Return the pages before a page, from the closest one, following the previous cursors."""
pages = []
while page.previous is not None:
page = self.search(storage, resource_types, search_request, page.previous)
pages.append(page)
return pages
@staticmethod
def ids(pages: list[Any]) -> list[str]:
return [resource.id for page in pages for resource in page.resources]
def test_cursor_pages_every_resource_once(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Following the next cursors returns every resource once, count by count."""
self.require(storage.supports_cursors, "cursor pagination")
names = ["alice", "bob", "carol", "dave", "erin"]
self.create_users(storage, user_type, user_model, *names)
pages = self.walk(
storage, [user_type], self.search_request([user_model], cursor="", count=2)
)
assert [len(page.resources) for page in pages] == [2, 2, 1]
assert sorted(r.user_name for page in pages for r in page.resources) == names
assert pages[0].previous is None
assert {page.total for page in pages} <= {5, None}
def test_cursor_pages_in_sort_order(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""The pages of a sorted search follow each other in the sort order."""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.announces(provider, "sort"), "sorting")
self.create_users(
storage, user_type, user_model, "dave", "alice", "carol", "bob"
)
pages = self.walk(
storage,
[user_type],
self.search_request(
[user_model],
cursor="",
count=3,
sort_by="userName",
sort_order="descending",
),
)
assert [r.user_name for page in pages for r in page.resources] == [
"dave",
"carol",
"bob",
"alice",
]
def test_cursor_pages_equal_sort_values_once(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""Resources with the same sort value, or without one, are returned once, even across pages."""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.announces(provider, "sort"), "sorting")
self.require("nick_name" in user_model.model_fields, "nickName")
for user_name, nick_name in [
("alice", "twin"),
("bob", None),
("carol", "twin"),
("dave", "twin"),
("erin", None),
]:
storage.create(
user_type, user_model(user_name=user_name, nick_name=nick_name)
)
pages = self.walk(
storage,
[user_type],
self.search_request([user_model], cursor="", count=2, sort_by="nickName"),
)
names = [r.user_name for page in pages for r in page.resources]
assert sorted(names[:3]) == ["alice", "carol", "dave"]
assert sorted(names[3:]) == ["bob", "erin"]
def test_cursor_pages_back(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Following the previous cursors from the last page returns the same pages, back to the first."""
self.require(storage.supports_cursors, "cursor pagination")
self.create_users(
storage, user_type, user_model, "alice", "bob", "carol", "dave", "erin"
)
search_request = self.search_request([user_model], cursor="", count=2)
forward = self.walk(storage, [user_type], search_request)
backward = self.walk_back(storage, [user_type], search_request, forward[-1])
assert [[r.id for r in page.resources] for page in reversed(backward)] == [
[r.id for r in page.resources] for page in forward[:-1]
]
assert backward[-1].previous is None
@pytest.mark.parametrize("deleted", ["last of the page", "first of the page"])
def test_cursor_gives_stable_pages_when_a_resource_of_the_page_is_deleted(
self, storage: Any, user_type: ResourceType, user_model: Any, deleted: str
) -> None:
"""Deleting a resource already returned does not skip nor repeat the next ones."""
self.require(storage.supports_cursors, "cursor pagination")
self.create_users(
storage, user_type, user_model, "alice", "bob", "carol", "dave", "erin"
)
search_request = self.search_request([user_model], cursor="", count=2)
expected = self.ids(self.walk(storage, [user_type], search_request))
first = self.search(storage, [user_type], search_request)
victim = first.resources[-1 if deleted == "last of the page" else 0]
storage.delete(user_type, victim.id)
rest = [self.search(storage, [user_type], search_request, first.next)]
while rest[-1].next is not None:
rest.append(
self.search(storage, [user_type], search_request, rest[-1].next)
)
assert self.ids([first, *rest]) == expected
def test_cursor_gives_stable_pages_when_a_resource_is_created(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Creating a resource between two pages does not skip nor repeat the existing ones."""
self.require(storage.supports_cursors, "cursor pagination")
self.create_users(
storage, user_type, user_model, "alice", "bob", "carol", "dave", "erin"
)
search_request = self.search_request([user_model], cursor="", count=2)
expected = self.ids(self.walk(storage, [user_type], search_request))
first = self.search(storage, [user_type], search_request)
(created,) = self.create_users(storage, user_type, user_model, "frank")
rest = [self.search(storage, [user_type], search_request, first.next)]
while rest[-1].next is not None:
rest.append(
self.search(storage, [user_type], search_request, rest[-1].next)
)
returned = self.ids([first, *rest])
assert [i for i in returned if i != created.id] == expected
def test_cursor_pages_back_after_a_deletion(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""Deleting the first resource of a page does not change the page before it."""
self.require(storage.supports_cursors, "cursor pagination")
self.create_users(
storage, user_type, user_model, "alice", "bob", "carol", "dave", "erin"
)
search_request = self.search_request([user_model], cursor="", count=2)
forward = self.walk(storage, [user_type], search_request)
storage.delete(user_type, forward[1].resources[0].id)
previous = self.search(
storage, [user_type], search_request, forward[1].previous
)
assert self.ids([previous]) == self.ids([forward[0]])
assert previous.previous is None
def test_cursor_with_count_zero(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""A count of 0 returns no resource and no cursor, so the client does not loop."""
self.require(storage.supports_cursors, "cursor pagination")
self.create_users(storage, user_type, user_model, "alice", "bob")
page = self.search(
storage, [user_type], self.search_request([user_model], cursor="", count=0)
)
assert page.resources == []
assert page.next is None
assert page.previous is None
assert page.total in (2, None)
def test_cursor_at_the_root(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
) -> None:
"""A cursor pages the resources of several resource types as one collection."""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.supports_root_search, "searching at the root")
users = self.create_users(storage, user_type, user_model, "alice", "bob")
group = self.create_group(storage, group_type, group_model, users[0])
pages = self.walk(
storage,
[user_type, group_type],
self.search_request([user_model, group_model], cursor="", count=1),
)
assert sorted(self.ids(pages)) == sorted([users[0].id, users[1].id, group.id])
def test_cursor_pages_back_at_the_root(
self,
storage: Any,
user_type: ResourceType,
group_type: ResourceType,
user_model: Any,
group_model: Any,
) -> None:
"""Following the previous cursors at the root returns the same pages, back to the first."""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.supports_root_search, "searching at the root")
users = self.create_users(storage, user_type, user_model, "alice", "bob")
self.create_group(storage, group_type, group_model, users[0])
resource_types = [user_type, group_type]
search_request = self.search_request(
[user_model, group_model], cursor="", count=1
)
forward = self.walk(storage, resource_types, search_request)
backward = self.walk_back(storage, resource_types, search_request, forward[-1])
assert [[r.id for r in page.resources] for page in reversed(backward)] == [
[r.id for r in page.resources] for page in forward[:-1]
]
assert backward[-1].previous is None
def test_cursor_pages_back_in_descending_order_with_missing_values(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""Following the previous cursors of a descending sort returns the same pages, with the resources without value first."""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.announces(provider, "sort"), "sorting")
self.require("nick_name" in user_model.model_fields, "nickName")
for user_name, nick_name in [
("alice", "a"),
("bob", None),
("carol", "c"),
("dave", "b"),
("erin", None),
]:
storage.create(
user_type, user_model(user_name=user_name, nick_name=nick_name)
)
search_request = self.search_request(
[user_model],
cursor="",
count=2,
sort_by="nickName",
sort_order="descending",
)
forward = self.walk(storage, [user_type], search_request)
backward = self.walk_back(storage, [user_type], search_request, forward[-1])
names = [r.user_name for page in forward for r in page.resources]
assert sorted(names[:2]) == ["bob", "erin"]
assert names[2:] == ["carol", "dave", "alice"]
assert [[r.id for r in page.resources] for page in reversed(backward)] == [
[r.id for r in page.resources] for page in forward[:-1]
]
assert backward[-1].previous is None
def test_cursor_pages_by_date(
self,
storage: Any,
provider: ScimProvider,
user_type: ResourceType,
user_model: Any,
) -> None:
"""A cursor pages a search sorted by date, in both directions.
Resources modified at the same time may come in any order.
"""
self.require(storage.supports_cursors, "cursor pagination")
self.require(self.announces(provider, "sort"), "sorting")
self.require(self.stores_last_modified, "meta.lastModified")
created = self.create_users(
storage, user_type, user_model, "alice", "bob", "carol", "dave", "erin"
)
search_request = self.search_request(
[user_model], cursor="", count=2, sort_by="meta.lastModified"
)
first = self.search_or_skip(storage, [user_type], search_request)
forward = [first]
while forward[-1].next is not None:
forward.append(
self.search(storage, [user_type], search_request, forward[-1].next)
)
backward = self.walk_back(storage, [user_type], search_request, forward[-1])
dates = [r.meta.last_modified for page in forward for r in page.resources]
assert sorted(self.ids(forward)) == sorted(user.id for user in created)
assert dates == sorted(dates)
assert [[r.id for r in page.resources] for page in reversed(backward)] == [
[r.id for r in page.resources] for page in forward[:-1]
]
assert backward[-1].previous is None
def test_operation(
self, storage: Any, user_type: ResourceType, user_model: Any
) -> None:
"""The operation context encloses the calls of one SCIM operation."""
with storage.operation():
(user,) = self.create_users(storage, user_type, user_model, "bjensen")
storage.get(user_type, user.id)
def test_operation_lets_exceptions_through(self, storage: Any) -> None:
"""An exception raised within an operation is not swallowed."""
with pytest.raises(RuntimeError), storage.operation():
raise RuntimeError
class BlockingStorage(ScimStorage):
"""Run the coroutines of an asynchronous storage one by one, for the contract tests."""
def __init__(self, storage: AsyncScimStorage, runner: asyncio.Runner) -> None:
self.storage = storage
self.runner = runner
self.supports_cursors = storage.supports_cursors
def run(self, coroutine: Coroutine[Any, Any, T]) -> T:
return self.runner.run(coroutine)
def get(
self,
resource_type: ResourceType,
resource_id: str,
*,
response_parameters: ResponseParameters[Any] | None = None,
) -> Resource[Any]:
return self.run(
self.storage.get(
resource_type,
resource_id,
**projection(self.storage, response_parameters),
)
)
def search(
self,
resource_types: list[ResourceType],
search_request: SearchRequest[Any],
*,
position: Any = None,
) -> SearchPage:
return self.run(
async_search_page(self.storage, resource_types, search_request, position)
)
def create(
self, resource_type: ResourceType, resource: Resource[Any]
) -> Resource[Any]:
return self.run(self.storage.create(resource_type, resource))
def update(
self,
resource_type: ResourceType,
resource: Resource[Any],
*,
expected_version: str | None = None,
) -> Resource[Any]:
return self.run(
self.storage.update(
resource_type, resource, expected_version=expected_version
)
)
def delete(
self,
resource_type: ResourceType,
resource_id: str,
*,
expected_version: str | None = None,
) -> None:
self.run(
self.storage.delete(
resource_type, resource_id, expected_version=expected_version
)
)
@contextmanager
def operation(self) -> Generator[None]:
manager = self.storage.operation()
self.run(manager.__aenter__())
try:
yield
except BaseException as exception:
self.run(
manager.__aexit__(type(exception), exception, exception.__traceback__)
)
raise
self.run(manager.__aexit__(None, None, None))
[docs]
class AsyncScimStorageContract(ScimStorageContract):
"""The rules every :class:`~scim2_server.storage.AsyncScimStorage` follows.
These are the rules of :class:`ScimStorageContract`. Each test runs the
coroutines of the storage on a single event loop, one after the other.
"""
@pytest.fixture
def storage(self, async_storage: AsyncScimStorage) -> Iterator[ScimStorage]:
with asyncio.Runner() as runner:
yield BlockingStorage(async_storage, runner)