|
| 1 | +# tests/test_smithery_config.py |
| 2 | +""" |
| 3 | +Test Smithery configuration parameter passing. |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +import os |
| 8 | +from unittest.mock import patch, MagicMock |
| 9 | +from fastmcp.client import Client |
| 10 | +from fastmcp.server.middleware import MiddlewareContext |
| 11 | +from linkedin_mcp_server.server import create_mcp_server |
| 12 | +from smithery_main import SmitheryConfigMiddleware |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_smithery_middleware_extracts_config(): |
| 17 | + """Test that SmitheryConfigMiddleware correctly extracts configuration from query parameters.""" |
| 18 | + middleware = SmitheryConfigMiddleware() |
| 19 | + |
| 20 | + # Mock MiddlewareContext with query parameters via environment |
| 21 | + context = MagicMock(spec=MiddlewareContext) |
| 22 | + context.fastmcp_context = None |
| 23 | + |
| 24 | + # Set query string in environment to simulate HTTP request |
| 25 | + os.environ["QUERY_STRING"] = ( |
| 26 | + "linkedin_email=test@example.com&linkedin_password=testpass123" |
| 27 | + ) |
| 28 | + |
| 29 | + # Mock call_next |
| 30 | + async def mock_call_next(ctx): |
| 31 | + # During tool execution, check that env vars are set |
| 32 | + assert os.environ.get("LINKEDIN_EMAIL") == "test@example.com" |
| 33 | + assert os.environ.get("LINKEDIN_PASSWORD") == "testpass123" |
| 34 | + return MagicMock() |
| 35 | + |
| 36 | + # Store original env vars |
| 37 | + original_email = os.environ.get("LINKEDIN_EMAIL") |
| 38 | + original_password = os.environ.get("LINKEDIN_PASSWORD") |
| 39 | + original_query_string = os.environ.get("QUERY_STRING") |
| 40 | + |
| 41 | + try: |
| 42 | + # Execute middleware |
| 43 | + await middleware.on_call_tool(context, mock_call_next) |
| 44 | + |
| 45 | + # After execution, env vars should be restored |
| 46 | + assert os.environ.get("LINKEDIN_EMAIL") == original_email |
| 47 | + assert os.environ.get("LINKEDIN_PASSWORD") == original_password |
| 48 | + |
| 49 | + print("✅ Smithery middleware correctly handles configuration") |
| 50 | + |
| 51 | + finally: |
| 52 | + # Cleanup |
| 53 | + if original_email is not None: |
| 54 | + os.environ["LINKEDIN_EMAIL"] = original_email |
| 55 | + elif "LINKEDIN_EMAIL" in os.environ: |
| 56 | + del os.environ["LINKEDIN_EMAIL"] |
| 57 | + |
| 58 | + if original_password is not None: |
| 59 | + os.environ["LINKEDIN_PASSWORD"] = original_password |
| 60 | + elif "LINKEDIN_PASSWORD" in os.environ: |
| 61 | + del os.environ["LINKEDIN_PASSWORD"] |
| 62 | + |
| 63 | + if original_query_string is not None: |
| 64 | + os.environ["QUERY_STRING"] = original_query_string |
| 65 | + elif "QUERY_STRING" in os.environ: |
| 66 | + del os.environ["QUERY_STRING"] |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_smithery_middleware_with_empty_config(): |
| 71 | + """Test that middleware works correctly with no configuration.""" |
| 72 | + middleware = SmitheryConfigMiddleware() |
| 73 | + |
| 74 | + # Mock context with no query parameters |
| 75 | + context = MagicMock(spec=MiddlewareContext) |
| 76 | + context.fastmcp_context = None |
| 77 | + |
| 78 | + # Mock call_next |
| 79 | + async def mock_call_next(ctx): |
| 80 | + return MagicMock() |
| 81 | + |
| 82 | + # Should not raise any errors |
| 83 | + result = await middleware.on_call_tool(context, mock_call_next) |
| 84 | + assert result is not None |
| 85 | + |
| 86 | + print("✅ Smithery middleware handles empty configuration") |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.asyncio |
| 90 | +async def test_smithery_server_with_middleware(): |
| 91 | + """Test that MCP server with Smithery middleware can be created and tools discovered.""" |
| 92 | + with patch("sys.argv", ["smithery_main.py"]): |
| 93 | + # Create server (simulate smithery_main.py) |
| 94 | + mcp = create_mcp_server() |
| 95 | + |
| 96 | + # Add middleware |
| 97 | + mcp.add_middleware(SmitheryConfigMiddleware()) |
| 98 | + |
| 99 | + # Test that tools are discoverable |
| 100 | + async with Client(mcp) as client: |
| 101 | + tools = await client.list_tools() |
| 102 | + |
| 103 | + tool_names = [tool.name for tool in tools] |
| 104 | + expected_tools = [ |
| 105 | + "get_person_profile", |
| 106 | + "get_company_profile", |
| 107 | + "get_job_details", |
| 108 | + "close_session", |
| 109 | + ] |
| 110 | + |
| 111 | + for expected_tool in expected_tools: |
| 112 | + assert expected_tool in tool_names, f"Tool '{expected_tool}' not found" |
| 113 | + |
| 114 | + print(f"✅ Smithery server with middleware: {len(tools)} tools discovered") |
| 115 | + |
| 116 | + |
| 117 | +def test_smithery_middleware_param_mapping(): |
| 118 | + """Test that SmitheryConfigMiddleware has correct parameter mapping.""" |
| 119 | + middleware = SmitheryConfigMiddleware() |
| 120 | + |
| 121 | + expected_mapping = { |
| 122 | + "linkedin_email": "LINKEDIN_EMAIL", |
| 123 | + "linkedin_password": "LINKEDIN_PASSWORD", |
| 124 | + } |
| 125 | + |
| 126 | + assert middleware.param_mapping == expected_mapping |
| 127 | + print("✅ Smithery middleware parameter mapping is correct") |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + # Run tests manually if executed directly |
| 132 | + import asyncio |
| 133 | + |
| 134 | + asyncio.run(test_smithery_middleware_extracts_config()) |
| 135 | + asyncio.run(test_smithery_middleware_with_empty_config()) |
| 136 | + asyncio.run(test_smithery_server_with_middleware()) |
| 137 | + test_smithery_middleware_param_mapping() |
| 138 | + print("🎉 All Smithery configuration tests passed!") |
0 commit comments