import pytest from fastapi.testclient import TestClient CSV_HEADER = "Type,Details,Particulars,Code,Reference,Amount,Date,ForeignCurrencyAmount,ConversionCharge\n" SAMPLE_CSV = CSV_HEADER + ( "Bank Fee,Monthly A/C Fee,,,,-8.50,30/04/2026,,\n" "Visa Purchase,4835-****-****-7294 Df,,Jaycar Chris,,-115.00,13/04/2026,,\n" "Direct Credit,Client Ltd,Inv 101,,,1150.00,20/04/2026,,\n" "Transfer,01-0822-0082180-30,Credit,Transfer,123,500.00,02/05/2026,,\n" "Visa Purchase,4835-****-****-7294 If, 0.58,Hetzner.Com, 0.07,-5.53,06/05/2026,EUR 3.20 converted at 0.58,\n" ) def make_client(tmp_path, monkeypatch, **env): """A TestClient for a fresh app using tmp_path as the data folder.""" defaults = {"AUTH_ENABLED": "true", "AUTH_REGISTER_OPEN": "true", "AUTH_INVITE_ONLY": "true"} for k in ("BOOTSTRAP_ADMIN_USERNAME", "BOOTSTRAP_ADMIN_PASSWORD", "CASHBOOK_STATEMENTS", "CASHBOOK_ALLOWED_HOSTS", "PUBLIC_BASE_URL", "SMTP_HOST"): monkeypatch.delenv(k, raising=False) monkeypatch.setenv("CASHBOOK_DATA", str(tmp_path)) monkeypatch.setenv("HOME", str(tmp_path / "home")) # so ~/Downloads/anz isn't listed for k, v in {**defaults, **env}.items(): monkeypatch.setenv(k, v) from cashbook_app.db import session from cashbook_app.main import create_app from cashbook_app.services import accounts session.reset_engine() accounts._failures.clear() return TestClient(create_app()) @pytest.fixture def client(tmp_path, monkeypatch): with make_client(tmp_path, monkeypatch) as c: yield c @pytest.fixture def admin(client): """client signed in as the first (admin) account.""" r = client.post("/api/auth/register", json={"username": "admin", "password": "admin-pass-1"}) assert r.status_code == 200, r.text return client def invite(client, **body): r = client.post("/api/users/invites", json=body) assert r.status_code == 200, r.text return r.json()["invite_url"].split("invite=")[1]