""" Integration test script for PortIQ API. Validates all major endpoints sequentially using the default login credentials. """ import os import sys import json import requests API_URL = "http://127.0.0.1:8000/api" def run_tests(): print("šŸš€ Starting PortIQ Integration Tests...") # 1. Test Login print("\nšŸ”‘ 1. Testing Login Endpoint (/api/auth/login)...") login_payload = { "role": "owner", "password": "tcr-owner" } r = requests.post(f"{API_URL}/auth/login", json=login_payload) if r.status_code != 200: print(f"āŒ Login failed with status code {r.status_code}: {r.text}") return False auth_data = r.json() token = auth_data.get("token") print(f"āœ… Login successful! Token: {token[:10]}...") headers = { "Authorization": f"Bearer {token}" } # 2. Test Get Holdings print("\nšŸ“‹ 2. Testing Get Holdings (/api/portfolio/holdings)...") r = requests.get(f"{API_URL}/portfolio/holdings", headers=headers) if r.status_code != 200: print(f"āŒ Get holdings failed: {r.text}") return False print(f"āœ… Get holdings successful! Found {len(r.json().get('holdings', []))} assets.") # 3. Test Upload CSV print("\nšŸ“¤ 3. Testing Upload CSV (/api/portfolio/upload)...") csv_data = ( "Instrument,Qty.,Avg. cost,LTP,Invested,Cur. val,P&L,Net chg.,Day chg.,\n" "TCS,10,3200.00,3400.00,32000.00,34000.00,2000.00,6.25,1.20,\n" "INFY,20,1500.00,1400.00,30000.00,28000.00,-2000.00,-6.67,-0.80,\n" ) files = { "file": ("test_holdings.csv", csv_data, "text/csv") } r = requests.post(f"{API_URL}/portfolio/upload", headers=headers, files=files) if r.status_code != 200: print(f"āŒ CSV upload failed: {r.text}") return False print("āœ… CSV upload successful!") # 4. Test Briefing Generation print("\nšŸ¤– 4. Testing Briefing Generation (/api/briefings/generate)...") print("āŒ› Generating briefing (this uses Gemini API, may take 5-15 seconds)...") r = requests.post(f"{API_URL}/briefings/generate?enable_finbert=false&save_history=true", headers=headers) if r.status_code != 200: print(f"āŒ Briefing generation failed: {r.text}") return False briefing_data = r.json() date_str = briefing_data.get("date") print(f"āœ… Briefing generated successfully for date: {date_str}!") print(f" Market Mood: {briefing_data.get('mood', {}).get('market')}") print(f" Opening Call: {briefing_data.get('mood', {}).get('nifty_call')}") print(f" Top Pick: {briefing_data.get('top_picks', [{}])[0].get('symbol')}") # 5. Test Briefing History Logs print("\nšŸ“… 5. Testing Briefing History logs (/api/briefings/history)...") r = requests.get(f"{API_URL}/briefings/history", headers=headers) if r.status_code != 200: print(f"āŒ History list failed: {r.text}") return False print(f"āœ… History retrieved! Found {len(r.json())} briefings.") # 6. Test Specific Briefing print(f"\nšŸ“– 6. Testing Fetch Briefing for {date_str} (/api/briefings/{{date}})...") r = requests.get(f"{API_URL}/briefings/{date_str}", headers=headers) if r.status_code != 200: print(f"āŒ Fetch specific briefing failed: {r.text}") return False print("āœ… Specific briefing details retrieved successfully!") # 7. Test AI Chat Companion print("\nšŸ’¬ 7. Testing AI Chat Assistant (/api/chat/ask)...") chat_payload = { "query": "Hi, what are my holdings and what is today's top pick?", "active_date": date_str, "chat_history": [] } r = requests.post(f"{API_URL}/chat/ask", headers=headers, json=chat_payload) if r.status_code != 200: print(f"āŒ Chat request failed: {r.text}") return False print("āœ… Chat response received successfully!") print(f" AI response: {r.json().get('response')[:150]}...") # 8. Test Performance Engine print("\nšŸ“ˆ 8. Testing Performance metrics (/api/performance)...") r = requests.get(f"{API_URL}/performance?refresh=true", headers=headers) if r.status_code != 200: print(f"āŒ Performance calculation failed: {r.text}") return False perf_data = r.json() print("āœ… Performance metrics computed successfully!") print(f" Composite AI Score: {perf_data.get('summary', {}).get('ai_score')}") print(f" Buy Win Rate: {perf_data.get('summary', {}).get('buy_win_rate')}%") print(f" Signals tracked: {perf_data.get('summary', {}).get('total_signals')}") print("\nšŸŽ‰ ALL TESTS PASSED SUCCESSFULLY! 🧠") return True if __name__ == "__main__": success = run_tests() sys.exit(0 if success else 1)