|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +"""Tests for MCP service configuration and branding.""" |
| 19 | + |
| 20 | +from unittest.mock import MagicMock, patch |
| 21 | + |
| 22 | +from superset.mcp_service.app import get_default_instructions, init_fastmcp_server |
| 23 | + |
| 24 | + |
| 25 | +def test_get_default_instructions_with_default_branding(): |
| 26 | + """Test that default branding produces Apache Superset in instructions.""" |
| 27 | + instructions = get_default_instructions() |
| 28 | + |
| 29 | + assert "Apache Superset" in instructions |
| 30 | + assert "Apache Superset MCP" in instructions |
| 31 | + assert "model context protocol" in instructions.lower() |
| 32 | + |
| 33 | + |
| 34 | +def test_get_default_instructions_with_custom_branding(): |
| 35 | + """Test that custom branding is reflected in instructions.""" |
| 36 | + custom_branding = "ACME Analytics" |
| 37 | + instructions = get_default_instructions(branding=custom_branding) |
| 38 | + |
| 39 | + assert custom_branding in instructions |
| 40 | + assert f"{custom_branding} MCP" in instructions |
| 41 | + # Should not contain default Apache Superset branding |
| 42 | + assert "Apache Superset" not in instructions |
| 43 | + |
| 44 | + |
| 45 | +def test_get_default_instructions_with_enterprise_branding(): |
| 46 | + """Test instructions with enterprise/white-label branding.""" |
| 47 | + enterprise_branding = "DataViz Platform" |
| 48 | + instructions = get_default_instructions(branding=enterprise_branding) |
| 49 | + |
| 50 | + assert enterprise_branding in instructions |
| 51 | + assert f"{enterprise_branding} MCP" in instructions |
| 52 | + # Verify it contains expected tool documentation |
| 53 | + assert "list_dashboards" in instructions |
| 54 | + assert "list_charts" in instructions |
| 55 | + assert "execute_sql" in instructions |
| 56 | + |
| 57 | + |
| 58 | +def test_init_fastmcp_server_with_default_app_name(): |
| 59 | + """Test that default APP_NAME produces Superset branding.""" |
| 60 | + # Mock Flask app config with default APP_NAME |
| 61 | + mock_flask_app = MagicMock() |
| 62 | + mock_flask_app.config.get.return_value = "Superset" |
| 63 | + |
| 64 | + # Patch at the import location to avoid actual Flask app creation |
| 65 | + with patch.dict( |
| 66 | + "sys.modules", |
| 67 | + {"superset.mcp_service.flask_singleton": MagicMock(app=mock_flask_app)}, |
| 68 | + ): |
| 69 | + with patch("superset.mcp_service.app.create_mcp_app") as mock_create: |
| 70 | + mock_mcp = MagicMock() |
| 71 | + mock_create.return_value = mock_mcp |
| 72 | + |
| 73 | + # Call with custom name to force create_mcp_app path |
| 74 | + init_fastmcp_server(name="Custom Name") |
| 75 | + |
| 76 | + # Verify create_mcp_app was called |
| 77 | + assert mock_create.called |
| 78 | + # Verify instructions use Superset branding (not Apache Superset) |
| 79 | + call_kwargs = mock_create.call_args[1] |
| 80 | + assert "Superset MCP" in call_kwargs["instructions"] |
| 81 | + assert "Superset dashboards" in call_kwargs["instructions"] |
| 82 | + |
| 83 | + |
| 84 | +def test_init_fastmcp_server_with_custom_app_name(): |
| 85 | + """Test that custom APP_NAME produces branded instructions.""" |
| 86 | + custom_app_name = "ACME Analytics" |
| 87 | + # Mock Flask app config with custom APP_NAME |
| 88 | + mock_flask_app = MagicMock() |
| 89 | + mock_flask_app.config.get.return_value = custom_app_name |
| 90 | + |
| 91 | + # Patch at the import location to avoid actual Flask app creation |
| 92 | + with patch.dict( |
| 93 | + "sys.modules", |
| 94 | + {"superset.mcp_service.flask_singleton": MagicMock(app=mock_flask_app)}, |
| 95 | + ): |
| 96 | + with patch("superset.mcp_service.app.create_mcp_app") as mock_create: |
| 97 | + mock_mcp = MagicMock() |
| 98 | + mock_create.return_value = mock_mcp |
| 99 | + |
| 100 | + # Call with custom name to force create_mcp_app path |
| 101 | + init_fastmcp_server(name="Custom Name") |
| 102 | + |
| 103 | + # Verify create_mcp_app was called |
| 104 | + assert mock_create.called |
| 105 | + # Verify instructions use custom branding |
| 106 | + call_kwargs = mock_create.call_args[1] |
| 107 | + assert custom_app_name in call_kwargs["instructions"] |
| 108 | + # Should not contain default Apache Superset branding |
| 109 | + assert "Apache Superset" not in call_kwargs["instructions"] |
| 110 | + |
| 111 | + |
| 112 | +def test_init_fastmcp_server_derives_server_name_from_app_name(): |
| 113 | + """Test that server name is derived from APP_NAME.""" |
| 114 | + custom_app_name = "DataViz Platform" |
| 115 | + expected_server_name = f"{custom_app_name} MCP Server" |
| 116 | + |
| 117 | + # Mock Flask app config |
| 118 | + mock_flask_app = MagicMock() |
| 119 | + mock_flask_app.config.get.return_value = custom_app_name |
| 120 | + |
| 121 | + # Patch at the import location to avoid actual Flask app creation |
| 122 | + with patch.dict( |
| 123 | + "sys.modules", |
| 124 | + {"superset.mcp_service.flask_singleton": MagicMock(app=mock_flask_app)}, |
| 125 | + ): |
| 126 | + with patch("superset.mcp_service.app.create_mcp_app") as mock_create: |
| 127 | + mock_mcp = MagicMock() |
| 128 | + mock_create.return_value = mock_mcp |
| 129 | + |
| 130 | + # Call without name parameter (should use default derived name) |
| 131 | + # Force custom params by passing instructions |
| 132 | + init_fastmcp_server(instructions="custom") |
| 133 | + |
| 134 | + # Verify create_mcp_app was called with derived name |
| 135 | + assert mock_create.called |
| 136 | + call_kwargs = mock_create.call_args[1] |
| 137 | + assert call_kwargs["name"] == expected_server_name |
0 commit comments