← Files Dispensa: lista de comprasARCHIVED FILE

tests/test_trace.py

1.18 KB · Nov 15, 2026 · 06:36 UTC

↓ Download file

See the change to this file →

import logging

import pytest

from mcp_despensa_hub.trace import iniciar_consulta, log_consulta, metadados_segredo


def test_log_consulta_inclui_etapa_e_id_sem_vazar_campo_bloqueado(caplog):
    cid = iniciar_consulta()

    with caplog.at_level(logging.INFO, logger="mcp_despensa_hub.trace"):
        log_consulta(
            logging.getLogger("mcp_despensa_hub.trace"),
            "usuario_ok",
            workos_user_id="user_1",
        )

    assert f"consulta etapa=usuario_ok cid={cid}" in caplog.text
    assert "workos_user_id=user_1" in caplog.text


@pytest.mark.parametrize(
    "campo",
    [
        "token",
        "access_token",
        "jwt",
        "password",
        "secret",
        "authorization",
    ],
)
def test_log_consulta_recusa_campo_secreto(campo):
    with pytest.raises(ValueError, match="campo de log bloqueado"):
        log_consulta(
            logging.getLogger("mcp_despensa_hub.trace"),
            "x",
            **{campo: "valor-secreto"},
        )


def test_metadados_segredo_nao_incluem_o_valor():
    valor = "jwt.abc-token-gigante"
    meta = metadados_segredo(valor)

    assert meta["segredo_len"] == len(valor)
    assert valor not in repr(meta)

SHA-256: 764ea2143b33decbc7d64fffebeba1c215861642b63e27e528906c6256d8f48e