-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_api_integration.py
More file actions
378 lines (309 loc) · 15.1 KB
/
Copy pathtest_api_integration.py
File metadata and controls
378 lines (309 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Integration tests for Trading Assistant API endpoints with validation.
Tests the actual Flask routes with validation decorators to ensure:
- Valid requests pass through successfully
- Invalid requests return proper 400 validation errors
- Response formats are consistent (APIResponse)
- All validated endpoints work correctly
"""
import json
import os
import pytest
# Set required environment variables for testing before importing web_app
os.environ["SECRET_KEY"] = "test_secret_key_for_integration_tests_only"
os.environ["POLYGON_API_KEY"] = "test_polygon_key"
os.environ["APCA_API_KEY_ID"] = "test_alpaca_key"
os.environ["APCA_API_SECRET_KEY"] = "test_alpaca_secret"
from web_app import app # noqa: E402
@pytest.fixture
def client():
"""Create test client for Flask app"""
app.config["TESTING"] = True
with app.test_client() as client:
yield client
class TestWatchlistEndpoints:
"""Test /api/watchlist/* endpoints"""
def test_add_to_watchlist_valid(self, client):
"""Test adding valid symbol to watchlist"""
response = client.post(
"/api/watchlist/add", json={"symbol": "AAPL", "reason": "Strong momentum"}, content_type="application/json"
)
# May return 400 if watchlist manager not initialized in test env
assert response.status_code in [200, 201, 400]
data = json.loads(response.data)
# If successful, check success field
if response.status_code in [200, 201]:
assert data.get("success") in [True, "success", "ok"]
def test_add_to_watchlist_invalid_symbol(self, client):
"""Test validation fails for invalid symbol"""
response = client.post(
"/api/watchlist/add", json={"symbol": "invalid123", "reason": "Test"}, content_type="application/json"
)
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
assert "symbol" in str(data).lower()
def test_add_to_watchlist_missing_symbol(self, client):
"""Test validation fails when symbol is missing"""
response = client.post("/api/watchlist/add", json={"reason": "Test"}, content_type="application/json")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_add_to_watchlist_not_json(self, client):
"""Test validation fails for non-JSON content"""
response = client.post("/api/watchlist/add", data="not json", content_type="text/plain")
assert response.status_code == 400
data = json.loads(response.data)
assert "json" in str(data).lower()
class TestAgentCommandEndpoint:
"""Test /api/agent_command endpoint"""
def test_agent_command_valid(self, client):
"""Test valid agent command"""
response = client.post("/api/agent_command", json={"command": "analyze AAPL"}, content_type="application/json")
# May return 200, 202, or 503 depending on agent availability
assert response.status_code in [200, 202, 503]
def test_agent_command_empty(self, client):
"""Test validation fails for empty command"""
response = client.post("/api/agent_command", json={"command": ""}, content_type="application/json")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_agent_command_too_long(self, client):
"""Test validation fails for command exceeding max length"""
response = client.post("/api/agent_command", json={"command": "x" * 501}, content_type="application/json")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
class TestChatEndpoint:
"""Test /api/chat endpoint"""
def test_chat_valid_message(self, client):
"""Test valid chat message"""
response = client.post(
"/api/chat", json={"message": "What is the current market status?"}, content_type="application/json"
)
assert response.status_code in [200, 503] # 503 if chat agent unavailable
def test_chat_empty_message(self, client):
"""Test validation fails for empty message"""
response = client.post("/api/chat", json={"message": ""}, content_type="application/json")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_chat_message_too_long(self, client):
"""Test validation fails for message exceeding max length"""
response = client.post("/api/chat", json={"message": "x" * 5001}, content_type="application/json")
assert response.status_code == 400
class TestAlertEndpoints:
"""Test /api/alerts/* endpoints"""
def test_create_alert_valid(self, client):
"""Test creating valid price alert"""
response = client.post(
"/api/alerts",
json={
"symbol": "AAPL",
"alert_type": "price_above",
"condition_value": 150.0,
"message": "AAPL above $150",
},
content_type="application/json",
)
# May fail if price_alerts_manager not initialized, but validation should pass
assert response.status_code in [200, 201, 400] # 400 if manager not initialized
data = json.loads(response.data)
if response.status_code in [200, 201]:
assert data.get("success") in [True, "success"]
def test_create_alert_invalid_symbol(self, client):
"""Test validation fails for invalid symbol"""
response = client.post(
"/api/alerts",
json={"symbol": "123", "alert_type": "price_above", "condition_value": 150.0},
content_type="application/json",
)
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_create_alert_negative_value(self, client):
"""Test validation fails for negative condition value"""
response = client.post(
"/api/alerts",
json={"symbol": "AAPL", "alert_type": "price_above", "condition_value": -10.0},
content_type="application/json",
)
assert response.status_code == 400
def test_get_alerts_with_filters(self, client):
"""Test getting alerts with query filters"""
response = client.get("/api/alerts?symbol=AAPL&status=active")
# May return 200 or 400 depending on manager initialization
assert response.status_code in [200, 400]
def test_get_alerts_invalid_symbol(self, client):
"""Test validation fails for invalid symbol in query"""
response = client.get("/api/alerts?symbol=invalid123")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
class TestOptionsEndpoints:
"""Test /api/options/* endpoints"""
def test_options_quotes_valid(self, client):
"""Test getting options quotes with valid data"""
response = client.post(
"/api/options/quotes",
json={"symbol": "AAPL", "expiration": "2025-12-19", "option_type": "call", "strike": 150.0},
content_type="application/json",
)
# May succeed or fail depending on data availability
assert response.status_code in [200, 400, 404, 503]
def test_options_quotes_invalid_expiration(self, client):
"""Test validation fails for invalid expiration date format"""
response = client.post(
"/api/options/quotes",
json={"symbol": "AAPL", "expiration": "12/19/2025", "option_type": "call", "strike": 150.0}, # Wrong format
content_type="application/json",
)
assert response.status_code == 400
def test_options_quotes_invalid_type(self, client):
"""Test validation fails for invalid option type"""
response = client.post(
"/api/options/quotes",
json={"symbol": "AAPL", "expiration": "2025-12-19", "option_type": "invalid", "strike": 150.0},
content_type="application/json",
)
assert response.status_code == 400
class TestEnhancedWatchlistEndpoints:
"""Test /api/enhanced-watchlist/* endpoints"""
def test_get_enhanced_watchlist(self, client):
"""Test getting enhanced watchlist"""
response = client.get("/api/enhanced-watchlist")
# Should succeed if manager is initialized
assert response.status_code in [200, 400]
def test_get_enhanced_watchlist_with_filters(self, client):
"""Test getting watchlist with query filters"""
response = client.get("/api/enhanced-watchlist?submitter_type=agent&status=active&limit=10")
assert response.status_code in [200, 400]
def test_get_enhanced_watchlist_invalid_limit(self, client):
"""Test validation fails for invalid limit"""
response = client.get("/api/enhanced-watchlist?limit=999")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_add_enhanced_watchlist_valid(self, client):
"""Test adding to enhanced watchlist"""
response = client.post(
"/api/enhanced-watchlist",
json={"symbol": "TSLA", "reason": "Breakout pattern", "confidence": 0.85, "priority": "high"},
content_type="application/json",
)
assert response.status_code in [200, 201, 400]
def test_add_enhanced_watchlist_invalid_confidence(self, client):
"""Test validation fails for confidence out of range"""
response = client.post(
"/api/enhanced-watchlist",
json={"symbol": "TSLA", "confidence": 1.5}, # Must be 0-1
content_type="application/json",
)
assert response.status_code == 400
class TestQueryParameterEndpoints:
"""Test GET endpoints with query parameter validation"""
def test_stock_chart_valid(self, client):
"""Test stock chart with valid query params"""
response = client.get("/api/stock_chart/AAPL?period=1D&interval=5m")
# May succeed or fail depending on data availability
assert response.status_code in [200, 400, 503]
def test_stock_chart_invalid_period(self, client):
"""Test validation fails for invalid period"""
response = client.get("/api/stock_chart/AAPL?period=INVALID")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
def test_portfolio_history_valid(self, client):
"""Test portfolio history with valid params"""
response = client.get("/api/portfolio_history?period=1M&timeframe=15Min")
assert response.status_code in [200, 400, 503]
def test_portfolio_history_invalid_timeframe(self, client):
"""Test validation fails for invalid timeframe"""
response = client.get("/api/portfolio_history?timeframe=INVALID")
assert response.status_code == 400
def test_live_signals_with_limit(self, client):
"""Test live signals with limit parameter"""
response = client.get("/api/live_signals?limit=10")
assert response.status_code in [200, 400]
def test_live_signals_invalid_limit(self, client):
"""Test validation fails for limit out of range"""
response = client.get("/api/live_signals?limit=999")
assert response.status_code == 400
def test_bulk_prices_valid(self, client):
"""Test bulk prices with valid params"""
response = client.get("/api/enhanced/bulk_prices?symbols=AAPL,GOOGL,MSFT&batch_size=3")
assert response.status_code in [200, 400]
def test_bulk_prices_invalid_batch_size(self, client):
"""Test validation fails for invalid batch size"""
response = client.get("/api/enhanced/bulk_prices?batch_size=999")
assert response.status_code == 400
class TestProposalEndpoints:
"""Test /api/proposals/* endpoints"""
def test_approve_proposal_valid(self, client):
"""Test approving proposal with valid data"""
response = client.post(
"/api/proposals/test-123/approve",
json={"proposal_id": "test-123", "approver": "test_user", "notes": "Looks good"},
content_type="application/json",
)
# May succeed or fail depending on whether proposal exists
assert response.status_code in [200, 400, 404]
def test_approve_proposal_missing_approver(self, client):
"""Test validation fails when approver is missing"""
response = client.post(
"/api/proposals/test-123/approve",
json={"proposal_id": "test-123", "notes": "Looks good"},
content_type="application/json",
)
assert response.status_code == 400
def test_reject_proposal_valid(self, client):
"""Test rejecting proposal"""
response = client.post(
"/api/proposals/test-123/reject",
json={"proposal_id": "test-123", "approver": "test_user", "notes": "Risk too high"},
content_type="application/json",
)
assert response.status_code in [200, 400, 404]
class TestAnalysisEndpoints:
"""Test analysis endpoints"""
def test_manual_analysis_valid(self, client):
"""Test manual analysis with valid symbols"""
response = client.post(
"/api/manual_analysis", json={"symbols": ["AAPL", "GOOGL", "MSFT"]}, content_type="application/json"
)
assert response.status_code in [200, 202, 400, 503]
def test_manual_analysis_too_many_symbols(self, client):
"""Test validation fails for too many symbols"""
response = client.post(
"/api/manual_analysis",
json={"symbols": ["SYM" + str(i) for i in range(51)]},
content_type="application/json",
)
assert response.status_code == 400
def test_manual_analysis_invalid_symbols(self, client):
"""Test validation fails for invalid symbol format"""
response = client.post(
"/api/manual_analysis", json={"symbols": ["AAPL", "123", "GOOGL"]}, content_type="application/json"
)
assert response.status_code == 400
class TestResponseConsistency:
"""Test that all endpoints return consistent APIResponse format"""
def test_validation_error_format(self, client):
"""Test that validation errors have consistent format"""
response = client.post("/api/watchlist/add", json={"symbol": "invalid123"}, content_type="application/json")
assert response.status_code == 400
data = json.loads(response.data)
# Check standard error response format
assert "success" in data
assert data["success"] is False
assert "error" in data
assert isinstance(data["error"], str)
def test_missing_json_error_format(self, client):
"""Test that missing JSON returns proper error"""
response = client.post("/api/watchlist/add", data="not json", content_type="text/plain")
assert response.status_code == 400
data = json.loads(response.data)
assert data["success"] is False
assert "json" in data["error"].lower()
if __name__ == "__main__":
pytest.main([__file__, "-v"])