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