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>
This commit is contained in:
1 parent
17b48f25e8
commit
4b3e5f0932
47 files changed
+4189
-862
No files matched your search
@@ -0,0 +1,126 @@
|
||||
from conftest import CSV_HEADER, SAMPLE_CSV
|
||||
|
||||
|
||||
def upload(client, name, text):
|
||||
return client.post(f"/api/upload?filename={name}", content=text.encode())
|
||||
|
||||
|
||||
def test_book_import_and_figures(admin):
|
||||
assert admin.post("/api/books", json={"name": "cashbook-2026-27", "year": 2026,
|
||||
"opening_balance": "100"}).status_code == 200
|
||||
assert admin.post("/api/books", json={"name": "cashbook-2026-27", "year": 2026}).status_code == 400
|
||||
|
||||
r = upload(admin, "april.csv", SAMPLE_CSV)
|
||||
assert r.status_code == 200
|
||||
path = r.json()["path"]
|
||||
|
||||
stmts = admin.get("/api/statements?book=cashbook-2026-27").json()
|
||||
assert [(s["name"], s["count"], s["imported"]) for s in stmts] == [("april.csv", 5, 0)]
|
||||
|
||||
preview = admin.post("/api/preview", json={"book": "cashbook-2026-27", "statements": [path]}).json()
|
||||
assert [p["status"] for p in preview] == ["new"] * 5
|
||||
fee = next(p for p in preview if p["reference"] == "Monthly A/C Fee")
|
||||
assert (fee["gst"], fee["category"]) == ("Excl GST", "Bank fees")
|
||||
|
||||
result = admin.post("/api/import", json={"book": "cashbook-2026-27", "statements": [path]}).json()
|
||||
assert sum(r["added"] for r in result) == 5
|
||||
# importing again adds nothing
|
||||
result = admin.post("/api/import", json={"book": "cashbook-2026-27", "statements": [path]}).json()
|
||||
assert sum(r["added"] for r in result) == 0
|
||||
|
||||
s = admin.get("/api/summary?book=cashbook-2026-27").json()
|
||||
apr, may = s["months"][0], s["months"][1]
|
||||
assert (apr["count"], may["count"]) == (3, 2)
|
||||
assert round(apr["closing"], 2) == 100 - 8.50 - 115 + 1150
|
||||
assert round(s["closing"], 2) == round(100 - 8.50 - 115 + 1150 + 500 - 5.53, 2)
|
||||
assert may["income_cats"]["Funds Intr."] == 500
|
||||
|
||||
|
||||
def test_edit_rows_and_gst_return(admin):
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
|
||||
admin.post("/api/import", json={"book": "b", "statements": [path]})
|
||||
|
||||
month = admin.get("/api/month?book=b&sheet=APR").json()
|
||||
sale = next(r for r in month["rows"] if r["income"] == 1150)
|
||||
jaycar = next(r for r in month["rows"] if r["expense"] == 115)
|
||||
assert jaycar["gst"] == "Incl GST" and jaycar["gst_out"] == 15.0
|
||||
|
||||
# mark the sale as GST-inclusive and allocate it
|
||||
r = admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Incl GST", "category": "Sales"})
|
||||
updated = next(x for x in r.json()["rows"] if x["row"] == sale["row"])
|
||||
assert (updated["gst_in"], updated["category"], updated["unallocated"]) == (150.0, "Sales", 0)
|
||||
assert admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"],
|
||||
"category": "Purchases"}).status_code == 400
|
||||
assert admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"],
|
||||
"gst": "Maybe"}).status_code == 400
|
||||
|
||||
p = admin.get("/api/gst?book=b&frequency=six").json()["periods"][0]
|
||||
assert p["due"] == "2026-10-28"
|
||||
b = p["boxes"]
|
||||
assert (b["5"], b["6"], b["8"], b["11"], b["12"], b["15"]) == (1150, 0, 150.0, 115, 15.0, 135.0)
|
||||
|
||||
# zero-rated sales count in boxes 5 and 6 but carry no GST
|
||||
admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Zero-rated"})
|
||||
b = admin.get("/api/gst?book=b").json()["periods"][0]["boxes"]
|
||||
assert (b["5"], b["6"], b["7"], b["8"], b["15"]) == (1150, 1150, 0, 0.0, -15.0)
|
||||
|
||||
two = admin.get("/api/gst?book=b&frequency=two").json()["periods"]
|
||||
assert [(x["end"], x["due"]) for x in two][:1] == [("2026-05-31", "2026-06-28")]
|
||||
assert two[-1]["due"] == "2027-05-07"
|
||||
|
||||
|
||||
def test_other_years_are_skipped(admin):
|
||||
admin.post("/api/books", json={"name": "old", "year": 2020})
|
||||
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
|
||||
preview = admin.post("/api/preview", json={"book": "old", "statements": [path]}).json()
|
||||
assert {p["status"] for p in preview} == {"wrong year"}
|
||||
result = admin.post("/api/import", json={"book": "old", "statements": [path]}).json()
|
||||
assert sum(r["added"] for r in result) == 0
|
||||
|
||||
|
||||
def test_csv_export(admin):
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
|
||||
admin.post("/api/import", json={"book": "b", "statements": [path]})
|
||||
r = admin.get("/api/export?book=b&format=csv")
|
||||
assert r.status_code == 200
|
||||
assert 'filename="b.csv"' in r.headers["content-disposition"]
|
||||
lines = r.content.decode("utf-8-sig").splitlines()
|
||||
assert lines[0].startswith("Date,Month,Reference")
|
||||
assert len(lines) == 6
|
||||
assert admin.get("/api/export?book=b&format=pdf").status_code == 400
|
||||
assert admin.get("/api/export?book=nope&format=csv").status_code == 404
|
||||
|
||||
|
||||
def test_statement_management(admin):
|
||||
r = upload(admin, "notes.csv", "a,b\n1,2\n")
|
||||
assert r.status_code == 400 and "ANZ" in r.json()["detail"]
|
||||
assert upload(admin, "evil.sh", "x").status_code == 400
|
||||
|
||||
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
|
||||
r = upload(admin, "again.csv", SAMPLE_CSV)
|
||||
assert r.status_code == 400 and "already here" in r.json()["detail"]
|
||||
|
||||
view = admin.get("/api/statement", params={"path": path}).json()
|
||||
assert len(view["transactions"]) == 5
|
||||
|
||||
assert admin.get("/api/statement/file", params={"path": path}).text.startswith("Type,Details")
|
||||
assert admin.get("/api/statement/file", params={"path": "/etc/passwd"}).status_code == 400
|
||||
|
||||
new_path = admin.post("/api/statement/rename", json={"path": path, "name": "april-2026.csv"}).json()["path"]
|
||||
assert new_path.endswith("april-2026.csv")
|
||||
assert admin.post("/api/statement/rename", json={"path": new_path, "name": "x.pdf"}).status_code == 400
|
||||
|
||||
assert admin.post("/api/statement/delete", json={"path": new_path}).status_code == 200
|
||||
assert admin.get("/api/statements").json() == []
|
||||
|
||||
|
||||
def test_csv_and_pdf_of_same_period_dont_double_up(admin):
|
||||
"""Matching is on date and amount, so differently-worded copies are recognised."""
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
first = upload(admin, "a.csv", SAMPLE_CSV).json()["path"]
|
||||
shouty = CSV_HEADER + "\n".join(line.upper() for line in SAMPLE_CSV.splitlines()[1:]) + "\n"
|
||||
second = upload(admin, "b.csv", shouty).json()["path"]
|
||||
result = admin.post("/api/import", json={"book": "b", "statements": [first, second]}).json()
|
||||
assert sum(r["added"] for r in result) == 5
|
||||
Reference in new issue
Block a user