Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /elisiariocouto_leggen /tests /unit /test_sync_notifications.py
| """Tests for sync service notification functionality.""" | |
| from unittest.mock import patch | |
| import pytest | |
| from leggen.services.sync_service import SyncService | |
| class TestSyncNotifications: | |
| """Test sync service notification functionality.""" | |
| async def test_sync_failure_sends_notification(self, mock_db_path): | |
| """Test that sync failures trigger notifications.""" | |
| sync_service = SyncService() | |
| # Mock the dependencies | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_details" | |
| ) as mock_get_details, | |
| patch.object( | |
| sync_service.notifications, "send_sync_failure_notification" | |
| ) as mock_send_notification, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| # Setup: One session with one account that will fail | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-123", | |
| "aspsp_name": "TEST_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "accounts": ["account-1"], | |
| } | |
| ] | |
| # Make account details fail | |
| mock_get_details.side_effect = Exception("API Error") | |
| # Execute: Run sync which should fail for the account | |
| await sync_service.sync_all_accounts() | |
| # Assert: Notification should be sent for the failed account | |
| mock_send_notification.assert_called_once() | |
| call_args = mock_send_notification.call_args[0][0] | |
| assert call_args["account_id"] == "account-1" | |
| assert "API Error" in call_args["error"] | |
| assert call_args["type"] == "account_sync_failure" | |
| async def test_expired_session_sends_notification(self, mock_db_path): | |
| """Test that expired sessions trigger expiry notifications.""" | |
| sync_service = SyncService() | |
| # Mock the dependencies | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.notifications, "send_expiry_notification" | |
| ) as mock_send_expiry, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| # Setup: One expired session (valid_until in the past) | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-expired", | |
| "aspsp_name": "EXPIRED_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "valid_until": "2020-01-01T00:00:00+00:00", | |
| "accounts": [], | |
| } | |
| ] | |
| # Execute: Run sync | |
| await sync_service.sync_all_accounts() | |
| # Assert: Expiry notification should be sent | |
| mock_send_expiry.assert_called_once() | |
| call_args = mock_send_expiry.call_args[0][0] | |
| assert call_args["session_id"] == "sess-expired" | |
| assert call_args["bank"] == "EXPIRED_BANK" | |
| assert call_args["status"] == "expired" | |
| assert call_args["days_left"] == 0 | |
| async def test_multiple_failures_send_multiple_notifications(self, mock_db_path): | |
| """Test that multiple account failures send multiple notifications.""" | |
| sync_service = SyncService() | |
| # Mock the dependencies | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_details" | |
| ) as mock_get_details, | |
| patch.object( | |
| sync_service.notifications, "send_sync_failure_notification" | |
| ) as mock_send_notification, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| # Setup: One session with two accounts that will fail | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-123", | |
| "aspsp_name": "TEST_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "accounts": ["account-1", "account-2"], | |
| } | |
| ] | |
| # Make all account details fail | |
| mock_get_details.side_effect = Exception("API Error") | |
| # Execute: Run sync | |
| await sync_service.sync_all_accounts() | |
| # Assert: Two notifications should be sent | |
| assert mock_send_notification.call_count == 2 | |
| async def test_successful_sync_no_failure_notification(self, mock_db_path): | |
| """Test that successful syncs don't send failure notifications.""" | |
| sync_service = SyncService() | |
| # Mock the dependencies | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_details" | |
| ) as mock_get_details, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_balances" | |
| ) as mock_get_balances, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_transactions" | |
| ) as mock_get_transactions, | |
| patch.object( | |
| sync_service.notifications, "send_sync_failure_notification" | |
| ) as mock_send_notification, | |
| patch.object(sync_service.notifications, "send_transaction_notifications"), | |
| patch.object(sync_service.accounts, "persist"), | |
| patch.object(sync_service.balances, "persist"), | |
| patch( | |
| "leggen.services.sync_service.process_transactions", | |
| return_value=([], 0), | |
| ), | |
| patch.object(sync_service.transactions, "persist", return_value=([], 0)), | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| # Setup: One session with one account that succeeds | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-123", | |
| "aspsp_name": "TEST_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "accounts": ["account-1"], | |
| "created_at": "2025-09-01T00:00:00Z", | |
| } | |
| ] | |
| mock_get_details.return_value = { | |
| "uid": "account-1", | |
| "account_id": {"iban": "TEST123"}, | |
| "name": "Test Account", | |
| "currency": "EUR", | |
| } | |
| mock_get_balances.return_value = { | |
| "balances": [{"balance_amount": {"amount": "100", "currency": "EUR"}}] | |
| } | |
| mock_get_transactions.return_value = {"transactions": []} | |
| # Execute: Run sync | |
| await sync_service.sync_all_accounts() | |
| # Assert: No failure notification should be sent | |
| mock_send_notification.assert_not_called() | |
| async def test_notification_failure_does_not_stop_sync(self, mock_db_path): | |
| """Test that notification failures don't stop the sync process.""" | |
| sync_service = SyncService() | |
| # Mock the dependencies | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.enablebanking, "get_account_details" | |
| ) as mock_get_details, | |
| patch.object( | |
| sync_service.notifications, "send_sync_failure_notification" | |
| ) as mock_send_notification, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| # Setup: One session with one account that will fail | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-123", | |
| "aspsp_name": "TEST_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "accounts": ["account-1"], | |
| } | |
| ] | |
| # Make account details fail | |
| mock_get_details.side_effect = Exception("API Error") | |
| # Make notification method fail | |
| mock_send_notification.side_effect = Exception("Notification Error") | |
| # Execute: Run sync - should not raise exception from notification | |
| result = await sync_service.sync_all_accounts() | |
| # The sync should complete with errors but not crash from notifications | |
| assert result.success is False | |
| assert len(result.errors) > 0 | |
| assert "API Error" in result.errors[0] | |
| async def test_expired_session_notifies_only_once(self, mock_db_path): | |
| """Expired sessions must not re-notify on every sync.""" | |
| sync_service = SyncService() | |
| with ( | |
| patch.object( | |
| sync_service.session_repo, "get_sessions" | |
| ) as mock_get_sessions, | |
| patch.object( | |
| sync_service.notifications, "send_expiry_notification" | |
| ) as mock_send_expiry, | |
| patch.object(sync_service.sync, "persist", return_value=1), | |
| ): | |
| mock_get_sessions.return_value = [ | |
| { | |
| "session_id": "sess-expired-dedup", | |
| "aspsp_name": "EXPIRED_BANK", | |
| "aspsp_country": "PT", | |
| "status": "active", | |
| "valid_until": "2020-01-01T00:00:00+00:00", | |
| "accounts": [], | |
| } | |
| ] | |
| await sync_service.sync_all_accounts() | |
| await sync_service.sync_all_accounts() | |
| mock_send_expiry.assert_called_once() | |
Xet Storage Details
- Size:
- 10.3 kB
- Xet hash:
- ba57911b6a4c767109d3750b272cceb03f68b1c40ed694474a2d30effcfbbf1b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.