"""Teste US-002 (PRD 5.6): request_id per cerere + header X-Request-ID.""" from __future__ import annotations 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, "rid.db")) monkeypatch.setenv("AUTOPASS_LOG_DIR", os.path.join(tmp, "logs")) 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 test_raspuns_are_header_x_request_id(client): r = client.get("/healthz") assert r.status_code == 200 assert r.headers.get("X-Request-ID"), "lipseste header X-Request-ID" def test_request_id_distinct_pe_cereri(client): a = client.get("/healthz").headers.get("X-Request-ID") b = client.get("/healthz").headers.get("X-Request-ID") assert a and b and a != b def test_request_id_pastrat_daca_clientul_trimite(client): r = client.get("/healthz", headers={"X-Request-ID": "corelare-abc"}) assert r.headers.get("X-Request-ID") == "corelare-abc" def test_request_id_propagat_in_log(client): """request_id e disponibil in log_event pe durata cererii (contextvar).""" from app import observ r = client.get("/healthz", headers={"X-Request-ID": "rid-xyz"}) assert r.headers["X-Request-ID"] == "rid-xyz" # In afara cererii, contextvar revine la None (reset in middleware) assert observ.request_id_var.get() is None