4
4
5
5
import pytest
6
6
7
- from mcp_server_rabbitmq .server import (
8
- enqueue ,
9
- fanout ,
10
- list_queues ,
11
- )
7
+ from mcp_server_rabbitmq .server import RabbitMQMCPServer
12
8
13
9
14
10
@pytest .fixture
15
- def setup_global_vars ():
16
- """Set up global variables for testing."""
17
- import mcp_server_rabbitmq .server as server
18
- server .rabbitmq_host = "localhost"
19
- server .rabbitmq_port = 5672
20
- server .rabbitmq_username = "guest"
21
- server .rabbitmq_password = "guest"
22
- server .rabbitmq_use_tls = False
23
- server .rabbitmq_api_port = 15672
11
+ def server ():
12
+ """Create a RabbitMQMCPServer instance for testing."""
13
+ with patch ("mcp_server_rabbitmq.server.FastMCP" ) as mock_fastmcp :
14
+ server_instance = RabbitMQMCPServer ()
15
+ server_instance .rabbitmq_host = "localhost"
16
+ server_instance .rabbitmq_port = 5672
17
+ server_instance .rabbitmq_username = "guest"
18
+ server_instance .rabbitmq_password = "guest"
19
+ server_instance .rabbitmq_use_tls = False
20
+ server_instance .rabbitmq_api_port = 15672
21
+ yield server_instance
24
22
25
23
26
24
class TestRabbitMQTools :
27
25
"""Test the RabbitMQ MCP tools."""
28
26
29
- @patch ("mcp_server_rabbitmq.server.RabbitMQConnection" )
30
- @patch ("mcp_server_rabbitmq.server.handle_enqueue" )
31
- def test_enqueue (self , mock_handle_enqueue , mock_connection , setup_global_vars ):
27
+ def test_enqueue (self , server ):
32
28
"""Test the enqueue tool."""
33
- mock_connection .return_value = MagicMock ()
34
-
35
- result = enqueue ("test_queue" , "test_message" )
36
-
37
- mock_connection .assert_called_once_with (
38
- "localhost" , 5672 , "guest" , "guest" , False
39
- )
40
- mock_handle_enqueue .assert_called_once_with (
41
- mock_connection .return_value , "test_queue" , "test_message"
42
- )
43
- assert "successfully" in result
44
-
45
- @patch ("mcp_server_rabbitmq.server.RabbitMQConnection" )
46
- @patch ("mcp_server_rabbitmq.server.handle_fanout" )
47
- def test_fanout (self , mock_handle_fanout , mock_connection , setup_global_vars ):
29
+ with patch ("mcp_server_rabbitmq.server.RabbitMQConnection" ) as mock_connection :
30
+ with patch ("mcp_server_rabbitmq.server.handle_enqueue" ) as mock_handle_enqueue :
31
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
32
+ mock_connection .return_value = MagicMock ()
33
+ mock_handle_enqueue .return_value = None
34
+
35
+ # Extract the enqueue function from the server
36
+ # This is the function that was registered with @mcp.tool()
37
+ enqueue_func = None
38
+ for name , func in server .mcp .tool .mock_calls :
39
+ if name == "()" and func [0 ].__name__ == "enqueue" :
40
+ enqueue_func = func [0 ]
41
+ break
42
+
43
+ assert enqueue_func is not None , "Could not find enqueue function"
44
+
45
+ # Call the function directly
46
+ result = enqueue_func ("test_queue" , "test_message" )
47
+
48
+ mock_connection .assert_called_once_with (
49
+ "localhost" , 5672 , "guest" , "guest" , False
50
+ )
51
+ mock_handle_enqueue .assert_called_once_with (
52
+ mock_connection .return_value , "test_queue" , "test_message"
53
+ )
54
+ assert "successfully" in result
55
+
56
+ def test_fanout (self , server ):
48
57
"""Test the fanout tool."""
49
- mock_connection .return_value = MagicMock ()
50
-
51
- result = fanout ("test_exchange" , "test_message" )
52
-
53
- mock_connection .assert_called_once_with (
54
- "localhost" , 5672 , "guest" , "guest" , False
55
- )
56
- mock_handle_fanout .assert_called_once_with (
57
- mock_connection .return_value , "test_exchange" , "test_message"
58
- )
59
- assert "successfully" in result
60
-
61
- @patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" )
62
- @patch ("mcp_server_rabbitmq.server.handle_list_queues" )
63
- def test_list_queues (self , mock_handle_list_queues , mock_admin , setup_global_vars ):
58
+ with patch ("mcp_server_rabbitmq.server.RabbitMQConnection" ) as mock_connection :
59
+ with patch ("mcp_server_rabbitmq.server.handle_fanout" ) as mock_handle_fanout :
60
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
61
+ mock_connection .return_value = MagicMock ()
62
+ mock_handle_fanout .return_value = None
63
+
64
+ # Extract the fanout function from the server
65
+ fanout_func = None
66
+ for name , func in server .mcp .tool .mock_calls :
67
+ if name == "()" and func [0 ].__name__ == "fanout" :
68
+ fanout_func = func [0 ]
69
+ break
70
+
71
+ assert fanout_func is not None , "Could not find fanout function"
72
+
73
+ # Call the function directly
74
+ result = fanout_func ("test_exchange" , "test_message" )
75
+
76
+ mock_connection .assert_called_once_with (
77
+ "localhost" , 5672 , "guest" , "guest" , False
78
+ )
79
+ mock_handle_fanout .assert_called_once_with (
80
+ mock_connection .return_value , "test_exchange" , "test_message"
81
+ )
82
+ assert "successfully" in result
83
+
84
+ def test_list_queues (self , server ):
64
85
"""Test the list_queues tool."""
65
- mock_admin .return_value = MagicMock ()
66
- mock_handle_list_queues .return_value = ["queue1" , "queue2" ]
67
-
68
- result = list_queues ()
69
-
70
- mock_admin .assert_called_once_with (
71
- "localhost" , 15672 , "guest" , "guest" , False
72
- )
73
- mock_handle_list_queues .assert_called_once_with (mock_admin .return_value )
74
- assert "['queue1', 'queue2']" == result
86
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
87
+ with patch ("mcp_server_rabbitmq.server.handle_list_queues" ) as mock_handle_list_queues :
88
+ mock_admin .return_value = MagicMock ()
89
+ mock_handle_list_queues .return_value = ["queue1" , "queue2" ]
90
+
91
+ # Extract the list_queues function from the server
92
+ list_queues_func = None
93
+ for name , func in server .mcp .tool .mock_calls :
94
+ if name == "()" and func [0 ].__name__ == "list_queues" :
95
+ list_queues_func = func [0 ]
96
+ break
97
+
98
+ assert list_queues_func is not None , "Could not find list_queues function"
99
+
100
+ # Call the function directly
101
+ result = list_queues_func ()
102
+
103
+ mock_admin .assert_called_once_with (
104
+ "localhost" , 15672 , "guest" , "guest" , False
105
+ )
106
+ mock_handle_list_queues .assert_called_once_with (mock_admin .return_value )
107
+ assert "['queue1', 'queue2']" == result
108
+
109
+ def test_list_exchanges (self , server ):
110
+ """Test the list_exchanges tool."""
111
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
112
+ with patch ("mcp_server_rabbitmq.server.handle_list_exchanges" ) as mock_handle_list_exchanges :
113
+ mock_admin .return_value = MagicMock ()
114
+ mock_handle_list_exchanges .return_value = ["exchange1" , "exchange2" ]
115
+
116
+ # Extract the list_exchanges function from the server
117
+ list_exchanges_func = None
118
+ for name , func in server .mcp .tool .mock_calls :
119
+ if name == "()" and func [0 ].__name__ == "list_exchanges" :
120
+ list_exchanges_func = func [0 ]
121
+ break
122
+
123
+ assert list_exchanges_func is not None , "Could not find list_exchanges function"
124
+
125
+ # Call the function directly
126
+ result = list_exchanges_func ()
127
+
128
+ mock_admin .assert_called_once_with (
129
+ "localhost" , 15672 , "guest" , "guest" , False
130
+ )
131
+ mock_handle_list_exchanges .assert_called_once_with (mock_admin .return_value )
132
+ assert "['exchange1', 'exchange2']" == result
133
+
134
+ def test_get_queue_info (self , server ):
135
+ """Test the get_queue_info tool."""
136
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
137
+ with patch ("mcp_server_rabbitmq.server.handle_get_queue_info" ) as mock_handle_get_queue_info :
138
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
139
+ mock_admin .return_value = MagicMock ()
140
+ mock_handle_get_queue_info .return_value = {"name" : "test_queue" , "messages" : 10 }
141
+
142
+ # Extract the get_queue_info function from the server
143
+ get_queue_info_func = None
144
+ for name , func in server .mcp .tool .mock_calls :
145
+ if name == "()" and func [0 ].__name__ == "get_queue_info" :
146
+ get_queue_info_func = func [0 ]
147
+ break
148
+
149
+ assert get_queue_info_func is not None , "Could not find get_queue_info function"
150
+
151
+ # Call the function directly
152
+ result = get_queue_info_func ("test_queue" )
153
+
154
+ mock_admin .assert_called_once_with (
155
+ "localhost" , 15672 , "guest" , "guest" , False
156
+ )
157
+ mock_handle_get_queue_info .assert_called_once_with (mock_admin .return_value , "test_queue" , "/" )
158
+ assert "{'name': 'test_queue', 'messages': 10}" == result
159
+
160
+ def test_delete_queue (self , server ):
161
+ """Test the delete_queue tool."""
162
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
163
+ with patch ("mcp_server_rabbitmq.server.handle_delete_queue" ) as mock_handle_delete_queue :
164
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
165
+ mock_admin .return_value = MagicMock ()
166
+
167
+ # Extract the delete_queue function from the server
168
+ delete_queue_func = None
169
+ for name , func in server .mcp .tool .mock_calls :
170
+ if name == "()" and func [0 ].__name__ == "delete_queue" :
171
+ delete_queue_func = func [0 ]
172
+ break
173
+
174
+ assert delete_queue_func is not None , "Could not find delete_queue function"
175
+
176
+ # Call the function directly
177
+ result = delete_queue_func ("test_queue" )
178
+
179
+ mock_admin .assert_called_once_with (
180
+ "localhost" , 15672 , "guest" , "guest" , False
181
+ )
182
+ mock_handle_delete_queue .assert_called_once_with (mock_admin .return_value , "test_queue" , "/" )
183
+ assert "successfully deleted" in result
184
+
185
+ def test_purge_queue (self , server ):
186
+ """Test the purge_queue tool."""
187
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
188
+ with patch ("mcp_server_rabbitmq.server.handle_purge_queue" ) as mock_handle_purge_queue :
189
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
190
+ mock_admin .return_value = MagicMock ()
191
+
192
+ # Extract the purge_queue function from the server
193
+ purge_queue_func = None
194
+ for name , func in server .mcp .tool .mock_calls :
195
+ if name == "()" and func [0 ].__name__ == "purge_queue" :
196
+ purge_queue_func = func [0 ]
197
+ break
198
+
199
+ assert purge_queue_func is not None , "Could not find purge_queue function"
200
+
201
+ # Call the function directly
202
+ result = purge_queue_func ("test_queue" )
203
+
204
+ mock_admin .assert_called_once_with (
205
+ "localhost" , 15672 , "guest" , "guest" , False
206
+ )
207
+ mock_handle_purge_queue .assert_called_once_with (mock_admin .return_value , "test_queue" , "/" )
208
+ assert "successfully purged" in result
209
+
210
+ def test_delete_exchange (self , server ):
211
+ """Test the delete_exchange tool."""
212
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
213
+ with patch ("mcp_server_rabbitmq.server.handle_delete_exchange" ) as mock_handle_delete_exchange :
214
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
215
+ mock_admin .return_value = MagicMock ()
216
+
217
+ # Extract the delete_exchange function from the server
218
+ delete_exchange_func = None
219
+ for name , func in server .mcp .tool .mock_calls :
220
+ if name == "()" and func [0 ].__name__ == "delete_exchange" :
221
+ delete_exchange_func = func [0 ]
222
+ break
223
+
224
+ assert delete_exchange_func is not None , "Could not find delete_exchange function"
225
+
226
+ # Call the function directly
227
+ result = delete_exchange_func ("test_exchange" )
228
+
229
+ mock_admin .assert_called_once_with (
230
+ "localhost" , 15672 , "guest" , "guest" , False
231
+ )
232
+ mock_handle_delete_exchange .assert_called_once_with (mock_admin .return_value , "test_exchange" , "/" )
233
+ assert "successfully deleted" in result
234
+
235
+ def test_get_exchange_info (self , server ):
236
+ """Test the get_exchange_info tool."""
237
+ with patch ("mcp_server_rabbitmq.server.RabbitMQAdmin" ) as mock_admin :
238
+ with patch ("mcp_server_rabbitmq.server.handle_get_exchange_info" ) as mock_handle_get_exchange_info :
239
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ):
240
+ mock_admin .return_value = MagicMock ()
241
+ mock_handle_get_exchange_info .return_value = {"name" : "test_exchange" , "type" : "fanout" }
242
+
243
+ # Extract the get_exchange_info function from the server
244
+ get_exchange_info_func = None
245
+ for name , func in server .mcp .tool .mock_calls :
246
+ if name == "()" and func [0 ].__name__ == "get_exchange_info" :
247
+ get_exchange_info_func = func [0 ]
248
+ break
249
+
250
+ assert get_exchange_info_func is not None , "Could not find get_exchange_info function"
251
+
252
+ # Call the function directly
253
+ result = get_exchange_info_func ("test_exchange" )
254
+
255
+ mock_admin .assert_called_once_with (
256
+ "localhost" , 15672 , "guest" , "guest" , False
257
+ )
258
+ mock_handle_get_exchange_info .assert_called_once_with (mock_admin .return_value , "test_exchange" , "/" )
259
+ assert "{'name': 'test_exchange', 'type': 'fanout'}" == result
260
+
261
+ def test_validate_rabbitmq_name_called (self , server ):
262
+ """Test that validate_rabbitmq_name is called for relevant functions."""
263
+ with patch ("mcp_server_rabbitmq.server.validate_rabbitmq_name" ) as mock_validate :
264
+ with patch ("mcp_server_rabbitmq.server.RabbitMQConnection" ):
265
+ with patch ("mcp_server_rabbitmq.server.handle_enqueue" ):
266
+ # Extract the enqueue function from the server
267
+ enqueue_func = None
268
+ for name , func in server .mcp .tool .mock_calls :
269
+ if name == "()" and func [0 ].__name__ == "enqueue" :
270
+ enqueue_func = func [0 ]
271
+ break
272
+
273
+ assert enqueue_func is not None , "Could not find enqueue function"
274
+
275
+ # Call the function directly
276
+ enqueue_func ("test_queue" , "test_message" )
277
+ mock_validate .assert_called_with ("test_queue" , "Queue name" )
278
+
279
+ mock_validate .reset_mock ()
280
+
281
+ with patch ("mcp_server_rabbitmq.server.RabbitMQConnection" ):
282
+ with patch ("mcp_server_rabbitmq.server.handle_fanout" ):
283
+ # Extract the fanout function from the server
284
+ fanout_func = None
285
+ for name , func in server .mcp .tool .mock_calls :
286
+ if name == "()" and func [0 ].__name__ == "fanout" :
287
+ fanout_func = func [0 ]
288
+ break
289
+
290
+ assert fanout_func is not None , "Could not find fanout function"
291
+
292
+ # Call the function directly
293
+ fanout_func ("test_exchange" , "test_message" )
294
+ mock_validate .assert_called_with ("test_exchange" , "Exchange name" )
0 commit comments