Open-source personal finance manager. Self-hosted, privacy-first.
"""Coverage-focused tests for app/services/connection_service.py.
Targets branches not exercised by test_connection_service.py:
- get_oauth_url / get_reauth_url (state storage + provider delegation)
- list_provider_institutions (normalization)
- _sync_holdings (create / archive / withdrawn paths)
- sync_connection: trigger_refresh outcomes, fuzzy-match merge, SessionExpired
and ProviderUserActionRequired error handling, status reset on generic error
- handle_oauth_callback: reconnect path + invalid-state guards
"""
import uuid
from datetime import date, datetime, timezone
from decimal import Decimal
from unittest.mock import AsyncMock, patch
import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.account import Account
from app.models.asset import Asset
from app.models.asset_group import AssetGroup
from app.models.bank_connection import BankConnection
from app.models.credit_card_bill import CreditCardBill
from app.models.transaction import Transaction
from app.models.user import User
from app.models.workspace import WorkspaceMember
from app.providers.base import (
AccountData,
ConnectionData,
HoldingData,
InstitutionData,
InstitutionListData,
ProviderNotConfiguredError,
ProviderUserActionRequired,
SessionExpiredError,
TransactionData,
)
from app.services.connection_service import (
_sync_holdings,
get_oauth_url,
get_reauth_url,
handle_oauth_callback,
list_provider_institutions,
sync_connection,
)
async def _make_connection(
session: AsyncSession, user_id, name="Bank", settings=None,
) -> BankConnection:
conn = BankConnection(
id=uuid.uuid4(), user_id=user_id, provider="test",
external_id=f"ext-{uuid.uuid4().hex[:8]}",
institution_name=name, credentials={"token": "fake"},
status="active", settings=settings,
last_sync_at=datetime.now(timezone.utc),
created_at=datetime.now(timezone.utc),
)
session.add(conn)
await session.commit()
await session.refresh(conn)
return conn
def _patch_helpers():
return (
patch("app.services.connection_service.detect_transfer_pairs", new_callable=AsyncMock),
patch("app.services.connection_service.stamp_primary_amount", new_callable=AsyncMock),
patch("app.services.connection_service.apply_rules_to_transaction", new_callable=AsyncMock),
)
@pytest.mark.asyncio
async def test_shared_workspace_sync_imports_as_connection_owner(
session: AsyncSession, test_user, test_workspace,
):
"""A workspace member may trigger sync without owning imported rows."""
requester = User(
id=uuid.uuid4(),
email="[email protected]",
hashed_password="not-used",
is_active=True,
is_superuser=False,
is_verified=True,
preferences={"currency_display": "BRL"},
)
session.add(requester)
await session.flush()
conn = await _make_connection(session, test_user.id, "Shared Broker")
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(
external_id="shared-account",
name="Brokerage",
type="checking",
balance=Decimal("100"),
currency="BRL",
),
])
mock_provider.get_transactions = AsyncMock(return_value=[
TransactionData(
external_id="shared-tx",
description="DIVIDEND",
amount=Decimal("10"),
date=date.today(),
type="credit",
currency="BRL",
),
])
mock_provider.get_holdings = AsyncMock(return_value=[
HoldingData(
external_id="shared-holding",
name="Shared Fund",
currency="BRL",
current_value=Decimal("500"),
),
])
p1, p2, p3 = _patch_helpers()
q1, q2, q3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
q1, q2, q3:
await sync_connection(session, conn.id, test_workspace.id, requester.id)
account = await session.scalar(select(Account).where(Account.external_id == "shared-account"))
transaction = await session.scalar(
select(Transaction).where(Transaction.external_id == "shared-tx")
)
asset = await session.scalar(select(Asset).where(Asset.external_id == "shared-holding"))
assert asset is not None
wallet = await session.get(AssetGroup, asset.group_id)
assert account is not None and account.user_id == test_user.id
assert transaction is not None and transaction.user_id == test_user.id
assert asset.user_id == test_user.id
assert wallet is not None and wallet.user_id == test_user.id
account.user_id = requester.id
transaction.user_id = requester.id
asset.user_id = requester.id
wallet.user_id = requester.id
bill = CreditCardBill(
id=uuid.uuid4(),
user_id=requester.id,
workspace_id=test_workspace.id,
account_id=account.id,
external_id="legacy-bill",
due_date=date.today(),
total_amount=Decimal("100"),
currency="BRL",
)
session.add(bill)
await session.commit()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn.id, test_workspace.id, requester.id)
await session.refresh(account)
await session.refresh(transaction)
await session.refresh(asset)
await session.refresh(wallet)
await session.refresh(bill)
assert account.user_id == test_user.id
assert transaction.user_id == test_user.id
assert asset.user_id == test_user.id
assert wallet.user_id == test_user.id
assert bill.user_id == test_user.id
assert bill.workspace_id == test_workspace.id
# ---------------------------------------------------------------------------
# get_oauth_url / get_reauth_url
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_get_oauth_url(test_user, test_workspace):
mock_provider = AsyncMock()
mock_provider.redirect_uri = "https://app/redirect"
mock_provider.get_oauth_url = AsyncMock(return_value="https://bank/authorize?state=xyz")
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
patch("app.services.connection_service.oauth_state.store_state",
new_callable=AsyncMock, return_value="state-token"):
url = await get_oauth_url("pluggy", test_user.id, test_workspace.id, {"country": "BR"})
assert url == "https://bank/authorize?state=xyz"
mock_provider.get_oauth_url.assert_awaited_once()
@pytest.mark.asyncio
async def test_get_reauth_url(session: AsyncSession, test_user, test_workspace):
conn = await _make_connection(session, test_user.id, settings={"flow_params": {"country": "DE"}})
mock_provider = AsyncMock()
mock_provider.redirect_uri = "https://app/redirect"
mock_provider.reauth_url = AsyncMock(return_value="https://bank/reauth")
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
patch("app.services.connection_service.oauth_state.store_state",
new_callable=AsyncMock, return_value="state-token"):
url = await get_reauth_url(session, conn.id, test_workspace.id, test_user.id)
assert url == "https://bank/reauth"
@pytest.mark.asyncio
async def test_get_reauth_url_not_found(session: AsyncSession, test_user, test_workspace):
with pytest.raises(ValueError, match="not found"):
await get_reauth_url(session, uuid.uuid4(), test_workspace.id, test_user.id)
# ---------------------------------------------------------------------------
# list_provider_institutions
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_list_provider_institutions():
mock_provider = AsyncMock()
mock_provider.list_institutions = AsyncMock(return_value=InstitutionListData(
countries=["DE", "FR"],
institutions=[
InstitutionData(
name="revolut_de", display_name="Revolut", country="DE",
logo="logo.png", bic="REVODEB2", psu_types=["personal"],
max_consent_days=90, max_history_days=730,
),
],
))
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
result = await list_provider_institutions("enable_banking", "DE")
assert result["countries"] == ["DE", "FR"]
assert len(result["institutions"]) == 1
inst = result["institutions"][0]
assert inst["name"] == "revolut_de"
assert inst["display_name"] == "Revolut"
assert inst["bic"] == "REVODEB2"
# ---------------------------------------------------------------------------
# _sync_holdings
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_holdings_creates_asset(session: AsyncSession, test_user):
conn = await _make_connection(session, test_user.id, "Broker")
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(return_value=[
HoldingData(
external_id="hold-1", name="VWCE ETF", currency="EUR",
current_value=Decimal("1200.00"), quantity=Decimal("10"),
purchase_price=Decimal("1000.00"), purchase_date=date(2026, 1, 1),
),
])
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"})
await session.commit()
asset = (await session.execute(
select(Asset).where(Asset.external_id == "hold-1")
)).scalar_one()
assert asset.name == "VWCE ETF"
assert asset.type == "investment"
assert asset.connection_id == conn.id
assert asset.workspace_id == conn.workspace_id
@pytest.mark.asyncio
async def test_sync_holdings_matches_asset_across_workspace_members(
session: AsyncSession, test_user, test_workspace
):
other_member = User(
id=uuid.uuid4(),
email="[email protected]",
hashed_password="not-used",
is_active=True,
is_superuser=False,
is_verified=True,
)
session.add(other_member)
await session.flush()
session.add(
WorkspaceMember(
id=uuid.uuid4(),
workspace_id=test_workspace.id,
user_id=other_member.id,
role="editor",
)
)
conn = await _make_connection(session, test_user.id, "Shared Broker")
existing = Asset(
id=uuid.uuid4(),
user_id=other_member.id,
workspace_id=test_workspace.id,
source="test",
external_id="shared-holding-id",
name="Existing holding",
type="investment",
currency="USD",
valuation_method="manual",
)
existing_group = AssetGroup(
id=uuid.uuid4(),
user_id=other_member.id,
workspace_id=test_workspace.id,
connection_id=None,
source="test",
external_id=conn.external_id,
name="Legacy wallet",
position=0,
)
session.add(existing_group)
await session.flush()
existing.group_id = existing_group.id
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(
return_value=[
HoldingData(
external_id="shared-holding-id",
name="Updated holding",
currency="USD",
current_value=Decimal("500"),
)
]
)
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"})
await session.commit()
matching = (
await session.execute(
select(Asset).where(
Asset.workspace_id == test_workspace.id,
Asset.source == "test",
Asset.external_id == "shared-holding-id",
)
)
).scalars().all()
assert [asset.id for asset in matching] == [existing.id]
assert matching[0].connection_id == conn.id
assert matching[0].name == "Updated holding"
assert matching[0].user_id == test_user.id
await session.refresh(existing_group)
assert existing_group.user_id == test_user.id
@pytest.mark.asyncio
async def test_sync_holdings_does_not_adopt_another_connections_wallet(
session: AsyncSession, test_user, test_workspace
):
other_member = User(
id=uuid.uuid4(),
email="[email protected]",
hashed_password="not-used",
is_active=True,
is_superuser=False,
is_verified=True,
)
session.add(other_member)
await session.flush()
session.add(
WorkspaceMember(
id=uuid.uuid4(),
workspace_id=test_workspace.id,
user_id=other_member.id,
role="editor",
)
)
current = await _make_connection(session, test_user.id, "Current Broker")
other = await _make_connection(session, other_member.id, "Other Broker")
other_group = AssetGroup(
id=uuid.uuid4(),
user_id=other_member.id,
workspace_id=test_workspace.id,
connection_id=other.id,
source="test",
external_id=other.external_id,
name="Other connection wallet",
position=0,
)
existing = Asset(
id=uuid.uuid4(),
user_id=other_member.id,
workspace_id=test_workspace.id,
connection_id=other.id,
group_id=other_group.id,
source="test",
external_id="shared-provider-holding",
name="Existing holding",
type="investment",
currency="USD",
valuation_method="manual",
)
session.add_all([other_group, existing])
await session.commit()
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(
return_value=[
HoldingData(
external_id="shared-provider-holding",
name="Updated holding",
currency="USD",
current_value=Decimal("500"),
)
]
)
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, current, {"token": "t"})
await session.commit()
await session.refresh(other_group)
await session.refresh(existing)
assert other_group.user_id == other_member.id
assert other_group.connection_id == other.id
assert existing.group_id == other_group.id
@pytest.mark.asyncio
async def test_sync_holdings_provider_error_swallowed(session: AsyncSession, test_user):
"""A failing get_holdings must not raise — investment data is best-effort."""
conn = await _make_connection(session, test_user.id, "FlakyBroker")
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(side_effect=RuntimeError("500"))
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"}) # no raise
@pytest.mark.asyncio
async def test_sync_holdings_withdrawn_new_skipped(session: AsyncSession, test_user):
"""A brand-new holding reported as withdrawn is skipped entirely."""
conn = await _make_connection(session, test_user.id, "ClosedBroker")
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(return_value=[
HoldingData(
external_id="dead-1", name="Closed Fund", currency="BRL",
current_value=Decimal("0"), is_withdrawn=True,
),
])
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"})
await session.commit()
rows = (await session.execute(
select(Asset).where(Asset.external_id == "dead-1")
)).scalars().all()
assert rows == []
@pytest.mark.asyncio
async def test_sync_holdings_archives_disappeared(session: AsyncSession, test_user):
"""A previously synced asset no longer in the provider response is archived."""
conn = await _make_connection(session, test_user.id, "GoneBroker")
existing = Asset(
id=uuid.uuid4(), user_id=test_user.id, connection_id=conn.id,
source="test", external_id="gone-1", name="Old", type="investment",
currency="BRL", is_archived=False, valuation_method="manual",
)
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(return_value=[]) # nothing returned
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"})
await session.commit()
await session.refresh(existing)
assert existing.is_archived is True
# ---------------------------------------------------------------------------
# sync_connection: trigger_refresh outcomes
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_trigger_refresh_needs_user_action(session: AsyncSession, test_user, test_workspace):
conn = await _make_connection(session, test_user.id, "RefreshBank")
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.trigger_refresh = AsyncMock(return_value="needs_user_action")
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
with pytest.raises(ProviderUserActionRequired, match="reconnect") as exc:
await sync_connection(
session, conn.id, test_workspace.id, test_user.id,
trigger_provider_refresh=True,
)
assert exc.value.code == "credentials_invalid"
# status marked error and committed
refreshed = (await session.execute(
select(BankConnection).where(BankConnection.id == conn.id)
)).scalar_one()
assert refreshed.status == "error"
assert refreshed.settings is not None
assert "last_provider_refresh_at" in refreshed.settings
@pytest.mark.asyncio
async def test_sync_trigger_refresh_refreshed_then_reads(session: AsyncSession, test_user, test_workspace):
conn = await _make_connection(session, test_user.id, "RefreshOK")
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.trigger_refresh = AsyncMock(return_value="refreshed")
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="ra-1", name="Checking", type="checking",
balance=Decimal("10"), currency="BRL"),
])
mock_provider.get_transactions = AsyncMock(return_value=[])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
result, _, _ = await sync_connection(
session, conn.id, test_workspace.id, test_user.id,
trigger_provider_refresh=True,
)
assert result.status == "active"
assert result.settings is not None
assert "last_provider_refresh_at" in result.settings
mock_provider.trigger_refresh.assert_awaited_once()
# ---------------------------------------------------------------------------
# sync_connection: fuzzy match merges a manual transaction
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_fuzzy_matches_manual_transaction(session: AsyncSession, test_user, test_workspace):
"""A synced tx that fuzzy-matches an existing manual tx merges into it
(incrementing merged_count) rather than inserting a duplicate."""
conn = await _make_connection(session, test_user.id, "FuzzyBank")
account = Account(
id=uuid.uuid4(), user_id=test_user.id, connection_id=conn.id,
external_id="fz-acc-1", name="Checking", type="checking",
balance=Decimal("0"), currency="BRL",
)
session.add(account)
await session.flush()
manual = Transaction(
id=uuid.uuid4(), user_id=test_user.id, account_id=account.id,
description="STARBUCKS COFFEE", amount=Decimal("25.00"),
date=date(2026, 4, 10), type="debit", source="manual",
external_id=None, created_at=datetime.now(timezone.utc),
)
session.add(manual)
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="fz-acc-1", name="Checking", type="checking",
balance=Decimal("0"), currency="BRL"),
])
mock_provider.get_transactions = AsyncMock(return_value=[
TransactionData(
external_id="fz-tx-1", description="STARBUCKS COFFEE",
amount=Decimal("25.00"), date=date(2026, 4, 10),
type="debit", currency="BRL", payee="Starbucks",
),
])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
result, merged, _ = await sync_connection(session, conn.id, test_workspace.id, test_user.id)
assert result.status == "active"
assert merged == 1
await session.refresh(manual)
assert manual.external_id == "fz-tx-1"
assert manual.source == "sync"
assert manual.payee == "Starbucks"
@pytest.mark.asyncio
async def test_sync_rematches_rekeyed_account_by_stable_external_id(
session: AsyncSession, test_user, test_workspace,
):
"""Reauthorising a provider re-keys its accounts; reuse the row, don't duplicate.
Enable Banking's `uid` is scoped to a single session, so reauth hands back a
new uid for the same real account. Matching only on external_id inserted a
second account row and left the original (with its transactions) behind.
`identification_hash` survives the reauth, so the sync must rebind the
existing row to the new uid.
"""
conn = await _make_connection(session, test_user.id, "ReauthBank")
conn_id, workspace_id, user_id = conn.id, test_workspace.id, test_user.id
existing = Account(
id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id,
connection_id=conn_id, external_id="old-uid", stable_external_id="hash-1",
name="Checking", type="checking", balance=Decimal("10"), currency="EUR",
)
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="new-uid", stable_external_id="hash-1", name="Checking",
type="checking", balance=Decimal("25"), currency="EUR"),
])
mock_provider.get_transactions = AsyncMock(return_value=[])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn_id, workspace_id, user_id)
rows = (await session.execute(
select(Account).where(Account.connection_id == conn_id)
)).scalars().all()
assert len(rows) == 1, f"re-keyed account was duplicated: {len(rows)} rows"
assert rows[0].id == existing.id
assert rows[0].external_id == "new-uid"
assert rows[0].stable_external_id == "hash-1"
@pytest.mark.asyncio
async def test_sync_rematches_legacy_account_by_masked_number(
session: AsyncSession, test_user, test_workspace,
):
"""Rows predating `stable_external_id` must survive a reauth too.
A user whose bank was failing never had a stable_external_id backfilled; the sync
that would have done it is the one that was failing. When the bank recovers
after a reconnect the uid is new and the hash is unknown to us, so the
masked identifier is the last thing that still identifies the account.
Match on it rather than insert a duplicate.
"""
conn = await _make_connection(session, test_user.id, "LegacyBank")
conn.provider = "enable_banking"
conn_id, workspace_id, user_id = conn.id, test_workspace.id, test_user.id
existing = Account(
id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id,
connection_id=conn_id, external_id="old-uid", stable_external_id=None,
masked_number="5531", name="CUENTA CORRIENTE", type="checking",
balance=Decimal("10"), currency="EUR",
)
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="new-uid", stable_external_id="hash-1", masked_number="5531",
name="CUENTA CORRIENTE", type="checking",
balance=Decimal("25"), currency="EUR"),
])
mock_provider.get_transactions = AsyncMock(return_value=[])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn_id, workspace_id, user_id)
rows = (await session.execute(
select(Account).where(Account.connection_id == conn_id)
)).scalars().all()
assert len(rows) == 1, f"legacy account was duplicated: {len(rows)} rows"
assert rows[0].id == existing.id
assert rows[0].external_id == "new-uid"
# The sync also backfills the hash it just learned, so the next reauth
# matches on the exact id instead of falling back to the mask.
assert rows[0].stable_external_id == "hash-1"
@pytest.mark.asyncio
async def test_sync_ambiguous_stable_external_id_does_not_match(
session: AsyncSession, test_user, test_workspace,
):
"""Two unclaimed rows with the same stable_external_id must not match.
EB does not guarantee the hash is unique. If two rows share the same
stable_external_id, the sync must fail closed (return None) rather than
fall through to the masked-number tier, which could merge unrelated
account histories.
"""
conn = await _make_connection(session, test_user.id, "AmbiguousBank")
conn_id, workspace_id, user_id = conn.id, test_workspace.id, test_user.id
existing1 = Account(
id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id,
connection_id=conn_id, external_id="uid-1", stable_external_id="hash-dup",
masked_number="1234", name="Checking A", type="checking", balance=Decimal("10"), currency="EUR",
)
existing2 = Account(
id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id,
connection_id=conn_id, external_id="uid-2", stable_external_id="hash-dup",
masked_number="5678", name="Checking B", type="checking", balance=Decimal("20"), currency="EUR",
)
session.add_all([existing1, existing2])
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="new-uid", stable_external_id="hash-dup",
masked_number="1234", name="Checking A", type="checking",
balance=Decimal("25"), currency="EUR"),
])
mock_provider.get_transactions = AsyncMock(return_value=[])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn_id, workspace_id, user_id)
rows = (await session.execute(
select(Account).where(Account.connection_id == conn_id)
)).scalars().all()
# Must insert a new row, not rebind either existing one
assert len(rows) == 3, f"ambiguous stable_external_id should not match: {len(rows)} rows"
assert all(r.external_id != "new-uid" for r in rows if r.id in (existing1.id, existing2.id))
@pytest.mark.asyncio
async def test_sync_masked_number_fallback_not_used_for_pluggy(
session: AsyncSession, test_user, test_workspace,
):
"""The masked-number fallback must be limited to Enable Banking.
Pluggy keeps external_id stable across syncs, so a missing account is
genuinely missing. The fallback should not apply to non-EB providers.
"""
conn = await _make_connection(session, test_user.id, "PluggyBank")
conn.provider = "pluggy"
conn_id, workspace_id, user_id = conn.id, test_workspace.id, test_user.id
existing = Account(
id=uuid.uuid4(), user_id=user_id, workspace_id=workspace_id,
connection_id=conn_id, external_id="pluggy-uid-1", stable_external_id=None,
masked_number="5531", name="CUENTA CORRIENTE", type="checking",
balance=Decimal("10"), currency="EUR",
)
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="pluggy-uid-2", stable_external_id=None,
masked_number="5531", name="CUENTA CORRIENTE", type="checking",
balance=Decimal("25"), currency="EUR"),
])
mock_provider.get_transactions = AsyncMock(return_value=[])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn_id, workspace_id, user_id)
rows = (await session.execute(
select(Account).where(Account.connection_id == conn_id)
)).scalars().all()
# Must insert a new row, not rebind the existing one
assert len(rows) == 2, f"masked-number fallback should not apply to Pluggy: {len(rows)} rows"
assert rows[0].id == existing.id
assert rows[0].external_id == "pluggy-uid-1"
# ---------------------------------------------------------------------------
# sync_connection: SessionExpired / ProviderUserActionRequired
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_unregistered_provider_keeps_status(
session: AsyncSession, test_user, test_workspace,
):
"""A provider missing from the registry is a server misconfiguration (e.g.
the Celery worker not loading .env) — the connection must keep its status
instead of flipping to "error", which would show a misleading reconnect
banner for perfectly healthy credentials."""
conn = await _make_connection(session, test_user.id, "GhostBank")
conn_id = conn.id
with pytest.raises(ProviderNotConfiguredError, match="not configured"):
await sync_connection(session, conn_id, test_workspace.id, test_user.id)
refreshed = (await session.execute(
select(BankConnection).where(BankConnection.id == conn_id)
)).scalar_one()
assert refreshed.status == "active"
@pytest.mark.asyncio
async def test_sync_session_expired_marks_expired(session: AsyncSession, test_user, test_workspace):
conn = await _make_connection(session, test_user.id, "ExpiredBank")
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(side_effect=SessionExpiredError("gone"))
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
with pytest.raises(SessionExpiredError):
await sync_connection(session, conn.id, test_workspace.id, test_user.id)
refreshed = (await session.execute(
select(BankConnection).where(BankConnection.id == conn.id)
)).scalar_one()
assert refreshed.status == "expired"
@pytest.mark.asyncio
async def test_sync_user_action_required_marks_error_status(
session: AsyncSession, test_user, test_workspace,
):
conn = await _make_connection(session, test_user.id, "ActionBank")
conn_id = conn.id
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(
side_effect=ProviderUserActionRequired("link accounts", code="restricted_mode")
)
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
with pytest.raises(ProviderUserActionRequired):
await sync_connection(session, conn_id, test_workspace.id, test_user.id)
# User-action failures are visible in the UI via the reconnect banner.
refreshed = (await session.execute(
select(BankConnection).where(BankConnection.id == conn_id)
)).scalar_one()
assert refreshed.status == "error"
# ---------------------------------------------------------------------------
# handle_oauth_callback: state guards + reconnect path
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_oauth_callback_invalid_state(session: AsyncSession, test_user, test_workspace):
with patch("app.services.connection_service.oauth_state.consume_state",
new_callable=AsyncMock, return_value=None):
with pytest.raises(ValueError, match="invalid or expired"):
await handle_oauth_callback(
session, test_workspace.id, test_user.id, "code", state="bad-state",
)
@pytest.mark.asyncio
async def test_oauth_callback_state_user_mismatch(session: AsyncSession, test_user, test_workspace):
with patch("app.services.connection_service.oauth_state.consume_state",
new_callable=AsyncMock,
return_value={"user_id": str(uuid.uuid4()),
"workspace_id": str(test_workspace.id),
"provider": "test"}):
with pytest.raises(ValueError, match="user does not match"):
await handle_oauth_callback(
session, test_workspace.id, test_user.id, "code", state="s",
)
@pytest.mark.asyncio
async def test_oauth_callback_missing_provider(session: AsyncSession, test_user, test_workspace):
with pytest.raises(ValueError, match="missing provider"):
await handle_oauth_callback(session, test_workspace.id, test_user.id, "code")
@pytest.mark.asyncio
async def test_oauth_callback_reconnect_updates_existing(
session: AsyncSession, test_user, test_workspace,
):
"""A callback carrying reconnect_connection_id updates the existing row in
place instead of creating a new connection."""
existing = await _make_connection(session, test_user.id, "OldName")
mock_provider = AsyncMock()
mock_provider.handle_oauth_callback = AsyncMock(return_value=ConnectionData(
external_id="reconnected-ext",
institution_name="NewName",
credentials={"token": "fresh"},
accounts=[],
))
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
patch("app.services.connection_service.oauth_state.consume_state",
new_callable=AsyncMock,
return_value={
"user_id": str(test_user.id),
"workspace_id": str(test_workspace.id),
"provider": "test",
"reconnect_connection_id": str(existing.id),
}):
result = await handle_oauth_callback(
session, test_workspace.id, test_user.id, "code", state="s",
)
assert result.id == existing.id
assert result.external_id == "reconnected-ext"
assert result.institution_name == "NewName"
assert result.credentials == {"token": "fresh"}
assert result.status == "active"
assert result.last_sync_at is None
# ---------------------------------------------------------------------------
# sync_connection: existing-tx update (pending -> posted) + new insert + FX
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_updates_existing_pending_to_posted(
session: AsyncSession, test_user, test_workspace,
):
"""An already-synced tx matched by external_id flips pending->posted."""
conn = await _make_connection(session, test_user.id, "PendingPostedBank")
account = Account(
id=uuid.uuid4(), user_id=test_user.id, connection_id=conn.id,
external_id="pp-acc-1", name="Checking", type="checking",
balance=Decimal("0"), currency="BRL",
)
session.add(account)
await session.flush()
session.add(Transaction(
id=uuid.uuid4(), user_id=test_user.id, account_id=account.id,
external_id="pp-tx-1", description="PENDING CHARGE",
amount=Decimal("60"), date=date(2026, 4, 12), type="debit",
currency="BRL", source="sync", status="pending",
created_at=datetime.now(timezone.utc),
))
await session.commit()
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="pp-acc-1", name="Checking", type="checking",
balance=Decimal("0"), currency="BRL"),
])
mock_provider.get_transactions = AsyncMock(return_value=[
TransactionData(
external_id="pp-tx-1", description="PENDING CHARGE",
amount=Decimal("60"), date=date(2026, 4, 12), type="debit",
currency="BRL", status="posted",
),
])
p1, p2, p3 = _patch_helpers()
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
p1, p2, p3:
await sync_connection(session, conn.id, test_workspace.id, test_user.id)
row = (await session.execute(
select(Transaction).where(Transaction.external_id == "pp-tx-1")
)).scalar_one()
assert row.status == "posted"
@pytest.mark.asyncio
async def test_sync_new_transaction_with_payee_and_fx(
session: AsyncSession, test_user, test_workspace,
):
"""New synced tx with a payee and a bank-provided FX conversion exercises
the payee resolution + amount_primary/fx_rate_used branch (no fallback
stamp_primary_amount call)."""
conn = await _make_connection(session, test_user.id, "FxBank")
# User primary currency is BRL (from conftest). Account in BRL, tx in USD
# with bank-provided BRL amount -> fx branch.
mock_provider = AsyncMock()
mock_provider.refresh_credentials = AsyncMock(return_value={"token": "t"})
mock_provider.get_accounts = AsyncMock(return_value=[
AccountData(external_id="fx-acc-1", name="Checking", type="checking",
balance=Decimal("0"), currency="BRL"),
])
mock_provider.get_transactions = AsyncMock(return_value=[
TransactionData(
external_id="fx-tx-1", description="AWS USD",
amount=Decimal("10.00"), date=date(2026, 4, 1), type="debit",
currency="USD", amount_in_account_currency=Decimal("52.00"),
payee="Amazon Web Services",
),
])
# Don't patch stamp_primary_amount here — the fx branch must bypass it.
with patch("app.services.connection_service.get_provider", return_value=mock_provider), \
patch("app.services.connection_service.detect_transfer_pairs", new_callable=AsyncMock), \
patch("app.services.connection_service.apply_rules_to_transaction", new_callable=AsyncMock):
await sync_connection(session, conn.id, test_workspace.id, test_user.id)
row = (await session.execute(
select(Transaction).where(Transaction.external_id == "fx-tx-1")
)).scalar_one()
assert row.payee == "Amazon Web Services"
assert row.payee_id is not None
assert row.amount_primary == Decimal("52.00")
assert row.fx_rate_used == Decimal("5.2")
# ---------------------------------------------------------------------------
# _sync_holdings: update an existing asset in place
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sync_holdings_updates_existing_asset(session: AsyncSession, test_user):
"""A holding matching an existing asset by external_id updates it in place
(name/currency/units) rather than creating a duplicate."""
conn = await _make_connection(session, test_user.id, "UpdBroker")
existing = Asset(
id=uuid.uuid4(), user_id=test_user.id, connection_id=conn.id,
source="test", external_id="upd-1", name="Old Name", type="investment",
currency="USD", units=Decimal("5"), is_archived=False,
valuation_method="manual",
)
session.add(existing)
await session.commit()
mock_provider = AsyncMock()
mock_provider.get_holdings = AsyncMock(return_value=[
HoldingData(
external_id="upd-1", name="New Name", currency="EUR",
current_value=Decimal("999.00"), quantity=Decimal("8"),
),
])
with patch("app.services.connection_service.get_provider", return_value=mock_provider):
await _sync_holdings(session, test_user.id, conn, {"token": "t"})
await session.commit()
rows = (await session.execute(
select(Asset).where(Asset.external_id == "upd-1")
)).scalars().all()
assert len(rows) == 1 # updated, not duplicated
assert rows[0].name == "New Name"
assert rows[0].currency == "EUR"
assert rows[0].units == Decimal("8")