|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import unittest |
| 17 | +from unittest import mock |
| 18 | +import asyncio |
| 19 | + |
| 20 | +from veadk.tools.mcp_tool.trusted_mcp_toolset import TrustedMcpToolset |
| 21 | +from veadk.tools.mcp_tool.trusted_mcp_session_manager import ( |
| 22 | + TrustedMcpSessionManager, |
| 23 | +) |
| 24 | +from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams |
| 25 | + |
| 26 | + |
| 27 | +class TestTrustedMcpComponents(unittest.TestCase): |
| 28 | + def setUp(self): |
| 29 | + # Create simple mock objects |
| 30 | + self.mock_stdio_params = mock.MagicMock() |
| 31 | + # Add serializable headers to stdio_params |
| 32 | + mock_headers = mock.MagicMock() |
| 33 | + mock_headers.copy.return_value = {"Content-Type": "application/json"} |
| 34 | + self.mock_stdio_params.headers = mock_headers |
| 35 | + # Add getattr method that throws AttributeError by default to simulate non-HTTP connection |
| 36 | + self.mock_stdio_params.getattr = mock.MagicMock(side_effect=AttributeError) |
| 37 | + |
| 38 | + self.mock_http_params = mock.MagicMock(spec=StreamableHTTPConnectionParams) |
| 39 | + self.mock_http_params.url = "http://mock-url.com" |
| 40 | + self.mock_http_params.timeout = 30 |
| 41 | + self.mock_http_params.sse_read_timeout = 60 |
| 42 | + self.mock_http_params.terminate_on_close = False |
| 43 | + # Add serializable headers to http_params |
| 44 | + mock_http_headers = mock.MagicMock() |
| 45 | + mock_http_headers.copy.return_value = { |
| 46 | + "Content-Type": "application/json", |
| 47 | + "x-trusted-mcp": "true", |
| 48 | + } |
| 49 | + self.mock_http_params.headers = mock_http_headers |
| 50 | + |
| 51 | + def test_trusted_mcp_toolset_init(self): |
| 52 | + """Test the initialization of TrustedMcpToolset""" |
| 53 | + # Mock TrustedMcpSessionManager and logger |
| 54 | + with ( |
| 55 | + mock.patch( |
| 56 | + "veadk.tools.mcp_tool.trusted_mcp_toolset.TrustedMcpSessionManager" |
| 57 | + ) as mock_session_manager, |
| 58 | + mock.patch("veadk.tools.mcp_tool.trusted_mcp_toolset.logger"), |
| 59 | + ): |
| 60 | + # Create instance directly without mocking parent initialization to automatically set necessary attributes |
| 61 | + toolset = TrustedMcpToolset( |
| 62 | + connection_params=self.mock_stdio_params, tool_filter=["read_file"] |
| 63 | + ) |
| 64 | + |
| 65 | + # Verify TrustedMcpSessionManager was created |
| 66 | + mock_session_manager.assert_called_once() |
| 67 | + |
| 68 | + # Verify _trusted_mcp_session_manager attribute is set |
| 69 | + self.assertIsNotNone(toolset._mcp_session_manager) |
| 70 | + |
| 71 | + def test_trusted_mcp_session_manager_create_client_trusted(self): |
| 72 | + """Test TrustedMcpSessionManager._create_client method - TrustedMCP mode""" |
| 73 | + # Mock trusted_mcp_client_context |
| 74 | + with mock.patch( |
| 75 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.trusted_mcp_client_context" |
| 76 | + ) as mock_trusted_client: |
| 77 | + # Create manager instance directly |
| 78 | + manager = TrustedMcpSessionManager( |
| 79 | + connection_params=self.mock_http_params, errlog=sys.stderr |
| 80 | + ) |
| 81 | + |
| 82 | + # Call _create_client with trusted_mcp header |
| 83 | + merged_headers = {"x-trusted-mcp": "true"} |
| 84 | + result = manager._create_client(merged_headers) |
| 85 | + |
| 86 | + # Verify trusted_mcp_client_context was called |
| 87 | + mock_trusted_client.assert_called_once() |
| 88 | + |
| 89 | + # Verify result |
| 90 | + self.assertEqual(result, mock_trusted_client.return_value) |
| 91 | + |
| 92 | + def test_trusted_mcp_session_manager_create_client_standard(self): |
| 93 | + """Test TrustedMcpSessionManager._create_client method - Standard mode""" |
| 94 | + # Mock parent class's _create_client method |
| 95 | + with mock.patch( |
| 96 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._create_client" |
| 97 | + ) as mock_super_create: |
| 98 | + expected_client = mock.MagicMock() |
| 99 | + mock_super_create.return_value = expected_client |
| 100 | + |
| 101 | + # Create manager instance directly without mocking parent initialization to automatically set necessary attributes |
| 102 | + manager = TrustedMcpSessionManager( |
| 103 | + connection_params=self.mock_stdio_params, errlog=sys.stderr |
| 104 | + ) |
| 105 | + |
| 106 | + # Call _create_client with normal headers |
| 107 | + merged_headers = {"content-type": "application/json"} |
| 108 | + result = manager._create_client(merged_headers) |
| 109 | + |
| 110 | + # Verify parent's _create_client was called |
| 111 | + mock_super_create.assert_called_once_with(merged_headers) |
| 112 | + |
| 113 | + # Verify result |
| 114 | + self.assertEqual(result, expected_client) |
| 115 | + |
| 116 | + def test_trusted_mcp_session_manager_trusted_create_session(self): |
| 117 | + """Test TrustedMcpSessionManager.create_session method - TrustedMCP mode""" |
| 118 | + |
| 119 | + # Use coroutine test helper to run async test |
| 120 | + async def run_test(): |
| 121 | + # Create a mock async context manager |
| 122 | + class MockAsyncContext: |
| 123 | + def __init__(self): |
| 124 | + self.session = mock.MagicMock() |
| 125 | + |
| 126 | + async def __aenter__(self): |
| 127 | + return self.session |
| 128 | + |
| 129 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 130 | + return False |
| 131 | + |
| 132 | + # Create a mock AsyncExitStack that supports async methods |
| 133 | + class MockAsyncExitStack: |
| 134 | + def __init__(self): |
| 135 | + self.entered_contexts = [] |
| 136 | + |
| 137 | + async def enter_async_context(self, context): |
| 138 | + self.entered_contexts.append(context) |
| 139 | + return await context.__aenter__() |
| 140 | + |
| 141 | + async def aclose(self): |
| 142 | + pass |
| 143 | + |
| 144 | + # Mock required functions and classes |
| 145 | + mock_trusted_context = MockAsyncContext() |
| 146 | + with ( |
| 147 | + mock.patch( |
| 148 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.trusted_mcp_client", |
| 149 | + return_value=mock_trusted_context, |
| 150 | + ), |
| 151 | + mock.patch( |
| 152 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._merge_headers", |
| 153 | + return_value={"x-trusted-mcp": "true"}, |
| 154 | + ), |
| 155 | + mock.patch( |
| 156 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._generate_session_key", |
| 157 | + return_value="session-key", |
| 158 | + ), |
| 159 | + mock.patch( |
| 160 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._is_session_disconnected", |
| 161 | + return_value=False, |
| 162 | + ), |
| 163 | + mock.patch( |
| 164 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.AsyncExitStack", |
| 165 | + return_value=MockAsyncExitStack(), |
| 166 | + ), |
| 167 | + ): |
| 168 | + # Create manager instance and set necessary attributes |
| 169 | + manager = TrustedMcpSessionManager( |
| 170 | + connection_params=self.mock_http_params, errlog=sys.stderr |
| 171 | + ) |
| 172 | + manager._sessions = {} |
| 173 | + manager._session_lock = asyncio.Lock() |
| 174 | + |
| 175 | + # Call create_session |
| 176 | + headers = {"x-trusted-mcp": "true"} |
| 177 | + result = await manager.create_session(headers) |
| 178 | + |
| 179 | + # Verify result is the mock session |
| 180 | + self.assertEqual(result, mock_trusted_context.session) |
| 181 | + |
| 182 | + # Run async test |
| 183 | + asyncio.run(run_test()) |
| 184 | + |
| 185 | + def test_trusted_mcp_session_manager_session_reuse(self): |
| 186 | + """Test TrustedMcpSessionManager.create_session method - Session reuse""" |
| 187 | + |
| 188 | + # Use coroutine test helper to run async test |
| 189 | + async def run_test(): |
| 190 | + # Mock necessary methods |
| 191 | + with ( |
| 192 | + mock.patch( |
| 193 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._merge_headers", |
| 194 | + return_value={"header": "value"}, |
| 195 | + ), |
| 196 | + mock.patch( |
| 197 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._generate_session_key", |
| 198 | + return_value="session-key", |
| 199 | + ), |
| 200 | + mock.patch( |
| 201 | + "veadk.tools.mcp_tool.trusted_mcp_session_manager.MCPSessionManager._is_session_disconnected", |
| 202 | + return_value=False, |
| 203 | + ), |
| 204 | + ): |
| 205 | + # Create manager instance |
| 206 | + manager = TrustedMcpSessionManager( |
| 207 | + connection_params=self.mock_http_params, errlog=sys.stderr |
| 208 | + ) |
| 209 | + manager._sessions = {} |
| 210 | + manager._session_lock = asyncio.Lock() |
| 211 | + |
| 212 | + # Set up an existing session |
| 213 | + existing_session = mock.MagicMock() |
| 214 | + existing_exit_stack = mock.MagicMock() |
| 215 | + manager._sessions = { |
| 216 | + "session-key": (existing_session, existing_exit_stack) |
| 217 | + } |
| 218 | + |
| 219 | + # Call create_session |
| 220 | + result = await manager.create_session({"header": "value"}) |
| 221 | + |
| 222 | + # Verify existing session was returned |
| 223 | + self.assertEqual(result, existing_session) |
| 224 | + |
| 225 | + # Run async test |
| 226 | + asyncio.run(run_test()) |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == "__main__": |
| 230 | + unittest.main() |
0 commit comments