Add manual sync buttons with batch limit, nginx timeouts for long syncs, message retry endpoint, and fix worker to skip disabled accounts and refresh schedules from the database every 2 minutes.
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
"""Tests for worker scheduler — disabled accounts must not sync automatically."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
|
|
|
from forwarder.worker import scheduler as sched
|
|
|
|
|
|
class TestSyncJobGuard:
|
|
def test_skips_when_account_disabled(self):
|
|
db = MagicMock()
|
|
account = MagicMock()
|
|
account.enabled = False
|
|
account.name = "Test Co"
|
|
db.get.return_value = account
|
|
|
|
with patch.object(sched, "get_session_factory") as factory:
|
|
factory.return_value = MagicMock(return_value=db)
|
|
with patch.object(sched, "sync_account") as sync_account:
|
|
sched._sync_job(42)
|
|
|
|
sync_account.assert_not_called()
|
|
|
|
def test_skips_when_account_missing(self):
|
|
db = MagicMock()
|
|
db.get.return_value = None
|
|
|
|
with patch.object(sched, "get_session_factory") as factory:
|
|
factory.return_value = MagicMock(return_value=db)
|
|
with patch.object(sched, "sync_account") as sync_account:
|
|
sched._sync_job(99)
|
|
|
|
sync_account.assert_not_called()
|
|
|
|
def test_runs_when_account_enabled(self):
|
|
db = MagicMock()
|
|
account = MagicMock()
|
|
account.enabled = True
|
|
account.name = "Active Co"
|
|
db.get.return_value = account
|
|
|
|
with patch.object(sched, "get_session_factory") as factory:
|
|
factory.return_value = MagicMock(return_value=db)
|
|
with patch.object(sched, "sync_account") as sync_account:
|
|
sched._sync_job(1)
|
|
|
|
sync_account.assert_called_once_with(db, 1, dry_run=False)
|
|
|
|
|
|
class TestRefreshSchedules:
|
|
def test_removes_job_for_disabled_account(self):
|
|
scheduler = BlockingScheduler()
|
|
scheduler.add_job(lambda: None, "interval", seconds=3600, id="sync-account-7")
|
|
|
|
account = MagicMock()
|
|
account.id = 1
|
|
account.enabled = True
|
|
account.name = "Active"
|
|
account.sync_interval_minutes = 15
|
|
|
|
db = MagicMock()
|
|
db.query.return_value.filter.return_value.all.return_value = [account]
|
|
|
|
with patch.object(sched, "get_session_factory") as factory:
|
|
factory.return_value = MagicMock(return_value=db)
|
|
sched._refresh_account_schedules(scheduler, default_interval=15)
|
|
|
|
job_ids = {j.id for j in scheduler.get_jobs()}
|
|
assert "sync-account-1" in job_ids
|
|
assert "sync-account-7" not in job_ids
|