Spaces:
Sleeping
Sleeping
Enhance API tests for new /data/analyze endpoint
Browse files- Added comprehensive tests for /data/analyze endpoint
- Implemented daily and intraday data validation
- Included multiple intervals (5m, 15m, 1h, 4h) testing
- Validated market status and rate limiting (20 req/min)
- Enhanced security tests including SQL injection and header checks
- Updated response validation for technical indicators
- Improved overall test structure and error handling
- api/index.py +663 -6
- logs/api.log +0 -0
- tests/test_api.py +349 -9
api/index.py
CHANGED
@@ -12,7 +12,7 @@ from contextlib import asynccontextmanager
|
|
12 |
import yaml
|
13 |
import importlib.metadata
|
14 |
import pytz
|
15 |
-
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends, Security
|
16 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
17 |
from pydantic import BaseModel, Field
|
18 |
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
@@ -139,6 +139,45 @@ class DownloadDataResponse(BaseModel):
|
|
139 |
updated_at: datetime
|
140 |
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
# --- AUTHENTICATION ---
|
143 |
|
144 |
security = HTTPBearer()
|
@@ -164,6 +203,121 @@ async def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(se
|
|
164 |
return credentials.credentials
|
165 |
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
# --- CONFIGURATION ---
|
168 |
|
169 |
class Config:
|
@@ -383,6 +537,80 @@ class YFinanceService:
|
|
383 |
self.config = config
|
384 |
self.logger = logging.getLogger(__name__)
|
385 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
386 |
def calculate_technical_indicators(self, df: pd.DataFrame) -> pd.DataFrame:
|
387 |
"""
|
388 |
Calculate technical indicators for a ticker's data.
|
@@ -860,7 +1088,8 @@ async def lifespan(app: FastAPI):
|
|
860 |
await database.engine.dispose()
|
861 |
logger.info("database_lifecycle event=connections_closed")
|
862 |
|
863 |
-
#
|
|
|
864 |
app = FastAPI(
|
865 |
title="Stock Monitoring API",
|
866 |
description="API for managing S&P 500 and Nasdaq 100 ticker data",
|
@@ -868,10 +1097,6 @@ app = FastAPI(
|
|
868 |
lifespan=lifespan,
|
869 |
)
|
870 |
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
# --- API ENDPOINTS ---
|
876 |
|
877 |
@app.get("/")
|
@@ -1370,6 +1595,438 @@ async def get_ticker_data(
|
|
1370 |
raise HTTPException(status_code=500, detail="Failed to fetch ticker data")
|
1371 |
|
1372 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1373 |
# Local execution configuration
|
1374 |
if __name__ == "__main__":
|
1375 |
import uvicorn
|
|
|
12 |
import yaml
|
13 |
import importlib.metadata
|
14 |
import pytz
|
15 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends, Security, Request, Response
|
16 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
17 |
from pydantic import BaseModel, Field
|
18 |
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
|
139 |
updated_at: datetime
|
140 |
|
141 |
|
142 |
+
class FinancialDataRequest(BaseModel):
|
143 |
+
tickers: List[str] = Field(..., description="Stock ticker symbols (e.g., ['AAPL', 'MSFT', 'GOOGL'])")
|
144 |
+
period: str = Field(default="3mo", description="Data period: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max")
|
145 |
+
intraday: bool = Field(default=False, description="Enable intraday data (pre-market to post-market)")
|
146 |
+
interval: str = Field(default="1d", description="Data interval: 1m,2m,5m,15m,30m,60m,90m,1h,4h,1d,5d,1wk,1mo,3mo")
|
147 |
+
|
148 |
+
|
149 |
+
class TechnicalIndicatorData(BaseModel):
|
150 |
+
ticker: str
|
151 |
+
datetime: str # Changed from 'date' to 'datetime' for intraday support
|
152 |
+
open: float
|
153 |
+
high: float
|
154 |
+
low: float
|
155 |
+
close: float
|
156 |
+
volume: int
|
157 |
+
sma_fast: Optional[float] = None # SMA 10
|
158 |
+
sma_med: Optional[float] = None # SMA 20
|
159 |
+
sma_slow: Optional[float] = None # SMA 50
|
160 |
+
|
161 |
+
|
162 |
+
class MarketStatus(BaseModel):
|
163 |
+
is_open: bool
|
164 |
+
market_state: str # REGULAR, PREPRE, PRE, POST, POSTPOST, CLOSED
|
165 |
+
timezone: str
|
166 |
+
|
167 |
+
|
168 |
+
class FinancialDataResponse(BaseModel):
|
169 |
+
success: bool
|
170 |
+
tickers: List[str]
|
171 |
+
period: str
|
172 |
+
interval: str
|
173 |
+
intraday: bool
|
174 |
+
total_data_points: int
|
175 |
+
date_range: Dict[str, str] # start_date, end_date
|
176 |
+
market_status: Optional[MarketStatus] = None
|
177 |
+
data: List[TechnicalIndicatorData]
|
178 |
+
calculated_at: datetime
|
179 |
+
|
180 |
+
|
181 |
# --- AUTHENTICATION ---
|
182 |
|
183 |
security = HTTPBearer()
|
|
|
203 |
return credentials.credentials
|
204 |
|
205 |
|
206 |
+
# --- RATE LIMITING ---
|
207 |
+
|
208 |
+
class RateLimiter:
|
209 |
+
def __init__(self):
|
210 |
+
self.requests = {} # {ip_address: {endpoint: [(timestamp, count), ...]}}
|
211 |
+
self.limits = {
|
212 |
+
"/data/analyze": {"requests": 20, "window": 60}, # 20 requests per minute
|
213 |
+
"default": {"requests": 100, "window": 60} # 100 requests per minute default
|
214 |
+
}
|
215 |
+
|
216 |
+
def is_allowed(self, client_ip: str, endpoint: str) -> tuple[bool, dict]:
|
217 |
+
"""
|
218 |
+
Check if request is within rate limits.
|
219 |
+
Returns (is_allowed, rate_info)
|
220 |
+
"""
|
221 |
+
current_time = time.time()
|
222 |
+
|
223 |
+
# Get limits for this endpoint
|
224 |
+
limit_config = self.limits.get(endpoint, self.limits["default"])
|
225 |
+
max_requests = limit_config["requests"]
|
226 |
+
window_seconds = limit_config["window"]
|
227 |
+
|
228 |
+
# Initialize tracking for this IP if needed
|
229 |
+
if client_ip not in self.requests:
|
230 |
+
self.requests[client_ip] = {}
|
231 |
+
|
232 |
+
if endpoint not in self.requests[client_ip]:
|
233 |
+
self.requests[client_ip][endpoint] = []
|
234 |
+
|
235 |
+
# Clean old requests outside the window
|
236 |
+
cutoff_time = current_time - window_seconds
|
237 |
+
self.requests[client_ip][endpoint] = [
|
238 |
+
(timestamp, count) for timestamp, count in self.requests[client_ip][endpoint]
|
239 |
+
if timestamp > cutoff_time
|
240 |
+
]
|
241 |
+
|
242 |
+
# Count current requests in window
|
243 |
+
current_count = sum(count for _, count in self.requests[client_ip][endpoint])
|
244 |
+
|
245 |
+
# Check if limit exceeded
|
246 |
+
if current_count >= max_requests:
|
247 |
+
return False, {
|
248 |
+
"allowed": False,
|
249 |
+
"current_count": current_count,
|
250 |
+
"limit": max_requests,
|
251 |
+
"window_seconds": window_seconds,
|
252 |
+
"reset_time": max(timestamp for timestamp, _ in self.requests[client_ip][endpoint]) + window_seconds
|
253 |
+
}
|
254 |
+
|
255 |
+
# Allow request and record it
|
256 |
+
self.requests[client_ip][endpoint].append((current_time, 1))
|
257 |
+
|
258 |
+
return True, {
|
259 |
+
"allowed": True,
|
260 |
+
"current_count": current_count + 1,
|
261 |
+
"limit": max_requests,
|
262 |
+
"window_seconds": window_seconds,
|
263 |
+
"remaining": max_requests - current_count - 1
|
264 |
+
}
|
265 |
+
|
266 |
+
# Global rate limiter instance
|
267 |
+
rate_limiter = RateLimiter()
|
268 |
+
|
269 |
+
async def check_rate_limit(request: Request, endpoint: str = "/data/analyze"):
|
270 |
+
"""
|
271 |
+
Dependency to check rate limits for endpoints.
|
272 |
+
"""
|
273 |
+
# Get client IP (handle proxies)
|
274 |
+
client_ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
|
275 |
+
if not client_ip:
|
276 |
+
client_ip = request.headers.get("x-real-ip", "")
|
277 |
+
if not client_ip:
|
278 |
+
client_ip = getattr(request.client, "host", "unknown")
|
279 |
+
|
280 |
+
is_allowed, rate_info = rate_limiter.is_allowed(client_ip, endpoint)
|
281 |
+
|
282 |
+
if not is_allowed:
|
283 |
+
reset_time = int(rate_info["reset_time"])
|
284 |
+
logger = logging.getLogger(__name__)
|
285 |
+
logger.warning(f"rate_limit_exceeded client_ip={client_ip} endpoint={endpoint} count={rate_info['current_count']} limit={rate_info['limit']}")
|
286 |
+
|
287 |
+
raise HTTPException(
|
288 |
+
status_code=429,
|
289 |
+
detail={
|
290 |
+
"error": "Rate limit exceeded",
|
291 |
+
"limit": rate_info["limit"],
|
292 |
+
"window_seconds": rate_info["window_seconds"],
|
293 |
+
"reset_time": reset_time,
|
294 |
+
"current_count": rate_info["current_count"]
|
295 |
+
},
|
296 |
+
headers={
|
297 |
+
"X-RateLimit-Limit": str(rate_info["limit"]),
|
298 |
+
"X-RateLimit-Remaining": "0",
|
299 |
+
"X-RateLimit-Reset": str(reset_time),
|
300 |
+
"Retry-After": str(int(rate_info["reset_time"] - time.time()))
|
301 |
+
}
|
302 |
+
)
|
303 |
+
|
304 |
+
return rate_info
|
305 |
+
|
306 |
+
async def add_security_headers(response: Response, rate_info: dict = None):
|
307 |
+
"""
|
308 |
+
Add security headers to response.
|
309 |
+
"""
|
310 |
+
response.headers["X-Content-Type-Options"] = "nosniff"
|
311 |
+
response.headers["X-Frame-Options"] = "DENY"
|
312 |
+
response.headers["X-XSS-Protection"] = "1; mode=block"
|
313 |
+
|
314 |
+
if rate_info:
|
315 |
+
response.headers["X-RateLimit-Limit"] = str(rate_info["limit"])
|
316 |
+
response.headers["X-RateLimit-Remaining"] = str(rate_info.get("remaining", 0))
|
317 |
+
|
318 |
+
return response
|
319 |
+
|
320 |
+
|
321 |
# --- CONFIGURATION ---
|
322 |
|
323 |
class Config:
|
|
|
537 |
self.config = config
|
538 |
self.logger = logging.getLogger(__name__)
|
539 |
|
540 |
+
def get_market_status(self, ticker: str) -> MarketStatus:
|
541 |
+
"""
|
542 |
+
Get current market status using yfinance's most reliable endpoints.
|
543 |
+
Uses multiple methods for accuracy: info, calendar, and recent data.
|
544 |
+
"""
|
545 |
+
try:
|
546 |
+
ticker_obj = yf.Ticker(ticker)
|
547 |
+
|
548 |
+
# Method 1: Try to get current data with 1-minute interval
|
549 |
+
# This is the most reliable way to check if market is currently active
|
550 |
+
current_data = None
|
551 |
+
market_state = 'UNKNOWN'
|
552 |
+
timezone_name = 'America/New_York'
|
553 |
+
|
554 |
+
try:
|
555 |
+
# Get very recent data to check market activity
|
556 |
+
current_data = ticker_obj.history(period="1d", interval="1m", prepost=True)
|
557 |
+
if not current_data.empty:
|
558 |
+
last_timestamp = current_data.index[-1]
|
559 |
+
now = datetime.now(last_timestamp.tz)
|
560 |
+
time_diff = (now - last_timestamp).total_seconds()
|
561 |
+
|
562 |
+
# If last data is within 5 minutes, market is likely active
|
563 |
+
if time_diff <= 300: # 5 minutes
|
564 |
+
# Check if it's during regular hours, pre-market, or post-market
|
565 |
+
hour = last_timestamp.hour
|
566 |
+
if 9 <= hour < 16: # Regular hours (9:30 AM - 4:00 PM ET, roughly)
|
567 |
+
market_state = 'REGULAR'
|
568 |
+
elif 4 <= hour < 9: # Pre-market (4:00 AM - 9:30 AM ET)
|
569 |
+
market_state = 'PRE'
|
570 |
+
elif 16 <= hour <= 20: # Post-market (4:00 PM - 8:00 PM ET)
|
571 |
+
market_state = 'POST'
|
572 |
+
else:
|
573 |
+
market_state = 'CLOSED'
|
574 |
+
else:
|
575 |
+
market_state = 'CLOSED'
|
576 |
+
|
577 |
+
except Exception as hist_error:
|
578 |
+
self.logger.debug(f"history_method_failed ticker={ticker} error={str(hist_error)}")
|
579 |
+
|
580 |
+
# Method 2: Use ticker.info as backup/validation
|
581 |
+
try:
|
582 |
+
info = ticker_obj.info
|
583 |
+
info_market_state = info.get('marketState', 'UNKNOWN')
|
584 |
+
timezone_name = info.get('exchangeTimezoneName', 'America/New_York')
|
585 |
+
|
586 |
+
# If history method failed, use info method
|
587 |
+
if market_state == 'UNKNOWN' and info_market_state != 'UNKNOWN':
|
588 |
+
market_state = info_market_state
|
589 |
+
|
590 |
+
except Exception as info_error:
|
591 |
+
self.logger.debug(f"info_method_failed ticker={ticker} error={str(info_error)}")
|
592 |
+
|
593 |
+
|
594 |
+
# Determine if market is open
|
595 |
+
is_open = market_state in ['REGULAR', 'PRE', 'POST']
|
596 |
+
|
597 |
+
self.logger.info(f"market_status_determined ticker={ticker} state={market_state} is_open={is_open} timezone={timezone_name}")
|
598 |
+
|
599 |
+
return MarketStatus(
|
600 |
+
is_open=is_open,
|
601 |
+
market_state=market_state,
|
602 |
+
timezone=timezone_name
|
603 |
+
)
|
604 |
+
|
605 |
+
except Exception as e:
|
606 |
+
self.logger.warning(f"market_status_check_failed ticker={ticker} error={str(e)}")
|
607 |
+
# Return conservative default status
|
608 |
+
return MarketStatus(
|
609 |
+
is_open=False,
|
610 |
+
market_state='UNKNOWN',
|
611 |
+
timezone='America/New_York'
|
612 |
+
)
|
613 |
+
|
614 |
def calculate_technical_indicators(self, df: pd.DataFrame) -> pd.DataFrame:
|
615 |
"""
|
616 |
Calculate technical indicators for a ticker's data.
|
|
|
1088 |
await database.engine.dispose()
|
1089 |
logger.info("database_lifecycle event=connections_closed")
|
1090 |
|
1091 |
+
# --------------------------
|
1092 |
+
# --- Create FastAPI app ---
|
1093 |
app = FastAPI(
|
1094 |
title="Stock Monitoring API",
|
1095 |
description="API for managing S&P 500 and Nasdaq 100 ticker data",
|
|
|
1097 |
lifespan=lifespan,
|
1098 |
)
|
1099 |
|
|
|
|
|
|
|
|
|
1100 |
# --- API ENDPOINTS ---
|
1101 |
|
1102 |
@app.get("/")
|
|
|
1595 |
raise HTTPException(status_code=500, detail="Failed to fetch ticker data")
|
1596 |
|
1597 |
|
1598 |
+
@app.post("/data/analyze")
|
1599 |
+
async def analyze_financial_data(
|
1600 |
+
request: FinancialDataRequest,
|
1601 |
+
http_request: Request,
|
1602 |
+
api_key: str = Depends(verify_api_key),
|
1603 |
+
rate_info: dict = Depends(check_rate_limit)
|
1604 |
+
):
|
1605 |
+
"""
|
1606 |
+
Download financial data for multiple tickers and calculate technical indicators without database storage.
|
1607 |
+
|
1608 |
+
**Security Features**:
|
1609 |
+
- **API Key Required**: Must provide valid API key in Authorization header
|
1610 |
+
- **Rate Limited**: Maximum 20 requests per minute per IP address
|
1611 |
+
- **Input Validation**: Comprehensive validation of ticker symbols and parameters
|
1612 |
+
- **Request Logging**: All requests are logged with IP address and timing
|
1613 |
+
|
1614 |
+
**Logic**:
|
1615 |
+
- Downloads real-time data from Yahoo Finance for the specified tickers and period
|
1616 |
+
- Optimized for multiple tickers by downloading them in a single batch request
|
1617 |
+
- Calculates technical indicators: SMA 10 (fast), SMA 20 (med), SMA 50 (slow)
|
1618 |
+
- Returns the data with technical indicators without storing in database
|
1619 |
+
- Useful for real-time analysis and testing without persisting data
|
1620 |
+
|
1621 |
+
**Authentication**:
|
1622 |
+
- **Header**: `Authorization: Bearer <your_api_key>`
|
1623 |
+
|
1624 |
+
**Rate Limits**:
|
1625 |
+
- **Limit**: 20 requests per minute per IP address
|
1626 |
+
- **Headers**: Response includes rate limit headers (X-RateLimit-*)
|
1627 |
+
|
1628 |
+
**Args**:
|
1629 |
+
- **request**: FinancialDataRequest (list of ticker symbols and period)
|
1630 |
+
- **http_request**: Request object (auto-injected for IP tracking)
|
1631 |
+
- **api_key**: API key for authentication (auto-injected)
|
1632 |
+
- **rate_info**: Rate limiting info (auto-injected)
|
1633 |
+
|
1634 |
+
**Supported periods**:
|
1635 |
+
- 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
|
1636 |
+
|
1637 |
+
**Supported intervals**:
|
1638 |
+
- 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 4h, 1d, 5d, 1wk, 1mo, 3mo
|
1639 |
+
|
1640 |
+
**Intraday Features**:
|
1641 |
+
- **Pre/Post Market**: Include extended hours data when `intraday=true`
|
1642 |
+
- **Market Status**: Real-time market status checking
|
1643 |
+
- **High Frequency**: Support for 5m to 4h intervals
|
1644 |
+
- **Restrictions**: Intraday limited to 1d/5d/1mo periods with 5m-4h intervals
|
1645 |
+
|
1646 |
+
**Example requests:**
|
1647 |
+
```bash
|
1648 |
+
# Daily data (default)
|
1649 |
+
curl -X POST "http://localhost:7860/data/analyze" \\
|
1650 |
+
-H "Authorization: Bearer your_api_key" \\
|
1651 |
+
-H "Content-Type: application/json" \\
|
1652 |
+
-d '{"tickers": ["AAPL", "MSFT"], "period": "3mo"}'
|
1653 |
+
|
1654 |
+
# Intraday 15-minute data with pre/post market
|
1655 |
+
curl -X POST "http://localhost:7860/data/analyze" \\
|
1656 |
+
-H "Authorization: Bearer your_api_key" \\
|
1657 |
+
-H "Content-Type: application/json" \\
|
1658 |
+
-d '{"tickers": ["AAPL"], "period": "1d", "interval": "15m", "intraday": true}'
|
1659 |
+
|
1660 |
+
# Hourly data for current week
|
1661 |
+
curl -X POST "http://localhost:7860/data/analyze" \\
|
1662 |
+
-H "Authorization: Bearer your_api_key" \\
|
1663 |
+
-H "Content-Type: application/json" \\
|
1664 |
+
-d '{"tickers": ["TSLA", "NVDA"], "period": "5d", "interval": "1h", "intraday": true}'
|
1665 |
+
```
|
1666 |
+
**Example request body**:
|
1667 |
+
```json
|
1668 |
+
{
|
1669 |
+
"tickers": ["TSLA", "NVDA"],
|
1670 |
+
"period": "5d",
|
1671 |
+
"interval": "1h",
|
1672 |
+
"intraday": true
|
1673 |
+
}
|
1674 |
+
```
|
1675 |
+
|
1676 |
+
**Example response:**
|
1677 |
+
```json
|
1678 |
+
{
|
1679 |
+
"success": true,
|
1680 |
+
"tickers": ["AAPL", "MSFT", "GOOGL"],
|
1681 |
+
"period": "3mo",
|
1682 |
+
"total_data_points": 195,
|
1683 |
+
"date_range": {
|
1684 |
+
"start_date": "2025-04-30",
|
1685 |
+
"end_date": "2025-07-31"
|
1686 |
+
},
|
1687 |
+
"data": [
|
1688 |
+
{
|
1689 |
+
"ticker": "AAPL",
|
1690 |
+
"date": "2025-07-31",
|
1691 |
+
"open": 150.25,
|
1692 |
+
"high": 152.80,
|
1693 |
+
"low": 149.50,
|
1694 |
+
"close": 151.75,
|
1695 |
+
"volume": 45123000,
|
1696 |
+
"sma_fast": 150.85,
|
1697 |
+
"sma_med": 149.92,
|
1698 |
+
"sma_slow": 148.15
|
1699 |
+
}
|
1700 |
+
],
|
1701 |
+
"calculated_at": "2025-07-31T14:15:26+00:00"
|
1702 |
+
}
|
1703 |
+
```
|
1704 |
+
|
1705 |
+
**Error Responses**:
|
1706 |
+
- **401**: Invalid or missing API key
|
1707 |
+
- **429**: Rate limit exceeded (includes Retry-After header)
|
1708 |
+
- **400**: Invalid input parameters
|
1709 |
+
- **404**: No data found for requested tickers
|
1710 |
+
- **500**: Internal server error
|
1711 |
+
"""
|
1712 |
+
try:
|
1713 |
+
logger = logging.getLogger(__name__)
|
1714 |
+
start_time = time.perf_counter()
|
1715 |
+
|
1716 |
+
# Security logging - get client IP for audit trail
|
1717 |
+
client_ip = http_request.headers.get("x-forwarded-for", "").split(",")[0].strip()
|
1718 |
+
if not client_ip:
|
1719 |
+
client_ip = http_request.headers.get("x-real-ip", "")
|
1720 |
+
if not client_ip:
|
1721 |
+
client_ip = getattr(http_request.client, "host", "unknown")
|
1722 |
+
|
1723 |
+
user_agent = http_request.headers.get("user-agent", "unknown")
|
1724 |
+
|
1725 |
+
# Enhanced input validation and security checks
|
1726 |
+
if not request.tickers or len(request.tickers) == 0:
|
1727 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=empty_tickers_list user_agent={user_agent}")
|
1728 |
+
raise HTTPException(
|
1729 |
+
status_code=400,
|
1730 |
+
detail="At least one ticker symbol is required."
|
1731 |
+
)
|
1732 |
+
|
1733 |
+
if len(request.tickers) > 50:
|
1734 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=too_many_tickers count={len(request.tickers)} user_agent={user_agent}")
|
1735 |
+
raise HTTPException(
|
1736 |
+
status_code=400,
|
1737 |
+
detail="Maximum 50 tickers allowed per request."
|
1738 |
+
)
|
1739 |
+
|
1740 |
+
# Clean and validate ticker symbols with enhanced security
|
1741 |
+
ticker_symbols = []
|
1742 |
+
for ticker in request.tickers:
|
1743 |
+
ticker_clean = str(ticker).upper().strip()
|
1744 |
+
|
1745 |
+
# Security: Check for malicious patterns
|
1746 |
+
if not ticker_clean or len(ticker_clean) > 10:
|
1747 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_ticker_length ticker={ticker} user_agent={user_agent}")
|
1748 |
+
raise HTTPException(
|
1749 |
+
status_code=400,
|
1750 |
+
detail=f"Invalid ticker symbol '{ticker}'. Must be 1-10 characters."
|
1751 |
+
)
|
1752 |
+
|
1753 |
+
# Security: Only allow alphanumeric characters and common symbols
|
1754 |
+
import re
|
1755 |
+
if not re.match(r'^[A-Z0-9\.\-\^]+$', ticker_clean):
|
1756 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_ticker_chars ticker={ticker} user_agent={user_agent}")
|
1757 |
+
raise HTTPException(
|
1758 |
+
status_code=400,
|
1759 |
+
detail=f"Invalid ticker symbol '{ticker}'. Only alphanumeric characters, dots, hyphens, and carets allowed."
|
1760 |
+
)
|
1761 |
+
|
1762 |
+
ticker_symbols.append(ticker_clean)
|
1763 |
+
|
1764 |
+
# Remove duplicates while preserving order
|
1765 |
+
seen = set()
|
1766 |
+
ticker_symbols = [x for x in ticker_symbols if not (x in seen or seen.add(x))]
|
1767 |
+
|
1768 |
+
# Validate period and interval with security logging
|
1769 |
+
valid_periods = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']
|
1770 |
+
valid_intervals = ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '4h', '1d', '5d', '1wk', '1mo', '3mo']
|
1771 |
+
|
1772 |
+
if request.period not in valid_periods:
|
1773 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_period period={request.period} user_agent={user_agent}")
|
1774 |
+
raise HTTPException(
|
1775 |
+
status_code=400,
|
1776 |
+
detail=f"Invalid period. Must be one of: {', '.join(valid_periods)}"
|
1777 |
+
)
|
1778 |
+
|
1779 |
+
if request.interval not in valid_intervals:
|
1780 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_interval interval={request.interval} user_agent={user_agent}")
|
1781 |
+
raise HTTPException(
|
1782 |
+
status_code=400,
|
1783 |
+
detail=f"Invalid interval. Must be one of: {', '.join(valid_intervals)}"
|
1784 |
+
)
|
1785 |
+
|
1786 |
+
# Validate intraday configuration
|
1787 |
+
if request.intraday:
|
1788 |
+
# For intraday data, restrict to shorter periods and specific intervals
|
1789 |
+
intraday_periods = ['1d', '5d', '1mo']
|
1790 |
+
intraday_intervals = ['5m', '15m', '30m', '60m', '90m', '1h', '4h']
|
1791 |
+
|
1792 |
+
if request.period not in intraday_periods:
|
1793 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_intraday_period period={request.period} user_agent={user_agent}")
|
1794 |
+
raise HTTPException(
|
1795 |
+
status_code=400,
|
1796 |
+
detail=f"For intraday data, period must be one of: {', '.join(intraday_periods)}"
|
1797 |
+
)
|
1798 |
+
|
1799 |
+
if request.interval not in intraday_intervals:
|
1800 |
+
logger.warning(f"security_validation_failed client_ip={client_ip} reason=invalid_intraday_interval interval={request.interval} user_agent={user_agent}")
|
1801 |
+
raise HTTPException(
|
1802 |
+
status_code=400,
|
1803 |
+
detail=f"For intraday data, interval must be one of: {', '.join(intraday_intervals)}"
|
1804 |
+
)
|
1805 |
+
|
1806 |
+
# Security audit log for successful request start
|
1807 |
+
logger.info(f"financial_data_analysis_started client_ip={client_ip} tickers={ticker_symbols} period={request.period} interval={request.interval} intraday={request.intraday} count={len(ticker_symbols)} api_key_valid=true user_agent={user_agent}")
|
1808 |
+
logger.info(f"rate_limit_info client_ip={client_ip} current_count={rate_info['current_count']} limit={rate_info['limit']} remaining={rate_info.get('remaining', 0)}")
|
1809 |
+
|
1810 |
+
# Get market status for the first ticker (representative)
|
1811 |
+
market_status = None
|
1812 |
+
if request.intraday or request.interval in ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '4h']:
|
1813 |
+
yfinance_svc = YFinanceService(config)
|
1814 |
+
market_status = yfinance_svc.get_market_status(ticker_symbols[0])
|
1815 |
+
logger.info(f"market_status_check ticker={ticker_symbols[0]} state={market_status.market_state} is_open={market_status.is_open}")
|
1816 |
+
|
1817 |
+
# Download data from Yahoo Finance - optimized for multiple tickers with interval support
|
1818 |
+
download_start = time.perf_counter()
|
1819 |
+
|
1820 |
+
# Configure download parameters
|
1821 |
+
download_params = {
|
1822 |
+
'period': request.period,
|
1823 |
+
'progress': False,
|
1824 |
+
'auto_adjust': True
|
1825 |
+
}
|
1826 |
+
|
1827 |
+
# Only group by ticker if we have multiple tickers
|
1828 |
+
if len(ticker_symbols) > 1:
|
1829 |
+
download_params['group_by'] = 'ticker'
|
1830 |
+
|
1831 |
+
# Add interval if different from default
|
1832 |
+
if request.interval != '1d':
|
1833 |
+
download_params['interval'] = request.interval
|
1834 |
+
|
1835 |
+
# For intraday data, include pre/post market data
|
1836 |
+
if request.intraday:
|
1837 |
+
download_params['prepost'] = True
|
1838 |
+
logger.info(f"intraday_download_enabled prepost=true interval={request.interval}")
|
1839 |
+
|
1840 |
+
data = yf.download(ticker_symbols, **download_params)
|
1841 |
+
download_end = time.perf_counter()
|
1842 |
+
|
1843 |
+
if data.empty:
|
1844 |
+
logger.warning(f"no_data_found tickers={ticker_symbols} period={request.period}")
|
1845 |
+
raise HTTPException(
|
1846 |
+
status_code=404,
|
1847 |
+
detail=f"No financial data found for tickers {ticker_symbols} with period {request.period}"
|
1848 |
+
)
|
1849 |
+
|
1850 |
+
logger.info(f"data_downloaded tickers_count={len(ticker_symbols)} rows={len(data)} duration_ms={(download_end-download_start)*1000:.2f}")
|
1851 |
+
|
1852 |
+
# Calculate technical indicators and convert to response format
|
1853 |
+
calc_start = time.perf_counter()
|
1854 |
+
if 'yfinance_svc' not in locals():
|
1855 |
+
yfinance_svc = YFinanceService(config)
|
1856 |
+
result_data = []
|
1857 |
+
all_dates = []
|
1858 |
+
|
1859 |
+
# Handle both single ticker and multi-ticker cases
|
1860 |
+
if len(ticker_symbols) == 1:
|
1861 |
+
# Single ticker case - flatten multi-level columns if they exist
|
1862 |
+
ticker = ticker_symbols[0]
|
1863 |
+
|
1864 |
+
# Check if we have multi-level columns and flatten them
|
1865 |
+
if isinstance(data.columns, pd.MultiIndex):
|
1866 |
+
# Flatten the multi-level columns by taking the first level (the actual column names)
|
1867 |
+
data.columns = data.columns.get_level_values(0)
|
1868 |
+
|
1869 |
+
data_with_indicators = yfinance_svc.calculate_technical_indicators(data)
|
1870 |
+
all_dates.extend(data_with_indicators.index.tolist())
|
1871 |
+
|
1872 |
+
for date_idx, row in data_with_indicators.iterrows():
|
1873 |
+
try:
|
1874 |
+
close_val = row['Close']
|
1875 |
+
if pd.isna(close_val):
|
1876 |
+
continue
|
1877 |
+
except (KeyError, ValueError):
|
1878 |
+
continue
|
1879 |
+
|
1880 |
+
# Format datetime based on data type (intraday vs daily)
|
1881 |
+
if request.intraday or request.interval in ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '4h']:
|
1882 |
+
datetime_str = date_idx.isoformat()
|
1883 |
+
else:
|
1884 |
+
datetime_str = date_idx.date().isoformat()
|
1885 |
+
|
1886 |
+
result_data.append(TechnicalIndicatorData(
|
1887 |
+
ticker=ticker,
|
1888 |
+
datetime=datetime_str,
|
1889 |
+
open=float(row['Open']),
|
1890 |
+
high=float(row['High']),
|
1891 |
+
low=float(row['Low']),
|
1892 |
+
close=float(row['Close']),
|
1893 |
+
volume=int(row['Volume']),
|
1894 |
+
sma_fast=float(row['sma_fast']) if pd.notna(row['sma_fast']) else None,
|
1895 |
+
sma_med=float(row['sma_med']) if pd.notna(row['sma_med']) else None,
|
1896 |
+
sma_slow=float(row['sma_slow']) if pd.notna(row['sma_slow']) else None
|
1897 |
+
))
|
1898 |
+
else:
|
1899 |
+
# Multiple tickers case - data is grouped by ticker
|
1900 |
+
processed_tickers = []
|
1901 |
+
for ticker in ticker_symbols:
|
1902 |
+
if ticker not in data.columns.get_level_values(0):
|
1903 |
+
logger.warning(f"ticker_data_missing ticker={ticker} reason=not_in_downloaded_data")
|
1904 |
+
continue
|
1905 |
+
|
1906 |
+
ticker_data = data[ticker]
|
1907 |
+
if ticker_data.empty:
|
1908 |
+
logger.warning(f"ticker_data_empty ticker={ticker}")
|
1909 |
+
continue
|
1910 |
+
|
1911 |
+
# Calculate technical indicators for this ticker
|
1912 |
+
ticker_data_with_indicators = yfinance_svc.calculate_technical_indicators(ticker_data)
|
1913 |
+
all_dates.extend(ticker_data_with_indicators.index.tolist())
|
1914 |
+
processed_tickers.append(ticker)
|
1915 |
+
|
1916 |
+
for date_idx, row in ticker_data_with_indicators.iterrows():
|
1917 |
+
try:
|
1918 |
+
close_val = row['Close']
|
1919 |
+
if pd.isna(close_val):
|
1920 |
+
continue
|
1921 |
+
except (KeyError, ValueError):
|
1922 |
+
continue
|
1923 |
+
|
1924 |
+
# Format datetime based on data type (intraday vs daily)
|
1925 |
+
if request.intraday or request.interval in ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '4h']:
|
1926 |
+
datetime_str = date_idx.isoformat()
|
1927 |
+
else:
|
1928 |
+
datetime_str = date_idx.date().isoformat()
|
1929 |
+
|
1930 |
+
result_data.append(TechnicalIndicatorData(
|
1931 |
+
ticker=ticker,
|
1932 |
+
datetime=datetime_str,
|
1933 |
+
open=float(row['Open']),
|
1934 |
+
high=float(row['High']),
|
1935 |
+
low=float(row['Low']),
|
1936 |
+
close=float(row['Close']),
|
1937 |
+
volume=int(row['Volume']),
|
1938 |
+
sma_fast=float(row['sma_fast']) if pd.notna(row['sma_fast']) else None,
|
1939 |
+
sma_med=float(row['sma_med']) if pd.notna(row['sma_med']) else None,
|
1940 |
+
sma_slow=float(row['sma_slow']) if pd.notna(row['sma_slow']) else None
|
1941 |
+
))
|
1942 |
+
|
1943 |
+
if not processed_tickers:
|
1944 |
+
raise HTTPException(
|
1945 |
+
status_code=404,
|
1946 |
+
detail=f"No valid data found for any of the requested tickers: {ticker_symbols}"
|
1947 |
+
)
|
1948 |
+
|
1949 |
+
calc_end = time.perf_counter()
|
1950 |
+
logger.info(f"indicators_calculated tickers_count={len(ticker_symbols)} duration_ms={(calc_end-calc_start)*1000:.2f}")
|
1951 |
+
|
1952 |
+
# Calculate date range
|
1953 |
+
start_date = min(all_dates).date() if all_dates else datetime.now().date()
|
1954 |
+
end_date = max(all_dates).date() if all_dates else datetime.now().date()
|
1955 |
+
|
1956 |
+
# Sort by ticker and datetime (most recent first)
|
1957 |
+
result_data.sort(key=lambda x: (x.ticker, x.datetime), reverse=True)
|
1958 |
+
|
1959 |
+
end_time = time.perf_counter()
|
1960 |
+
total_duration = end_time - start_time
|
1961 |
+
|
1962 |
+
# Security audit log for successful completion
|
1963 |
+
logger.info(f"financial_data_analysis_completed client_ip={client_ip} tickers={ticker_symbols} data_points={len(result_data)} total_duration_ms={total_duration*1000:.2f} status=success")
|
1964 |
+
|
1965 |
+
# Create response with security headers
|
1966 |
+
from fastapi.responses import JSONResponse
|
1967 |
+
|
1968 |
+
# Create response data
|
1969 |
+
response_data = {
|
1970 |
+
"success": True,
|
1971 |
+
"tickers": ticker_symbols,
|
1972 |
+
"period": request.period,
|
1973 |
+
"interval": request.interval,
|
1974 |
+
"intraday": request.intraday,
|
1975 |
+
"total_data_points": len(result_data),
|
1976 |
+
"date_range": {
|
1977 |
+
"start_date": start_date.isoformat(),
|
1978 |
+
"end_date": end_date.isoformat()
|
1979 |
+
},
|
1980 |
+
"market_status": {
|
1981 |
+
"is_open": market_status.is_open,
|
1982 |
+
"market_state": market_status.market_state,
|
1983 |
+
"timezone": market_status.timezone
|
1984 |
+
} if market_status else None,
|
1985 |
+
"data": [
|
1986 |
+
{
|
1987 |
+
"ticker": item.ticker,
|
1988 |
+
"datetime": item.datetime,
|
1989 |
+
"open": item.open,
|
1990 |
+
"high": item.high,
|
1991 |
+
"low": item.low,
|
1992 |
+
"close": item.close,
|
1993 |
+
"volume": item.volume,
|
1994 |
+
"sma_fast": item.sma_fast,
|
1995 |
+
"sma_med": item.sma_med,
|
1996 |
+
"sma_slow": item.sma_slow
|
1997 |
+
}
|
1998 |
+
for item in result_data
|
1999 |
+
],
|
2000 |
+
"calculated_at": datetime.now(pytz.UTC).isoformat()
|
2001 |
+
}
|
2002 |
+
|
2003 |
+
# Return JSONResponse with security headers
|
2004 |
+
return JSONResponse(
|
2005 |
+
content=response_data,
|
2006 |
+
headers={
|
2007 |
+
"X-RateLimit-Limit": str(rate_info["limit"]),
|
2008 |
+
"X-RateLimit-Remaining": str(rate_info.get("remaining", 0)),
|
2009 |
+
"X-Content-Type-Options": "nosniff",
|
2010 |
+
"X-Frame-Options": "DENY",
|
2011 |
+
"X-XSS-Protection": "1; mode=block"
|
2012 |
+
}
|
2013 |
+
)
|
2014 |
+
|
2015 |
+
except HTTPException:
|
2016 |
+
raise
|
2017 |
+
except Exception as e:
|
2018 |
+
logger = logging.getLogger(__name__)
|
2019 |
+
# Security audit log for errors
|
2020 |
+
client_ip = http_request.headers.get("x-forwarded-for", "").split(",")[0].strip()
|
2021 |
+
if not client_ip:
|
2022 |
+
client_ip = getattr(http_request.client, "host", "unknown")
|
2023 |
+
logger.error(f"financial_data_analysis_failed client_ip={client_ip} tickers={request.tickers} error={str(e)} status=error")
|
2024 |
+
raise HTTPException(
|
2025 |
+
status_code=500,
|
2026 |
+
detail=f"Failed to analyze financial data for tickers {request.tickers}: {str(e)}"
|
2027 |
+
)
|
2028 |
+
|
2029 |
+
|
2030 |
# Local execution configuration
|
2031 |
if __name__ == "__main__":
|
2032 |
import uvicorn
|
logs/api.log
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
tests/test_api.py
CHANGED
@@ -3,12 +3,16 @@
|
|
3 |
Automated API Testing Script for Stock Monitoring API
|
4 |
Tests authentication, endpoints, and security features.
|
5 |
|
6 |
-
Updated for new API architecture:
|
7 |
-
-
|
8 |
-
-
|
9 |
-
-
|
10 |
-
-
|
11 |
-
-
|
|
|
|
|
|
|
|
|
12 |
"""
|
13 |
|
14 |
import requests
|
@@ -19,6 +23,8 @@ from dotenv import load_dotenv
|
|
19 |
|
20 |
# Load environment variables from parent directory
|
21 |
load_dotenv(dotenv_path="../.env")
|
|
|
|
|
22 |
PORT = os.getenv("PORT", "7860")
|
23 |
print(f"Using PORT: {PORT}")
|
24 |
|
@@ -105,6 +111,7 @@ def test_protected_endpoints_no_auth():
|
|
105 |
("POST", "/tickers/update", {"force_refresh": False}),
|
106 |
("POST", "/tickers/update-async", {"force_refresh": False}),
|
107 |
("POST", "/data/download-all", {"force_refresh": False, "force_indicators": False}),
|
|
|
108 |
("GET", "/tasks", None),
|
109 |
("DELETE", "/tasks/old", None)
|
110 |
]
|
@@ -137,6 +144,7 @@ def test_protected_endpoints_invalid_auth():
|
|
137 |
protected_endpoints = [
|
138 |
("POST", "/tickers/update", {"force_refresh": False}),
|
139 |
("POST", "/data/download-all", {"force_refresh": False, "force_indicators": False}),
|
|
|
140 |
("GET", "/tasks", None),
|
141 |
]
|
142 |
|
@@ -321,7 +329,11 @@ def main():
|
|
321 |
"""Run all tests."""
|
322 |
print("π§ͺ Starting Stock Monitoring API Tests")
|
323 |
print(f"π Base URL: {BASE_URL}")
|
324 |
-
|
|
|
|
|
|
|
|
|
325 |
|
326 |
all_tests_passed = True
|
327 |
|
@@ -333,6 +345,11 @@ def main():
|
|
333 |
all_tests_passed &= test_protected_endpoints_valid_auth()
|
334 |
all_tests_passed &= test_data_endpoints()
|
335 |
all_tests_passed &= test_technical_indicators()
|
|
|
|
|
|
|
|
|
|
|
336 |
all_tests_passed &= test_sql_injection_safety()
|
337 |
|
338 |
# Final results
|
@@ -345,8 +362,15 @@ def main():
|
|
345 |
print("β
Public endpoints are accessible")
|
346 |
print("β
Bulk data download with freshness check is working")
|
347 |
print("β
Technical indicators (SMA 10, 20, 50) are working")
|
348 |
-
print("β
|
349 |
-
print("β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
else:
|
351 |
print("β SOME TESTS FAILED!")
|
352 |
print("β οΈ Please check the API implementation")
|
@@ -411,5 +435,321 @@ def test_technical_indicators():
|
|
411 |
|
412 |
return all_passed
|
413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
if __name__ == "__main__":
|
415 |
exit(main())
|
|
|
3 |
Automated API Testing Script for Stock Monitoring API
|
4 |
Tests authentication, endpoints, and security features.
|
5 |
|
6 |
+
Updated for new API architecture with /data/analyze endpoint:
|
7 |
+
- Added comprehensive /data/analyze endpoint testing
|
8 |
+
- Tests intraday data with multiple intervals (5m, 15m, 1h, 4h)
|
9 |
+
- Tests pre/post market data functionality
|
10 |
+
- Tests market status checking
|
11 |
+
- Tests rate limiting (20 req/min)
|
12 |
+
- Tests security features and input validation
|
13 |
+
- Tests multiple tickers optimization
|
14 |
+
- Validates technical indicators on all intervals
|
15 |
+
- Tests daily vs intraday data formats
|
16 |
"""
|
17 |
|
18 |
import requests
|
|
|
23 |
|
24 |
# Load environment variables from parent directory
|
25 |
load_dotenv(dotenv_path="../.env")
|
26 |
+
# Also try loading from current directory
|
27 |
+
load_dotenv()
|
28 |
PORT = os.getenv("PORT", "7860")
|
29 |
print(f"Using PORT: {PORT}")
|
30 |
|
|
|
111 |
("POST", "/tickers/update", {"force_refresh": False}),
|
112 |
("POST", "/tickers/update-async", {"force_refresh": False}),
|
113 |
("POST", "/data/download-all", {"force_refresh": False, "force_indicators": False}),
|
114 |
+
("POST", "/data/analyze", {"tickers": ["AAPL"], "period": "1d"}),
|
115 |
("GET", "/tasks", None),
|
116 |
("DELETE", "/tasks/old", None)
|
117 |
]
|
|
|
144 |
protected_endpoints = [
|
145 |
("POST", "/tickers/update", {"force_refresh": False}),
|
146 |
("POST", "/data/download-all", {"force_refresh": False, "force_indicators": False}),
|
147 |
+
("POST", "/data/analyze", {"tickers": ["AAPL"], "period": "1d"}),
|
148 |
("GET", "/tasks", None),
|
149 |
]
|
150 |
|
|
|
329 |
"""Run all tests."""
|
330 |
print("π§ͺ Starting Stock Monitoring API Tests")
|
331 |
print(f"π Base URL: {BASE_URL}")
|
332 |
+
if API_KEY:
|
333 |
+
print(f"π API Key: {API_KEY[:10]}...")
|
334 |
+
else:
|
335 |
+
print("β API Key not found in environment variables!")
|
336 |
+
return 1
|
337 |
|
338 |
all_tests_passed = True
|
339 |
|
|
|
345 |
all_tests_passed &= test_protected_endpoints_valid_auth()
|
346 |
all_tests_passed &= test_data_endpoints()
|
347 |
all_tests_passed &= test_technical_indicators()
|
348 |
+
all_tests_passed &= test_analyze_endpoint_daily()
|
349 |
+
all_tests_passed &= test_analyze_endpoint_intraday()
|
350 |
+
all_tests_passed &= test_analyze_endpoint_validation()
|
351 |
+
all_tests_passed &= test_analyze_endpoint_rate_limiting()
|
352 |
+
all_tests_passed &= test_analyze_endpoint_security()
|
353 |
all_tests_passed &= test_sql_injection_safety()
|
354 |
|
355 |
# Final results
|
|
|
362 |
print("β
Public endpoints are accessible")
|
363 |
print("β
Bulk data download with freshness check is working")
|
364 |
print("β
Technical indicators (SMA 10, 20, 50) are working")
|
365 |
+
print("β
/data/analyze endpoint with daily data is functional")
|
366 |
+
print("β
/data/analyze endpoint with intraday data is functional")
|
367 |
+
print("β
Market status checking is working")
|
368 |
+
print("β
Multiple tickers optimization is working")
|
369 |
+
print("β
Rate limiting (20 req/min) is enforced")
|
370 |
+
print("β
Security headers and input validation are active")
|
371 |
+
print("β
Pre/post market data functionality is working")
|
372 |
+
print("β
Multiple intervals (5m, 15m, 1h, 4h) are supported")
|
373 |
+
print("β
Intraday vs daily datetime formatting is correct")
|
374 |
else:
|
375 |
print("β SOME TESTS FAILED!")
|
376 |
print("β οΈ Please check the API implementation")
|
|
|
435 |
|
436 |
return all_passed
|
437 |
|
438 |
+
def test_analyze_endpoint_daily():
|
439 |
+
"""Test /data/analyze endpoint with daily data."""
|
440 |
+
print_test_header("Data Analyze Endpoint - Daily Data")
|
441 |
+
|
442 |
+
all_passed = True
|
443 |
+
|
444 |
+
# Test 1: Single ticker daily data
|
445 |
+
try:
|
446 |
+
payload = {
|
447 |
+
"tickers": ["AAPL"],
|
448 |
+
"period": "1mo"
|
449 |
+
}
|
450 |
+
response = requests.post(
|
451 |
+
f"{BASE_URL}/data/analyze",
|
452 |
+
headers=HEADERS_VALID_AUTH,
|
453 |
+
json=payload,
|
454 |
+
timeout=30
|
455 |
+
)
|
456 |
+
passed = response.status_code == 200
|
457 |
+
all_passed &= print_result("/data/analyze (single ticker)", "POST", 200, response.status_code, passed)
|
458 |
+
|
459 |
+
if passed:
|
460 |
+
data = response.json()
|
461 |
+
print(f" π Success: {data.get('success')}")
|
462 |
+
print(f" π Tickers: {data.get('tickers')}")
|
463 |
+
print(f" π
Period: {data.get('period')}")
|
464 |
+
print(f" π Interval: {data.get('interval')}")
|
465 |
+
print(f" π Intraday: {data.get('intraday')}")
|
466 |
+
print(f" π Data points: {data.get('total_data_points')}")
|
467 |
+
print(f" π
Date range: {data.get('date_range', {}).get('start_date')} to {data.get('date_range', {}).get('end_date')}")
|
468 |
+
|
469 |
+
# Validate response structure
|
470 |
+
if data.get('data') and len(data['data']) > 0:
|
471 |
+
sample = data['data'][0]
|
472 |
+
print(f" π° Sample close: ${sample.get('close', 0):.2f}")
|
473 |
+
print(f" π Has SMA Fast: {sample.get('sma_fast') is not None}")
|
474 |
+
print(f" π Has SMA Med: {sample.get('sma_med') is not None}")
|
475 |
+
print(f" π Has SMA Slow: {sample.get('sma_slow') is not None}")
|
476 |
+
# Check datetime format for daily data (should be date only)
|
477 |
+
datetime_str = sample.get('datetime', '')
|
478 |
+
print(f" π
DateTime format: {datetime_str} (daily: {len(datetime_str) == 10})")
|
479 |
+
|
480 |
+
except Exception as e:
|
481 |
+
print(f"β Single ticker daily test failed: {e}")
|
482 |
+
all_passed = False
|
483 |
+
|
484 |
+
# Small delay to avoid rate limiting
|
485 |
+
time.sleep(1)
|
486 |
+
|
487 |
+
# Test 2: Multiple tickers daily data
|
488 |
+
try:
|
489 |
+
payload = {
|
490 |
+
"tickers": ["AAPL", "MSFT", "GOOGL"],
|
491 |
+
"period": "5d"
|
492 |
+
}
|
493 |
+
response = requests.post(
|
494 |
+
f"{BASE_URL}/data/analyze",
|
495 |
+
headers=HEADERS_VALID_AUTH,
|
496 |
+
json=payload,
|
497 |
+
timeout=40
|
498 |
+
)
|
499 |
+
passed = response.status_code == 200
|
500 |
+
all_passed &= print_result("/data/analyze (multi ticker)", "POST", 200, response.status_code, passed)
|
501 |
+
|
502 |
+
if passed:
|
503 |
+
data = response.json()
|
504 |
+
print(f" π Tickers: {len(data.get('tickers', []))}")
|
505 |
+
print(f" π Data points: {data.get('total_data_points')}")
|
506 |
+
|
507 |
+
# Check rate limit headers
|
508 |
+
rate_limit = response.headers.get('X-RateLimit-Limit')
|
509 |
+
rate_remaining = response.headers.get('X-RateLimit-Remaining')
|
510 |
+
if rate_limit:
|
511 |
+
print(f" π¦ Rate limit: {rate_remaining}/{rate_limit}")
|
512 |
+
|
513 |
+
except Exception as e:
|
514 |
+
print(f"β Multi ticker daily test failed: {e}")
|
515 |
+
all_passed = False
|
516 |
+
|
517 |
+
return all_passed
|
518 |
+
|
519 |
+
def test_analyze_endpoint_intraday():
|
520 |
+
"""Test /data/analyze endpoint with intraday data."""
|
521 |
+
print_test_header("Data Analyze Endpoint - Intraday Data")
|
522 |
+
|
523 |
+
all_passed = True
|
524 |
+
|
525 |
+
# Test different intervals
|
526 |
+
intervals_to_test = [
|
527 |
+
("15m", "15-minute intervals"),
|
528 |
+
("1h", "1-hour intervals"),
|
529 |
+
("4h", "4-hour intervals")
|
530 |
+
]
|
531 |
+
|
532 |
+
for interval, description in intervals_to_test:
|
533 |
+
try:
|
534 |
+
payload = {
|
535 |
+
"tickers": ["AAPL"],
|
536 |
+
"period": "1d",
|
537 |
+
"interval": interval,
|
538 |
+
"intraday": True
|
539 |
+
}
|
540 |
+
response = requests.post(
|
541 |
+
f"{BASE_URL}/data/analyze",
|
542 |
+
headers=HEADERS_VALID_AUTH,
|
543 |
+
json=payload,
|
544 |
+
timeout=30
|
545 |
+
)
|
546 |
+
passed = response.status_code == 200
|
547 |
+
all_passed &= print_result(f"/data/analyze ({interval})", "POST", 200, response.status_code, passed)
|
548 |
+
|
549 |
+
if passed:
|
550 |
+
data = response.json()
|
551 |
+
print(f" π {description}: {data.get('total_data_points')} points")
|
552 |
+
print(f" π Intraday: {data.get('intraday')}")
|
553 |
+
print(f" π Interval: {data.get('interval')}")
|
554 |
+
|
555 |
+
# Check market status
|
556 |
+
market_status = data.get('market_status')
|
557 |
+
if market_status:
|
558 |
+
print(f" π Market open: {market_status.get('is_open')}")
|
559 |
+
print(f" π Market state: {market_status.get('market_state')}")
|
560 |
+
print(f" π Timezone: {market_status.get('timezone')}")
|
561 |
+
|
562 |
+
# Check datetime format for intraday (should include time)
|
563 |
+
if data.get('data') and len(data['data']) > 0:
|
564 |
+
sample = data['data'][0]
|
565 |
+
datetime_str = sample.get('datetime', '')
|
566 |
+
print(f" π
DateTime format: {datetime_str[:19]}... (intraday: {len(datetime_str) > 10})")
|
567 |
+
|
568 |
+
# Add delay to avoid rate limiting during tests
|
569 |
+
time.sleep(2)
|
570 |
+
|
571 |
+
except Exception as e:
|
572 |
+
print(f"β Intraday {interval} test failed: {e}")
|
573 |
+
all_passed = False
|
574 |
+
|
575 |
+
return all_passed
|
576 |
+
|
577 |
+
def test_analyze_endpoint_validation():
|
578 |
+
"""Test /data/analyze endpoint input validation."""
|
579 |
+
print_test_header("Data Analyze Endpoint - Input Validation")
|
580 |
+
|
581 |
+
all_passed = True
|
582 |
+
|
583 |
+
# Test invalid inputs
|
584 |
+
test_cases = [
|
585 |
+
# (payload, expected_status, description)
|
586 |
+
({"tickers": [], "period": "1d"}, 400, "Empty tickers list"),
|
587 |
+
({"tickers": ["INVALID!!!"], "period": "1d"}, 400, "Invalid ticker characters"),
|
588 |
+
({"tickers": ["AAPL"], "period": "invalid"}, 400, "Invalid period"),
|
589 |
+
({"tickers": ["AAPL"], "period": "1d", "interval": "invalid"}, 400, "Invalid interval"),
|
590 |
+
({"tickers": ["AAPL"], "period": "1y", "interval": "5m", "intraday": True}, 400, "Invalid intraday period"),
|
591 |
+
({"tickers": ["AAPL"], "period": "1d", "interval": "1d", "intraday": True}, 400, "Invalid intraday interval"),
|
592 |
+
({"tickers": ["A" * 50] * 51, "period": "1d"}, 400, "Too many tickers"),
|
593 |
+
]
|
594 |
+
|
595 |
+
for payload, expected_status, description in test_cases:
|
596 |
+
try:
|
597 |
+
response = requests.post(
|
598 |
+
f"{BASE_URL}/data/analyze",
|
599 |
+
headers=HEADERS_VALID_AUTH,
|
600 |
+
json=payload,
|
601 |
+
timeout=15
|
602 |
+
)
|
603 |
+
passed = response.status_code == expected_status
|
604 |
+
all_passed &= print_result(f"/data/analyze ({description})", "POST", expected_status, response.status_code, passed)
|
605 |
+
|
606 |
+
if not passed:
|
607 |
+
print(f" π Response: {response.text[:100]}...")
|
608 |
+
|
609 |
+
# Small delay to avoid rate limiting except for rate limit test
|
610 |
+
if "Too many tickers" not in description:
|
611 |
+
time.sleep(0.5)
|
612 |
+
|
613 |
+
except Exception as e:
|
614 |
+
print(f"β Validation test '{description}' failed: {e}")
|
615 |
+
all_passed = False
|
616 |
+
|
617 |
+
return all_passed
|
618 |
+
|
619 |
+
def test_analyze_endpoint_rate_limiting():
|
620 |
+
"""Test rate limiting on /data/analyze endpoint."""
|
621 |
+
print_test_header("Data Analyze Endpoint - Rate Limiting")
|
622 |
+
|
623 |
+
all_passed = True
|
624 |
+
|
625 |
+
print(" β° Waiting 5 seconds to start with fresh rate limit...")
|
626 |
+
time.sleep(5)
|
627 |
+
print(" π¦ Testing rate limit (20 requests/minute)...")
|
628 |
+
|
629 |
+
# Make requests quickly to test rate limiting
|
630 |
+
payload = {"tickers": ["AAPL"], "period": "1d"}
|
631 |
+
successful_requests = 0
|
632 |
+
rate_limited_requests = 0
|
633 |
+
|
634 |
+
for i in range(15): # Try 15 requests (moderate test, not exhausting all)
|
635 |
+
try:
|
636 |
+
response = requests.post(
|
637 |
+
f"{BASE_URL}/data/analyze",
|
638 |
+
headers=HEADERS_VALID_AUTH,
|
639 |
+
json=payload,
|
640 |
+
timeout=10
|
641 |
+
)
|
642 |
+
|
643 |
+
if response.status_code == 200:
|
644 |
+
successful_requests += 1
|
645 |
+
elif response.status_code == 429:
|
646 |
+
rate_limited_requests += 1
|
647 |
+
print(f" π¦ Rate limited on request {i+1}")
|
648 |
+
|
649 |
+
# Check rate limit headers
|
650 |
+
retry_after = response.headers.get('Retry-After')
|
651 |
+
if retry_after:
|
652 |
+
print(f" β° Retry after: {retry_after} seconds")
|
653 |
+
break
|
654 |
+
else:
|
655 |
+
print(f" β οΈ Unexpected status {response.status_code} on request {i+1}")
|
656 |
+
|
657 |
+
# Very small delay between requests
|
658 |
+
time.sleep(0.05)
|
659 |
+
|
660 |
+
except Exception as e:
|
661 |
+
print(f"β Rate limit test request {i+1} failed: {e}")
|
662 |
+
|
663 |
+
print(f" β
Successful requests: {successful_requests}")
|
664 |
+
print(f" π¦ Rate limited requests: {rate_limited_requests}")
|
665 |
+
|
666 |
+
# Rate limiting should kick in
|
667 |
+
passed = successful_requests > 0 and rate_limited_requests > 0
|
668 |
+
all_passed &= passed
|
669 |
+
|
670 |
+
if passed:
|
671 |
+
print(" β
Rate limiting is working correctly")
|
672 |
+
else:
|
673 |
+
print(" β Rate limiting may not be working properly")
|
674 |
+
|
675 |
+
return all_passed
|
676 |
+
|
677 |
+
def test_analyze_endpoint_security():
|
678 |
+
"""Test security features of /data/analyze endpoint."""
|
679 |
+
print_test_header("Data Analyze Endpoint - Security Features")
|
680 |
+
|
681 |
+
all_passed = True
|
682 |
+
|
683 |
+
print(" β° Waiting 10 seconds before security tests...")
|
684 |
+
time.sleep(10)
|
685 |
+
|
686 |
+
# Test 1: SQL injection attempts in ticker names
|
687 |
+
injection_attempts = [
|
688 |
+
"'; DROP TABLE tickers; --",
|
689 |
+
"' OR '1'='1",
|
690 |
+
"AAPL'; DELETE FROM tasks; --",
|
691 |
+
"<script>alert('xss')</script>",
|
692 |
+
"../../etc/passwd"
|
693 |
+
]
|
694 |
+
|
695 |
+
for injection in injection_attempts:
|
696 |
+
try:
|
697 |
+
payload = {"tickers": [injection], "period": "1d"}
|
698 |
+
response = requests.post(
|
699 |
+
f"{BASE_URL}/data/analyze",
|
700 |
+
headers=HEADERS_VALID_AUTH,
|
701 |
+
json=payload,
|
702 |
+
timeout=15
|
703 |
+
)
|
704 |
+
|
705 |
+
# Should return 400 (validation error) for malicious input
|
706 |
+
# But if rate limited, skip this specific test
|
707 |
+
if response.status_code == 429:
|
708 |
+
print(f" π¦ Rate limited during injection test: {injection[:20]}... (skipping)")
|
709 |
+
continue
|
710 |
+
|
711 |
+
passed = response.status_code == 400
|
712 |
+
all_passed &= print_result(f"/data/analyze (injection: {injection[:20]}...)", "POST", 400, response.status_code, passed)
|
713 |
+
|
714 |
+
# Small delay to avoid rate limiting
|
715 |
+
time.sleep(0.5)
|
716 |
+
|
717 |
+
except Exception as e:
|
718 |
+
print(f"β Security test failed: {e}")
|
719 |
+
all_passed = False
|
720 |
+
|
721 |
+
# Test 2: Check security headers in response
|
722 |
+
try:
|
723 |
+
payload = {"tickers": ["AAPL"], "period": "1d"}
|
724 |
+
response = requests.post(
|
725 |
+
f"{BASE_URL}/data/analyze",
|
726 |
+
headers=HEADERS_VALID_AUTH,
|
727 |
+
json=payload,
|
728 |
+
timeout=15
|
729 |
+
)
|
730 |
+
|
731 |
+
if response.status_code == 200:
|
732 |
+
security_headers = [
|
733 |
+
'X-Content-Type-Options',
|
734 |
+
'X-Frame-Options',
|
735 |
+
'X-XSS-Protection'
|
736 |
+
]
|
737 |
+
|
738 |
+
headers_found = 0
|
739 |
+
for header in security_headers:
|
740 |
+
if header in response.headers:
|
741 |
+
headers_found += 1
|
742 |
+
print(f" π‘οΈ {header}: {response.headers[header]}")
|
743 |
+
|
744 |
+
passed = headers_found >= 2 # At least 2 security headers
|
745 |
+
all_passed &= passed
|
746 |
+
print(f" π‘οΈ Security headers: {headers_found}/{len(security_headers)}")
|
747 |
+
|
748 |
+
except Exception as e:
|
749 |
+
print(f"β Security headers test failed: {e}")
|
750 |
+
all_passed = False
|
751 |
+
|
752 |
+
return all_passed
|
753 |
+
|
754 |
if __name__ == "__main__":
|
755 |
exit(main())
|