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

+57 -56
View File
@@ -28,66 +28,68 @@ EXPECTED_TOOLS = {
"idee_etiqueter",
}
def test_health():
with TestClient(app) as client:
r = client.get("/health")
assert r.status_code == 200
data = r.json()
assert data.get("status") == "ok"
assert data.get("service") == "taf-mcp"
assert data.get("tools") == 6
@pytest.fixture(scope="module")
def client():
with TestClient(app) as c:
yield c
def test_auth_rejection():
with TestClient(app) as client:
# Sans header 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
# Mauvais token
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_health(client):
r = client.get("/health")
assert r.status_code == 200
data = r.json()
assert data.get("status") == "ok"
assert data.get("service") == "taf-mcp"
assert data.get("tools") == 6
def test_mcp_session_and_tools_list():
with TestClient(app) as client:
# Initialize
r_init = client.post(
"/mcp",
headers=AUTH_HEADERS,
json={
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0"}
}
def test_auth_rejection(client):
# Sans header 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
# Mauvais token
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(client):
# Initialize
r_init = client.post(
"/mcp",
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 sid is not None and len(sid) > 0
}
)
assert r_init.status_code == 200
sid = r_init.headers.get("mcp-session-id")
assert sid is not None and len(sid) > 0
# Initialized notification
headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid}
r_notif = client.post(
"/mcp",
headers=headers_sid,
json={"jsonrpc": "2.0", "method": "notifications/initialized"}
)
assert r_notif.status_code in (200, 202)
# Initialized notification
headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid}
r_notif = client.post(
"/mcp",
headers=headers_sid,
json={"jsonrpc": "2.0", "method": "notifications/initialized"}
)
assert r_notif.status_code in (200, 202)
# Tools list
r_tools = client.post(
"/mcp",
headers=headers_sid,
json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"}
)
assert r_tools.status_code == 200
data = r_tools.json()
tools = data.get("result", {}).get("tools", [])
tool_names = {t["name"] for t in tools}
assert tool_names == EXPECTED_TOOLS
# Tools list
r_tools = client.post(
"/mcp",
headers=headers_sid,
json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"}
)
assert r_tools.status_code == 200
data = r_tools.json()
tools = data.get("result", {}).get("tools", [])
tool_names = {t["name"] for t in tools}
assert tool_names == EXPECTED_TOOLS
def test_rule_a_text_intouchable():
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):
import main
# Mock get_all_tasks renvoyant une tâche de l'espace 'taches'
monkeypatch.setattr(main, "get_all_tasks", lambda: [
{"id": "task-taches-1", "text": "Faire les courses", "space": "taches", "deleted": False}
])