Skip to content

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

22.5 KB
Raw Blame
"""Extended API tests covering missing lines in transactions, rules, assets,
accounts, and import endpoints."""
import uuid
from datetime import date

import pytest
from httpx import AsyncClient

from app.models.account import Account
from app.models.transaction import Transaction


# ---------------------------------------------------------------------------
# Transactions API — export and bulk categorize
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_export_transactions_csv(
    client: AsyncClient, auth_headers, test_transactions: list[Transaction],
):
    """GET /api/transactions/export returns CSV with proper headers."""
    response = await client.get("/api/transactions/export", headers=auth_headers)
    assert response.status_code == 200
    assert "text/csv" in response.headers["content-type"]
    content = response.text
    # Should have header row + transaction rows
    lines = content.strip().split("\n")
    assert len(lines) > 1
    header = lines[0].replace("\ufeff", "")  # Remove BOM
    assert "date" in header
    assert "description" in header
    assert "amount" in header


@pytest.mark.asyncio
async def test_export_transactions_with_filters(
    client: AsyncClient, auth_headers, test_transactions, test_account,
):
    """Export with filters returns filtered results."""
    response = await client.get(
        "/api/transactions/export",
        params={"account_id": str(test_account.id)},
        headers=auth_headers,
    )
    assert response.status_code == 200
    assert "text/csv" in response.headers["content-type"]


@pytest.mark.asyncio
async def test_export_transactions_with_search(
    client: AsyncClient, auth_headers, test_transactions,
):
    """Export with search query returns filtered results."""
    response = await client.get(
        "/api/transactions/export",
        params={"q": "UBER"},
        headers=auth_headers,
    )
    assert response.status_code == 200
    content = response.text
    assert "UBER" in content


@pytest.mark.asyncio
async def test_export_transactions_by_type(
    client: AsyncClient, auth_headers, test_transactions,
):
    """Export filtered by type."""
    response = await client.get(
        "/api/transactions/export",
        params={"type": "debit"},
        headers=auth_headers,
    )
    assert response.status_code == 200
    assert "text/csv" in response.headers["content-type"]


@pytest.mark.asyncio
async def test_export_uncategorized(
    client: AsyncClient, auth_headers, test_transactions,
):
    """Export uncategorized transactions."""
    response = await client.get(
        "/api/transactions/export",
        params={"uncategorized": "true"},
        headers=auth_headers,
    )
    assert response.status_code == 200


@pytest.mark.asyncio
async def test_bulk_categorize(
    client: AsyncClient, auth_headers, test_transactions, test_categories,
):
    """PATCH /api/transactions/bulk-categorize updates multiple transactions."""
    # Get uncategorized transactions (NETFLIX and PIX RECEBIDO)
    uncategorized_ids = [
        str(t.id) for t in test_transactions if t.category_id is None
    ]
    assert len(uncategorized_ids) >= 1

    response = await client.patch(
        "/api/transactions/bulk-categorize",
        headers=auth_headers,
        json={
            "transaction_ids": uncategorized_ids,
            "category_id": str(test_categories[0].id),
        },
    )
    assert response.status_code == 200
    data = response.json()
    assert data["updated"] == len(uncategorized_ids)


@pytest.mark.asyncio
async def test_update_transaction_not_found(
    client: AsyncClient, auth_headers, test_transactions,
):
    """PATCH nonexistent transaction returns 404."""
    response = await client.patch(
        "/api/transactions/00000000-0000-0000-0000-000000000000",
        headers=auth_headers,
        json={"description": "Ghost"},
    )
    assert response.status_code == 404


# ---------------------------------------------------------------------------
# Rules API — missing coverage
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_create_duplicate_rule(
    client: AsyncClient, auth_headers, test_rules, test_categories,
):
    """Creating a rule with duplicate name returns 409."""
    existing_name = test_rules[0].name
    response = await client.post(
        "/api/rules",
        headers=auth_headers,
        json={
            "name": existing_name,
            "conditions_op": "and",
            "conditions": [{"field": "description", "op": "contains", "value": "TEST"}],
            "actions": [{"op": "set_category", "value": str(test_categories[0].id)}],
            "priority": 5,
        },
    )
    assert response.status_code == 409


@pytest.mark.asyncio
async def test_update_rule_not_found(client: AsyncClient, auth_headers, test_rules):
    """PATCH nonexistent rule returns 404."""
    response = await client.patch(
        "/api/rules/00000000-0000-0000-0000-000000000000",
        headers=auth_headers,
        json={"name": "Ghost"},
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_delete_rule_not_found(client: AsyncClient, auth_headers, test_rules):
    """DELETE nonexistent rule returns 404."""
    response = await client.delete(
        "/api/rules/00000000-0000-0000-0000-000000000000",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_list_rule_packs(client: AsyncClient, auth_headers, test_rules):
    """GET /api/rules/packs returns available rule packs."""
    response = await client.get("/api/rules/packs", headers=auth_headers)
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data, list)
    # Each pack has expected fields
    for pack in data:
        assert "code" in pack
        assert "name" in pack
        assert "flag" in pack
        assert "rule_count" in pack
        assert "installed" in pack


@pytest.mark.asyncio
async def test_install_rule_pack(client: AsyncClient, auth_headers, test_categories):
    """POST /api/rules/packs/{code}/install installs a rule pack."""
    # First list packs to get a valid code
    packs_resp = await client.get("/api/rules/packs", headers=auth_headers)
    packs = packs_resp.json()
    assert packs, "the bundled rule packs must be available"

    pack_code = packs[0]["code"]
    response = await client.post(
        f"/api/rules/packs/{pack_code}/install",
        headers=auth_headers,
    )
    assert response.status_code == 200
    assert "installed" in response.json()


@pytest.mark.asyncio
async def test_install_rule_pack_not_found(client: AsyncClient, auth_headers, test_categories):
    """POST /api/rules/packs/nonexistent/install returns 404."""
    response = await client.post(
        "/api/rules/packs/nonexistent/install",
        headers=auth_headers,
    )
    assert response.status_code == 404


# ---------------------------------------------------------------------------
# Accounts API — balance-history and additional endpoints
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_account_balance_history_api(client: AsyncClient, auth_headers):
    """GET /api/accounts/{id}/balance-history returns daily series."""
    # Create account with transactions
    acc_resp = await client.post(
        "/api/accounts",
        headers=auth_headers,
        json={"name": "Hist API", "type": "checking", "balance": 1000, "currency": "BRL"},
    )
    assert acc_resp.status_code == 201
    account_id = acc_resp.json()["id"]

    response = await client.get(
        f"/api/accounts/{account_id}/balance-history",
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data, list)
    assert len(data) > 0
    assert "date" in data[0]
    assert "balance" in data[0]


@pytest.mark.asyncio
async def test_account_balance_history_with_dates(client: AsyncClient, auth_headers):
    """Balance history with custom date range."""
    acc_resp = await client.post(
        "/api/accounts",
        headers=auth_headers,
        json={"name": "Date Hist", "type": "checking", "balance": 500, "currency": "BRL"},
    )
    account_id = acc_resp.json()["id"]
    today = date.today()

    response = await client.get(
        f"/api/accounts/{account_id}/balance-history",
        params={
            "from": today.replace(day=1).isoformat(),
            "to": today.isoformat(),
        },
        headers=auth_headers,
    )
    assert response.status_code == 200


@pytest.mark.asyncio
async def test_account_balance_history_not_found(client: AsyncClient, auth_headers):
    """Balance history for nonexistent account returns 404."""
    response = await client.get(
        "/api/accounts/00000000-0000-0000-0000-000000000000/balance-history",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_account_summary_with_date_params(client: AsyncClient, auth_headers):
    """Account summary accepts from/to date parameters."""
    acc_resp = await client.post(
        "/api/accounts",
        headers=auth_headers,
        json={"name": "Sum Dates", "type": "checking", "balance": 0, "currency": "BRL"},
    )
    account_id = acc_resp.json()["id"]
    today = date.today()

    response = await client.get(
        f"/api/accounts/{account_id}/summary",
        params={
            "from": today.replace(day=1).isoformat(),
            "to": today.isoformat(),
        },
        headers=auth_headers,
    )
    assert response.status_code == 200


@pytest.mark.asyncio
async def test_close_account_api_not_found(client: AsyncClient, auth_headers):
    """Close nonexistent account returns 404."""
    response = await client.post(
        "/api/accounts/00000000-0000-0000-0000-000000000000/close",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_reopen_account_api_not_found(client: AsyncClient, auth_headers):
    """Reopen nonexistent account returns 404."""
    response = await client.post(
        "/api/accounts/00000000-0000-0000-0000-000000000000/reopen",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_delete_account_api_not_found(client: AsyncClient, auth_headers):
    """Delete nonexistent account returns 404."""
    response = await client.delete(
        "/api/accounts/00000000-0000-0000-0000-000000000000",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_update_account_api_not_found(client: AsyncClient, auth_headers):
    """Update nonexistent account returns 404."""
    response = await client.patch(
        "/api/accounts/00000000-0000-0000-0000-000000000000",
        headers=auth_headers,
        json={"name": "Ghost"},
    )
    assert response.status_code == 404


# ---------------------------------------------------------------------------
# Assets API — portfolio trend and missing endpoints
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_portfolio_trend(client: AsyncClient, auth_headers, test_user):
    """GET /api/assets/portfolio-trend returns data."""
    response = await client.get("/api/assets/portfolio-trend", headers=auth_headers)
    assert response.status_code == 200
    data = response.json()
    assert "assets" in data
    assert "trend" in data
    assert "total" in data


@pytest.mark.asyncio
async def test_update_asset_not_found(client: AsyncClient, auth_headers):
    """PATCH nonexistent asset returns 404."""
    response = await client.patch(
        f"/api/assets/{uuid.uuid4()}",
        headers=auth_headers,
        json={"name": "Ghost"},
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_delete_asset_not_found(client: AsyncClient, auth_headers):
    """DELETE nonexistent asset returns 404."""
    response = await client.delete(
        f"/api/assets/{uuid.uuid4()}", headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_list_asset_values_not_found(client: AsyncClient, auth_headers):
    """GET values for nonexistent asset returns 404."""
    response = await client.get(
        f"/api/assets/{uuid.uuid4()}/values", headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_add_asset_value_not_found(client: AsyncClient, auth_headers):
    """POST value to nonexistent asset returns 404."""
    response = await client.post(
        f"/api/assets/{uuid.uuid4()}/values",
        headers=auth_headers,
        json={"amount": 1000, "date": "2026-01-01"},
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_delete_asset_value_not_found(client: AsyncClient, auth_headers):
    """DELETE nonexistent asset value returns 404."""
    response = await client.delete(
        f"/api/assets/values/{uuid.uuid4()}", headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_get_asset_value_trend_not_found(client: AsyncClient, auth_headers):
    """GET value-trend for nonexistent asset returns 404."""
    response = await client.get(
        f"/api/assets/{uuid.uuid4()}/value-trend", headers=auth_headers,
    )
    assert response.status_code == 404


# ---------------------------------------------------------------------------
# Connections API — transfer detection and additional
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_detect_transfers_api(client: AsyncClient, auth_headers):
    """POST /api/connections/transfers/detect returns pairs_created count."""
    response = await client.post(
        "/api/connections/transfers/detect", headers=auth_headers,
    )
    assert response.status_code == 200
    assert "pairs_created" in response.json()


@pytest.mark.asyncio
async def test_unlink_transfer_not_found(client: AsyncClient, auth_headers):
    """DELETE nonexistent transfer pair returns 404."""
    response = await client.delete(
        f"/api/connections/transfers/{uuid.uuid4()}", headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_reconnect_token_not_found(client: AsyncClient, auth_headers):
    """POST reconnect-token for nonexistent connection returns 404."""
    response = await client.post(
        f"/api/connections/{uuid.uuid4()}/reconnect-token",
        headers=auth_headers,
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_reconnect_token_no_item_id(
    client: AsyncClient, auth_headers, test_connection,
):
    """POST reconnect-token when connection has no item_id returns 400."""
    response = await client.post(
        f"/api/connections/{test_connection.id}/reconnect-token",
        headers=auth_headers,
    )
    assert response.status_code == 400
    assert "item_id" in response.json()["detail"].lower()


# ---------------------------------------------------------------------------
# Dashboard API — balance-history and projected-transactions
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_dashboard_projected_transactions(
    client: AsyncClient, auth_headers, test_categories, test_account,
):
    """GET /api/dashboard/projected-transactions returns projections."""
    # Create a recurring transaction
    next_month = date.today().replace(day=1)
    if next_month.month == 12:
        next_month = next_month.replace(year=next_month.year + 1, month=1)
    else:
        next_month = next_month.replace(month=next_month.month + 1)

    await client.post(
        "/api/recurring-transactions",
        headers=auth_headers,
        json={
            "description": "Proj Test",
            "amount": 100,
            "type": "debit",
            "frequency": "monthly",
            "start_date": next_month.isoformat(),
            "category_id": str(test_categories[0].id),
            "account_id": str(test_account.id),
        },
    )

    response = await client.get(
        "/api/dashboard/projected-transactions",
        params={"month": next_month.isoformat()},
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data, list)
    assert len(data) >= 1


@pytest.mark.asyncio
async def test_dashboard_balance_history(client: AsyncClient, auth_headers):
    """GET /api/dashboard/balance-history returns current month and baseline data."""
    # Create an account first
    await client.post(
        "/api/accounts",
        headers=auth_headers,
        json={"name": "BH Test", "type": "checking", "balance": 1000, "currency": "BRL"},
    )

    response = await client.get(
        "/api/dashboard/balance-history", headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()
    assert "current" in data
    assert "opening_balance" in data
    assert "baseline" in data
    assert isinstance(data["current"], list)
    assert len(data["baseline"]) == len(data["current"])


@pytest.mark.asyncio
async def test_dashboard_balance_history_accepts_comparison_months(
    client: AsyncClient, auth_headers
):
    response = await client.get(
        "/api/dashboard/balance-history",
        params=[
            ("month", "2026-04-01"),
            ("comparison_months", "2026-01-01"),
            ("comparison_months", "2026-03-01"),
        ],
        headers=auth_headers,
    )

    assert response.status_code == 200
    data = response.json()
    assert data["comparison"] is None
    assert len(data["baseline"]) == len(data["current"])

    months_response = await client.get(
        "/api/dashboard/balance-history/months", headers=auth_headers
    )
    assert months_response.status_code == 200
    assert isinstance(months_response.json(), list)


@pytest.mark.asyncio
async def test_dashboard_balance_history_rejects_non_prior_comparison_month(
    client: AsyncClient, auth_headers
):
    response = await client.get(
        "/api/dashboard/balance-history",
        params=[
            ("month", "2026-04-01"),
            ("comparison_months", "2026-04-01"),
        ],
        headers=auth_headers,
    )

    assert response.status_code == 422


# ---------------------------------------------------------------------------
# Import API — additional edge cases
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_import_duplicate_skips(
    client: AsyncClient, auth_headers, test_account: Account,
):
    """Re-importing the same transactions skips duplicates."""
    transactions = [
        {
            "description": "Dup Test",
            "amount": "99.99",
            "date": "2026-01-15",
            "type": "debit",
        },
    ]

    # First import
    resp1 = await client.post(
        "/api/transactions/import",
        headers=auth_headers,
        json={
            "account_id": str(test_account.id),
            "transactions": transactions,
        },
    )
    assert resp1.status_code == 201
    assert resp1.json()["imported"] == 1

    # Second import — same data should be skipped
    resp2 = await client.post(
        "/api/transactions/import",
        headers=auth_headers,
        json={
            "account_id": str(test_account.id),
            "transactions": transactions,
        },
    )
    assert resp2.status_code == 201
    assert resp2.json()["imported"] == 0
    assert resp2.json()["skipped"] == 1


@pytest.mark.asyncio
async def test_preview_ofx_file_unknown_extension(client: AsyncClient, auth_headers):
    """Preview with unknown extension tries auto-detection chain."""
    csv_content = b"date,description,amount\n2026-01-15,Test,-50.00\n"
    response = await client.post(
        "/api/transactions/import/preview",
        headers=auth_headers,
        files={"file": ("data.txt", csv_content, "text/plain")},
    )
    # Auto-detection tries ofx → qif → camt → csv.  QIF parser may "succeed"
    # with 0 transactions before reaching CSV, so we only assert the code path runs.
    assert response.status_code == 200
    data = response.json()
    assert data["detected_format"] in ("csv", "qif", "camt")


@pytest.mark.asyncio
async def test_preview_qif_format(client: AsyncClient, auth_headers):
    """Preview QIF file."""
    qif_content = b"!Type:Bank\nD01/15/2026\nT-50.00\nPTest Payment\n^\n"
    response = await client.post(
        "/api/transactions/import/preview",
        headers=auth_headers,
        files={"file": ("data.qif", qif_content, "application/qif")},
    )
    assert response.status_code == 200
    data = response.json()
    assert data["detected_format"] == "qif"
    assert len(data["transactions"]) >= 1


@pytest.mark.asyncio
async def test_preview_csv_with_date_format(client: AsyncClient, auth_headers):
    """Preview CSV with explicit date format."""
    csv_content = b"date,description,amount\n03/15/2026,US Format,-30.00\n"
    response = await client.post(
        "/api/transactions/import/preview",
        headers=auth_headers,
        files={"file": ("us.csv", csv_content, "text/csv")},
        data={"date_format": "MM/DD/YYYY"},
    )
    assert response.status_code == 200
    data = response.json()
    assert len(data["transactions"]) >= 1


@pytest.mark.asyncio
async def test_preview_csv_with_flip_amount(client: AsyncClient, auth_headers):
    """Preview CSV with flip_amount option."""
    csv_content = b"date,description,amount\n2026-01-15,Inverted,50.00\n"
    response = await client.post(
        "/api/transactions/import/preview",
        headers=auth_headers,
        files={"file": ("flip.csv", csv_content, "text/csv")},
        data={"flip_amount": "true"},
    )
    assert response.status_code == 200
    data = response.json()
    # Amount was positive, flip makes it negative -> debit
    assert data["transactions"][0]["type"] == "debit"


@pytest.mark.asyncio
async def test_preview_csv_split_columns(client: AsyncClient, auth_headers):
    """Preview CSV with inflow/outflow split columns."""
    csv_content = b"date,description,inflow,outflow\n2026-01-15,Salary,5000,\n2026-01-16,Rent,,1500\n"
    response = await client.post(
        "/api/transactions/import/preview",
        headers=auth_headers,
        files={"file": ("split.csv", csv_content, "text/csv")},
        data={"inflow_column": "inflow", "outflow_column": "outflow"},
    )
    assert response.status_code == 200
    data = response.json()
    assert len(data["transactions"]) == 2
    assert data["transactions"][0]["type"] == "credit"
    assert data["transactions"][1]["type"] == "debit"