Skip to content

Commit 561a7b8

Browse files
committed
js changes
Signed-off-by: Rakhi Dutta <[email protected]>
1 parent a4bb41f commit 561a7b8

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

mcpgateway/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,7 @@ async def admin_add_resource(request: Request, db: Session = Depends(get_db), us
26682668
error_message = ErrorFormatter.format_database_error(ex)
26692669
logger.error(f"IntegrityError in admin_add_resource: {error_message}")
26702670
return JSONResponse(status_code=409, content=error_message)
2671+
26712672
logger.error(f"Error in admin_add_resource: {ex}")
26722673
return JSONResponse(content={"message": str(ex), "success": False}, status_code=500)
26732674

tests/e2e/test_admin_apis.py

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from unittest.mock import patch
4141
from urllib.parse import quote
4242
import uuid
43+
import logging
4344

4445
# Third-Party
4546
from httpx import AsyncClient
@@ -53,6 +54,13 @@
5354
from mcpgateway.db import Base
5455
from mcpgateway.main import app, get_db
5556

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+
5664
# pytest.skip("Temporarily disabling this suite", allow_module_level=True)
5765

5866
# -------------------------
@@ -85,6 +93,9 @@ async def temp_db():
8593
poolclass=StaticPool,
8694
)
8795

96+
# Log the database path for debugging
97+
logging.debug(f"Using temporary database at: {db_path}")
98+
8899
# Create all tables
89100
Base.metadata.create_all(bind=engine)
90101

@@ -114,6 +125,7 @@ def override_auth():
114125
yield engine
115126

116127
# Cleanup
128+
logging.debug("Cleaning up temporary database and overrides.")
117129
app.dependency_overrides.clear()
118130
os.close(db_fd)
119131
os.unlink(db_path)
@@ -337,70 +349,47 @@ async def test_admin_tool_name_conflict(self, client: AsyncClient, mock_settings
337349
class TestAdminResourceAPIs:
338350
"""Test admin resource management endpoints."""
339351

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",
344357
"name": "Test Resource",
345358
"description": "A test resource",
346359
"mimeType": "text/plain",
347360
"content": "Sample content",
348361
}
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)
350365
assert response.status_code == 200
351366
result = response.json()
352367
assert result["success"] is True
353368
assert "message" in result and "Add resource registered successfully!" in result["message"]
354369

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",
359373
"description": "A test resource",
360374
# Missing 'uri', 'mimeType', and 'content'
361375
}
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)
363377
assert response.status_code == 500
364-
# result = response.json()
365-
# assert result["success"] is False
366-
# assert "error" in result
367378

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": "",
376386
}
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)
378388
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)
400392
assert response.status_code == 409
401-
# result = response.json()
402-
# assert result["success"] is False
403-
# assert "error" in result
404393

405394

406395
# -------------------------

tests/unit/mcpgateway/test_admin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,8 @@ async def test_admin_add_resource(self, mock_register_resource, mock_request, mo
388388

389389
# Assert
390390
mock_register_resource.assert_called_once()
391-
assert isinstance(result, RedirectResponse)
392-
assert result.status_code == 303
393-
assert "/admin#resources" in result.headers["location"]
394-
391+
assert result.status_code == 200
392+
395393
@patch.object(ResourceService, "update_resource")
396394
async def test_admin_edit_resource(self, mock_update_resource, mock_request, mock_db):
397395
"""Test editing a resource through admin UI."""

0 commit comments

Comments
 (0)