fix(server): handle multiple lifespan runs and module-scoped testclient

This commit is contained in:
bolbol committed 2026-10-28 08:13:32 +00:00
1 parent 15339a62f2
commit b75e952738
2 files changed
+60 -56

No files matched your search

+3
View File
@@ -213,6 +213,9 @@ async def health(request):
@asynccontextmanager @asynccontextmanager
async def lifespan(app_instance): async def lifespan(app_instance):
global session_manager
if getattr(session_manager, "_has_started", False):
session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True)
async with session_manager.run(): async with session_manager.run():
yield yield
+57 -56
View File
@@ -28,66 +28,68 @@ EXPECTED_TOOLS = {
"idee_etiqueter", "idee_etiqueter",
} }
def test_health(): @pytest.fixture(scope="module")
with TestClient(app) as client: def client():
r = client.get("/health") with TestClient(app) as c:
assert r.status_code == 200 yield c
data = r.json()
assert data.get("status") == "ok"
assert data.get("service") == "taf-mcp"
assert data.get("tools") == 6
def test_auth_rejection(): def test_health(client):
with TestClient(app) as client: r = client.get("/health")
# Sans header auth assert r.status_code == 200
r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) data = r.json()
assert r1.status_code == 401 assert data.get("status") == "ok"
assert data.get("service") == "taf-mcp"
# Mauvais token assert data.get("tools") == 6
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_mcp_session_and_tools_list(): def test_auth_rejection(client):
with TestClient(app) as client: # Sans header auth
# Initialize r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"})
r_init = client.post( assert r1.status_code == 401
"/mcp",
headers=AUTH_HEADERS, # Mauvais token
json={ r2 = client.post("/mcp", headers={"Authorization": "Bearer faux-jeton", "Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"})
"jsonrpc": "2.0", assert r2.status_code == 401
"id": 1,
"method": "initialize", def test_mcp_session_and_tools_list(client):
"params": { # Initialize
"protocolVersion": "2025-03-26", r_init = client.post(
"capabilities": {}, "/mcp",
"clientInfo": {"name": "test-client", "version": "1.0"} headers=AUTH_HEADERS,
} json={
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0"}
} }
) }
assert r_init.status_code == 200 )
sid = r_init.headers.get("mcp-session-id") assert r_init.status_code == 200
assert sid is not None and len(sid) > 0 sid = r_init.headers.get("mcp-session-id")
assert sid is not None and len(sid) > 0
# Initialized notification # Initialized notification
headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid} headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid}
r_notif = client.post( r_notif = client.post(
"/mcp", "/mcp",
headers=headers_sid, headers=headers_sid,
json={"jsonrpc": "2.0", "method": "notifications/initialized"} json={"jsonrpc": "2.0", "method": "notifications/initialized"}
) )
assert r_notif.status_code in (200, 202) assert r_notif.status_code in (200, 202)
# Tools list # Tools list
r_tools = client.post( r_tools = client.post(
"/mcp", "/mcp",
headers=headers_sid, headers=headers_sid,
json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"} json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"}
) )
assert r_tools.status_code == 200 assert r_tools.status_code == 200
data = r_tools.json() data = r_tools.json()
tools = data.get("result", {}).get("tools", []) tools = data.get("result", {}).get("tools", [])
tool_names = {t["name"] for t in tools} tool_names = {t["name"] for t in tools}
assert tool_names == EXPECTED_TOOLS assert tool_names == EXPECTED_TOOLS
def test_rule_a_text_intouchable(): def test_rule_a_text_intouchable():
from main import idee_recadrer, idee_statut, idee_etiqueter from main import idee_recadrer, idee_statut, idee_etiqueter
@@ -106,7 +108,6 @@ def test_rule_a_text_intouchable():
def test_rule_b_taches_refused(monkeypatch): def test_rule_b_taches_refused(monkeypatch):
import main import main
# Mock get_all_tasks renvoyant une tâche de l'espace 'taches'
monkeypatch.setattr(main, "get_all_tasks", lambda: [ monkeypatch.setattr(main, "get_all_tasks", lambda: [
{"id": "task-taches-1", "text": "Faire les courses", "space": "taches", "deleted": False} {"id": "task-taches-1", "text": "Faire les courses", "space": "taches", "deleted": False}
]) ])