ila-microblog.pub/tests/test_admin.py

28 wiersze
862 B
Python

2022-06-22 19:15:07 +00:00
import typing
import starlette
2022-06-22 18:11:22 +00:00
from fastapi.testclient import TestClient
from app.main import app
2022-06-22 19:15:07 +00:00
def test_admin_endpoints_are_authenticated(client: TestClient) -> None:
2022-06-22 18:11:22 +00:00
routes_tested = []
for route in app.routes:
2022-06-22 19:15:07 +00:00
route = typing.cast(starlette.routing.Route, route)
2022-06-22 18:11:22 +00:00
if not route.path.startswith("/admin") or route.path == "/admin/login":
continue
2022-06-22 19:15:07 +00:00
for method in route.methods: # type: ignore
2022-06-22 18:11:22 +00:00
resp = client.request(method, route.path)
# Admin routes should redirect to the login page
assert resp.status_code == 302, f"{method} {route.path} is unauthenticated"
2022-07-10 09:07:36 +00:00
assert resp.headers.get("Location", "").startswith(
"http://testserver/admin/login"
)
2022-06-22 18:11:22 +00:00
routes_tested.append((method, route.path))
assert len(routes_tested) > 0