Extract fail2ban restart orchestration into jail_service
This commit is contained in:
@@ -406,19 +406,9 @@ class TestRestartFail2ban:
|
||||
|
||||
async def test_204_on_success(self, config_client: AsyncClient) -> None:
|
||||
"""POST /api/config/restart returns 204 when fail2ban restarts cleanly."""
|
||||
with (
|
||||
patch(
|
||||
"app.routers.config_misc.jail_service.restart",
|
||||
AsyncMock(return_value=None),
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.start_daemon",
|
||||
AsyncMock(return_value=True),
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.wait_for_fail2ban",
|
||||
AsyncMock(return_value=True),
|
||||
),
|
||||
with patch(
|
||||
"app.routers.config_misc.jail_service.restart_daemon",
|
||||
AsyncMock(return_value=True),
|
||||
):
|
||||
resp = await config_client.post("/api/config/restart")
|
||||
|
||||
@@ -426,19 +416,9 @@ class TestRestartFail2ban:
|
||||
|
||||
async def test_503_when_fail2ban_does_not_come_back(self, config_client: AsyncClient) -> None:
|
||||
"""POST /api/config/restart returns 503 when fail2ban does not come back online."""
|
||||
with (
|
||||
patch(
|
||||
"app.routers.config_misc.jail_service.restart",
|
||||
AsyncMock(return_value=None),
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.start_daemon",
|
||||
AsyncMock(return_value=True),
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.wait_for_fail2ban",
|
||||
AsyncMock(return_value=False),
|
||||
),
|
||||
with patch(
|
||||
"app.routers.config_misc.jail_service.restart_daemon",
|
||||
AsyncMock(return_value=False),
|
||||
):
|
||||
resp = await config_client.post("/api/config/restart")
|
||||
|
||||
@@ -449,7 +429,7 @@ class TestRestartFail2ban:
|
||||
from app.services.jail_service import JailOperationError
|
||||
|
||||
with patch(
|
||||
"app.routers.config_misc.jail_service.restart",
|
||||
"app.routers.config_misc.jail_service.restart_daemon",
|
||||
AsyncMock(side_effect=JailOperationError("stop failed")),
|
||||
):
|
||||
resp = await config_client.post("/api/config/restart")
|
||||
@@ -461,33 +441,24 @@ class TestRestartFail2ban:
|
||||
from app.exceptions import Fail2BanConnectionError
|
||||
|
||||
with patch(
|
||||
"app.routers.config_misc.jail_service.restart",
|
||||
"app.routers.config_misc.jail_service.restart_daemon",
|
||||
AsyncMock(side_effect=Fail2BanConnectionError("no socket", "/fake.sock")),
|
||||
):
|
||||
resp = await config_client.post("/api/config/restart")
|
||||
|
||||
assert resp.status_code == 502
|
||||
|
||||
async def test_start_daemon_called_after_stop(self, config_client: AsyncClient) -> None:
|
||||
"""start_daemon is called after a successful stop."""
|
||||
mock_start = AsyncMock(return_value=True)
|
||||
with (
|
||||
patch(
|
||||
"app.routers.config_misc.jail_service.restart",
|
||||
AsyncMock(return_value=None),
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.start_daemon",
|
||||
mock_start,
|
||||
),
|
||||
patch(
|
||||
"app.routers.config_misc.wait_for_fail2ban",
|
||||
AsyncMock(return_value=True),
|
||||
),
|
||||
async def test_service_restart_daemon_called(self, config_client: AsyncClient) -> None:
|
||||
"""The router delegates restart orchestration to jail_service.restart_daemon."""
|
||||
mock_restart = AsyncMock(return_value=True)
|
||||
with patch(
|
||||
"app.routers.config_misc.jail_service.restart_daemon",
|
||||
mock_restart,
|
||||
):
|
||||
await config_client.post("/api/config/restart")
|
||||
resp = await config_client.post("/api/config/restart")
|
||||
|
||||
mock_start.assert_awaited_once()
|
||||
assert resp.status_code == 204
|
||||
mock_restart.assert_awaited_once()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user