T5 (tools/import_dbf.py): citire prestatii_rar.DBF / mapare_prestatii.DBF cu dbfread, raport dry-run (randuri valide/duplicate/goale, mapari orfane = cod necunoscut in nomenclator), --commit cu upsert idempotent in tranzactie. Dashboard: browser nomenclator, indicator stare RAR (indisponibil? derivat din ultimul login < 30h, coada arata ultima stare locala), export audit CSV (/v1/audit/export?status=sent|all&date_from&date_to, b64Image exclus, coloana purge_after pentru retentia 90z). Verify: 11 teste noi (test_import_dbf 6, test_dashboard 5), suita 111 pass, dry-run real pe DBF-urile din repo + smoke live dashboard/CSV. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
"""Teste dashboard + audit CSV: nomenclator browser, stare RAR, export CSV."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import io
|
|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(monkeypatch):
|
|
tmp = tempfile.mkdtemp()
|
|
monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "t.db"))
|
|
from app.config import get_settings
|
|
|
|
get_settings.cache_clear()
|
|
from app.main import app
|
|
|
|
with TestClient(app) as c:
|
|
yield c
|
|
get_settings.cache_clear()
|
|
|
|
|
|
def _body(**over):
|
|
prez = {
|
|
"vin": "WVWZZZ1KZAW000123",
|
|
"nr_inmatriculare": "B999TST",
|
|
"data_prestatie": "2026-06-15",
|
|
"odometru_final": "123456",
|
|
"prestatii": [{"cod_prestatie": "OE-1"}],
|
|
}
|
|
prez.update(over)
|
|
return {"rar_credentials": {"email": "x@y.ro", "password": "s"}, "prezentari": [prez]}
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Dashboard render + fragmente #
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
def test_dashboard_renders_with_rar_state(client):
|
|
r = client.get("/")
|
|
assert r.status_code == 200
|
|
# worker neavand heartbeat -> stare RAR necunoscuta (worker oprit)
|
|
assert "worker oprit" in r.text
|
|
assert "Nomenclator RAR" in r.text
|
|
|
|
|
|
def test_nomenclator_fragment_lists_seed(client):
|
|
r = client.get("/_fragments/nomenclator")
|
|
assert r.status_code == 200
|
|
# seed fallback are 18 coduri; OE-1 + R-ODO trebuie sa apara
|
|
assert "OE-1" in r.text
|
|
assert "R-ODO" in r.text
|
|
|
|
|
|
def test_submissions_fragment_empty_state(client):
|
|
r = client.get("/_fragments/submissions")
|
|
assert r.status_code == 200
|
|
assert "Coada e goala" in r.text
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Audit CSV export #
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
def test_audit_export_sent_only(client):
|
|
# un submission queued (validare ok) + unul needs_data
|
|
client.post("/v1/prezentari", json=_body())
|
|
client.post("/v1/prezentari", json=_body(vin="BAD"))
|
|
|
|
# status implicit = sent -> niciun rand (nimic trimis inca), doar header
|
|
r = client.get("/v1/audit/export")
|
|
assert r.status_code == 200
|
|
assert r.headers["content-type"].startswith("text/csv")
|
|
assert "attachment" in r.headers["content-disposition"]
|
|
rows = list(csv.DictReader(io.StringIO(r.text)))
|
|
assert rows == []
|
|
|
|
# status=all -> ambele, cu coloane-cheie populate, fara b64_image
|
|
r = client.get("/v1/audit/export?status=all")
|
|
rows = list(csv.DictReader(io.StringIO(r.text)))
|
|
assert len(rows) == 2
|
|
assert "vin" in rows[0]
|
|
assert "b64_image" not in rows[0]
|
|
vins = {row["vin"] for row in rows}
|
|
assert "WVWZZZ1KZAW000123" in vins
|
|
# prestatii = coduri concatenate
|
|
assert any(row["prestatii"] == "OE-1" for row in rows)
|
|
|
|
|
|
def test_audit_export_marks_sent_after_update(client):
|
|
client.post("/v1/prezentari", json=_body())
|
|
# marcam manual sent (worker ar face asta dupa postPrezentare reusit)
|
|
from app.db import get_connection
|
|
|
|
conn = get_connection()
|
|
try:
|
|
conn.execute("UPDATE submissions SET status='sent', id_prezentare=68514 WHERE id=1")
|
|
finally:
|
|
conn.close()
|
|
|
|
rows = list(csv.DictReader(io.StringIO(client.get("/v1/audit/export").text)))
|
|
assert len(rows) == 1
|
|
assert rows[0]["status"] == "sent"
|
|
assert rows[0]["id_prezentare"] == "68514"
|