|
40 | 40 | from unittest.mock import patch
|
41 | 41 | from urllib.parse import quote
|
42 | 42 | import uuid
|
| 43 | +import logging |
43 | 44 |
|
44 | 45 | # Third-Party
|
45 | 46 | from httpx import AsyncClient
|
|
53 | 54 | from mcpgateway.db import Base
|
54 | 55 | from mcpgateway.main import app, get_db
|
55 | 56 |
|
| 57 | +# Configure logging for debugging |
| 58 | +def setup_logging(): |
| 59 | + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
| 60 | + |
| 61 | +setup_logging() |
| 62 | + |
| 63 | + |
56 | 64 | # pytest.skip("Temporarily disabling this suite", allow_module_level=True)
|
57 | 65 |
|
58 | 66 | # -------------------------
|
@@ -85,6 +93,9 @@ async def temp_db():
|
85 | 93 | poolclass=StaticPool,
|
86 | 94 | )
|
87 | 95 |
|
| 96 | + # Log the database path for debugging |
| 97 | + logging.debug(f"Using temporary database at: {db_path}") |
| 98 | + |
88 | 99 | # Create all tables
|
89 | 100 | Base.metadata.create_all(bind=engine)
|
90 | 101 |
|
@@ -114,6 +125,7 @@ def override_auth():
|
114 | 125 | yield engine
|
115 | 126 |
|
116 | 127 | # Cleanup
|
| 128 | + logging.debug("Cleaning up temporary database and overrides.") |
117 | 129 | app.dependency_overrides.clear()
|
118 | 130 | os.close(db_fd)
|
119 | 131 | os.unlink(db_path)
|
@@ -337,70 +349,47 @@ async def test_admin_tool_name_conflict(self, client: AsyncClient, mock_settings
|
337 | 349 | class TestAdminResourceAPIs:
|
338 | 350 | """Test admin resource management endpoints."""
|
339 | 351 |
|
340 |
| - async def test_admin_add_resource(self, client: AsyncClient, mock_settings): |
341 |
| - """Test adding a resource via the admin UI.""" |
342 |
| - form_data = { |
343 |
| - "uri": mock_settings.default_resource_uri, |
| 352 | + async def test_admin_add_resource(self, client: AsyncClient, mock_settings, temp_db): |
| 353 | + """Test adding a resource via the admin UI with new logic.""" |
| 354 | + # Define valid form data |
| 355 | + valid_form_data = { |
| 356 | + "uri": "test://resource1", |
344 | 357 | "name": "Test Resource",
|
345 | 358 | "description": "A test resource",
|
346 | 359 | "mimeType": "text/plain",
|
347 | 360 | "content": "Sample content",
|
348 | 361 | }
|
349 |
| - response = await client.post("/admin/resources", data=form_data, headers=TEST_AUTH_HEADER) |
| 362 | + |
| 363 | + # Test successful resource creation |
| 364 | + response = await client.post("/admin/resources", data=valid_form_data, headers=TEST_AUTH_HEADER) |
350 | 365 | assert response.status_code == 200
|
351 | 366 | result = response.json()
|
352 | 367 | assert result["success"] is True
|
353 | 368 | assert "message" in result and "Add resource registered successfully!" in result["message"]
|
354 | 369 |
|
355 |
| - async def test_admin_add_resource_missing_fields(self, client: AsyncClient, mock_settings): |
356 |
| - """Test adding a resource with missing required fields.""" |
357 |
| - form_data = { |
358 |
| - "name": "Test Resource1", |
| 370 | + # Test missing required fields |
| 371 | + invalid_form_data = { |
| 372 | + "name": "Test Resource", |
359 | 373 | "description": "A test resource",
|
360 | 374 | # Missing 'uri', 'mimeType', and 'content'
|
361 | 375 | }
|
362 |
| - response = await client.post("/admin/resources", data=form_data, headers=TEST_AUTH_HEADER) |
| 376 | + response = await client.post("/admin/resources", data=invalid_form_data, headers=TEST_AUTH_HEADER) |
363 | 377 | assert response.status_code == 500
|
364 |
| - # result = response.json() |
365 |
| - # assert result["success"] is False |
366 |
| - # assert "error" in result |
367 | 378 |
|
368 |
| - async def test_admin_add_resource_invalid_mime_type(self, client: AsyncClient, mock_settings): |
369 |
| - """Test adding a resource with an invalid MIME type.""" |
370 |
| - form_data = { |
371 |
| - "uri": mock_settings.default_resource_uri, |
372 |
| - "name": "Test Resource", |
373 |
| - "description": "A test resource", |
374 |
| - "mimeType": "invalid/type", |
375 |
| - "content": "Sample content", |
| 379 | + # Test ValidationError (422) |
| 380 | + invalid_validation_data = { |
| 381 | + "uri": "", |
| 382 | + "name": "", |
| 383 | + "description": "", |
| 384 | + "mimeType": "", |
| 385 | + "content": "", |
376 | 386 | }
|
377 |
| - response = await client.post("/admin/resources", data=form_data, headers=TEST_AUTH_HEADER) |
| 387 | + response = await client.post("/admin/resources", data=invalid_validation_data, headers=TEST_AUTH_HEADER) |
378 | 388 | assert response.status_code == 422
|
379 |
| - # result = response.json() |
380 |
| - # assert result["success"] is False |
381 |
| - # assert "error" in result |
382 |
| - |
383 |
| - async def test_admin_add_resource_duplicate_uri(self, client: AsyncClient, mock_settings): |
384 |
| - """Test adding a resource with a duplicate URI.""" |
385 |
| - form_data = { |
386 |
| - "uri": mock_settings.default_resource_uri, |
387 |
| - "name": "Test Resource", |
388 |
| - "description": "A test resource", |
389 |
| - "mimeType": "text/plain", |
390 |
| - "content": "Sample content", |
391 |
| - } |
392 |
| - # Add the resource for the first time |
393 |
| - response = await client.post("/admin/resources", data=form_data, headers=TEST_AUTH_HEADER) |
394 |
| - assert response.status_code == 200 |
395 |
| - # result = response.json() |
396 |
| - # assert result["success"] is True |
397 |
| - |
398 |
| - # Try adding the same resource again |
399 |
| - response = await client.post("/admin/resources", data=form_data, headers=TEST_AUTH_HEADER) |
| 389 | + |
| 390 | + # Test duplicate URI |
| 391 | + response = await client.post("/admin/resources", data=valid_form_data, headers=TEST_AUTH_HEADER) |
400 | 392 | assert response.status_code == 409
|
401 |
| - # result = response.json() |
402 |
| - # assert result["success"] is False |
403 |
| - # assert "error" in result |
404 | 393 |
|
405 | 394 |
|
406 | 395 | # -------------------------
|
|
0 commit comments