Refactor blocklist schedule management into service

This commit is contained in:
2026-04-14 15:25:36 +02:00
parent 58bb769a35
commit b70dc6fa7a
6 changed files with 161 additions and 75 deletions

View File

@@ -361,7 +361,7 @@ class TestGetSchedule:
async def test_schedule_returns_200(self, bl_client: AsyncClient) -> None:
"""GET /api/blocklists/schedule returns 200."""
with patch(
"app.routers.blocklist.blocklist_service.get_schedule_info",
"app.routers.blocklist.blocklist_service.get_schedule_info_with_runtime",
new=AsyncMock(return_value=_make_schedule_info()),
):
resp = await bl_client.get("/api/blocklists/schedule")
@@ -370,7 +370,7 @@ class TestGetSchedule:
async def test_schedule_response_has_config(self, bl_client: AsyncClient) -> None:
"""Schedule response includes the config sub-object."""
with patch(
"app.routers.blocklist.blocklist_service.get_schedule_info",
"app.routers.blocklist.blocklist_service.get_schedule_info_with_runtime",
new=AsyncMock(return_value=_make_schedule_info()),
):
resp = await bl_client.get("/api/blocklists/schedule")
@@ -396,7 +396,7 @@ class TestGetSchedule:
last_run_errors=True,
)
with patch(
"app.routers.blocklist.blocklist_service.get_schedule_info",
"app.routers.blocklist.blocklist_service.get_schedule_info_with_runtime",
new=AsyncMock(return_value=info_with_errors),
):
resp = await bl_client.get("/api/blocklists/schedule")
@@ -425,13 +425,8 @@ class TestUpdateSchedule:
last_run_at=None,
)
with patch(
"app.routers.blocklist.blocklist_service.set_schedule",
new=AsyncMock(),
), patch(
"app.routers.blocklist.blocklist_service.get_schedule_info",
"app.routers.blocklist.blocklist_service.update_schedule",
new=AsyncMock(return_value=new_info),
), patch(
"app.routers.blocklist.blocklist_import_task.reschedule",
):
resp = await bl_client.put(
"/api/blocklists/schedule",

View File

@@ -9,7 +9,11 @@ import aiosqlite
import pytest
from app.db import init_db
from app.models.blocklist import BlocklistSource, ScheduleConfig, ScheduleFrequency
from app.models.blocklist import (
BlocklistSource,
ScheduleConfig,
ScheduleFrequency,
)
from app.services import blocklist_service
# ---------------------------------------------------------------------------
@@ -346,6 +350,47 @@ class TestSchedule:
info = await blocklist_service.get_schedule_info(db, None)
assert info.last_run_errors is True
async def test_get_schedule_info_with_runtime_uses_scheduler_metadata(
self, db: aiosqlite.Connection
) -> None:
"""get_schedule_info_with_runtime derives next_run_at from the scheduler."""
next_run = MagicMock()
next_run.isoformat.return_value = "2099-01-01T00:00:00+00:00"
scheduler = MagicMock()
scheduler.get_job.return_value = MagicMock(next_run_time=next_run)
info = await blocklist_service.get_schedule_info_with_runtime(db, scheduler)
assert info.next_run_at == "2099-01-01T00:00:00+00:00"
async def test_update_schedule_persists_and_schedules_job(
self, db: aiosqlite.Connection
) -> None:
"""update_schedule must persist the config and schedule a job."""
settings = MagicMock(
fail2ban_socket="/var/run/fail2ban/fail2ban.sock",
database_path=":memory:",
)
http_session = MagicMock()
scheduler = MagicMock()
scheduler.get_job.return_value = None
config = ScheduleConfig(
frequency=ScheduleFrequency.daily,
hour=4,
minute=15,
)
info = await blocklist_service.update_schedule(
db,
scheduler,
http_session,
settings,
config,
)
assert info.config.frequency == ScheduleFrequency.daily
scheduler.add_job.assert_called_once()
# ---------------------------------------------------------------------------
# Geo prewarm cache filtering