tests: dynamic test using new fixture

This commit is contained in:
Chris Milne 2026-06-24 10:10:29 +01:00
parent 1e438244aa
commit ed01e2515f

View file

@ -1,11 +1,12 @@
""" """
[GET]/user/self/claims is not tested because it requires OIDC authentication. [GET]/user/self/claims is not tested because it requires OIDC authentication.
[DELETE/user/ is not tested because the testing client cannot attach a body to a delete request. [DELETE]/user/ is not tested because the testing client cannot attach a body to a delete request.
""" """
from typing import Any
import pytest import pytest
from httpx import AsyncClient from httpx import AsyncClient
from fastapi.routing import APIRoute, iter_route_contexts
from .conftest import generate_query_and_status from .conftest import generate_query_and_status
@ -153,9 +154,13 @@ async def test_get_self_orgs_success(default_client: AsyncClient):
@pytest.mark.anyio @pytest.mark.anyio
async def test_get_self_orgs_dynamic(default_client: AsyncClient): async def test_get_self_orgs_dynamic(
default_client: AsyncClient, route_data: dict[str, dict[str, Any]]
):
method = "GET" method = "GET"
path = "/user/self/orgs" path = "/user/self/orgs"
auth_level = "User"
query = ""
expected_data = { expected_data = {
"organisations": [ "organisations": [
{ {
@ -178,24 +183,19 @@ async def test_get_self_orgs_dynamic(default_client: AsyncClient):
] ]
} }
resp = await default_client.get(path) # req_func = getattr(default_client, method); resp = await req_func(url=path, json=body)
resp = await default_client.get(f"{path}{query}")
contexts = list(iter_route_contexts(default_client._transport.app.routes)) # ty:ignore[unresolved-attribute] route = route_data.get(f"{method.upper()}{path}")
assert route is not None
route = next( assert route["auth_level"] == auth_level
route.route
for route in contexts
if isinstance(route.route, APIRoute)
and path in route.route.path
and isinstance(route.methods, set)
and method in route.methods
)
assert resp.status_code == route.status_code assert resp.status_code == route["status_code"]
if route.status_code == 204: if route["status_code"] == 204:
return return
expected_response_schema = route.response_model expected_response_schema = route["response_model"]
data = resp.json() data = resp.json()
response_model = expected_response_schema(**data) response_model = expected_response_schema(**data)