Standardize API response envelopes: use items for collection responses and update tests

This commit is contained in:
2026-04-28 20:48:00 +02:00
parent 1c673d600c
commit b27765928a
23 changed files with 186 additions and 104 deletions

View File

@@ -34,7 +34,7 @@ async def jails_client(tmp_path: Path) -> AsyncClient: # type: ignore[misc]
settings = Settings(
database_path=str(tmp_path / "jails_test.db"),
fail2ban_socket="/tmp/fake.sock",
session_secret="test-jails-secret",
session_secret="test-jails-secret-0000000000000000000000",
session_duration_minutes=60,
timezone="UTC",
log_level="debug",
@@ -108,7 +108,9 @@ def _detail(name: str = "sshd") -> JailDetailResponse:
currently_failed=1,
total_failed=50,
),
)
),
ignore_list=["127.0.0.1"],
ignore_self=False,
)
@@ -122,7 +124,7 @@ class TestGetJails:
async def test_200_when_authenticated(self, jails_client: AsyncClient) -> None:
"""GET /api/jails returns 200 with a JailListResponse."""
mock_response = JailListResponse(jails=[_summary()], total=1)
mock_response = JailListResponse(items=[_summary()], total=1)
with patch(
"app.routers.jails.jail_service.list_jails",
AsyncMock(return_value=mock_response),
@@ -132,7 +134,7 @@ class TestGetJails:
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
assert data["jails"][0]["name"] == "sshd"
assert data["items"][0]["name"] == "sshd"
async def test_401_when_unauthenticated(self, jails_client: AsyncClient) -> None:
"""GET /api/jails returns 401 without a session cookie."""
@@ -144,14 +146,14 @@ class TestGetJails:
async def test_response_shape(self, jails_client: AsyncClient) -> None:
"""GET /api/jails response contains expected fields."""
mock_response = JailListResponse(jails=[_summary()], total=1)
mock_response = JailListResponse(items=[_summary()], total=1)
with patch(
"app.routers.jails.jail_service.list_jails",
AsyncMock(return_value=mock_response),
):
resp = await jails_client.get("/api/jails")
jail = resp.json()["jails"][0]
jail = resp.json()["items"][0]
assert "name" in jail
assert "enabled" in jail
assert "running" in jail
@@ -359,7 +361,7 @@ class TestIgnoreIpEndpoints:
"""Tests for ignore-list management endpoints."""
async def test_get_ignore_list(self, jails_client: AsyncClient) -> None:
"""GET /api/jails/sshd/ignoreip returns 200 with a list."""
"""GET /api/jails/sshd/ignoreip returns 200 with a wrapped response."""
with patch(
"app.routers.jails.jail_service.get_ignore_list",
AsyncMock(return_value=["127.0.0.1"]),
@@ -367,7 +369,7 @@ class TestIgnoreIpEndpoints:
resp = await jails_client.get("/api/jails/sshd/ignoreip")
assert resp.status_code == 200
assert "127.0.0.1" in resp.json()
assert resp.json() == {"items": ["127.0.0.1"], "total": 1}
async def test_add_ignore_ip_returns_201(self, jails_client: AsyncClient) -> None:
"""POST /api/jails/sshd/ignoreip returns 201 on success."""