Files
jimmyandClaude Opus 5.5 4b3e5f0932 Move to FastAPI with accounts, SQLite books, export and tax returns
Storage and export
- Keep books and transactions in SQLite (data/cashbook.db) instead of
  .ods files; existing .ods books are migrated once at startup
- Export a book as the cashbook spreadsheet (.xlsx/.ods, built from the
  template via LibreOffice) or as CSV
- GST rounded per transaction to match the spreadsheet

Tax
- GST return page with IRD GST101A boxes 5-15 per six-monthly,
  two-monthly or monthly period, due dates, and a Zero-rated GST type
- Income tax page with IR10 (financial statements summary) and IR4
  (company return) boxes, from the cashbook plus year-end amounts typed
  in and saved per book

FastAPI and accounts (following led-editor)
- Replace the stdlib server with a FastAPI app (src/cashbook_app) run by
  uvicorn; dependencies in Pipfile
- Cookie sessions with bcrypt passwords, first-run admin setup or
  BOOTSTRAP_ADMIN_*, invites (link or email), sign-in throttling,
  password change that signs out other sessions
- Users page for administrators: rename, reset password, promote or
  demote, delete, list and revoke invites; last admin is protected
- Replaces the shared CASHBOOK_PASSWORD basic auth; AUTH_* settings in
  .env (see .env.example)
- Docker image uses a venv with system site packages for python3-uno
- pytest suite for auth, user management, imports, GST and tax figures

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 22:43:18 +13:00

168 lines
8.2 KiB
Python

from fastapi.testclient import TestClient
from conftest import invite, make_client
def test_status_and_first_run_setup(client):
s = client.get("/api/auth/status").json()
assert s == {"auth_enabled": True, "register_open": True, "invite_required": True, "setup_required": True}
# the first account needs no invite and becomes the administrator, signed straight in
r = client.post("/api/auth/register", json={"username": "owner", "password": "password-1"})
assert r.status_code == 200
assert r.json()["is_superuser"] is True
assert client.get("/api/auth/me").json()["user"]["username"] == "owner"
assert client.get("/api/auth/status").json()["setup_required"] is False
def test_api_and_pages_need_sign_in(client):
for path in ("/api/books", "/api/statements", "/api/summary?book=x", "/api/users", "/api/export?book=x"):
assert client.get(path).status_code == 401, path
assert client.post("/api/books", json={"name": "x", "year": 2026}).status_code == 401
assert client.get("/", follow_redirects=False).headers["location"] == "/login"
assert client.get("/users", follow_redirects=False).status_code == 307
# public bits
assert client.get("/healthz").json() == {"ok": True}
assert client.get("/login").status_code == 200
def test_registration_needs_invite_after_setup(admin, tmp_path, monkeypatch):
other = TestClient(admin.app)
r = other.post("/api/auth/register", json={"username": "stranger", "password": "password-1"})
assert r.status_code == 403
token = invite(admin, email="bob@example.com")
r = other.post("/api/auth/register", json={"username": "bob", "password": "password-1", "invite_token": token})
assert r.status_code == 200
assert r.json()["is_superuser"] is False
# invites work once
r = other.post("/api/auth/register", json={"username": "bob2", "password": "password-1", "invite_token": token})
assert r.status_code == 409
def test_admin_invite_grants_admin(admin):
token = invite(admin, grants_superuser=True)
other = TestClient(admin.app)
r = other.post("/api/auth/register", json={"username": "second", "password": "password-1", "invite_token": token})
assert r.json()["is_superuser"] is True
def test_login_logout_and_wrong_password(admin):
c = TestClient(admin.app)
assert c.post("/api/auth/login", json={"username": "admin", "password": "nope-nope"}).status_code == 401
assert c.post("/api/auth/login", json={"username": "ghost", "password": "nope-nope"}).status_code == 401
r = c.post("/api/auth/login", json={"username": "ADMIN", "password": "admin-pass-1"}) # case-insensitive
assert r.status_code == 200
assert c.get("/api/books").status_code == 200
c.post("/api/auth/logout")
assert c.get("/api/books").status_code == 401
def test_failed_sign_ins_are_throttled(admin):
c = TestClient(admin.app)
for _ in range(5):
assert c.post("/api/auth/login", json={"username": "admin", "password": "wrong-pass"}).status_code == 401
r = c.post("/api/auth/login", json={"username": "admin", "password": "admin-pass-1"})
assert r.status_code == 429
def test_change_password_signs_out_other_sessions(admin):
laptop = TestClient(admin.app)
laptop.post("/api/auth/login", json={"username": "admin", "password": "admin-pass-1"})
assert laptop.get("/api/books").status_code == 200
r = admin.post("/api/auth/password", json={"current_password": "wrong", "new_password": "new-pass-123"})
assert r.status_code == 400
r = admin.post("/api/auth/password", json={"current_password": "admin-pass-1", "new_password": "new-pass-123"})
assert r.status_code == 200
assert admin.get("/api/books").status_code == 200 # this session stays
assert laptop.get("/api/books").status_code == 401 # other devices are signed out
assert laptop.post("/api/auth/login", json={"username": "admin", "password": "new-pass-123"}).status_code == 200
def test_user_management(admin):
token = invite(admin)
bob = TestClient(admin.app)
bob_id = bob.post("/api/auth/register", json={"username": "bob", "password": "password-1",
"invite_token": token}).json()["id"]
# regular users can't manage users
assert bob.get("/api/users").status_code == 403
assert bob.post("/api/users/invites", json={}).status_code == 403
assert bob.get("/api/books").status_code == 200
users = admin.get("/api/users").json()
assert [u["username"] for u in users] == ["admin", "bob"]
assert admin.patch(f"/api/users/{bob_id}", json={"username": "robert"}).json()["username"] == "robert"
assert admin.patch(f"/api/users/{bob_id}", json={"username": "admin"}).status_code == 400
assert admin.patch(f"/api/users/{bob_id}", json={"is_superuser": True}).json()["is_superuser"] is True
# resetting a password signs them out
assert admin.patch(f"/api/users/{bob_id}", json={"password": "reset-pass-1"}).status_code == 200
assert bob.get("/api/books").status_code == 401
admin_id = users[0]["id"]
assert admin.delete(f"/api/users/{admin_id}").status_code == 400 # not yourself
assert admin.patch(f"/api/users/{admin_id}", json={"is_superuser": False}).status_code == 400
assert admin.delete(f"/api/users/{bob_id}").status_code == 200
assert admin.delete(f"/api/users/{bob_id}").status_code == 404
def test_last_admin_is_protected(admin):
token = invite(admin)
bob = TestClient(admin.app)
bob_id = bob.post("/api/auth/register", json={"username": "bob", "password": "password-1",
"invite_token": token}).json()["id"]
admin.patch(f"/api/users/{bob_id}", json={"is_superuser": True})
admin_id = next(u["id"] for u in admin.get("/api/users").json() if u["username"] == "admin")
# bob demotes admin, then can't demote or delete himself as the last admin
bob.post("/api/auth/login", json={"username": "bob", "password": "password-1"})
assert bob.patch(f"/api/users/{admin_id}", json={"is_superuser": False}).status_code == 200
assert bob.patch(f"/api/users/{bob_id}", json={"is_superuser": False}).status_code == 400
def test_invites_list_and_revoke(admin):
invite(admin, email="a@example.com")
pending = admin.get("/api/users/invites").json()
assert [i["email"] for i in pending] == ["a@example.com"]
assert admin.delete(f"/api/users/invites/{pending[0]['id']}").status_code == 200
assert admin.get("/api/users/invites").json() == []
assert admin.post("/api/users/invites", json={"email": "not-an-email"}).status_code == 422
def test_cross_site_changes_are_refused(admin):
r = admin.post("/api/books", json={"name": "x", "year": 2026}, headers={"Origin": "https://evil.example"})
assert r.status_code == 403
r = admin.post("/api/books", json={"name": "x", "year": 2026}, headers={"Origin": "http://testserver"})
assert r.status_code == 200
def test_auth_disabled_is_open(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_ENABLED="false") as c:
assert c.get("/api/books").status_code == 200
assert c.get("/", follow_redirects=False).status_code == 200
assert c.get("/api/auth/me").json() == {"auth_enabled": False, "user": None}
assert c.get("/api/users").status_code == 400
def test_empty_auth_setting_keeps_auth_on(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_ENABLED="") as c:
assert c.get("/api/books").status_code == 401
def test_bootstrap_admin(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, BOOTSTRAP_ADMIN_USERNAME="boss",
BOOTSTRAP_ADMIN_PASSWORD="boss-pass-1") as c:
assert c.get("/api/auth/status").json()["setup_required"] is False
r = c.post("/api/auth/login", json={"username": "boss", "password": "boss-pass-1"})
assert r.json()["is_superuser"] is True
def test_open_registration_without_invites(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_INVITE_ONLY="false") as c:
c.post("/api/auth/register", json={"username": "first", "password": "password-1"})
other = TestClient(c.app)
r = other.post("/api/auth/register", json={"username": "second", "password": "password-1"})
assert r.status_code == 200 and r.json()["is_superuser"] is False