164 lines
7.6 KiB
Python
164 lines
7.6 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from app.parsers.base import ParsedStatement, ParsedTransaction
|
|
|
|
H = {"Authorization": "Bearer test-key"}
|
|
|
|
FAKE = ParsedStatement(
|
|
bank="dkb", iban="DE02120300000000202051",
|
|
period_start=date(2026, 6, 1), period_end=date(2026, 6, 30),
|
|
opening_balance=Decimal("100.00"), closing_balance=Decimal("40.00"),
|
|
transactions=[ParsedTransaction(date(2026, 6, 3), date(2026, 6, 3),
|
|
Decimal("-60.00"), "Miete Juni", "Vermieter")])
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_parse(monkeypatch):
|
|
monkeypatch.setattr("app.services.importer.parse_pdf", lambda p: FAKE)
|
|
|
|
|
|
# Ein Auszug mit ZWEI identischen Buchungen (gleicher dedup_hash). Saldo bleibt
|
|
# konsistent (100 - 60 - 60 = -20). Beide muessen bestaetigt werden koennen.
|
|
FAKE_TWINS = ParsedStatement(
|
|
bank="dkb", iban="DE02120300000000202051",
|
|
period_start=date(2026, 6, 1), period_end=date(2026, 6, 30),
|
|
opening_balance=Decimal("100.00"), closing_balance=Decimal("-20.00"),
|
|
transactions=[
|
|
ParsedTransaction(date(2026, 6, 3), date(2026, 6, 3),
|
|
Decimal("-60.00"), "Miete Juni", "Vermieter"),
|
|
ParsedTransaction(date(2026, 6, 3), date(2026, 6, 3),
|
|
Decimal("-60.00"), "Miete Juni", "Vermieter"),
|
|
])
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_parse_twins(monkeypatch):
|
|
monkeypatch.setattr("app.services.importer.parse_pdf", lambda p: FAKE_TWINS)
|
|
|
|
|
|
def _upload(client, name="auszug.pdf"):
|
|
return client.post("/api/imports/upload", headers=H,
|
|
files={"file": (name, b"%PDF-fake", "application/pdf")})
|
|
|
|
|
|
def test_upload_preview_confirm(client, fake_parse, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
r = _upload(client)
|
|
assert r.status_code == 201
|
|
sid = r.json()["id"]
|
|
prev = client.get(f"/api/imports/{sid}/preview", headers=H).json()
|
|
assert prev["balance_ok"] is True
|
|
assert len(prev["transactions"]) == 1
|
|
assert client.post(f"/api/imports/{sid}/confirm", headers=H).status_code == 200
|
|
txs = client.get("/api/transactions", headers=H).json()
|
|
assert len(txs) == 1 and txs[0]["status"] == "confirmed"
|
|
# Zweiter Import desselben Auszugs: alles Duplikate
|
|
sid2 = _upload(client, "auszug2.pdf").json()["id"]
|
|
prev2 = client.get(f"/api/imports/{sid2}/preview", headers=H).json()
|
|
assert prev2["duplicates"] == 1
|
|
client.post(f"/api/imports/{sid2}/confirm", headers=H)
|
|
assert len(client.get("/api/transactions", headers=H).json()) == 1
|
|
|
|
|
|
def test_confirm_rechecks_duplicates_across_two_drafts(client, fake_parse, tmp_path, monkeypatch):
|
|
# Beide Auszuege werden importiert, bevor einer bestaetigt ist -> zum
|
|
# Importzeitpunkt ist keine Buchung ein Duplikat (dedup prueft nur gegen
|
|
# bestaetigte). Erst beim zweiten Confirm faellt auf, dass die Buchung nun
|
|
# bereits bestaetigt existiert. Der Re-Check muss sie ueberspringen, sonst
|
|
# wird dieselbe Buchung doppelt bestaetigt.
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
sid1 = _upload(client, "a1.pdf").json()["id"]
|
|
sid2 = _upload(client, "a2.pdf").json()["id"]
|
|
assert client.post(f"/api/imports/{sid1}/confirm", headers=H).status_code == 200
|
|
assert client.post(f"/api/imports/{sid2}/confirm", headers=H).status_code == 200
|
|
# Zweites Confirm darf die Buchung nicht erneut bestaetigen.
|
|
assert len(client.get("/api/transactions", headers=H).json()) == 1
|
|
# Zweites Confirm eines bereits bestaetigten Imports -> 409.
|
|
assert client.post(f"/api/imports/{sid1}/confirm", headers=H).status_code == 409
|
|
|
|
|
|
def test_confirm_keeps_identical_twins_within_same_statement(client, fake_parse_twins, tmp_path, monkeypatch):
|
|
# Zwei identische Buchungen im SELBEN Auszug teilen sich den dedup_hash. Der
|
|
# Confirm-Re-Check darf nur statement-uebergreifende Duplikate abfangen,
|
|
# sonst wuerde die erste bestaetigte Buchung ihre Zwillingsbuchung als
|
|
# Duplikat markieren und den vom Preview geprueften Saldo zerstoeren.
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
sid = _upload(client).json()["id"]
|
|
prev = client.get(f"/api/imports/{sid}/preview", headers=H).json()
|
|
assert prev["balance_ok"] is True
|
|
assert prev["duplicates"] == 0
|
|
assert client.post(f"/api/imports/{sid}/confirm", headers=H).status_code == 200
|
|
txs = client.get("/api/transactions", headers=H).json()
|
|
assert len(txs) == 2
|
|
assert all(t["status"] == "confirmed" for t in txs)
|
|
|
|
|
|
def test_upload_sanitizes_path_traversal_filename(client, fake_parse, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
r = client.post("/api/imports/upload", headers=H,
|
|
files={"file": ("../evil.pdf", b"%PDF-fake", "application/pdf")})
|
|
assert r.status_code == 201
|
|
# Der Dateiname darf keine Pfad-Anteile aus dem Client uebernehmen: nur
|
|
# der Basisname landet im Uploads-Verzeichnis, nichts entkommt nach oben.
|
|
assert not (tmp_path / "evil.pdf").exists()
|
|
assert (tmp_path / "uploads" / "evil.pdf").exists()
|
|
assert not (tmp_path / "inbox" / "evil.pdf").exists()
|
|
|
|
|
|
def test_upload_rejects_non_pdf(client, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
r = client.post("/api/imports/upload", headers=H,
|
|
files={"file": ("x.txt", b"not a pdf", "text/plain")})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_delete_error_import_returns_204(client, db):
|
|
# Ein fehlgeschlagener Import (status="error") muss verworfen werden
|
|
# koennen - nur bestaetigte Importe sind vor dem Loeschen geschuetzt.
|
|
from app.models.tables import Statement
|
|
db.add(Statement(filename="kaputt.pdf", bank="dkb", status="error",
|
|
error_message="PDF nicht lesbar"))
|
|
db.commit()
|
|
sid = db.query(Statement).one().id
|
|
assert client.delete(f"/api/imports/{sid}", headers=H).status_code == 204
|
|
|
|
|
|
def test_upload_corrupt_pdf_produces_error_statement_not_500(client, tmp_path, monkeypatch):
|
|
# Datei besteht die Endungspruefung (.pdf), ist aber strukturell kein
|
|
# gueltiges PDF -> pdfplumber wirft eine eigene Exception (keine
|
|
# ParserError). Das darf nicht als unbehandelter 500 durchschlagen,
|
|
# sondern muss wie "Bank nicht erkannt" als sauberer Fehler-Import
|
|
# sichtbar werden.
|
|
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
|
|
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
r = client.post("/api/imports/upload", headers=H,
|
|
files={"file": ("kaputt.pdf", b"not a pdf", "application/pdf")})
|
|
assert r.status_code == 201
|
|
body = r.json()
|
|
assert body["status"] == "error"
|
|
assert "PDF nicht lesbar" in body["error_message"]
|
|
# Datei bleibt im Posteingang liegen, wird nicht ins Uploads-Verzeichnis verschoben.
|
|
assert (tmp_path / "inbox" / "kaputt.pdf").exists()
|
|
assert not (tmp_path / "uploads" / "kaputt.pdf").exists()
|