|
| 1 | +# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +"""Unit tests for base_url functionality in server and main modules.""" |
| 6 | + |
| 7 | +import os |
| 8 | +from unittest.mock import MagicMock, patch |
| 9 | + |
| 10 | +from typer.testing import CliRunner |
| 11 | + |
| 12 | +from deepset_mcp.main import app |
| 13 | +from deepset_mcp.server import configure_mcp_server |
| 14 | +from deepset_mcp.tool_models import WorkspaceMode |
| 15 | + |
| 16 | + |
| 17 | +class TestConfigureMcpServerBaseUrl: |
| 18 | + """Test the configure_mcp_server function with base_url parameter.""" |
| 19 | + |
| 20 | + @patch("deepset_mcp.server.register_tools") |
| 21 | + def test_configure_mcp_server_passes_base_url(self, mock_register_tools: MagicMock) -> None: |
| 22 | + """Test that configure_mcp_server passes base_url to register_tools.""" |
| 23 | + mock_server = MagicMock() |
| 24 | + custom_url = "https://custom.api.example.com" |
| 25 | + |
| 26 | + configure_mcp_server( |
| 27 | + mcp_server_instance=mock_server, |
| 28 | + tools_to_register={"list_pipelines"}, |
| 29 | + workspace_mode=WorkspaceMode.STATIC, |
| 30 | + deepset_api_key="test-key", |
| 31 | + deepset_api_url=custom_url, |
| 32 | + deepset_workspace="test-workspace", |
| 33 | + ) |
| 34 | + |
| 35 | + # Verify register_tools was called with base_url |
| 36 | + mock_register_tools.assert_called_once() |
| 37 | + call_args = mock_register_tools.call_args |
| 38 | + assert call_args[1]["base_url"] == custom_url |
| 39 | + |
| 40 | + @patch("deepset_mcp.server.register_tools") |
| 41 | + def test_configure_mcp_server_without_base_url(self, mock_register_tools: MagicMock) -> None: |
| 42 | + """Test that configure_mcp_server works without base_url.""" |
| 43 | + mock_server = MagicMock() |
| 44 | + |
| 45 | + configure_mcp_server( |
| 46 | + mcp_server_instance=mock_server, |
| 47 | + tools_to_register={"list_pipelines"}, |
| 48 | + workspace_mode=WorkspaceMode.STATIC, |
| 49 | + deepset_api_key="test-key", |
| 50 | + deepset_workspace="test-workspace", |
| 51 | + ) |
| 52 | + |
| 53 | + # Verify register_tools was called with base_url=None |
| 54 | + mock_register_tools.assert_called_once() |
| 55 | + call_args = mock_register_tools.call_args |
| 56 | + assert call_args[1]["base_url"] is None |
| 57 | + |
| 58 | + |
| 59 | +class TestMainCliBaseUrl: |
| 60 | + """Test the main CLI with base_url parameter.""" |
| 61 | + |
| 62 | + def setup_method(self) -> None: |
| 63 | + """Set up test method.""" |
| 64 | + self.runner = CliRunner() |
| 65 | + # Clear environment variables that might interfere |
| 66 | + for key in ["DEEPSET_API_KEY", "DEEPSET_WORKSPACE", "DEEPSET_API_URL"]: |
| 67 | + if key in os.environ: |
| 68 | + del os.environ[key] |
| 69 | + |
| 70 | + @patch("deepset_mcp.main.configure_mcp_server") |
| 71 | + @patch("deepset_mcp.main.FastMCP") |
| 72 | + def test_main_with_api_url_option(self, mock_fastmcp: MagicMock, mock_configure: MagicMock) -> None: |
| 73 | + """Test main CLI with --api-url option.""" |
| 74 | + mock_mcp_instance = MagicMock() |
| 75 | + mock_fastmcp.return_value = mock_mcp_instance |
| 76 | + custom_url = "https://custom.api.example.com" |
| 77 | + |
| 78 | + result = self.runner.invoke( |
| 79 | + app, |
| 80 | + [ |
| 81 | + "--api-key", |
| 82 | + "test-key", |
| 83 | + "--workspace", |
| 84 | + "test-workspace", |
| 85 | + "--api-url", |
| 86 | + custom_url, |
| 87 | + ], |
| 88 | + ) |
| 89 | + |
| 90 | + assert result.exit_code == 0 |
| 91 | + mock_configure.assert_called_once() |
| 92 | + call_args = mock_configure.call_args |
| 93 | + assert call_args[1]["deepset_api_url"] == custom_url |
| 94 | + |
| 95 | + @patch("deepset_mcp.main.configure_mcp_server") |
| 96 | + @patch("deepset_mcp.main.FastMCP") |
| 97 | + def test_main_with_api_url_env_var(self, mock_fastmcp: MagicMock, mock_configure: MagicMock) -> None: |
| 98 | + """Test main CLI with DEEPSET_API_URL environment variable.""" |
| 99 | + mock_mcp_instance = MagicMock() |
| 100 | + mock_fastmcp.return_value = mock_mcp_instance |
| 101 | + custom_url = "https://env.api.example.com" |
| 102 | + |
| 103 | + with patch.dict( |
| 104 | + os.environ, |
| 105 | + { |
| 106 | + "DEEPSET_API_KEY": "test-key", |
| 107 | + "DEEPSET_WORKSPACE": "test-workspace", |
| 108 | + "DEEPSET_API_URL": custom_url, |
| 109 | + }, |
| 110 | + ): |
| 111 | + result = self.runner.invoke(app, []) |
| 112 | + |
| 113 | + assert result.exit_code == 0 |
| 114 | + mock_configure.assert_called_once() |
| 115 | + call_args = mock_configure.call_args |
| 116 | + assert call_args[1]["deepset_api_url"] == custom_url |
| 117 | + |
| 118 | + @patch("deepset_mcp.main.configure_mcp_server") |
| 119 | + @patch("deepset_mcp.main.FastMCP") |
| 120 | + def test_main_cli_option_overrides_env_var(self, mock_fastmcp: MagicMock, mock_configure: MagicMock) -> None: |
| 121 | + """Test that CLI option overrides environment variable for api_url.""" |
| 122 | + mock_mcp_instance = MagicMock() |
| 123 | + mock_fastmcp.return_value = mock_mcp_instance |
| 124 | + cli_url = "https://cli.api.example.com" |
| 125 | + env_url = "https://env.api.example.com" |
| 126 | + |
| 127 | + with patch.dict( |
| 128 | + os.environ, |
| 129 | + { |
| 130 | + "DEEPSET_API_KEY": "test-key", |
| 131 | + "DEEPSET_WORKSPACE": "test-workspace", |
| 132 | + "DEEPSET_API_URL": env_url, |
| 133 | + }, |
| 134 | + ): |
| 135 | + result = self.runner.invoke( |
| 136 | + app, |
| 137 | + ["--api-url", cli_url], |
| 138 | + ) |
| 139 | + |
| 140 | + assert result.exit_code == 0 |
| 141 | + mock_configure.assert_called_once() |
| 142 | + call_args = mock_configure.call_args |
| 143 | + assert call_args[1]["deepset_api_url"] == cli_url |
| 144 | + |
| 145 | + @patch("deepset_mcp.main.configure_mcp_server") |
| 146 | + @patch("deepset_mcp.main.FastMCP") |
| 147 | + def test_main_without_api_url(self, mock_fastmcp: MagicMock, mock_configure: MagicMock) -> None: |
| 148 | + """Test main CLI without api_url uses None.""" |
| 149 | + mock_mcp_instance = MagicMock() |
| 150 | + mock_fastmcp.return_value = mock_mcp_instance |
| 151 | + |
| 152 | + result = self.runner.invoke( |
| 153 | + app, |
| 154 | + [ |
| 155 | + "--api-key", |
| 156 | + "test-key", |
| 157 | + "--workspace", |
| 158 | + "test-workspace", |
| 159 | + ], |
| 160 | + ) |
| 161 | + |
| 162 | + assert result.exit_code == 0 |
| 163 | + mock_configure.assert_called_once() |
| 164 | + call_args = mock_configure.call_args |
| 165 | + assert call_args[1]["deepset_api_url"] is None |
| 166 | + |
| 167 | + def test_main_help_shows_api_url_option(self) -> None: |
| 168 | + """Test that help text shows the api-url option.""" |
| 169 | + result = self.runner.invoke(app, ["--help"]) |
| 170 | + |
| 171 | + assert result.exit_code == 0 |
| 172 | + assert "--api-url" in result.stdout |
| 173 | + assert "Deepset API base URL" in result.stdout |
| 174 | + assert "DEEPSET_API_URL" in result.stdout |
0 commit comments