test(server,ssap,cli): harden HTTP auth integration and remove tautological PERMISSIONS import
Core Architecture & Invariant CI / validate (pull_request) Successful in 15s

- Replace production PERMISSIONS import in test_ssap.py with explicit 16-item spec literal list
- Add test_server_http_app_allows_authenticated_mcp_initialize_request to verify Bearer auth and DNS-rebinding settings pass valid initialize calls to MCPServer
- Verify the ASGI app handed to uvicorn_run in test_cli.py rejects unauthenticated POST /mcp with 401
This commit is contained in:
kiskaadee committed 2026-11-15 22:59:03 -05:00
1 parent a6d33ae9e3
commit a9421c30f7
3 files changed
+91 -5

No files matched your search

+8 -2
View File
@@ -5,6 +5,7 @@ import stat
from pathlib import Path
from typing import Any
import httpx
import pytest
from mcp.server.mcpserver import MCPServer
@@ -51,7 +52,7 @@ def test_cli_serve_http_fails_closed_when_bearer_token_missing_or_blank(
assert called is False
def test_cli_serve_http_runs_uvicorn_with_configured_host_and_port(tmp_path: Path) -> None:
async def test_cli_serve_http_runs_uvicorn_with_configured_host_and_port(tmp_path: Path) -> None:
captured: list[tuple[ASGIApp, str, int]] = []
def fake_uvicorn(app: ASGIApp, host: str, port: int) -> None:
@@ -65,10 +66,15 @@ def test_cli_serve_http_runs_uvicorn_with_configured_host_and_port(tmp_path: Pat
assert rc == 0
assert len(captured) == 1
_, host, port = captured[0]
app, host, port = captured[0]
assert host == "127.0.0.1"
assert port == 8765
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:8765") as client:
unauth = await client.post("/mcp", json={"jsonrpc": "2.0", "id": 1, "method": "initialize"})
assert unauth.status_code == 401
def test_cli_serve_stdio_delegates_to_stdio_runner_without_bearer_token(tmp_path: Path) -> None:
ran: list[MCPServer[Any]] = []
+63
View File
@@ -394,3 +394,66 @@ async def test_server_http_app_lifespan_starts_and_closes_manager_with_sighup(
"lifespan.shutdown.complete",
]
assert ssap_ws.closed is True
async def test_server_http_app_allows_authenticated_mcp_initialize_request(
tmp_path: Path,
) -> None:
sent_requests: list[tuple[str, dict[str, Any]]] = []
ssap_ws = _online_transport(sent_requests)
async def connect_fn(_url: str) -> FakeWebSocketTransport:
return ssap_ws
config = _make_config(tmp_path)
mgr = TvManager(config, connect_fn=connect_fn, backoff_steps=(10.0,))
app = create_http_app(config, manager=mgr)
incoming: list[MutableMapping[str, Any]] = [
{"type": "lifespan.startup"},
{"type": "lifespan.shutdown"},
]
startup_done = asyncio.Event()
allow_shutdown = asyncio.Event()
async def receive() -> MutableMapping[str, Any]:
if incoming[0]["type"] == "lifespan.shutdown":
await allow_shutdown.wait()
return incoming.pop(0)
async def send(msg: MutableMapping[str, Any]) -> None:
if msg["type"] == "lifespan.startup.complete":
startup_done.set()
async def _run_lifespan() -> None:
await app({"type": "lifespan"}, receive, send)
lifespan_task: asyncio.Task[None] = asyncio.create_task(_run_lifespan())
try:
await asyncio.wait_for(startup_done.wait(), timeout=2.0)
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(
transport=transport, base_url="http://127.0.0.1:8765"
) as client:
resp = await client.post(
"/mcp",
headers={
"Authorization": "Bearer test-bearer-token",
"Accept": "application/json, text/event-stream",
},
json={
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "0.1.0"},
},
},
)
assert resp.status_code == 200
assert "lg-smtv-mcp" in resp.text
finally:
allow_shutdown.set()
await asyncio.wait_for(lifespan_task, timeout=2.0)
+20 -3
View File
@@ -6,7 +6,7 @@ import pytest
from lg_smtv_mcp.errors import ErrorCode, TvError
from lg_smtv_mcp.tv_core.protocols import WebSocketClosedError
from lg_smtv_mcp.tv_core.ssap import PERMISSIONS, SsapClient
from lg_smtv_mcp.tv_core.ssap import SsapClient
from tests.conftest import FakeWebSocketTransport
@@ -35,8 +35,25 @@ async def test_ssap_register_sends_all_permissions_even_with_saved_client_key()
reg_msg = json.loads(transport.sent[0])
assert reg_msg["type"] == "register"
assert reg_msg["payload"]["client-key"] == "saved-key-123"
assert len(PERMISSIONS) == 16
assert reg_msg["payload"]["manifest"]["permissions"] == list(PERMISSIONS)
expected_permissions = [
"LAUNCH",
"CONTROL_AUDIO",
"CONTROL_DISPLAY",
"CONTROL_POWER",
"CONTROL_INPUT_TEXT",
"CONTROL_INPUT_MEDIA_PLAYBACK",
"CONTROL_INPUT_JOYSTICK",
"CONTROL_INPUT_TV",
"CONTROL_TV_MEDIA",
"CONTROL_MOUSE_AND_KEYBOARD",
"READ_INSTALLED_APPS",
"READ_RUNNING_APPS",
"READ_INPUT_DEVICE_LIST",
"READ_TV_CHANNEL_LIST",
"READ_POWER_STATE",
"WRITE_NOTIFICATION_TOAST",
]
assert reg_msg["payload"]["manifest"]["permissions"] == expected_permissions
async def test_ssap_request_demuxes_interleaved_subscribe_push_frame() -> None: