File size: 1,743 Bytes
5cd3cff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
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}