Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

32.3 KB
Raw Blame
"""Unit tests for the SimpleFIN provider.

The bridge is fully fakeable via ``httpx.MockTransport`` — no SimpleFIN
credentials needed, no network. Each test stands up the smallest payload
required and asserts the parse / dispatch behavior we care about.
"""
from __future__ import annotations

import base64
from datetime import date, timedelta
from decimal import Decimal
from unittest.mock import patch

import httpx
import pytest

from app.providers.base import ProviderUserActionRequired, SessionExpiredError
from app.providers.simplefin import (
    SimpleFinProvider,
    _accounts_url_and_auth,
    _decode_setup_token,
    _epoch_to_date,
)


def _encode_token(url: str) -> str:
    return base64.b64encode(url.encode("utf-8")).decode("ascii")


def _patched_client(handler):
    """Replace SimpleFinProvider._client with one wired to a MockTransport."""

    transport = httpx.MockTransport(handler)

    async def fake_client(self, credentials=None):  # noqa: ANN001
        return httpx.AsyncClient(transport=transport, timeout=30)

    return patch.object(SimpleFinProvider, "_client", fake_client)


# ----- pure helpers -----------------------------------------------------------


def test_decode_setup_token_round_trips():
    raw = _encode_token("https://bridge.simplefin.org/simplefin/claim/abc123")
    assert (
        _decode_setup_token(raw)
        == "https://bridge.simplefin.org/simplefin/claim/abc123"
    )


def test_decode_setup_token_strips_whitespace_and_repads():
    raw = _encode_token("https://bridge.simplefin.org/simplefin/claim/xyz")
    # Strip padding to simulate a copy-pasted token, surround with whitespace.
    sloppy = "  " + raw.rstrip("=") + "\n  "
    assert "claim/xyz" in _decode_setup_token(sloppy)


def test_decode_setup_token_rejects_empty():
    with pytest.raises(ValueError):
        _decode_setup_token("   ")


def test_decode_setup_token_rejects_non_url():
    with pytest.raises(ValueError):
        _decode_setup_token(_encode_token("ftp://nope.example"))


def test_decode_setup_token_rejects_garbage():
    with pytest.raises(ValueError):
        _decode_setup_token("this-is-not-base64!!!@@@")


def test_epoch_to_date_handles_unset():
    assert _epoch_to_date(None) is None
    assert _epoch_to_date("") is None
    assert _epoch_to_date(0) is None


def test_epoch_to_date_parses_seconds():
    assert _epoch_to_date(1672531200) == date(2023, 1, 1)


def test_accounts_url_and_auth_strips_userinfo():
    url, auth = _accounts_url_and_auth("https://u:[email protected]/simplefin")
    assert url == "https://bridge.example/simplefin/accounts"
    assert auth == ("u", "p")


# ----- claim flow -------------------------------------------------------------


@pytest.mark.asyncio
async def test_handle_oauth_callback_claims_and_parses_accounts():
    """Token paste → claim → first /accounts → ConnectionData."""

    state = {"step": "claim"}

    def handler(request: httpx.Request) -> httpx.Response:
        if state["step"] == "claim":
            assert request.method == "POST"
            assert request.url.path.endswith("/simplefin/claim/demo")
            state["step"] = "accounts"
            return httpx.Response(200, text="https://u:[email protected]/simplefin")
        # /accounts request
        assert request.method == "GET"
        assert request.url.path == "/simplefin/accounts"
        return httpx.Response(
            200,
            json={
                "errlist": [],
                "connections": [
                    {"conn_id": "CON-1", "name": "Demo Bank"}
                ],
                "accounts": [
                    {
                        "id": "acc-1",
                        "name": "Checking",
                        "currency": "USD",
                        "balance": "1234.56",
                        "conn_id": "CON-1",
                        "transactions": [],
                        "holdings": [],
                    }
                ],
            },
        )

    provider = SimpleFinProvider()
    token = _encode_token("https://bridge.example/simplefin/claim/demo")
    with _patched_client(handler):
        conn = await provider.handle_oauth_callback(token)

    assert conn.external_id == "CON-1"
    assert conn.institution_name == "Demo Bank"
    assert "access_url_enc" in conn.credentials
    # The plaintext URL must never end up in credentials.
    assert "u:p@" not in str(conn.credentials.get("access_url_enc"))
    assert len(conn.accounts) == 1
    acc = conn.accounts[0]
    assert acc.external_id == "acc-1"
    assert acc.balance == Decimal("1234.56")
    assert acc.currency == "USD"


@pytest.mark.asyncio
async def test_handle_oauth_callback_403_signals_reused_token():
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(403, text="token already used")

    provider = SimpleFinProvider()
    token = _encode_token("https://bridge.example/simplefin/claim/demo")
    with _patched_client(handler):
        with pytest.raises(ProviderUserActionRequired) as exc:
            await provider.handle_oauth_callback(token)
    assert exc.value.code == "setup_token_used"


@pytest.mark.asyncio
async def test_claim_auth_errlist_soft_warning_keeps_healthy_accounts(caplog):
    """A con.auth warning for one institution must not sink a claim whose
    payload still carries usable accounts from the other institutions. The
    second half pins the other direction: with no accounts at all the same
    warning still hard-fails the claim."""
    import logging as stdlogging

    def make_handler(accounts: list):
        state = {"step": "claim"}

        def handler(request: httpx.Request) -> httpx.Response:
            if state["step"] == "claim":
                state["step"] = "accounts"
                return httpx.Response(200, text="https://u:[email protected]/simplefin")
            return httpx.Response(
                200,
                json={
                    "errlist": [
                        {"code": "con.auth", "msg": "Institution A auth required", "conn_id": "INST-A"}
                    ],
                    "connections": [
                        {"conn_id": "INST-A", "name": "Institution A"},
                        {"conn_id": "INST-B", "name": "Institution B"},
                    ],
                    "accounts": accounts,
                },
            )

        return handler

    healthy_account = {
        "id": "acc-healthy",
        "name": "Healthy Checking",
        "currency": "USD",
        "balance": "42.00",
        "conn_id": "INST-B",
        "transactions": [],
        "holdings": [],
    }

    provider = SimpleFinProvider()
    token = _encode_token("https://bridge.example/simplefin/claim/demo")
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(make_handler([healthy_account])):
        conn = await provider.handle_oauth_callback(token)
    assert [acc.external_id for acc in conn.accounts] == ["acc-healthy"]
    assert any("con.auth" in rec.getMessage() for rec in caplog.records)

    provider = SimpleFinProvider()
    token = _encode_token("https://bridge.example/simplefin/claim/demo")
    with _patched_client(make_handler([])):
        with pytest.raises(ProviderUserActionRequired) as exc:
            await provider.handle_oauth_callback(token)
    assert exc.value.code == "credentials_invalid"


# ----- error mapping ----------------------------------------------------------


@pytest.mark.asyncio
async def test_auth_errlist_raises_user_action_required():
    """SimpleFIN ``con.auth`` / ``gen.auth`` → user must regenerate the token."""

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "errlist": [
                    {"code": "con.auth", "msg": "Authentication failed", "conn_id": "C"}
                ],
                "accounts": [],
            },
        )

    creds = {"access_url_enc": None, "access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with _patched_client(handler):
        with pytest.raises(ProviderUserActionRequired) as exc:
            await provider.get_accounts(creds)
    assert exc.value.code == "credentials_invalid"


@pytest.mark.asyncio
async def test_auth_errlist_with_usable_accounts_is_soft_warning(caplog):
    """A stale SimpleFIN sub-connection must not block healthy accounts."""
    import logging as stdlogging

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "errlist": [
                    {"code": "con.auth", "msg": "Apple Card auth required", "conn_id": "APPLE"}
                ],
                "accounts": [
                    {
                        "id": "acc-healthy",
                        "name": "Healthy Checking",
                        "currency": "USD",
                        "balance": "42.00",
                    }
                ],
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(handler):
        accounts = await provider.get_accounts(creds)

    assert [acc.external_id for acc in accounts] == ["acc-healthy"]
    assert any("con.auth" in rec.getMessage() for rec in caplog.records)


@pytest.mark.asyncio
async def test_act_failed_is_soft_warning(caplog):
    """``act.failed`` is transient — keep going, just log."""
    import logging as stdlogging

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "errlist": [
                    {"code": "act.failed", "msg": "transient", "account_id": "X"}
                ],
                "accounts": [
                    {
                        "id": "acc-1",
                        "name": "Checking",
                        "currency": "USD",
                        "balance": "10.00",
                    }
                ],
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(handler):
        accounts = await provider.get_accounts(creds)
    assert len(accounts) == 1
    assert any("act.failed" in rec.getMessage() for rec in caplog.records)


@pytest.mark.asyncio
async def test_401_response_signals_credentials_invalid():
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(401, text="nope")

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        with pytest.raises(ProviderUserActionRequired):
            await SimpleFinProvider().get_accounts(creds)


@pytest.mark.asyncio
async def test_accounts_request_moves_url_userinfo_to_auth_header():
    def handler(request: httpx.Request) -> httpx.Response:
        assert request.url.userinfo == b""
        assert "authorization" in request.headers
        return httpx.Response(200, json={"accounts": []})

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        await SimpleFinProvider().get_accounts(creds)


@pytest.mark.asyncio
async def test_missing_access_url_raises_session_expired():
    with pytest.raises(SessionExpiredError):
        await SimpleFinProvider().get_accounts({})


@pytest.mark.asyncio
async def test_get_transactions_auth_errlist_with_requested_account_is_soft_warning(caplog):
    """A top-level auth warning must not block requested-account transactions."""
    import logging as stdlogging

    def handler(request: httpx.Request) -> httpx.Response:
        assert request.url.params.get("account") == "acc-healthy"
        return httpx.Response(
            200,
            json={
                "errlist": [
                    {"code": "con.auth", "msg": "Apple Card auth required", "conn_id": "APPLE"}
                ],
                "accounts": [
                    {
                        "id": "acc-healthy",
                        "transactions": [
                            {
                                "id": "tx-healthy",
                                "amount": "-12.34",
                                "posted": 1672531200,
                                "description": "Coffee",
                            }
                        ],
                    }
                ],
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(handler):
        txns = await SimpleFinProvider().get_transactions(
            creds, "acc-healthy", since=date(2023, 1, 1)
        )

    assert [txn.external_id for txn in txns] == ["tx-healthy"]
    assert any("con.auth" in rec.getMessage() for rec in caplog.records)


@pytest.mark.asyncio
async def test_get_transactions_expired_account_soft_skips_and_flags_reauth(caplog):
    """A per-account auth expiry must skip the account, not abort (issue #1077).

    The bridge answers a per-account query for an expired sub-connection
    with no accounts plus a ``con.auth`` errlist entry. The provider
    records the warning for the sync layer and returns what it has (nothing
    for this account) instead of raising, so the rest of the connection
    keeps syncing and the connection stays flagged for reauthorization.
    """
    import logging as stdlogging

    def handler(request: httpx.Request) -> httpx.Response:
        assert request.url.params.get("account") == "acc-expired"
        return httpx.Response(
            200,
            json={
                "errlist": [
                    {"code": "con.auth", "msg": "Authentication required", "conn_id": "C-B"}
                ],
                "accounts": [],
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(handler):
        txns = await provider.get_transactions(creds, "acc-expired", since=date(2026, 9, 1))

    assert txns == []
    assert [w["code"] for w in provider.action_required_warnings] == ["con.auth"]
    assert provider.incomplete_account_ids == {"acc-expired"}
    assert any("reauthorization" in rec.getMessage() for rec in caplog.records)


@pytest.mark.asyncio
async def test_get_transactions_http_refusal_soft_skips_and_flags_reauth(caplog):
    """An HTTP refusal of the per-account query must skip, not abort (issue #1077)."""
    import logging as stdlogging

    def handler(request: httpx.Request) -> httpx.Response:
        assert request.url.params.get("account") == "acc-expired"
        return httpx.Response(403, text="authentication required")

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with caplog.at_level(stdlogging.WARNING, logger="app.providers.simplefin"), _patched_client(handler):
        txns = await provider.get_transactions(creds, "acc-expired", since=date(2026, 9, 1))

    assert txns == []
    assert [w["code"] for w in provider.action_required_warnings] == ["credentials_invalid"]
    assert provider.incomplete_account_ids == {"acc-expired"}
    assert any("refused" in rec.getMessage() for rec in caplog.records)


@pytest.mark.asyncio
async def test_get_transactions_refusal_after_first_chunk_marks_fetch_incomplete():
    """A refusal on a later window chunk keeps the earlier chunk's rows but
    marks the account's fetch as incomplete, so the sync layer does not
    treat the partial result as everything the bridge still has."""
    calls: list[str] = []

    def handler(request: httpx.Request) -> httpx.Response:
        calls.append(request.url.params.get("start-date"))
        if len(calls) == 1:
            return httpx.Response(200, json={
                "errlist": [],
                "accounts": [{
                    "id": "acc-1", "currency": "USD", "balance": "10.00",
                    "transactions": [{
                        "id": "tx-early", "posted": 1767225600,
                        "amount": "-5.00", "description": "EARLY ROW",
                    }],
                }],
            })
        return httpx.Response(403, text="authentication required")

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with _patched_client(handler):
        txns = await provider.get_transactions(
            creds, "acc-1", since=date.today() - timedelta(days=150)
        )

    assert len(calls) == 2
    assert [t.external_id for t in txns] == ["tx-early"]
    assert provider.incomplete_account_ids == {"acc-1"}


# ----- transactions -----------------------------------------------------------


@pytest.mark.asyncio
async def test_get_transactions_filters_by_account_and_parses_signs():
    """Negative amount → debit; positive → credit."""

    def handler(request: httpx.Request) -> httpx.Response:
        assert request.url.path == "/simplefin/accounts"
        assert "u:p@" not in str(request.url)
        assert request.headers["authorization"].startswith("Basic ")
        # We always request a specific account
        assert request.url.params.get("account") == "acc-1"
        assert request.url.params.get("pending") == "1"
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "currency": "USD",
                        "balance": "0",
                        "transactions": [
                            {
                                "id": "t1",
                                "amount": "-12.34",
                                "posted": 1672531200,  # 2023-01-01 UTC
                                "description": "Coffee",
                                "payee": "Cafe",
                            },
                            {
                                "id": "t2",
                                "amount": "100.00",
                                "posted": 1672617600,  # 2023-01-02 UTC
                                "description": "Payroll",
                                "pending": True,
                            },
                        ],
                    },
                    {  # noise — a different account in the same response
                        "id": "acc-2",
                        "transactions": [
                            {"id": "tX", "amount": "5", "posted": 1672531200},
                        ],
                    },
                ]
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    provider = SimpleFinProvider()
    with _patched_client(handler):
        txns = await provider.get_transactions(
            creds, "acc-1", since=date(2023, 1, 1)
        )
    by_id = {t.external_id: t for t in txns}
    assert set(by_id) == {"t1", "t2"}
    assert by_id["t1"].type == "debit"
    assert by_id["t1"].amount == Decimal("12.34")
    assert by_id["t1"].status == "posted"
    assert by_id["t2"].status == "pending"
    assert by_id["t2"].type == "credit"


@pytest.mark.asyncio
async def test_get_transactions_uses_transacted_at_when_posted_zero():
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "transactions": [
                            {
                                "id": "t1",
                                "amount": "-12.34",
                                "posted": 0,
                                "transacted_at": 1672617600,
                                "description": "Coffee",
                            },
                        ],
                    },
                ]
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        txns = await SimpleFinProvider().get_transactions(
            creds, "acc-1", since=date(2023, 1, 1)
        )

    assert txns[0].date == date(2023, 1, 2)


@pytest.mark.asyncio
async def test_get_transactions_chunks_long_windows():
    """``since`` more than 90 days ago → multiple requests with shifting windows."""

    calls: list[tuple[str, str]] = []

    def handler(request: httpx.Request) -> httpx.Response:
        calls.append(
            (
                request.url.params.get("start-date", ""),
                request.url.params.get("end-date", ""),
            )
        )
        return httpx.Response(
            200, json={"accounts": [{"id": "acc-1", "transactions": []}]}
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    today = date.today()
    long_ago = today - timedelta(days=200)
    with _patched_client(handler):
        await SimpleFinProvider().get_transactions(creds, "acc-1", since=long_ago)
    assert len(calls) >= 3  # 200 days / 90-day window


@pytest.mark.asyncio
async def test_get_transactions_followup_rewinds_one_valid_full_window():
    calls: list[tuple[int, int]] = []

    def handler(request: httpx.Request) -> httpx.Response:
        calls.append((
            int(request.url.params["start-date"]),
            int(request.url.params["end-date"]),
        ))
        return httpx.Response(
            200, json={"accounts": [{"id": "acc-1", "transactions": []}]}
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        await SimpleFinProvider().get_transactions(
            creds, "acc-1", since=date.today() - timedelta(days=14)
        )

    assert len(calls) == 1
    assert calls[0][1] - calls[0][0] == 45 * 24 * 60 * 60


# ----- holdings ---------------------------------------------------------------


@pytest.mark.asyncio
async def test_get_holdings_parses_investment_data():
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "currency": "USD",
                        "holdings": [
                            {
                                "id": "h-1",
                                "description": "Apple",
                                "symbol": "AAPL",
                                "market_value": "105884.80",
                                "shares": "550.0",
                                "purchase_price": "0.10",
                                "cost_basis": "55.00",
                            },
                            {  # no market value → dropped
                                "id": "h-2",
                                "description": "Mystery",
                                "shares": "1",
                            },
                        ],
                    }
                ]
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        holdings = await SimpleFinProvider().get_holdings(creds)
    assert len(holdings) == 1
    h = holdings[0]
    assert h.external_id == "h-1"
    assert h.current_value == Decimal("105884.80")
    assert h.quantity == Decimal("550.0")
    assert (h.metadata or {}).get("symbol") == "AAPL"
    # Also promoted to the dedicated column, not just the metadata blob.
    assert h.ticker == "AAPL"


@pytest.mark.asyncio
async def test_get_holdings_crypto_ticker_currency_falls_back_to_account_currency():
    """A connector-supplied ticker like ``DOGE`` isn't an ISO currency code.

    ``HoldingData.currency`` maps to a ``VARCHAR(3)`` DB column — writing a
    4-letter ticker there overflows and used to crash the entire sync for
    the account (see issue #448). It should fall back to the account's
    currency instead.
    """

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "currency": "USD",
                        "holdings": [
                            {
                                "id": "h-crypto",
                                "description": "Dogecoin",
                                "symbol": "DOGE",
                                "currency": "DOGE",
                                "market_value": "42.00",
                                "shares": "100",
                            },
                        ],
                    }
                ]
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        holdings = await SimpleFinProvider().get_holdings(creds)
    assert len(holdings) == 1
    assert holdings[0].currency == "USD"
    # The ticker itself belongs in the dedicated 32-char column, not in
    # `currency` — that's the pairing issue #448 asks for.
    assert holdings[0].ticker == "DOGE"


@pytest.mark.asyncio
async def test_get_accounts_non_iso_currency_falls_back_to_usd():
    """The same connector quirk on an *account* would overflow accounts.currency.

    Accounts are upserted before holdings during a sync, so an unguarded
    account currency crashes the connection before the holdings guard is
    ever reached.
    """

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "name": "Crypto Wallet",
                        "currency": "DOGE",
                        "balance": "10.00",
                        "transactions": [],
                    }
                ]
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        accounts = await SimpleFinProvider().get_accounts(creds)
    assert accounts[0].currency == "USD"


def test_build_transaction_non_iso_currency_is_none():
    """A bogus transaction currency resolves to None, not a bad 3-char write.

    The sync layer reads `txn_data.currency or acc_data.currency or
    user_currency`, so None correctly defers to the account's currency.
    """
    raw = {
        "id": "t1",
        "amount": "-1.00",
        "posted": 1672531200,
        "currency": "DOGE",
        "description": "buy",
    }
    txn = SimpleFinProvider._build_transaction(raw, "description")
    assert txn is not None
    assert txn.currency is None
    # A real ISO code still passes through, normalized.
    ok = SimpleFinProvider._build_transaction({**raw, "currency": "eur"}, "description")
    assert ok is not None
    assert ok.currency == "EUR"


# ----- misc -------------------------------------------------------------------


def test_flow_type_is_token():
    p = SimpleFinProvider()
    assert p.flow_type == "token"
    assert p.name == "simplefin"


def test_get_oauth_url_raises_for_token_flow():
    with pytest.raises(NotImplementedError):
        SimpleFinProvider().get_oauth_url("https://x", "state")


# ----- multi-institution payloads (issue #345) --------------------------------


def test_parse_accounts_maps_each_account_to_its_own_institution():
    """connections[] entries are matched to accounts by conn_id, so a Setup
    Token spanning several institutions labels each account with its own."""
    payload = {
        "connections": [
            {"conn_id": "CON-1", "name": "First Bank", "org_url": "https://first.example"},
            {"conn_id": "CON-2", "name": "Second Brokerage", "org_url": "https://second.example"},
        ],
        "accounts": [
            {"id": "a1", "name": "Checking", "currency": "USD", "balance": "10", "conn_id": "CON-1"},
            {"id": "a2", "name": "IRA", "currency": "USD", "balance": "20", "conn_id": "CON-2"},
            {"id": "a3", "name": "Orphan", "currency": "USD", "balance": "30"},
        ],
    }

    institution_name, accounts = SimpleFinProvider._parse_accounts(payload)

    # Connection-level name keeps the previous first-entry behavior.
    assert institution_name == "First Bank"
    by_id = {a.external_id: a for a in accounts}
    assert by_id["a1"].institution_name == "First Bank"
    assert by_id["a1"].institution_external_id == "CON-1"
    assert "first.example" in (by_id["a1"].institution_logo_url or "")
    assert by_id["a2"].institution_name == "Second Brokerage"
    assert by_id["a2"].institution_external_id == "CON-2"
    assert "second.example" in (by_id["a2"].institution_logo_url or "")
    # No conn_id → no per-account institution; serialize falls back to the connection.
    assert by_id["a3"].institution_name is None
    assert by_id["a3"].institution_external_id is None
    assert by_id["a3"].institution_logo_url is None


def test_parse_accounts_connection_without_name_or_url_is_harmless():
    """A nameless connections[] entry is skipped; a named one without a URL
    yields a name but no logo."""
    payload = {
        "connections": [
            {"conn_id": "CON-1"},
            {"conn_id": "CON-2", "name": "Bare Bank"},
        ],
        "accounts": [
            {"id": "a1", "name": "A", "currency": "USD", "balance": "1", "conn_id": "CON-1"},
            {"id": "a2", "name": "B", "currency": "USD", "balance": "2", "conn_id": "CON-2"},
        ],
    }

    _, accounts = SimpleFinProvider._parse_accounts(payload)
    by_id = {a.external_id: a for a in accounts}
    assert by_id["a1"].institution_name is None
    assert by_id["a2"].institution_name == "Bare Bank"
    assert by_id["a2"].institution_logo_url is None


def test_parse_accounts_falls_back_to_account_org_object():
    """Spec-style servers attach an ``org`` object per account instead of a
    top-level connections[]; the feature still works there (review on #654)."""
    payload = {
        "accounts": [
            {
                "id": "a1", "name": "Checking", "currency": "USD", "balance": "10",
                "org": {"name": "Org Bank", "domain": "orgbank.example", "id": "ORG-1"},
            },
            {
                "id": "a2", "name": "Savings", "currency": "USD", "balance": "20",
                "org": {"domain": "nameless.example"},
            },
        ],
    }

    _, accounts = SimpleFinProvider._parse_accounts(payload)
    by_id = {a.external_id: a for a in accounts}
    assert by_id["a1"].institution_name == "Org Bank"
    assert by_id["a1"].institution_external_id == "ORG-1"
    assert "orgbank.example" in (by_id["a1"].institution_logo_url or "")
    # A nameless org still identifies the bank by domain.
    assert by_id["a2"].institution_name == "nameless.example"
    assert by_id["a2"].institution_external_id == "nameless.example"
    assert "nameless.example" in (by_id["a2"].institution_logo_url or "")


@pytest.mark.asyncio
async def test_get_holdings_carries_the_owning_account():
    """Each holding is stamped with its owning account so the sync can build
    one wallet per investment account (issue #345)."""

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "accounts": [
                    {
                        "id": "acc-1",
                        "name": "Employer 401(k)",
                        "currency": "USD",
                        "holdings": [
                            {"id": "h-1", "description": "Apple", "symbol": "AAPL",
                             "market_value": "10.00", "shares": "1"},
                        ],
                    },
                    {
                        "id": "acc-2",
                        "name": "Rollover IRA",
                        "currency": "USD",
                        "holdings": [
                            {"id": "h-2", "description": "Bonds", "symbol": "BND",
                             "market_value": "5.00", "shares": "1"},
                        ],
                    },
                ],
            },
        )

    creds = {"access_url": "https://u:[email protected]/simplefin"}
    with _patched_client(handler):
        holdings = await SimpleFinProvider().get_holdings(creds)
    by_id = {h.external_id: h for h in holdings}
    assert by_id["h-1"].account_external_id == "acc-1"
    assert by_id["h-1"].account_name == "Employer 401(k)"
    assert by_id["h-2"].account_external_id == "acc-2"
    assert by_id["h-2"].account_name == "Rollover IRA"