microblog.pub/tests/test_admin.py

28 lines
862 B
Python
Raw Permalink Normal View History

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