pydantic incluía las variables de entorno (contraseñas demo) en el mensaje de validación; hide_input_in_errors lo evita.
91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
"""`SECRET_KEY` obligatoria: sin ella la app no arranca, salvo con `APP_ENV=test`."""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from app.config import Settings
|
|
|
|
RAIZ = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@pytest.fixture
|
|
def entorno_limpio(monkeypatch: pytest.MonkeyPatch) -> pytest.MonkeyPatch:
|
|
for variable in ("SECRET_KEY", "APP_ENV"):
|
|
monkeypatch.delenv(variable, raising=False)
|
|
return monkeypatch
|
|
|
|
|
|
@pytest.mark.parametrize("clave", [None, "", " ", "dev-solo-local-cambiar"])
|
|
@pytest.mark.parametrize("app_env", [None, "dev", "prod"])
|
|
def test_sin_secret_key_propia_las_settings_fallan(
|
|
entorno_limpio: pytest.MonkeyPatch, clave: str | None, app_env: str | None
|
|
) -> None:
|
|
if clave is not None:
|
|
entorno_limpio.setenv("SECRET_KEY", clave)
|
|
if app_env is not None:
|
|
entorno_limpio.setenv("APP_ENV", app_env)
|
|
|
|
with pytest.raises(ValidationError, match="SECRET_KEY no está definida"):
|
|
Settings(_env_file=None)
|
|
|
|
|
|
def test_el_error_no_vuelca_las_contrasenas_del_entorno(
|
|
entorno_limpio: pytest.MonkeyPatch,
|
|
) -> None:
|
|
entorno_limpio.setenv("DEMO_PASSWORD_OFICIAL", "contrasena-que-no-debe-salir")
|
|
|
|
with pytest.raises(ValidationError) as error:
|
|
Settings(_env_file=None)
|
|
assert "contrasena-que-no-debe-salir" not in str(error.value)
|
|
|
|
|
|
def test_con_app_env_test_arranca_sin_secret_key(entorno_limpio: pytest.MonkeyPatch) -> None:
|
|
entorno_limpio.setenv("APP_ENV", "test")
|
|
|
|
settings = Settings(_env_file=None)
|
|
|
|
assert len(settings.secret_key) >= 32
|
|
assert Settings(_env_file=None).secret_key != settings.secret_key # se genera por proceso
|
|
|
|
|
|
def test_una_secret_key_propia_se_respeta(entorno_limpio: pytest.MonkeyPatch) -> None:
|
|
entorno_limpio.setenv("SECRET_KEY", "una-clave-propia-y-larga")
|
|
assert Settings(_env_file=None).secret_key == "una-clave-propia-y-larga"
|
|
|
|
|
|
def _arrancar_app(tmp_path: Path, **extra: str) -> subprocess.CompletedProcess[str]:
|
|
"""Importa `app.main` en un proceso aparte, sin `.env` y sin las variables del test."""
|
|
entorno = {
|
|
clave: valor
|
|
for clave, valor in os.environ.items()
|
|
if clave not in ("SECRET_KEY", "APP_ENV")
|
|
}
|
|
entorno |= {"PYTHONPATH": str(RAIZ), "DATABASE_URL": "sqlite://", **extra}
|
|
return subprocess.run(
|
|
[sys.executable, "-c", "import app.main"],
|
|
cwd=tmp_path,
|
|
env=entorno,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=60,
|
|
)
|
|
|
|
|
|
def test_sin_secret_key_la_app_no_levanta(tmp_path: Path) -> None:
|
|
proceso = _arrancar_app(tmp_path, DEMO_PASSWORD_OFICIAL="contrasena-que-no-debe-salir")
|
|
|
|
assert proceso.returncode != 0
|
|
assert "SECRET_KEY no está definida" in proceso.stderr
|
|
assert "contrasena-que-no-debe-salir" not in proceso.stderr
|
|
|
|
|
|
def test_con_app_env_test_la_app_levanta(tmp_path: Path) -> None:
|
|
proceso = _arrancar_app(tmp_path, APP_ENV="test")
|
|
|
|
assert proceso.returncode == 0, proceso.stderr
|