gabriel / musehub public
test_mcp_gate_errors.py python
127 lines 5.6 KB
Raw
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ breaking 1 day ago
1 """MCP gate and dispatcher errors carry ``error_code`` (musehub#295).
2
3 The server's ``initialize`` instructions tell agents that every error has a machine-readable ``error_code`` and to
4 branch on it. Executor errors always did; these tests pin the refusals produced before or around the executor: the
5 authentication gate, the scope gate, repository access, owner/slug resolution, an unknown tool and an unhandled
6 exception. None of them may echo raw exception text. Database-free: every DB touch is stubbed.
7 """
8
9 from __future__ import annotations
10
11 import json
12 from collections.abc import Iterator
13 from unittest.mock import AsyncMock, patch
14
15 import pytest
16
17 from musehub.auth.repo_access import mcp_caller_scope
18 from musehub.mcp.dispatcher import _handle_tools_call, handle_batch
19 from musehub.types.json_types import JSONObject
20
21 pytestmark = pytest.mark.no_db
22
23 _SECRET = "postgresql://musehub:[email protected]/musehub"
24
25
26 def _err(result: JSONObject) -> dict[str, object]:
27 assert result["isError"] is True
28 content = result["content"]
29 assert isinstance(content, list)
30 body = json.loads(content[0]["text"]) # type: ignore[index]
31 assert isinstance(body.get("error_code"), str) and isinstance(body.get("error_message"), str), body
32 assert "error" not in body
33 return body
34
35
36 @pytest.fixture()
37 def db_ready() -> Iterator[None]:
38 with patch("musehub.services.musehub_mcp_executor._check_db_available", return_value=None):
39 yield
40
41
42 @pytest.mark.asyncio
43 async def test_auth_gate_in_a_batch_is_unauthenticated() -> None:
44 out = await handle_batch([{"jsonrpc": "2.0", "id": 1, "method": "tools/call",
45 "params": {"name": "musehub_create_issue", "arguments": {}}}], user_id=None)
46 body = _err(out[0]["result"]) # type: ignore[arg-type]
47 assert body["error_code"] == "unauthenticated" and "hint" in body
48
49
50 @pytest.mark.asyncio
51 async def test_scope_gate_is_forbidden() -> None:
52 token = mcp_caller_scope.set(["repo:read"])
53 try:
54 out = await _handle_tools_call({"name": "musehub_create_issue", "arguments": {}}, user_id="agent-1", session=None)
55 finally:
56 mcp_caller_scope.reset(token)
57 body = _err(out)
58 assert body["error_code"] == "forbidden" and "issue:write" in str(body["error_message"])
59
60
61 @pytest.mark.asyncio
62 async def test_repo_access_gate_is_repo_not_found(db_ready: None) -> None:
63 with patch("musehub.mcp.dispatcher._caller_can_read_repo", AsyncMock(return_value=False)):
64 out = await _handle_tools_call({"name": "musehub_list_branches", "arguments": {"repo_id": "r1"}},
65 user_id=None, session=None)
66 assert _err(out)["error_code"] == "repo_not_found"
67
68
69 @pytest.mark.asyncio
70 async def test_repo_write_gate_is_forbidden(db_ready: None) -> None:
71 with (
72 patch("musehub.mcp.dispatcher._caller_can_read_repo", AsyncMock(return_value=True)),
73 patch("musehub.mcp.dispatcher._caller_can_write_repo", AsyncMock(return_value=False)),
74 ):
75 out = await _handle_tools_call({"name": "musehub_list_webhook_deliveries", "arguments": {"repo_id": "r1"}},
76 user_id="alice", session=None)
77 assert _err(out)["error_code"] == "forbidden"
78
79
80 @pytest.mark.asyncio
81 async def test_database_unavailable_is_db_unavailable() -> None:
82 with patch("musehub.db.database._async_session_factory", None):
83 out = await _handle_tools_call({"name": "musehub_list_branches", "arguments": {"owner": "o", "slug": "s"}},
84 user_id=None, session=None)
85 assert _err(out)["error_code"] == "db_unavailable"
86
87
88 @pytest.mark.asyncio
89 async def test_resolution_failure_is_internal_error_without_the_exception_text(db_ready: None) -> None:
90 with patch("musehub.db.database.AsyncSessionLocal", side_effect=OSError(f"connect to {_SECRET} refused")):
91 out = await _handle_tools_call({"name": "musehub_list_branches", "arguments": {"owner": "o", "slug": "s"}},
92 user_id=None, session=None)
93 body = _err(out)
94 assert body["error_code"] == "internal_error" and "hunter2" not in json.dumps(body)
95
96
97 @pytest.mark.asyncio
98 async def test_unknown_tool_is_invalid_args() -> None:
99 out = await _handle_tools_call({"name": "musehub_no_such_tool", "arguments": {}}, user_id="alice", session=None)
100 body = _err(out)
101 assert body["error_code"] == "invalid_args" and "tools/list" in str(body["hint"])
102
103
104 @pytest.mark.asyncio
105 async def test_executor_exception_is_internal_error_without_the_exception_text() -> None:
106 with patch("musehub.services.musehub_mcp_executor.execute_whoami", AsyncMock(side_effect=RuntimeError(_SECRET))):
107 out = await _handle_tools_call({"name": "musehub_whoami", "arguments": {}}, user_id="alice", session=None)
108 body = _err(out)
109 assert body["error_code"] == "internal_error" and "hunter2" not in json.dumps(body)
110
111
112 def test_the_http_signature_gate_carries_an_error_code() -> None:
113 from fastapi import FastAPI
114 from fastapi.testclient import TestClient
115
116 from musehub.api.routes import mcp as route
117 from musehub.auth.request_signing import optional_signed_request
118 from musehub.rate_limits import limiter
119
120 app = FastAPI()
121 app.state.limiter = limiter
122 app.include_router(route.router)
123 app.dependency_overrides[optional_signed_request] = lambda: None
124 with TestClient(app) as c:
125 r = c.post("/mcp", json={"jsonrpc": "2.0", "id": 1, "method": "tools/call",
126 "params": {"name": "musehub_create_issue", "arguments": {}}})
127 assert r.status_code == 401 and r.json()["error"]["data"]["error_code"] == "unauthenticated"
File History 3 commits
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ 1 day ago
sha256:87f4c4d976afad92db715748495c09600f4aafed5f924e6b4b1664aab5a86fad merge: rc26 — MCP wave (#291 #292 #295 #324) Opus 5.5 minor ⚠ 1 day ago
sha256:e0601efe55c167c25a25964556649adc67857bd02e415016a026c852e1a535b2 fix(mcp): gate and dispatcher errors carry error_code/error… Opus 5.5 minor ⚠ 2 days ago