Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /elisiariocouto_leggen /tests /unit /test_api_sync.py
| """Tests for the sync API route and per-account sync filtering.""" | |
| import tomllib | |
| from datetime import UTC, datetime | |
| from pathlib import Path | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| from leggen.api.models.sync import SyncResult | |
| from leggen.background.scheduler import scheduler | |
| from leggen.repositories import AccountRepository | |
| from leggen.services.sync_service import SyncAlreadyRunningError, SyncService | |
| from leggen.utils.config import config as config_singleton | |
| from tests.conftest import reset_config_singleton | |
| def _sync_result() -> SyncResult: | |
| now = datetime.now(UTC) | |
| return SyncResult( | |
| success=True, | |
| accounts_processed=1, | |
| transactions_added=2, | |
| transactions_updated=0, | |
| balances_updated=1, | |
| duration_seconds=0.5, | |
| errors=[], | |
| started_at=now, | |
| completed_at=now, | |
| ) | |
| def mock_sync_service(): | |
| """Replace the scheduler's sync service with a mock for route tests.""" | |
| mock_service = MagicMock() | |
| mock_service.sync_all_accounts = AsyncMock(return_value=_sync_result()) | |
| scheduler.sync_service = mock_service | |
| yield mock_service | |
| scheduler._sync_service = None | |
| class TestSyncAPI: | |
| """Test POST /sync account_ids handling.""" | |
| def test_sync_all_accounts_when_no_body( | |
| self, fastapi_app, api_client, mock_db_path, mock_sync_service | |
| ): | |
| response = api_client.post("/api/v1/sync") | |
| assert response.status_code == 200 | |
| mock_sync_service.sync_all_accounts.assert_called_once_with( | |
| False, "api", account_ids=None | |
| ) | |
| def test_sync_with_account_ids_forwards_them( | |
| self, | |
| fastapi_app, | |
| api_client, | |
| mock_db_path, | |
| mock_sync_service, | |
| mock_account_repo, | |
| ): | |
| mock_account_repo.get_accounts.return_value = [{"id": "IBAN1"}] | |
| fastapi_app.dependency_overrides[AccountRepository] = lambda: mock_account_repo | |
| response = api_client.post("/api/v1/sync", json={"account_ids": ["IBAN1"]}) | |
| fastapi_app.dependency_overrides.clear() | |
| assert response.status_code == 200 | |
| mock_account_repo.get_accounts.assert_called_once_with(account_ids=["IBAN1"]) | |
| mock_sync_service.sync_all_accounts.assert_called_once_with( | |
| False, "api", account_ids=["IBAN1"] | |
| ) | |
| def test_sync_with_unknown_account_id_returns_404( | |
| self, | |
| fastapi_app, | |
| api_client, | |
| mock_db_path, | |
| mock_sync_service, | |
| mock_account_repo, | |
| ): | |
| mock_account_repo.get_accounts.return_value = [{"id": "IBAN1"}] | |
| fastapi_app.dependency_overrides[AccountRepository] = lambda: mock_account_repo | |
| response = api_client.post( | |
| "/api/v1/sync", json={"account_ids": ["IBAN1", "bogus"]} | |
| ) | |
| fastapi_app.dependency_overrides.clear() | |
| assert response.status_code == 404 | |
| assert "bogus" in response.json()["detail"] | |
| mock_sync_service.sync_all_accounts.assert_not_called() | |
| def test_sync_with_empty_account_ids_syncs_all( | |
| self, fastapi_app, api_client, mock_db_path, mock_sync_service | |
| ): | |
| response = api_client.post( | |
| "/api/v1/sync", json={"account_ids": [], "full_sync": True} | |
| ) | |
| assert response.status_code == 200 | |
| mock_sync_service.sync_all_accounts.assert_called_once_with( | |
| True, "api", account_ids=None | |
| ) | |
| def test_sync_already_running_returns_409( | |
| self, fastapi_app, api_client, mock_db_path, mock_sync_service | |
| ): | |
| mock_sync_service.sync_all_accounts.side_effect = SyncAlreadyRunningError( | |
| "Sync is already running" | |
| ) | |
| response = api_client.post("/api/v1/sync") | |
| assert response.status_code == 409 | |
| body = response.json() | |
| assert body["detail"] == "Sync is already running" | |
| assert body["code"] == "SYNC_ALREADY_RUNNING" | |
| def test_sync_failure_returns_sanitized_500( | |
| self, fastapi_app, mock_db_path, mock_sync_service, raw_api_client | |
| ): | |
| mock_sync_service.sync_all_accounts.side_effect = Exception( | |
| "sqlite3.OperationalError: /secret/path/leggen.db is locked" | |
| ) | |
| response = raw_api_client.post("/api/v1/sync") | |
| assert response.status_code == 500 | |
| assert response.json()["detail"] == "Internal server error." | |
| assert "/secret/path" not in response.text | |
| def restore_config(): | |
| """Load the canonical test config, snapshot the file, and leave the | |
| singleton clean afterwards. | |
| Other tests leave stale state on the config singleton, so reset before | |
| loading — and reset again on teardown (rather than restoring the | |
| possibly-stale snapshot) so later tests start clean too. | |
| """ | |
| reset_config_singleton() | |
| config_singleton.load_config() | |
| config_path = Path(config_singleton._config_path) | |
| original_bytes = config_path.read_bytes() | |
| yield config_singleton | |
| config_path.write_bytes(original_bytes) | |
| reset_config_singleton() | |
| class TestSyncScheduleAPI: | |
| """Test PUT /sync/schedule config handling.""" | |
| def test_update_sync_schedule_preserves_backup_schedule( | |
| self, api_client, restore_config | |
| ): | |
| """Updating the sync schedule must not reset scheduler.backup | |
| to defaults (regression: update_section replaced the whole section).""" | |
| custom_backup = {"enabled": False, "hour": 12, "minute": 30} | |
| scheduler_section = dict(restore_config.scheduler_config) | |
| scheduler_section["backup"] = custom_backup | |
| restore_config.update_section("scheduler", scheduler_section) | |
| response = api_client.put( | |
| "/api/v1/sync/schedule", json={"enabled": True, "hour": 5, "minute": 15} | |
| ) | |
| assert response.status_code == 200 | |
| assert response.json()["hour"] == 5 | |
| backup = restore_config.scheduler_config["backup"] | |
| assert backup["enabled"] is False | |
| assert backup["hour"] == 12 | |
| assert backup["minute"] == 30 | |
| # The surviving backup schedule must also be on disk | |
| with open(restore_config._config_path, "rb") as f: | |
| on_disk = tomllib.load(f) | |
| assert on_disk["scheduler"]["backup"] == custom_backup | |
| assert on_disk["scheduler"]["sync"]["hour"] == 5 | |
| class TestPerAccountSyncFilter: | |
| """Test SyncService.sync_all_accounts account_ids filtering.""" | |
| DETAILS = { | |
| "account-1": { | |
| "account_id": {"iban": "IBAN1"}, | |
| "name": "Account One", | |
| "currency": "EUR", | |
| }, | |
| "account-2": { | |
| "account_id": {"iban": "IBAN2"}, | |
| "name": "Account Two", | |
| "currency": "EUR", | |
| }, | |
| } | |
| def _session(self, accounts) -> dict: | |
| return { | |
| "session_id": "sess-123", | |
| "aspsp_name": "TEST_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "accounts": accounts, | |
| "created_at": "2025-09-01T00:00:00Z", | |
| } | |
| async def _run_sync(self, sync_service, sessions, account_ids): | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions", return_value=sessions | |
| ), | |
| patch.object( | |
| sync_service.enablebanking, "get_account_details" | |
| ) as mock_get_details, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_balances", return_value={} | |
| ), | |
| patch.object( | |
| sync_service.enablebanking, "get_account_transactions", return_value={} | |
| ), | |
| patch.object(sync_service.accounts, "persist") as mock_persist, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| mock_get_details.side_effect = lambda uid: self.DETAILS[uid] | |
| result = await sync_service.sync_all_accounts(account_ids=account_ids) | |
| return result, mock_get_details, mock_persist | |
| async def test_filters_by_db_account_id(self, mock_db_path): | |
| """Requesting a DB account ID (IBAN) syncs only that account and | |
| never touches the others (their IBAN is known from the session).""" | |
| sessions = [ | |
| self._session( | |
| [ | |
| {"uid": "account-1", "account_id": {"iban": "IBAN1"}}, | |
| {"uid": "account-2", "account_id": {"iban": "IBAN2"}}, | |
| ] | |
| ) | |
| ] | |
| result, mock_get_details, mock_persist = await self._run_sync( | |
| SyncService(), sessions, account_ids=["IBAN1"] | |
| ) | |
| assert result.accounts_processed == 1 | |
| mock_get_details.assert_called_once_with("account-1") | |
| mock_persist.assert_called_once() | |
| assert mock_persist.call_args.args[0]["id"] == "IBAN1" | |
| async def test_filters_by_enablebanking_uid(self, mock_db_path): | |
| """Requesting an EnableBanking UID also works.""" | |
| sessions = [ | |
| self._session( | |
| [ | |
| {"uid": "account-1", "account_id": {"iban": "IBAN1"}}, | |
| {"uid": "account-2", "account_id": {"iban": "IBAN2"}}, | |
| ] | |
| ) | |
| ] | |
| result, mock_get_details, mock_persist = await self._run_sync( | |
| SyncService(), sessions, account_ids=["account-2"] | |
| ) | |
| assert result.accounts_processed == 1 | |
| mock_get_details.assert_called_once_with("account-2") | |
| assert mock_persist.call_args.args[0]["id"] == "IBAN2" | |
| async def test_filters_when_session_lacks_iban(self, mock_db_path): | |
| """Accounts stored as plain UID strings (no local IBAN) resolve | |
| through account details: the requested one syncs, the rest skip.""" | |
| sessions = [self._session(["account-1", "account-2"])] | |
| result, mock_get_details, mock_persist = await self._run_sync( | |
| SyncService(), sessions, account_ids=["IBAN1"] | |
| ) | |
| # Both accounts need a details call to learn their IBAN, but only | |
| # the requested one is processed | |
| assert mock_get_details.call_count == 2 | |
| assert result.accounts_processed == 1 | |
| mock_persist.assert_called_once() | |
| assert mock_persist.call_args.args[0]["id"] == "IBAN1" | |
| async def test_no_filter_syncs_everything(self, mock_db_path): | |
| sessions = [ | |
| self._session( | |
| [ | |
| {"uid": "account-1", "account_id": {"iban": "IBAN1"}}, | |
| {"uid": "account-2", "account_id": {"iban": "IBAN2"}}, | |
| ] | |
| ) | |
| ] | |
| result, mock_get_details, mock_persist = await self._run_sync( | |
| SyncService(), sessions, account_ids=None | |
| ) | |
| assert result.accounts_processed == 2 | |
| assert mock_get_details.call_count == 2 | |
Xet Storage Details
- Size:
- 11 kB
- Xet hash:
- c13a070671e52623c5232ce42b57c56acd491fc44630dc233d5ece3c871bb945
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.