116 lines
4.1 KiB
Python
116 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Tests unitaires et d'intégration pour le serveur MCP taf-mcp."""
|
|
import os
|
|
import json
|
|
import pytest
|
|
from starlette.testclient import TestClient
|
|
|
|
# Mock environnement de test si absent
|
|
if "TAF_MCP_TOKEN_OPENCLAW_PERSO" not in os.environ:
|
|
os.environ["TAF_MCP_TOKEN_OPENCLAW_PERSO"] = "test_token_openclaw_perso_123456789"
|
|
if "TAF_TOKEN" not in os.environ:
|
|
os.environ["TAF_TOKEN"] = "mock_taf_token"
|
|
|
|
from main import app, mcp
|
|
|
|
TEST_TOKEN = os.environ["TAF_MCP_TOKEN_OPENCLAW_PERSO"]
|
|
AUTH_HEADERS = {
|
|
"Authorization": f"Bearer {TEST_TOKEN}",
|
|
"Accept": "application/json, text/event-stream"
|
|
}
|
|
|
|
EXPECTED_TOOLS = {
|
|
"idee_ajouter",
|
|
"idees_lister",
|
|
"idee_rechercher",
|
|
"idee_recadrer",
|
|
"idee_statut",
|
|
"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
|
|
|
|
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_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"}
|
|
}
|
|
}
|
|
)
|
|
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)
|
|
|
|
# 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
|
|
|
|
with pytest.raises(ValueError, match="le texte brut de Nabil est intouchable, utiliser idee_recadrer"):
|
|
idee_recadrer(id="123", reformulation="reformule", text="nouveau")
|
|
|
|
with pytest.raises(ValueError, match="le texte brut de Nabil est intouchable, utiliser idee_recadrer"):
|
|
idee_recadrer(id="123", reformulation="reformule", texte="nouveau")
|
|
|
|
with pytest.raises(ValueError, match="le texte brut de Nabil est intouchable, utiliser idee_recadrer"):
|
|
idee_statut(id="123", statut="en_cours", text="nouveau")
|
|
|
|
with pytest.raises(ValueError, match="le texte brut de Nabil est intouchable, utiliser idee_recadrer"):
|
|
idee_etiqueter(id="123", etiquettes=["test"], texte="nouveau")
|
|
|
|
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}
|
|
])
|
|
|
|
with pytest.raises(ValueError, match="appartient à l'espace taches"):
|
|
main.check_idea_access("task-taches-1")
|