import pytest from httpx import AsyncClient, ASGITransport from app.main import app from app.services.dni_parser_service import DNIParsingService @pytest.mark.asyncio async def test_parse_dni_api(): transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: # Autenticación previa login_res = await client.post( "/api/v1/auth/login", json={"username": "guardia", "password": "guardia"}, ) token = login_res.cookies.get("access_token") raw_qr = "0034567890@PEREZ@JUAN CARLOS@M@30123456@A@01/01/1985@01/01/2020@200" res = await client.post( "/api/v1/access/parse-dni", json={"raw_data": raw_qr}, cookies={"access_token": token}, ) assert res.status_code == 200 data = res.json() assert data["valido"] is True assert data["documento"] == "30123456" assert data["apellido"] == "PEREZ" assert data["nombre"] == "JUAN CARLOS" assert data["sexo"] == "M" @pytest.mark.asyncio async def test_search_people_and_lots(): transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: login_res = await client.post( "/api/v1/auth/login", json={"username": "guardia", "password": "guardia"}, ) token = login_res.cookies.get("access_token") # Búsqueda de personas p_res = await client.get( "/api/v1/access/people/search?q=TEST", cookies={"access_token": token}, ) assert p_res.status_code == 200 assert isinstance(p_res.json(), list) # Búsqueda de lotes l_res = await client.get( "/api/v1/access/lots/search?q=1", cookies={"access_token": token}, ) assert l_res.status_code == 200 assert isinstance(l_res.json(), list) @pytest.mark.asyncio async def test_hardware_manual_open(): transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: login_res = await client.post( "/api/v1/auth/login", json={"username": "guardia", "password": "guardia"}, ) token = login_res.cookies.get("access_token") res = await client.post( "/api/v1/hardware/modules/100004/open", json={"modulo_id": 100004, "motivo": "TEST_AUDIT"}, cookies={"access_token": token}, ) assert res.status_code == 200 data = res.json() assert data["status"] == "ok" assert data["modulo_id"] == 100004 assert data["latencia_ms"] < 500.0 @pytest.mark.asyncio async def test_flujo_completo_ingreso_y_salida(): transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: login_res = await client.post( "/api/v1/auth/login", json={"username": "guardia", "password": "guardia"}, ) token = login_res.cookies.get("access_token") doc_test = "99887766" # 1. Registro de Ingreso entry_payload = { "modulo_ingreso_id": 100004, "documento": doc_test, "apellido": "TESTER", "nombre": "INTEGRAL", "sexo": "M", "patente": "TST999", "lote_codigo": "100", "motivo": "PRUEBA AUTOMATIZADA", "cantidad_ocupantes": 1, "abrir_barrera": True, } entry_res = await client.post( "/api/v1/access/entry", json=entry_payload, cookies={"access_token": token}, ) assert entry_res.status_code == 200 tck_data = entry_res.json() assert tck_data["status"] == "ok" ticket_id = tck_data["ticket_id"] assert ticket_id > 0 assert tck_data["barrier_opened"] is True # 2. Verificar que aparezca en tickets activos active_res = await client.get( "/api/v1/access/tickets/active", cookies={"access_token": token}, ) assert active_res.status_code == 200 tickets = active_res.json() ids_activos = [t["ticket_id"] for t in tickets] assert ticket_id in ids_activos # 3. Intentar reingreso (Antipassback) dup_res = await client.post( "/api/v1/access/entry", json=entry_payload, cookies={"access_token": token}, ) assert dup_res.status_code == 409 assert "Antipassback" in dup_res.json()["detail"] # 4. Registrar Salida exit_payload = { "ticket_id": ticket_id, "modulo_salida_id": 100005, "patente_salida": "TST999", "abrir_barrera": True, } exit_res = await client.post( "/api/v1/access/exit", json=exit_payload, cookies={"access_token": token}, ) assert exit_res.status_code == 200 exit_data = exit_res.json() assert exit_data["status"] == "ok" assert exit_data["barrier_opened"] is True # 5. Verificar que ya no esté en activos active_res2 = await client.get( "/api/v1/access/tickets/active", cookies={"access_token": token}, ) ids_activos2 = [t["ticket_id"] for t in active_res2.json()] assert ticket_id not in ids_activos2 # Teardown / Limpieza from app.core.database import db_session with db_session() as conn: cursor = conn.cursor() cursor.execute("DELETE FROM PerDocCatVal WHERE Documento = %s", (doc_test,)) cursor.execute("DELETE FROM Per WHERE Apellido = 'TESTER'") cursor.execute("DELETE FROM pdtmdl WHERE ModuloID IN (100004, 100005) AND Extra = %s", (str(ticket_id),)) cursor.execute("DELETE FROM mvthist WHERE TicketID = %s", (ticket_id,)) cursor.execute("DELETE FROM tckhst WHERE TicketID = %s", (ticket_id,))