From dc0f5e0909ad142f2c85e11b27752fc47e1f1090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Tue, 6 Oct 2026 16:52:14 -0600 Subject: [PATCH] test(laboratorio): pantallas de riesgo y laboratorio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simular no escribe; guardar sin rol oficial da 403 y con ponderaciones inválidas 400 con mensajes; comparar trae las tres secciones. Los tests de navegación y autoregistro dejan de fijar a mano qué pantallas existen. --- tests/test_auth.py | 6 +- tests/test_laboratorio.py | 316 +++++++++++++++++++++++++++++ tests/test_routers_autoregistro.py | 11 +- 3 files changed, 331 insertions(+), 2 deletions(-) create mode 100644 tests/test_laboratorio.py diff --git a/tests/test_auth.py b/tests/test_auth.py index fd4bad7..c337d16 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -235,7 +235,11 @@ def test_inicio_muestra_tenant_regimen_actividad_y_los_ocho_pasos(cliente: TestC assert "Cartera" in lateral # Lo que aún no existe va deshabilitado y dice en qué fase llega; Inicio ya es enlace. - assert lateral.count('aria-disabled="true"') == 9 + # Un paso sin pantalla todavía va deshabilitado con su fase; uno con pantalla, como enlace. + from app.routers import NAVEGACION + + sin_pantalla = [e for e in NAVEGACION if f'href="{e.ruta}"' not in lateral] + assert lateral.count('aria-disabled="true"') == len(sin_pantalla) > 0 assert "fase 4" in lateral assert 'href="/inicio"' in lateral assert 'href="/expediente"' not in lateral diff --git a/tests/test_laboratorio.py b/tests/test_laboratorio.py new file mode 100644 index 0000000..4d26093 --- /dev/null +++ b/tests/test_laboratorio.py @@ -0,0 +1,316 @@ +"""Pantallas de riesgo y laboratorio con el cliente de prueba (Fase 2, PR 3).""" + +import time +from collections.abc import Iterator +from pathlib import Path + +import pytest +import sqlalchemy as sa +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session, sessionmaker + +from app.db import crear_engine, crear_session_factory +from app.importers.chm_schema import importar_esquema +from app.main import crear_app +from app.models import ( + AgregadoCliente, + AuditTrail, + Base, + Cliente, + EvaluacionRiesgo, + Indicador, + ModeloRiesgo, + NivelIndicador, + Umbral, +) +from app.models.usuario import Rol +from app.services.riesgo import evaluar_cartera +from seed.usuarios_demo import sembrar +from tests.fixtures.oraculo import FIXTURE + +CONTRASENAS = { + Rol.ANALISTA: "clave-analista-1", + Rol.OFICIAL: "clave-oficial-2", + Rol.AUDITOR: "clave-auditor-3", +} +COMPENSADO = {"ponderaciones": {"T1": "0.25", "T3": "0.05"}} +TABLAS_DEL_MODELO = ( + ModeloRiesgo, + Indicador, + NivelIndicador, + Umbral, + EvaluacionRiesgo, + AgregadoCliente, +) + + +@pytest.fixture +def fabrica(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> sessionmaker[Session]: + engine = crear_engine("sqlite://") + Base.metadata.create_all(engine) + fabrica = crear_session_factory(engine) + monkeypatch.setattr("app.db.SessionLocal", fabrica) + with fabrica() as sesion: + importar_esquema(sesion, FIXTURE, "pytest", sal="sal", ruta_reglas=tmp_path / "no.json") + sembrar(sesion, CONTRASENAS) + evaluar_cartera(sesion, "inicial") + sesion.commit() + return fabrica + + +@pytest.fixture +def cliente(fabrica: sessionmaker[Session]) -> Iterator[TestClient]: + with TestClient(crear_app(), follow_redirects=False) as c: + yield c + + +def entrar(c: TestClient, rol: Rol) -> None: + respuesta = c.post("/login", data={"usuario": rol.value, "contrasena": CONTRASENAS[rol]}) + assert respuesta.status_code == 303 + + +def conteos(fabrica: sessionmaker[Session]) -> dict[str, int]: + with fabrica() as sesion: + return { + t.__tablename__: sesion.scalar(sa.select(sa.func.count()).select_from(t)) + for t in TABLAS_DEL_MODELO + } + + +def v2_id(fabrica: sessionmaker[Session]) -> int: + with fabrica() as sesion: + return sesion.scalar(sa.select(ModeloRiesgo.id).where(ModeloRiesgo.version == "v2")) + + +# --- Pantallas de riesgo ------------------------------------------------------------------- + + +def test_riesgo_lista_la_cartera_y_se_habilita_en_la_navegacion(cliente: TestClient) -> None: + entrar(cliente, Rol.AUDITOR) + respuesta = cliente.get("/riesgo") + + assert respuesta.status_code == 200 + assert "30 clientes evaluados" in respuesta.text + lateral = respuesta.text[respuesta.text.index('