Big Sweep: Security + Race-Conditions + Tests + DSGVO + A11y, SW by-v1095
SECURITY (auth.py, routes/auth.py, database.py, main.py) - JWT bekommt jti; Logout trägt in neue jwt_blacklist-Tabelle ein, decode_token() prüft → server-side Invalidierung - JWT-Expiry default 30 → 7 Tage (ENV JWT_EXPIRY_DAYS überschreibt) - Sliding-Refresh-Middleware: erneuert Cookie wenn >50% verbraucht (Schwelle via JWT_REFRESH_FRACTION, Default 2) - Login-Lockout in DB-Tabelle login_attempts (5 Versuche / 15 Min, überlebt Container-Restart) — alte In-Memory-Lockouts ersetzt - SMTP-Versand: alle 'except: pass' durch logger.exception ersetzt; Fehlversuche landen in failed_emails-Tabelle für späteres Retry - Referral-Counter Race gefixt: UPDATE partner_codes SET uses=uses+1 ... WHERE uses<max_uses RETURNING — atomar statt SELECT+UPDATE RACE CONDITIONS (routes/invoices.py, database.py) - Neue invoice_counters-Tabelle für atomare Nummernvergabe - _next_invoice_number nutzt BEGIN IMMEDIATE + atomares UPDATE - Funktioniert für RG- und ST-Prefixe (Stornorechnungen) - Race-Test verifiziert (5 Threads × 20 Calls = 100 eindeutige Nummern) VERSION + TESTS + ERROR-DIGEST (VERSION, Makefile, tests/, scheduler.py) - Neue VERSION-Datei (Single Source of Truth) — main.py liest beim Startup - Makefile-Target 'make bump' propagiert in sw.js, app.js, index.html - Makefile-Target 'make test' setzt venv auf, läuft pytest - 19 Smoke-Tests in tests/ (health, auth, diary, invoice) — alle grün - Scheduler: täglicher _job_error_digest um 06:30 → schickt Error- Zusammenfassung an ADMIN_EMAIL (still wenn keine Errors) DSGVO + A11Y + ERSTE-HILFE - landing.html: 'HTML und ODS' → 'JSON' (tatsächlich implementiert) - datenschutz.js: Sektion Account-Löschung erweitert (sofort gelöscht / anonymisiert / 10 Jahre für Rechnungen) - erste-hilfe.js: prominentes Warning-Banner oben (ersetzt keine Tierarzt-Beratung); Notfallnummern gruppiert nach Land, TODO-Platz- halter für AT-Uni-Klinik, CH Tox Info Suisse, CH Tierspital Zürich - ui.js Modal: ESC schließt, Focus-Trap, Auto-Focus erstes Element, Restore Focus auf vorigen Caller - impressum.js Kontaktformular: Labels mit for=cf-name etc. NEUE DB-TABELLEN (idempotent via CREATE TABLE IF NOT EXISTS) - jwt_blacklist, login_attempts, failed_emails, invoice_counters NEUE ENV-VARS - JWT_REFRESH_FRACTION (Default 2) - JWT_EXPIRY_DAYS Default geändert (30 → 7)
This commit is contained in:
parent
6224044654
commit
9394bab1fb
23 changed files with 1208 additions and 78 deletions
91
tests/test_invoice.py
Normal file
91
tests/test_invoice.py
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
"""Smoke-Tests fuer Rechnungs-CRUD (Admin)."""
|
||||
|
||||
import os
|
||||
import secrets
|
||||
|
||||
|
||||
def test_admin_create_invoice(client, admin):
|
||||
"""POST /api/admin/invoices als Admin -> 201 mit Rechnungsnummer."""
|
||||
r = client.post(
|
||||
"/api/admin/invoices",
|
||||
headers=admin["headers"],
|
||||
json={
|
||||
"recipient_name": "Max Mustermann",
|
||||
"recipient_email": "max@example.com",
|
||||
"items": [{"description": "Pro-Jahresabo", "quantity": 1, "unit_price": 29.0}],
|
||||
"service_period": "2026-01-01 - 2026-12-31",
|
||||
"notes": "Test-Rechnung",
|
||||
},
|
||||
)
|
||||
assert r.status_code == 201, r.text
|
||||
data = r.json()
|
||||
assert data["invoice_number"].startswith("RG-")
|
||||
assert data["amount_gross"] == 29.0
|
||||
assert data["status"] == "draft"
|
||||
assert len(data["items"]) == 1
|
||||
|
||||
|
||||
def test_admin_list_invoices(client, admin):
|
||||
"""GET /api/admin/invoices listet vorhandene Rechnungen."""
|
||||
# Eine erstellen
|
||||
client.post(
|
||||
"/api/admin/invoices",
|
||||
headers=admin["headers"],
|
||||
json={
|
||||
"recipient_name": "Tester Listing",
|
||||
"recipient_email": f"list-{secrets.token_hex(3)}@example.com",
|
||||
"items": [{"description": "Sub", "quantity": 1, "unit_price": 49.0}],
|
||||
},
|
||||
)
|
||||
r = client.get("/api/admin/invoices", headers=admin["headers"])
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) >= 1
|
||||
|
||||
|
||||
def test_non_admin_cannot_create_invoice(client, user):
|
||||
"""Normaler User -> 403."""
|
||||
r = client.post(
|
||||
"/api/admin/invoices",
|
||||
headers=user["headers"],
|
||||
json={
|
||||
"recipient_name": "x",
|
||||
"recipient_email": "x@example.com",
|
||||
"items": [{"description": "x", "quantity": 1, "unit_price": 1.0}],
|
||||
},
|
||||
)
|
||||
assert r.status_code == 403
|
||||
|
||||
|
||||
def test_admin_send_invoice(client, admin, tmp_path, monkeypatch):
|
||||
"""Send-Endpoint generiert PDF, ruft (gemockten) Mailer auf, setzt Status auf 'sent'."""
|
||||
# Paperless-Verzeichnis auf tmp_path setzen
|
||||
scaninput = tmp_path / "scaninput"
|
||||
scaninput.mkdir()
|
||||
monkeypatch.setenv("SCANINPUT_DIR", str(scaninput))
|
||||
monkeypatch.setenv("PAPERLESS_URL", "") # kein HTTP-Call
|
||||
monkeypatch.setenv("KLEINUNTERNEHMER", "true")
|
||||
|
||||
# Rechnung anlegen
|
||||
r = client.post(
|
||||
"/api/admin/invoices",
|
||||
headers=admin["headers"],
|
||||
json={
|
||||
"recipient_name": "Send-Test",
|
||||
"recipient_email": f"send-{secrets.token_hex(3)}@example.com",
|
||||
"items": [{"description": "Sub", "quantity": 1, "unit_price": 29.0}],
|
||||
"service_period": "2026-01-01 - 2026-12-31",
|
||||
},
|
||||
)
|
||||
assert r.status_code == 201, r.text
|
||||
inv = r.json()
|
||||
|
||||
# Senden
|
||||
r2 = client.post(f"/api/admin/invoices/{inv['id']}/send", headers=admin["headers"])
|
||||
assert r2.status_code == 200, r2.text
|
||||
|
||||
# Status auf 'sent'?
|
||||
r3 = client.get(f"/api/admin/invoices/{inv['id']}", headers=admin["headers"])
|
||||
assert r3.status_code == 200
|
||||
assert r3.json()["status"] == "sent"
|
||||
Loading…
Add table
Add a link
Reference in a new issue