|
| 1 | +"""Tests for the RabbitMQ MCP Server with streamable HTTP support.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +from mcp_server_rabbitmq.server import RabbitMQMCPServer |
| 7 | + |
| 8 | + |
| 9 | +class TestRabbitMQMCPServer(unittest.TestCase): |
| 10 | + """Test the RabbitMQMCPServer class.""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + """Set up test fixtures.""" |
| 14 | + self.server = RabbitMQMCPServer( |
| 15 | + rabbitmq_host="test-host", |
| 16 | + rabbitmq_port=5672, |
| 17 | + rabbitmq_username="test-user", |
| 18 | + rabbitmq_password="test-pass", |
| 19 | + rabbitmq_use_tls=False, |
| 20 | + rabbitmq_api_port=15672, |
| 21 | + ) |
| 22 | + |
| 23 | + @patch("mcp_server_rabbitmq.server.FastMCP") |
| 24 | + def test_server_initialization(self, mock_fastmcp): |
| 25 | + """Test that the server initializes correctly.""" |
| 26 | + server = RabbitMQMCPServer( |
| 27 | + rabbitmq_host="test-host", |
| 28 | + rabbitmq_port=5672, |
| 29 | + rabbitmq_username="test-user", |
| 30 | + rabbitmq_password="test-pass", |
| 31 | + rabbitmq_use_tls=False, |
| 32 | + rabbitmq_api_port=15672, |
| 33 | + ) |
| 34 | + |
| 35 | + # Verify FastMCP was initialized correctly |
| 36 | + mock_fastmcp.assert_called_once_with( |
| 37 | + "mcp-server-rabbitmq", |
| 38 | + instructions="Manage RabbitMQ message brokers and interact with queues and exchanges.", |
| 39 | + ) |
| 40 | + |
| 41 | + # Verify connection parameters were stored |
| 42 | + self.assertEqual(server.rabbitmq_host, "test-host") |
| 43 | + self.assertEqual(server.rabbitmq_port, 5672) |
| 44 | + self.assertEqual(server.rabbitmq_username, "test-user") |
| 45 | + self.assertEqual(server.rabbitmq_password, "test-pass") |
| 46 | + self.assertEqual(server.rabbitmq_use_tls, False) |
| 47 | + self.assertEqual(server.rabbitmq_api_port, 15672) |
| 48 | + |
| 49 | + @patch("mcp_server_rabbitmq.server.BearerAuthProvider") |
| 50 | + @patch("mcp_server_rabbitmq.server.FastMCP") |
| 51 | + def test_run_with_streamable_http(self, mock_fastmcp, mock_bearer_auth): |
| 52 | + """Test that the server runs with streamable HTTP transport.""" |
| 53 | + # Create a mock FastMCP instance |
| 54 | + mock_mcp_instance = MagicMock() |
| 55 | + mock_fastmcp.return_value = mock_mcp_instance |
| 56 | + |
| 57 | + # Create a mock BearerAuthProvider instance |
| 58 | + mock_auth_provider = MagicMock() |
| 59 | + mock_bearer_auth.return_value = mock_auth_provider |
| 60 | + |
| 61 | + # Create server instance |
| 62 | + server = RabbitMQMCPServer( |
| 63 | + rabbitmq_host="test-host", |
| 64 | + rabbitmq_port=5672, |
| 65 | + rabbitmq_username="test-user", |
| 66 | + rabbitmq_password="test-pass", |
| 67 | + rabbitmq_use_tls=False, |
| 68 | + rabbitmq_api_port=15672, |
| 69 | + ) |
| 70 | + |
| 71 | + # Create mock args for HTTP mode |
| 72 | + args = MagicMock() |
| 73 | + args.http = True |
| 74 | + args.http_auth_jwks_uri = "https://example.com/jwks" |
| 75 | + args.http_auth_issuer = "test-issuer" |
| 76 | + args.http_auth_audience = "test-audience" |
| 77 | + args.http_auth_required_scopes = ["test-scope"] |
| 78 | + args.server_port = 8888 |
| 79 | + |
| 80 | + # Run the server |
| 81 | + server.run(args) |
| 82 | + |
| 83 | + # Verify BearerAuthProvider was initialized correctly |
| 84 | + mock_bearer_auth.assert_called_once_with( |
| 85 | + jwks_uri="https://example.com/jwks", |
| 86 | + issuer="test-issuer", |
| 87 | + audience="test-audience", |
| 88 | + required_scopes=["test-scope"], |
| 89 | + ) |
| 90 | + |
| 91 | + # Verify auth provider was set |
| 92 | + self.assertEqual(mock_mcp_instance.auth, mock_auth_provider) |
| 93 | + |
| 94 | + # Verify run was called with streamable-http transport |
| 95 | + mock_mcp_instance.run.assert_called_once_with( |
| 96 | + transport="streamable-http", |
| 97 | + host="127.0.0.1", |
| 98 | + port=8888, |
| 99 | + path="/mcp", |
| 100 | + ) |
| 101 | + |
| 102 | + @patch("mcp_server_rabbitmq.server.FastMCP") |
| 103 | + def test_run_without_http(self, mock_fastmcp): |
| 104 | + """Test that the server runs with default transport.""" |
| 105 | + # Create a mock FastMCP instance |
| 106 | + mock_mcp_instance = MagicMock() |
| 107 | + mock_fastmcp.return_value = mock_mcp_instance |
| 108 | + |
| 109 | + # Create server instance |
| 110 | + server = RabbitMQMCPServer( |
| 111 | + rabbitmq_host="test-host", |
| 112 | + rabbitmq_port=5672, |
| 113 | + rabbitmq_username="test-user", |
| 114 | + rabbitmq_password="test-pass", |
| 115 | + rabbitmq_use_tls=False, |
| 116 | + rabbitmq_api_port=15672, |
| 117 | + ) |
| 118 | + |
| 119 | + # Create mock args for non-HTTP mode |
| 120 | + args = MagicMock() |
| 121 | + args.http = False |
| 122 | + |
| 123 | + # Run the server |
| 124 | + server.run(args) |
| 125 | + |
| 126 | + # Verify run was called with default transport |
| 127 | + mock_mcp_instance.run.assert_called_once_with() |
| 128 | + |
| 129 | + @patch("mcp_server_rabbitmq.server.FastMCP") |
| 130 | + def test_run_with_http_missing_jwks_uri(self, mock_fastmcp): |
| 131 | + """Test that the server raises an error when JWKS URI is missing.""" |
| 132 | + # Create server instance |
| 133 | + server = RabbitMQMCPServer( |
| 134 | + rabbitmq_host="test-host", |
| 135 | + rabbitmq_port=5672, |
| 136 | + rabbitmq_username="test-user", |
| 137 | + rabbitmq_password="test-pass", |
| 138 | + rabbitmq_use_tls=False, |
| 139 | + rabbitmq_api_port=15672, |
| 140 | + ) |
| 141 | + |
| 142 | + # Create mock args with missing JWKS URI |
| 143 | + args = MagicMock() |
| 144 | + args.http = True |
| 145 | + args.http_auth_jwks_uri = "" |
| 146 | + |
| 147 | + # Verify that running the server raises a ValueError |
| 148 | + with self.assertRaises(ValueError) as context: |
| 149 | + server.run(args) |
| 150 | + |
| 151 | + self.assertEqual(str(context.exception), "Please set --http-auth-jwks-uri") |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + unittest.main() |
0 commit comments