|
| 1 | +import threading |
| 2 | +import wsgiref.simple_server |
| 3 | + |
| 4 | +import portpicker |
| 5 | +import pytest |
| 6 | +from httpx import Client |
| 7 | +from typing import Annotated |
| 8 | +from typing import Union |
| 9 | + |
| 10 | +from scim2_client.engines.httpx import SyncSCIMClient |
| 11 | + |
| 12 | +scim2_server = pytest.importorskip("scim2_server") |
| 13 | +from scim2_server.backend import InMemoryBackend # noqa: E402 |
| 14 | +from scim2_server.provider import SCIMProvider # noqa: E402 |
| 15 | + |
| 16 | +from scim2_models import EnterpriseUser |
| 17 | +from scim2_models import Extension |
| 18 | +from scim2_models import Group |
| 19 | +from scim2_models import Meta |
| 20 | +from scim2_models import ResourceType |
| 21 | +from scim2_models import Required |
| 22 | +from scim2_models import User |
| 23 | + |
| 24 | + |
| 25 | +class OtherExtension(Extension): |
| 26 | + schemas: Annotated[list[str], Required.true] = ["urn:ietf:params:scim:schemas:extension:Other:1.0:User"] |
| 27 | + |
| 28 | + test: str | None = None |
| 29 | + test2: list[str] | None = None |
| 30 | + |
| 31 | + |
| 32 | +def get_schemas(): |
| 33 | + schemas = [ |
| 34 | + User.to_schema(), |
| 35 | + Group.to_schema(), |
| 36 | + OtherExtension.to_schema(), |
| 37 | + EnterpriseUser.to_schema() |
| 38 | + ] |
| 39 | + |
| 40 | + # SCIMProvider register_schema requires meta object to be set |
| 41 | + for schema in schemas: |
| 42 | + schema.meta = Meta(resource_type="Schema") |
| 43 | + |
| 44 | + return schemas |
| 45 | + |
| 46 | +def get_resource_types(): |
| 47 | + resource_types = [ |
| 48 | + ResourceType.from_resource(User[Union[EnterpriseUser, OtherExtension]]), |
| 49 | + ResourceType.from_resource(Group) |
| 50 | + ] |
| 51 | + |
| 52 | + # SCIMProvider register_resource_type requires meta object to be set |
| 53 | + for resource_type in resource_types: |
| 54 | + resource_type.meta = Meta(resource_type="ResourceType") |
| 55 | + |
| 56 | + return resource_types |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture(scope="session") |
| 60 | +def server(): |
| 61 | + backend = InMemoryBackend() |
| 62 | + provider = SCIMProvider(backend) |
| 63 | + for schema in get_schemas(): |
| 64 | + provider.register_schema(schema) |
| 65 | + for resource_type in get_resource_types(): |
| 66 | + provider.register_resource_type(resource_type) |
| 67 | + |
| 68 | + host = "localhost" |
| 69 | + port = portpicker.pick_unused_port() |
| 70 | + httpd = wsgiref.simple_server.make_server(host, port, provider) |
| 71 | + |
| 72 | + server_thread = threading.Thread(target=httpd.serve_forever) |
| 73 | + server_thread.start() |
| 74 | + try: |
| 75 | + yield host, port |
| 76 | + finally: |
| 77 | + httpd.shutdown() |
| 78 | + server_thread.join() |
| 79 | + |
| 80 | +def test_discovery_resource_types_multiple_extensions(server): |
| 81 | + host, port = server |
| 82 | + client = Client(base_url=f"http://{host}:{port}") |
| 83 | + scim_client = SyncSCIMClient(client) |
| 84 | + |
| 85 | + scim_client.discover() |
| 86 | + assert scim_client.get_resource_model("User") |
| 87 | + assert scim_client.get_resource_model("Group") |
0 commit comments