Skip to content

Commit 438c755

Browse files
committed
Fix mock endpoint tests
1 parent 76e9da0 commit 438c755

1 file changed

Lines changed: 53 additions & 15 deletions

File tree

backend/tests/test_mock_endpoints.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from fastapi.testclient import TestClient
77

88
from main import app
9+
from middleware.auth_middleware import verify_token
910
from models.user import GoogleOAuthData, UserInDB
1011
from services.auth_service import AuthService
1112

@@ -15,6 +16,11 @@
1516
auth_service = AuthService()
1617

1718

19+
def mock_verify_token():
20+
"""Mock verify_token that returns user_001"""
21+
return "user_001"
22+
23+
1824
@pytest.fixture
1925
def sample_user():
2026
"""Sample user for testing - uses UUID that matches our mock project ownership"""
@@ -75,7 +81,8 @@ def test_get_current_user(test_client, sample_user, test_access_token):
7581

7682
def test_get_projects(test_client, test_access_token):
7783
"""Test get projects endpoint"""
78-
with patch("api.projects.verify_token"):
84+
app.dependency_overrides[verify_token] = mock_verify_token
85+
try:
7986
response = test_client.get(
8087
"/projects?page=1&limit=10",
8188
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -86,11 +93,14 @@ def test_get_projects(test_client, test_access_token):
8693
assert "items" in data["data"]
8794
assert "total" in data["data"]
8895
assert len(data["data"]["items"]) >= 0
96+
finally:
97+
app.dependency_overrides.clear()
8998

9099

91100
def test_create_project(test_client, test_access_token):
92101
"""Test create project endpoint"""
93-
with patch("api.projects.verify_token"):
102+
app.dependency_overrides[verify_token] = mock_verify_token
103+
try:
94104
response = test_client.post(
95105
"/projects",
96106
json={"name": "Test Project", "description": "Test description"},
@@ -101,11 +111,14 @@ def test_create_project(test_client, test_access_token):
101111
assert data["success"] is True
102112
assert data["data"]["project"]["name"] == "Test Project"
103113
assert "upload_url" in data["data"]
114+
finally:
115+
app.dependency_overrides.clear()
104116

105117

106118
def test_get_project(test_client, test_access_token):
107119
"""Test get single project endpoint"""
108-
with patch("api.projects.verify_token"):
120+
app.dependency_overrides[verify_token] = mock_verify_token
121+
try:
109122
response = test_client.get(
110123
"/projects/project_001",
111124
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -115,11 +128,14 @@ def test_get_project(test_client, test_access_token):
115128
assert data["success"] is True
116129
assert data["data"]["id"] == "project_001"
117130
assert data["data"]["name"] == "Sales Data Analysis"
131+
finally:
132+
app.dependency_overrides.clear()
118133

119134

120135
def test_csv_preview(test_client, test_access_token):
121136
"""Test CSV preview endpoint"""
122-
with patch("api.chat.verify_token"):
137+
app.dependency_overrides[verify_token] = mock_verify_token
138+
try:
123139
response = test_client.get(
124140
"/chat/project_001/preview",
125141
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -130,11 +146,14 @@ def test_csv_preview(test_client, test_access_token):
130146
assert "columns" in data["data"]
131147
assert "sample_data" in data["data"]
132148
assert len(data["data"]["columns"]) > 0
149+
finally:
150+
app.dependency_overrides.clear()
133151

134152

135153
def test_send_message(test_client, test_access_token):
136154
"""Test send chat message endpoint"""
137-
with patch("api.chat.verify_token"):
155+
app.dependency_overrides[verify_token] = mock_verify_token
156+
try:
138157
response = test_client.post(
139158
"/chat/project_001/message",
140159
json={"message": "Show me total sales by product"},
@@ -146,11 +165,14 @@ def test_send_message(test_client, test_access_token):
146165
assert "message" in data["data"]
147166
assert "result" in data["data"]
148167
assert data["data"]["result"]["result_type"] in ["table", "chart", "summary"]
168+
finally:
169+
app.dependency_overrides.clear()
149170

150171

151172
def test_query_suggestions(test_client, test_access_token):
152173
"""Test query suggestions endpoint"""
153-
with patch("api.chat.verify_token"):
174+
app.dependency_overrides[verify_token] = mock_verify_token
175+
try:
154176
response = test_client.get(
155177
"/chat/project_001/suggestions",
156178
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -160,12 +182,14 @@ def test_query_suggestions(test_client, test_access_token):
160182
assert data["success"] is True
161183
assert len(data["data"]) > 0
162184
assert all("text" in suggestion for suggestion in data["data"])
185+
finally:
186+
app.dependency_overrides.clear()
163187

164188

165189
def test_unauthorized_access(test_client):
166190
"""Test that endpoints require authentication"""
167191
response = test_client.get("/projects")
168-
assert response.status_code == 403
192+
assert response.status_code == 401
169193

170194

171195
def test_invalid_token(test_client):
@@ -210,7 +234,8 @@ def test_refresh_token(test_client, sample_user):
210234

211235
def test_project_status(test_client, test_access_token):
212236
"""Test project status endpoint"""
213-
with patch("api.projects.verify_token"):
237+
app.dependency_overrides[verify_token] = mock_verify_token
238+
try:
214239
response = test_client.get(
215240
"/projects/project_001/status",
216241
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -220,26 +245,31 @@ def test_project_status(test_client, test_access_token):
220245
assert data["success"] is True
221246
assert "status" in data["data"]
222247
assert "progress" in data["data"]
248+
finally:
249+
app.dependency_overrides.clear()
223250

224251

225252
def test_get_upload_url(test_client, test_access_token):
226253
"""Test get upload URL endpoint"""
227-
with patch("api.projects.verify_token"):
228-
response = test_client.post(
254+
app.dependency_overrides[verify_token] = mock_verify_token
255+
try:
256+
response = test_client.get(
229257
"/projects/project_001/upload-url",
230-
json={"filename": "new_data.csv", "content_type": "text/csv"},
231258
headers={"Authorization": f"Bearer {test_access_token}"},
232259
)
233260
assert response.status_code == 200
234261
data = response.json()
235262
assert data["success"] is True
236263
assert "upload_url" in data["data"]
237-
assert "object_path" in data["data"]
264+
assert "upload_fields" in data["data"]
265+
finally:
266+
app.dependency_overrides.clear()
238267

239268

240269
def test_get_messages(test_client, test_access_token):
241270
"""Test get chat messages endpoint"""
242-
with patch("api.chat.verify_token"):
271+
app.dependency_overrides[verify_token] = mock_verify_token
272+
try:
243273
response = test_client.get(
244274
"/chat/project_001/messages",
245275
headers={"Authorization": f"Bearer {test_access_token}"},
@@ -249,6 +279,8 @@ def test_get_messages(test_client, test_access_token):
249279
assert data["success"] is True
250280
assert "items" in data["data"]
251281
assert len(data["data"]["items"]) >= 0
282+
finally:
283+
app.dependency_overrides.clear()
252284

253285

254286
def test_invalid_google_token(test_client):
@@ -265,17 +297,21 @@ def test_invalid_google_token(test_client):
265297

266298
def test_project_not_found(test_client, test_access_token):
267299
"""Test project not found error"""
268-
with patch("api.projects.verify_token"):
300+
app.dependency_overrides[verify_token] = mock_verify_token
301+
try:
269302
response = test_client.get(
270303
"/projects/nonexistent_project",
271304
headers={"Authorization": f"Bearer {test_access_token}"},
272305
)
273306
assert response.status_code == 404
307+
finally:
308+
app.dependency_overrides.clear()
274309

275310

276311
def test_chart_query_response(test_client, test_access_token):
277312
"""Test chart query response type"""
278-
with patch("api.chat.verify_token"):
313+
app.dependency_overrides[verify_token] = mock_verify_token
314+
try:
279315
response = test_client.post(
280316
"/chat/project_001/message",
281317
json={"message": "show me a chart"},
@@ -285,3 +321,5 @@ def test_chart_query_response(test_client, test_access_token):
285321
data = response.json()
286322
assert data["data"]["result"]["result_type"] == "chart"
287323
assert "chart_config" in data["data"]["result"]
324+
finally:
325+
app.dependency_overrides.clear()

0 commit comments

Comments
 (0)