|
| 1 | +"""Test that server modules can be imported and initialized for both profiles. |
| 2 | +
|
| 3 | +This test ensures that both developer and secops server modules can be |
| 4 | +successfully imported and that their tool registration doesn't have syntax errors |
| 5 | +(like using mcp.add_tool instead of mcp.tool). |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_env_no_http(): |
| 16 | + """Mock environment variables to prevent HTTP server from starting.""" |
| 17 | + with patch.dict("os.environ", {"MCP_PORT": ""}, clear=False): |
| 18 | + yield |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_gitguardian_modules(): |
| 23 | + """Mock GitGuardian API modules to avoid actual API calls during import.""" |
| 24 | + with ( |
| 25 | + patch("gg_api_core.mcp_server.get_client") as mock_get_client, |
| 26 | + patch("gg_api_core.scopes.set_developer_scopes") as mock_set_dev_scopes, |
| 27 | + patch("gg_api_core.scopes.set_secops_scopes") as mock_set_secops_scopes, |
| 28 | + ): |
| 29 | + # Mock client |
| 30 | + mock_client = MagicMock() |
| 31 | + mock_client.get_current_token_info = AsyncMock(return_value={"scopes": ["scan"]}) |
| 32 | + mock_get_client.return_value = mock_client |
| 33 | + |
| 34 | + yield { |
| 35 | + "get_client": mock_get_client, |
| 36 | + "set_dev_scopes": mock_set_dev_scopes, |
| 37 | + "set_secops_scopes": mock_set_secops_scopes, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def clean_module_imports(module_name: str): |
| 42 | + """Remove a module and its submodules from sys.modules.""" |
| 43 | + modules_to_remove = [key for key in sys.modules if key.startswith(module_name)] |
| 44 | + for module in modules_to_remove: |
| 45 | + del sys.modules[module] |
| 46 | + |
| 47 | + |
| 48 | +class TestServerProfiles: |
| 49 | + """Test that both server profiles can be initialized successfully.""" |
| 50 | + |
| 51 | + def test_developer_server_imports_successfully(self, mock_gitguardian_modules, mock_env_no_http): |
| 52 | + """Test that the developer server module can be imported without errors. |
| 53 | +
|
| 54 | + This test would catch issues like: |
| 55 | + - Using mcp.add_tool instead of mcp.tool |
| 56 | + - Syntax errors in tool registration |
| 57 | + - Missing imports |
| 58 | + """ |
| 59 | + # Clean any previous imports |
| 60 | + clean_module_imports("developer_mcp_server") |
| 61 | + |
| 62 | + try: |
| 63 | + # Import the developer server module |
| 64 | + import developer_mcp_server.server as dev_server |
| 65 | + |
| 66 | + # Verify the server was created |
| 67 | + assert hasattr(dev_server, "mcp") |
| 68 | + assert dev_server.mcp is not None |
| 69 | + |
| 70 | + # Verify it's a GitGuardianFastMCP instance |
| 71 | + from gg_api_core.mcp_server import GitGuardianFastMCP |
| 72 | + |
| 73 | + assert isinstance(dev_server.mcp, GitGuardianFastMCP) |
| 74 | + |
| 75 | + # Verify the server has the expected name |
| 76 | + assert dev_server.mcp.name == "GitGuardian Developer" |
| 77 | + |
| 78 | + except AttributeError as e: |
| 79 | + if "add_tool" in str(e): |
| 80 | + pytest.fail(f"Developer server is using mcp.add_tool instead of mcp.tool: {e}") |
| 81 | + raise |
| 82 | + except Exception as e: |
| 83 | + pytest.fail(f"Failed to import developer server: {e}") |
| 84 | + |
| 85 | + def test_secops_server_imports_successfully(self, mock_gitguardian_modules, mock_env_no_http): |
| 86 | + """Test that the secops server module can be imported without errors. |
| 87 | +
|
| 88 | + This test would catch issues like: |
| 89 | + - Using mcp.add_tool instead of mcp.tool |
| 90 | + - Syntax errors in tool registration |
| 91 | + - Missing imports |
| 92 | + """ |
| 93 | + # Clean any previous imports |
| 94 | + clean_module_imports("secops_mcp_server") |
| 95 | + |
| 96 | + try: |
| 97 | + # Import the secops server module |
| 98 | + import secops_mcp_server.server as secops_server |
| 99 | + |
| 100 | + # Verify the server was created |
| 101 | + assert hasattr(secops_server, "mcp") |
| 102 | + assert secops_server.mcp is not None |
| 103 | + |
| 104 | + # Verify it's a GitGuardianFastMCP instance |
| 105 | + from gg_api_core.mcp_server import GitGuardianFastMCP |
| 106 | + |
| 107 | + assert isinstance(secops_server.mcp, GitGuardianFastMCP) |
| 108 | + |
| 109 | + # Verify the server has the expected name |
| 110 | + assert secops_server.mcp.name == "GitGuardian SecOps" |
| 111 | + |
| 112 | + except AttributeError as e: |
| 113 | + if "add_tool" in str(e): |
| 114 | + pytest.fail(f"SecOps server is using mcp.add_tool instead of mcp.tool: {e}") |
| 115 | + raise |
| 116 | + except Exception as e: |
| 117 | + pytest.fail(f"Failed to import secops server: {e}") |
| 118 | + |
| 119 | + @pytest.mark.asyncio |
| 120 | + async def test_developer_server_tools_registered(self, mock_gitguardian_modules, mock_env_no_http): |
| 121 | + """Test that developer server has tools registered properly.""" |
| 122 | + # Clean any previous imports |
| 123 | + clean_module_imports("developer_mcp_server") |
| 124 | + |
| 125 | + import developer_mcp_server.server as dev_server |
| 126 | + |
| 127 | + # Mock the _fetch_token_scopes to avoid actual API calls |
| 128 | + dev_server.mcp._fetch_token_scopes = AsyncMock() |
| 129 | + dev_server.mcp._token_scopes = {"scan", "incidents:read"} |
| 130 | + |
| 131 | + # List tools - this would fail if any tool was registered incorrectly |
| 132 | + tools = await dev_server.mcp.list_tools() |
| 133 | + |
| 134 | + # Verify we have some tools registered |
| 135 | + assert len(tools) > 0, "Developer server should have tools registered" |
| 136 | + |
| 137 | + @pytest.mark.asyncio |
| 138 | + async def test_secops_server_tools_registered(self, mock_gitguardian_modules, mock_env_no_http): |
| 139 | + """Test that secops server has tools registered properly.""" |
| 140 | + # Clean any previous imports |
| 141 | + clean_module_imports("secops_mcp_server") |
| 142 | + |
| 143 | + import secops_mcp_server.server as secops_server |
| 144 | + |
| 145 | + # Mock the _fetch_token_scopes to avoid actual API calls |
| 146 | + secops_server.mcp._fetch_token_scopes = AsyncMock() |
| 147 | + secops_server.mcp._token_scopes = {"scan", "incidents:read", "incidents:write"} |
| 148 | + |
| 149 | + # List tools - this would fail if any tool was registered incorrectly |
| 150 | + tools = await secops_server.mcp.list_tools() |
| 151 | + |
| 152 | + # Verify we have some tools registered |
| 153 | + assert len(tools) > 0, "SecOps server should have tools registered" |
| 154 | + |
| 155 | + # Verify some expected secops-specific tools are present |
| 156 | + tool_names = [tool.name for tool in tools] |
| 157 | + # Check for a secops-specific tool that should be registered |
| 158 | + assert "assign_incident" in tool_names, "SecOps server should have assign_incident tool" |
| 159 | + assert "create_code_fix_request" in tool_names, "SecOps server should have create_code_fix_request tool" |
| 160 | + |
| 161 | + def test_both_servers_can_coexist(self, mock_gitguardian_modules, mock_env_no_http): |
| 162 | + """Test that both server modules can be imported in the same test session. |
| 163 | +
|
| 164 | + This ensures there are no naming conflicts or import issues. |
| 165 | + """ |
| 166 | + # Clean any previous imports |
| 167 | + clean_module_imports("developer_mcp_server") |
| 168 | + clean_module_imports("secops_mcp_server") |
| 169 | + |
| 170 | + try: |
| 171 | + import developer_mcp_server.server as dev_server |
| 172 | + import secops_mcp_server.server as secops_server |
| 173 | + |
| 174 | + # Both servers should be distinct instances |
| 175 | + assert dev_server.mcp is not secops_server.mcp |
| 176 | + assert dev_server.mcp.name != secops_server.mcp.name |
| 177 | + |
| 178 | + except Exception as e: |
| 179 | + pytest.fail(f"Failed to import both servers: {e}") |
0 commit comments