35
35
os .environ ["MCPGATEWAY_UI_ENABLED" ] = "true"
36
36
37
37
# Standard
38
- import tempfile
39
- from typing import AsyncGenerator
38
+ import logging
40
39
from unittest .mock import patch
41
40
from urllib .parse import quote
42
41
import uuid
43
- import logging
44
42
45
43
# Third-Party
46
44
from httpx import AsyncClient
47
45
import pytest
48
46
import pytest_asyncio
49
- from sqlalchemy import create_engine
50
- from sqlalchemy .orm import sessionmaker
51
- from sqlalchemy .pool import StaticPool
52
47
53
- # First-Party
54
- from mcpgateway .db import Base
55
- from mcpgateway .main import app , get_db
48
+ # from mcpgateway.db import Base
49
+ # from mcpgateway.main import app, get_db
56
50
57
51
58
52
# Configure logging for debugging
@@ -77,72 +71,23 @@ def setup_logging():
77
71
# Fixtures
78
72
# -------------------------
79
73
@pytest_asyncio .fixture
80
- async def temp_db ():
81
- """
82
- Create a temporary SQLite database for testing.
83
-
84
- This fixture creates a fresh database for each test, ensuring complete
85
- isolation between tests. The database is automatically cleaned up after
86
- the test completes.
87
- """
88
- # Create temporary file for SQLite database
89
- db_fd , db_path = tempfile .mkstemp (suffix = ".db" )
90
-
91
- # Create engine with SQLite
92
- engine = create_engine (
93
- f"sqlite:///{ db_path } " ,
94
- connect_args = {"check_same_thread" : False },
95
- poolclass = StaticPool ,
96
- )
97
-
98
- # Log the database path for debugging
99
- logging .debug (f"Using temporary database at: { db_path } " )
100
-
101
- # Create all tables
102
- Base .metadata .create_all (bind = engine )
103
-
104
- # Create session factory
105
- TestSessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
106
-
107
- # Override the get_db dependency
108
- def override_get_db ():
109
- db = TestSessionLocal ()
110
- try :
111
- yield db
112
- finally :
113
- db .close ()
114
-
115
- app .dependency_overrides [get_db ] = override_get_db
116
-
117
- # Override authentication for all tests
74
+ async def client (app_with_temp_db ):
118
75
# First-Party
119
76
from mcpgateway .utils .verify_credentials import require_auth , require_basic_auth
120
77
121
- def override_auth ():
122
- return TEST_USER
123
-
124
- app .dependency_overrides [require_auth ] = override_auth
125
- app .dependency_overrides [require_basic_auth ] = override_auth
126
-
127
- yield engine
78
+ app_with_temp_db .dependency_overrides [require_auth ] = lambda : TEST_USER
79
+ app_with_temp_db .dependency_overrides [require_basic_auth ] = lambda : TEST_USER
128
80
129
- # Cleanup
130
- logging .debug ("Cleaning up temporary database and overrides." )
131
- app .dependency_overrides .clear ()
132
- os .close (db_fd )
133
- os .unlink (db_path )
134
-
135
-
136
- @pytest_asyncio .fixture
137
- async def client (temp_db ) -> AsyncGenerator [AsyncClient , None ]:
138
- """Create an async test client with the test database."""
139
81
# Third-Party
140
82
from httpx import ASGITransport , AsyncClient
141
83
142
- transport = ASGITransport (app = app )
84
+ transport = ASGITransport (app = app_with_temp_db )
143
85
async with AsyncClient (transport = transport , base_url = "http://test" ) as ac :
144
86
yield ac
145
87
88
+ app_with_temp_db .dependency_overrides .pop (require_auth , None )
89
+ app_with_temp_db .dependency_overrides .pop (require_basic_auth , None )
90
+
146
91
147
92
@pytest_asyncio .fixture
148
93
async def mock_settings ():
@@ -351,7 +296,7 @@ async def test_admin_tool_name_conflict(self, client: AsyncClient, mock_settings
351
296
class TestAdminResourceAPIs :
352
297
"""Test admin resource management endpoints."""
353
298
354
- async def test_admin_add_resource (self , client : AsyncClient , mock_settings , temp_db ):
299
+ async def test_admin_add_resource (self , client : AsyncClient , mock_settings ):
355
300
"""Test adding a resource via the admin UI with new logic."""
356
301
# Define valid form data
357
302
valid_form_data = {
0 commit comments