Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| import requests | |
| import json | |
| BASE_URL = "http://localhost:8000/api" | |
| def test_auth(): | |
| print("π Testing Authentication APIs...") | |
| # Test Registration | |
| print("\n1. Testing Registration") | |
| reg_data = {"email": "testuser@example.com", "password": "testpass123"} | |
| response = requests.post(f"{BASE_URL}/auth/register", json=reg_data) | |
| print(f"Register: {response.status_code} - {response.json()}") | |
| # Test Login | |
| print("\n2. Testing Login") | |
| login_data = {"email": "testuser@example.com", "password": "testpass123"} | |
| response = requests.post(f"{BASE_URL}/auth/login", json=login_data) | |
| print(f"Login: {response.status_code} - {response.json()}") | |
| cookies = response.cookies | |
| # Test Me endpoint | |
| print("\n3. Testing /auth/me") | |
| response = requests.get(f"{BASE_URL}/auth/me", cookies=cookies) | |
| print(f"Me: {response.status_code} - {response.json()}") | |
| return cookies | |
| def test_websites(cookies): | |
| print("\nπ Testing Website APIs...") | |
| # Add website | |
| print("\n1. Adding Website") | |
| website_data = {"url": "https://example.com", "name": "Test Site"} | |
| response = requests.post(f"{BASE_URL}/websites/", json=website_data, cookies=cookies) | |
| print(f"Add Website: {response.status_code} - {response.json()}") | |
| # Get websites | |
| print("\n2. Getting Websites") | |
| response = requests.get(f"{BASE_URL}/websites/", cookies=cookies) | |
| print(f"Get Websites: {response.status_code} - {response.json()}") | |
| if response.status_code == 200: | |
| websites = response.json() | |
| if websites: | |
| website_id = websites[0]["id"] | |
| # Verify website | |
| print(f"\n3. Verifying Website {website_id}") | |
| response = requests.post(f"{BASE_URL}/websites/{website_id}/verify", cookies=cookies) | |
| print(f"Verify: {response.status_code} - {response.json()}") | |
| # Fetch content | |
| print(f"\n4. Fetching Content for Website {website_id}") | |
| response = requests.post(f"{BASE_URL}/websites/{website_id}/fetch-content", cookies=cookies) | |
| print(f"Fetch Content: {response.status_code} - {response.json()}") | |
| return website_id | |
| return None | |
| def test_chat(website_id): | |
| print("\n㪠Testing Chat API...") | |
| if website_id: | |
| chat_data = {"message": "Hello, how can I help?", "website_id": website_id} | |
| response = requests.post(f"{BASE_URL}/chat/", json=chat_data) | |
| print(f"Chat: {response.status_code} - {response.json()}") | |
| def test_actions(website_id): | |
| print("\nπ― Testing Actions API...") | |
| if website_id: | |
| contact_data = { | |
| "website_id": website_id, | |
| "visitor_name": "John Doe", | |
| "visitor_email": "john@example.com", | |
| "message": "I need help with your product", | |
| "chat_context": "Previous conversation about pricing" | |
| } | |
| response = requests.post(f"{BASE_URL}/actions/contact-owner", json=contact_data) | |
| print(f"Contact Owner: {response.status_code} - {response.json()}") | |
| def test_scrape(website_id, cookies): | |
| print("\nπ Testing Scrape API...") | |
| if website_id: | |
| # Verify script | |
| print(f"\n1. Verifying Script for Website {website_id}") | |
| response = requests.post(f"{BASE_URL}/scrape/{website_id}/verify-script", cookies=cookies) | |
| print(f"Verify Script: {response.status_code} - {response.json()}") | |
| # Process content | |
| print(f"\n2. Processing Content for Website {website_id}") | |
| response = requests.post(f"{BASE_URL}/scrape/{website_id}/process-content", cookies=cookies) | |
| print(f"Process Content: {response.status_code} - {response.json()}") | |
| def main(): | |
| print("π Starting API Tests...") | |
| try: | |
| # Test authentication | |
| cookies = test_auth() | |
| # Test websites | |
| website_id = test_websites(cookies) | |
| # Test chat | |
| test_chat(website_id) | |
| # Test actions | |
| test_actions(website_id) | |
| # Test scrape | |
| test_scrape(website_id, cookies) | |
| print("\nβ All API tests completed!") | |
| except Exception as e: | |
| print(f"\nβ Test failed: {e}") | |
| if __name__ == "__main__": | |
| main() |