Files
rar-autopass/tests/test_dashboard.py
Claude Agent 958b182e8e feat(config): web_auth_required ON implicit (login obligatoriu pe rutele web)
Inverseaza default-ul C12: rutele web cer sesiune + CSRF implicit (sigur pentru
prod). Dev rapid pe contul 1 = opt-out explicit AUTOPASS_WEB_AUTH_REQUIRED=false.
Testele de comportament import/dashboard marcate explicit dev-mode; test nou
blocheaza default-ul. 394 teste pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 18:27:09 +00:00

113 lines
3.7 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"))
# Comportament in mod dev (fallback cont 1, fara login/CSRF); auth web e
# default ON in prod — testat separat in test_web_*.
monkeypatch.setenv("AUTOPASS_WEB_AUTH_REQUIRED", "false")
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"