import logging import pytest import config_validator as cv OBLIGATORIAS = [v for grupo in cv.OBLIGATORIAS.values() for v in grupo] OPCIONALES = ["RUTA_RED_FACTURACION", "RUC_EMPRESA", "LOG_LEVEL", *cv.VERIFICACION_SSL] @pytest.fixture def env_completo(monkeypatch): """Entorno válido y sin variables opcionales (para partir de un estado conocido).""" for v in OBLIGATORIAS: monkeypatch.setenv(v, "valor") for v in OPCIONALES: monkeypatch.delenv(v, raising=False) def test_entorno_completo_no_tiene_errores(env_completo): errores, _ = cv.validar_configuracion() assert errores == [] def test_faltantes_se_agrupan_por_servicio(env_completo, monkeypatch): monkeypatch.delenv("CLIENT_ID") monkeypatch.setenv("HANA_USER", " ") # solo espacios cuenta como vacío errores, _ = cv.validar_configuracion() assert len(errores) == 2 assert any(e.startswith("Microsoft Graph") and "CLIENT_ID" in e for e in errores) assert any(e.startswith("SAP HANA") and "HANA_USER" in e for e in errores) def test_ruta_de_red_sin_ruc_es_error(env_completo, monkeypatch): monkeypatch.setenv("RUTA_RED_FACTURACION", "//srv/facturas") errores, _ = cv.validar_configuracion() assert any("RUC_EMPRESA" in e for e in errores) def test_sin_ruta_de_red_solo_advierte(env_completo): errores, advertencias = cv.validar_configuracion() assert errores == [] assert any("RUTA_RED_FACTURACION" in a for a in advertencias) def test_advertencias_de_ssl(env_completo, monkeypatch): _, advertencias = cv.validar_configuracion() assert sum("Verificación SSL" in a for a in advertencias) == 2 monkeypatch.setenv("VERIFY_SSL", "TRUE") # no distingue mayúsculas _, advertencias = cv.validar_configuracion() assert not any("Microsoft Graph" in a and "SSL" in a for a in advertencias) assert any("SAP B1" in a and "SSL" in a for a in advertencias) def test_log_level_invalido_advierte(env_completo, monkeypatch): monkeypatch.setenv("LOG_LEVEL", "CHISME") _, advertencias = cv.validar_configuracion() assert any("LOG_LEVEL" in a for a in advertencias) monkeypatch.setenv("LOG_LEVEL", "debug") _, advertencias = cv.validar_configuracion() assert not any("LOG_LEVEL" in a for a in advertencias) def test_verificar_termina_con_codigo_1_si_hay_errores(env_completo, monkeypatch, caplog): monkeypatch.delenv("EMAIL") with caplog.at_level(logging.ERROR), pytest.raises(SystemExit) as salida: cv.verificar_configuracion() assert salida.value.code == 1 assert "EMAIL" in caplog.text def test_verificar_no_termina_si_todo_esta_bien(env_completo): cv.verificar_configuracion() # no debe lanzar SystemExit