From 796844ee0ac0d4a3b3ca3fbc2353daf00a9fd626 Mon Sep 17 00:00:00 2001 From: user <70670632+stuxf@users.noreply.github.com> Date: Sat, 25 Apr 2026 03:46:45 +0000 Subject: [PATCH] test(mcp): explicit registry mock in TestMCPPublicRouteGuard Greptile review feedback (P2): the two negative `.well-known`-substring tests fell through to `_target_servers_use_oauth2`, which queries `global_mcp_server_manager.get_mcp_server_by_name`. Without an explicit mock the tests passed only because the real registry happens to be empty in the test process. Mock the manager to return None so the assertion exercises the fail-closed path explicitly. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../auth/test_user_api_key_auth_mcp.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py index c6dd1d83bc..a349654074 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py @@ -817,10 +817,18 @@ class TestMCPPublicRouteGuard: async def mock_user_api_key_auth_fails(api_key, request): raise HTTPException(status_code=401, detail="Invalid API key") - with patch( - "litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp.user_api_key_auth", - side_effect=mock_user_api_key_auth_fails, + with ( + patch( + "litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp.user_api_key_auth", + side_effect=mock_user_api_key_auth_fails, + ), + patch( + "litellm.proxy._experimental.mcp_server.mcp_server_manager.global_mcp_server_manager" + ) as mock_mgr, ): + # Explicit unresolvable target — proves auth still fails even + # when the registry has no info to fall back to. + mock_mgr.get_mcp_server_by_name.return_value = None with pytest.raises(HTTPException) as exc_info: await MCPRequestHandler.process_mcp_request(scope) assert exc_info.value.status_code == 401 @@ -842,10 +850,16 @@ class TestMCPPublicRouteGuard: async def mock_user_api_key_auth_fails(api_key, request): raise HTTPException(status_code=401, detail="Invalid API key") - with patch( - "litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp.user_api_key_auth", - side_effect=mock_user_api_key_auth_fails, + with ( + patch( + "litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp.user_api_key_auth", + side_effect=mock_user_api_key_auth_fails, + ), + patch( + "litellm.proxy._experimental.mcp_server.mcp_server_manager.global_mcp_server_manager" + ) as mock_mgr, ): + mock_mgr.get_mcp_server_by_name.return_value = None with pytest.raises(HTTPException) as exc_info: await MCPRequestHandler.process_mcp_request(scope) assert exc_info.value.status_code == 401