|
| 1 | +import httpx2 |
| 2 | +import pytest |
| 3 | + |
| 4 | +from scim2_server.backend import InMemoryBackend |
| 5 | +from scim2_server.provider import SCIMApplication |
| 6 | +from scim2_server.tenants import TenantDispatcher |
| 7 | + |
| 8 | +BASE_URL = "https://scim.example.com" |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def factory(scim_provider): |
| 13 | + return lambda tenant: SCIMApplication(InMemoryBackend(), scim_provider) |
| 14 | + |
| 15 | + |
| 16 | +def make_client(dispatcher, script_name=""): |
| 17 | + transport = httpx2.WSGITransport(app=dispatcher, script_name=script_name) |
| 18 | + return httpx2.Client(transport=transport, base_url=BASE_URL) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def dispatcher(factory): |
| 23 | + return TenantDispatcher(factory, dynamic=True) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def client(dispatcher): |
| 28 | + with make_client(dispatcher) as client: |
| 29 | + yield client |
| 30 | + |
| 31 | + |
| 32 | +def test_resources_are_isolated_between_tenants(client, fake_user_data): |
| 33 | + """A resource created in a tenant is not visible from another tenant.""" |
| 34 | + user_id = client.post("/a/v2/Users", json=fake_user_data[0]).json()["id"] |
| 35 | + |
| 36 | + assert client.get(f"/a/v2/Users/{user_id}").status_code == 200 |
| 37 | + assert client.get(f"/b/v2/Users/{user_id}").status_code == 404 |
| 38 | + assert client.get("/a/v2/Users").json()["totalResults"] == 1 |
| 39 | + assert client.get("/b/v2/Users").json()["totalResults"] == 0 |
| 40 | + |
| 41 | + |
| 42 | +def test_same_user_name_in_two_tenants(client, fake_user_data): |
| 43 | + """Uniqueness only considers the resources of the tenant.""" |
| 44 | + assert client.post("/a/v2/Users", json=fake_user_data[0]).status_code == 201 |
| 45 | + assert client.post("/b/v2/Users", json=fake_user_data[0]).status_code == 201 |
| 46 | + assert client.post("/a/v2/Users", json=fake_user_data[0]).status_code == 409 |
| 47 | + |
| 48 | + |
| 49 | +def test_location_includes_the_tenant(client, fake_user_data): |
| 50 | + """The location of a resource starts with the prefix of its tenant.""" |
| 51 | + r = client.post("/a/v2/Users", json=fake_user_data[0]) |
| 52 | + location = f"{BASE_URL}/a/v2/Users/{r.json()['id']}" |
| 53 | + assert r.headers["Location"] == location |
| 54 | + assert r.json()["meta"]["location"] == location |
| 55 | + assert client.get("/a/Users").json()["Resources"][0]["meta"]["location"] == ( |
| 56 | + location |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_bulk_location_includes_the_tenant(client, fake_user_data): |
| 61 | + """The location of a bulk operation starts with the prefix of its tenant.""" |
| 62 | + r = client.post( |
| 63 | + "/a/v2/Bulk", |
| 64 | + json={ |
| 65 | + "schemas": ["urn:ietf:params:scim:api:messages:2.0:BulkRequest"], |
| 66 | + "Operations": [ |
| 67 | + { |
| 68 | + "method": "POST", |
| 69 | + "path": "/Users", |
| 70 | + "bulkId": "u", |
| 71 | + "data": fake_user_data[0], |
| 72 | + } |
| 73 | + ], |
| 74 | + }, |
| 75 | + ) |
| 76 | + assert r.json()["Operations"][0]["location"].startswith(f"{BASE_URL}/a/v2/Users/") |
| 77 | + |
| 78 | + |
| 79 | +def test_discovery_is_served_in_every_tenant(client): |
| 80 | + """The discovery endpoints are available in each tenant, under its prefix.""" |
| 81 | + r = client.get("/a/v2/ServiceProviderConfig") |
| 82 | + assert r.status_code == 200 |
| 83 | + assert r.json()["meta"]["location"] == f"{BASE_URL}/a/v2/ServiceProviderConfig" |
| 84 | + |
| 85 | + |
| 86 | +def test_tenant_under_a_mount_prefix(dispatcher, fake_user_data): |
| 87 | + """The tenant comes after the prefix the dispatcher is mounted under.""" |
| 88 | + with make_client(dispatcher, script_name="/scim") as client: |
| 89 | + r = client.post("/a/v2/Users", json=fake_user_data[0]) |
| 90 | + assert r.json()["meta"]["location"].startswith(f"{BASE_URL}/scim/a/v2/Users/") |
| 91 | + |
| 92 | + |
| 93 | +def test_dynamic_tenant_is_created_once(client, dispatcher): |
| 94 | + """The first request to an unknown tenant creates it, later ones reuse it.""" |
| 95 | + assert dispatcher.applications == {} |
| 96 | + client.get("/a/v2/Users") |
| 97 | + application = dispatcher.applications["a"] |
| 98 | + client.get("/a/v2/Users") |
| 99 | + assert dispatcher.applications == {"a": application} |
| 100 | + |
| 101 | + |
| 102 | +def test_static_tenants(factory, fake_user_data): |
| 103 | + """Without dynamic tenants, only the declared tenants exist.""" |
| 104 | + dispatcher = TenantDispatcher(factory, tenants=["a"]) |
| 105 | + with make_client(dispatcher) as client: |
| 106 | + assert client.post("/a/v2/Users", json=fake_user_data[0]).status_code == 201 |
| 107 | + r = client.get("/b/v2/Users") |
| 108 | + assert r.status_code == 404 |
| 109 | + assert r.headers["Content-Type"] == "application/scim+json" |
| 110 | + assert r.json() == { |
| 111 | + "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"], |
| 112 | + "status": "404", |
| 113 | + "detail": "Unknown tenant", |
| 114 | + } |
| 115 | + assert list(dispatcher.applications) == ["a"] |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("path", ["/", "/v2/Users", "//Users"]) |
| 119 | +def test_request_without_tenant(client, dispatcher, path): |
| 120 | + """A request whose path has no valid tenant gets a 404 and creates nothing.""" |
| 121 | + r = client.get(path) |
| 122 | + assert r.status_code == 404 |
| 123 | + assert r.json()["detail"] == "Unknown tenant" |
| 124 | + assert dispatcher.applications == {} |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize("tenant", ["", "v2", "a/b"]) |
| 128 | +def test_invalid_static_tenant(factory, tenant): |
| 129 | + """A declared tenant must be a single path segment other than the version.""" |
| 130 | + with pytest.raises(ValueError, match="Invalid tenant name"): |
| 131 | + TenantDispatcher(factory, tenants=[tenant]) |
| 132 | + |
| 133 | + |
| 134 | +def test_tenant_from_a_header(factory, fake_user_data): |
| 135 | + """A subclass can read the tenant from a header and keep the path unchanged.""" |
| 136 | + |
| 137 | + class HeaderTenantDispatcher(TenantDispatcher): |
| 138 | + def select_tenant(self, environ): |
| 139 | + return environ.get("HTTP_X_TENANT") |
| 140 | + |
| 141 | + dispatcher = HeaderTenantDispatcher(factory, dynamic=True) |
| 142 | + with make_client(dispatcher) as client: |
| 143 | + r = client.post("/v2/Users", json=fake_user_data[0], headers={"X-Tenant": "a"}) |
| 144 | + assert r.json()["meta"]["location"].startswith(f"{BASE_URL}/v2/Users/") |
| 145 | + assert ( |
| 146 | + client.get("/v2/Users", headers={"X-Tenant": "b"}).json()["totalResults"] |
| 147 | + == 0 |
| 148 | + ) |
| 149 | + assert client.get("/v2/Users").status_code == 404 |
0 commit comments