tests: route_data fixture
Gets the actual auth level, expected status code, and expected response model from the route definitions. This can be used to make happy-path tests a standard shape, and with better coverage. Auth tests can be directly incorporated into this test shape.
This commit is contained in:
parent
7dad2e920e
commit
1e438244aa
1 changed files with 52 additions and 0 deletions
|
|
@ -256,6 +256,58 @@ def generate_body_and_status(params: dict[str, str]) -> list[tuple[dict, int]]:
|
|||
return body_and_status
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def route_data():
|
||||
routes: dict[str, dict] = {}
|
||||
|
||||
contexts = list(iter_route_contexts(app.routes))
|
||||
|
||||
for route in contexts:
|
||||
if not route.methods:
|
||||
continue
|
||||
if not isinstance(route.route, APIRoute):
|
||||
continue
|
||||
|
||||
dep_func_names = set()
|
||||
|
||||
unchecked = [route.route.dependant]
|
||||
while unchecked:
|
||||
dependant = unchecked.pop(0)
|
||||
ck = dependant.cache_key[0]
|
||||
if hasattr(ck, "__name__"):
|
||||
dep_func_names.add(ck.__name__)
|
||||
unchecked += [
|
||||
dep for dep in dependant.dependencies if isinstance(dep, Dependant)
|
||||
]
|
||||
|
||||
auth_level = None
|
||||
if "get_current_user" in dep_func_names:
|
||||
auth_level = "User"
|
||||
if (
|
||||
"org_body_root_claims" in dep_func_names
|
||||
or "org_query_root_claims" in dep_func_names
|
||||
):
|
||||
auth_level = "Root User"
|
||||
if "user_model_super_admin" in dep_func_names:
|
||||
auth_level = "Super Admin"
|
||||
if "valid_service_key" in dep_func_names:
|
||||
auth_level = "API Key"
|
||||
|
||||
for method in route.methods:
|
||||
if method in {"HEAD", "OPTIONS"}:
|
||||
continue
|
||||
|
||||
route_key = f"{method.upper()}{route.route.path}"
|
||||
|
||||
routes[route_key] = {
|
||||
"status_code": route.route.status_code,
|
||||
"response_model": route.route.response_model,
|
||||
"auth_level": auth_level,
|
||||
}
|
||||
|
||||
return routes
|
||||
|
||||
|
||||
def get_testable_routes():
|
||||
routes = []
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue