microblog.pub/tests/test_admin.py

28 lines
862 B
Python
Raw Permalink Normal View History

2022-06-22 21:15:07 +02:00
import typing
import starlette
2022-06-22 20:11:22 +02:00
from fastapi.testclient import TestClient
from app.main import app
2022-06-22 21:15:07 +02:00
def test_admin_endpoints_are_authenticated(client: TestClient) -> None:
2022-06-22 20:11:22 +02:00
routes_tested = []
for route in app.routes:
2022-06-22 21:15:07 +02:00
route = typing.cast(starlette.routing.Route, route)
2022-06-22 20:11:22 +02:00
if not route.path.startswith("/admin") or route.path == "/admin/login":
continue
2022-06-22 21:15:07 +02:00
for method in route.methods: # type: ignore
2022-06-22 20:11:22 +02: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 11:07:36 +02:00
assert resp.headers.get("Location", "").startswith(
"http://testserver/admin/login"
)
2022-06-22 20:11:22 +02:00
routes_tested.append((method, route.path))
assert len(routes_tested) > 0