# -*- coding: utf-8 -*- import os import json import pytest from starlette.testclient import TestClient # Mock environnement de test os.environ["TREBLO_MCP_TOKEN_NYORA"] = "test_nyora_token_123456789012345678901234567890123456" os.environ["TREBLO_MCP_TOKEN_PERSO"] = "test_perso_token_123456789012345678901234567890123456" os.environ["TREBLO_KEY_NYORA"] = "test_nyora_key" os.environ["TREBLO_KEY_PERSO"] = "test_perso_key" from main import app, mcp TEST_TOKEN_NYORA = os.environ["TREBLO_MCP_TOKEN_NYORA"] AUTH_HEADERS_NYORA = { "Authorization": f"Bearer {TEST_TOKEN_NYORA}", "Accept": "application/json, text/event-stream" } EXPECTED_TOOLS = {"generer", "recuperer", "solde", "statut", "valider"} @pytest.fixture(scope="module") def client(): with TestClient(app) as c: yield c def test_health(client): r = client.get("/health") assert r.status_code == 200 data = r.json() assert data.get("status") == "ok" assert data.get("service") == "treblo-mcp" assert data.get("tools") == 5 def test_auth_rejection(client): # Sans auth r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) assert r1.status_code == 401 # Faux jeton r2 = client.post("/mcp", headers={"Authorization": "Bearer faux-jeton", "Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) assert r2.status_code == 401 def test_tools_list(client): # Initialise session r_init = client.post("/mcp", headers=AUTH_HEADERS_NYORA, json={ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0"} } }) assert r_init.status_code == 200 sid = r_init.headers.get("mcp-session-id") headers = dict(AUTH_HEADERS_NYORA) if sid: headers["mcp-session-id"] = sid client.post("/mcp", headers=headers, json={ "jsonrpc": "2.0", "method": "notifications/initialized" }) r_list = client.post("/mcp", headers=headers, json={ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }) assert r_list.status_code == 200 res = r_list.json() tools = {t["name"] for t in res.get("result", {}).get("tools", [])} assert tools == EXPECTED_TOOLS