""" Tests de `POST /api/v1/internal/reservas/tick`: no usa `get_current_user` (no hay usuario humano detrás de un cron externo) — se protege con `X-Internal-Token` comparado contra `settings.internal_tick_secret`. """ from fastapi.testclient import TestClient from app.config import settings TICK_URL = "/api/v1/internal/reservas/tick" def test_tick_sin_header_401(client: TestClient, monkeypatch): monkeypatch.setattr(settings, "internal_tick_secret", "el-secreto-correcto") resp = client.post(TICK_URL) assert resp.status_code == 422 # Header(...) obligatorio, FastAPI valida antes def test_tick_token_incorrecto_401(client: TestClient, monkeypatch): monkeypatch.setattr(settings, "internal_tick_secret", "el-secreto-correcto") resp = client.post(TICK_URL, headers={"X-Internal-Token": "otro-valor"}) assert resp.status_code == 401 assert resp.json()["detail"]["codigo"] == "NO_AUTENTICADO" def test_tick_secreto_no_configurado_401(client: TestClient, monkeypatch): """internal_tick_secret="" (default) rechaza todo — falla cerrado, no abierto.""" monkeypatch.setattr(settings, "internal_tick_secret", "") resp = client.post(TICK_URL, headers={"X-Internal-Token": "cualquiera"}) assert resp.status_code == 401 def test_tick_ok(client: TestClient, monkeypatch): monkeypatch.setattr(settings, "internal_tick_secret", "el-secreto-correcto") monkeypatch.setattr( "app.services.cama_service.procesar_transiciones_automaticas", lambda db: {"iniciadas": 2, "completadas": 1}, ) resp = client.post(TICK_URL, headers={"X-Internal-Token": "el-secreto-correcto"}) assert resp.status_code == 200 assert resp.json() == {"iniciadas": 2, "completadas": 1}