fix(server): handle multiple lifespan runs and module-scoped testclient
This commit is contained in:
1 parent
15339a62f2
commit
b75e952738
2 files changed
+11
-7
No files matched your search
@@ -213,6 +213,9 @@ async def health(request):
|
||||
|
||||
@asynccontextmanager
|
||||
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():
|
||||
yield
|
||||
|
||||
|
||||
@@ -28,8 +28,12 @@ EXPECTED_TOOLS = {
|
||||
"idee_etiqueter",
|
||||
}
|
||||
|
||||
def test_health():
|
||||
with TestClient(app) as client:
|
||||
@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()
|
||||
@@ -37,8 +41,7 @@ def test_health():
|
||||
assert data.get("service") == "taf-mcp"
|
||||
assert data.get("tools") == 6
|
||||
|
||||
def test_auth_rejection():
|
||||
with TestClient(app) as client:
|
||||
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
|
||||
@@ -47,8 +50,7 @@ def test_auth_rejection():
|
||||
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:
|
||||
def test_mcp_session_and_tools_list(client):
|
||||
# Initialize
|
||||
r_init = client.post(
|
||||
"/mcp",
|
||||
@@ -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}
|
||||
])
|
||||
|
||||
Reference in new issue
Block a user