import uuid import json import asyncio import httpx from app.main import app async def run_tests(): print("šŸš€ Running Startup-Grade REST API Verification (FastAPI)...") # API KEY for authentication API_KEY = "ScamPatch_ftw2026Guvi" HEADERS = {"x-api-key": API_KEY} # Use httpx with ASGITransport for modern testing async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: # 1. Health Check try: response = await client.get("/") assert response.status_code == 200 print("āœ… Health Check Passed") except Exception as e: print(f"āŒ Health Check Failed: {e}") # 2. Admin Report try: response = await client.get("/admin/report", headers=HEADERS) assert response.status_code == 200 print("āœ… Admin Auth & Persistence Passed") except Exception as e: print(f"āŒ Admin Report Failed: {e}") # 2.1 Syndicate Graph try: response = await client.get("/syndicate/graph", headers=HEADERS) assert response.status_code == 200 data = response.json() assert "nodes" in data print("āœ… Syndicate Graph API Passed") except Exception as e: print(f"āŒ Syndicate Graph Failed: {e}") # 3. Webhook (Agentic Loop - First Message) try: session_id = f"test_{uuid.uuid4().hex[:8]}" payload = { "sessionId": session_id, "message": { "sender": "scammer", "text": "Your bank account will be blocked today. Verify immediately.", "timestamp": 1770005528731 }, "conversationHistory": [], "apiKey": API_KEY } print(f" šŸ“¤ Sending First Message: {payload['message']['text']}") response = await client.post("/webhook", json=payload, headers=HEADERS) if response.status_code != 200: print(f"āŒ Webhook failed with status {response.status_code}: {response.text}") else: data = response.json() assert data["status"] == "success" print("āœ… First Message Loop Passed") print(f" šŸ¤– Agent Reply: {data['reply']}") # 4. Webhook (Follow-up Message) follow_up_payload = { "sessionId": session_id, "message": { "sender": "scammer", "text": "Share your UPI ID to avoid account suspension.", "timestamp": 1770005528731 }, "conversationHistory": [ { "sender": "scammer", "text": "Your bank account will be blocked today. Verify immediately.", "timestamp": 1770005528731 }, { "sender": "user", "text": data["reply"], "timestamp": 1770005528731 } ], "apiKey": API_KEY } print(f" šŸ“¤ Sending Follow-up Message: {follow_up_payload['message']['text']}") response = await client.post("/webhook", json=follow_up_payload, headers=HEADERS) if response.status_code != 200: print(f"āŒ Follow-up Webhook failed: {response.text}") else: data = response.json() assert data["status"] == "success" print("āœ… Follow-up Message Loop Passed") print(f" šŸ¤– Agent Reply: {data['reply']}") if "metadata" in data: print(f" šŸ“Š Syndicate Score: {data['metadata'].get('syndicate_score', 0)}") print(f" šŸ” Scam Detected: {data['metadata'].get('scam_detected')}") except Exception as e: print(f"āŒ Webhook Test Error: {e}") print("\nšŸŽ‰ PROJECT STATUS: EVALUATION READY") if __name__ == "__main__": asyncio.run(run_tests())