customeragent-api / server /tests /test_api.py
anasraza526's picture
Clean deploy to Hugging Face
ac90985
#!/usr/bin/env python3
"""
QA Test Suite for Customer Agent API
"""
import requests
import json
import time
BASE_URL = "http://localhost:8000"
def test_health_endpoint():
"""Test health check endpoint"""
print("πŸ” Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/health")
assert response.status_code == 200
data = response.json()
assert "status" in data
print("βœ… Health endpoint working")
return True
except Exception as e:
print(f"❌ Health endpoint failed: {e}")
return False
def test_user_registration():
"""Test user registration"""
print("πŸ” Testing user registration...")
try:
test_user = {
"email": f"test_{int(time.time())}@example.com",
"password": "testpassword123",
"name": "Test User"
}
response = requests.post(f"{BASE_URL}/api/auth/register", json=test_user)
assert response.status_code == 200
data = response.json()
assert "access_token" in data
print("βœ… User registration working")
return test_user, data["access_token"]
except Exception as e:
print(f"❌ User registration failed: {e}")
return None, None
def test_website_creation(token):
"""Test website creation"""
print("πŸ” Testing website creation...")
try:
headers = {"Authorization": f"Bearer {token}"}
website_data = {
"url": "https://example.com",
"name": "Test Website",
"industry": "technology",
"tone": "friendly"
}
response = requests.post(f"{BASE_URL}/api/websites/", json=website_data, headers=headers)
assert response.status_code == 200
data = response.json()
assert "id" in data
print("βœ… Website creation working")
return data["id"]
except Exception as e:
print(f"❌ Website creation failed: {e}")
return None
def test_chat_functionality(website_id):
"""Test chat functionality"""
print("πŸ” Testing chat functionality...")
try:
chat_data = {
"message": "Hello, what services do you offer?",
"website_id": website_id,
"session_id": f"test_session_{int(time.time())}"
}
response = requests.post(f"{BASE_URL}/api/chat", json=chat_data)
assert response.status_code == 200
data = response.json()
assert "response" in data
assert len(data["response"]) > 0
print("βœ… Chat functionality working")
return True
except Exception as e:
print(f"❌ Chat functionality failed: {e}")
return False
def test_widget_endpoint(website_id):
"""Test widget greeting endpoint"""
print("πŸ” Testing widget greeting...")
try:
response = requests.get(f"{BASE_URL}/api/websites/{website_id}/greeting")
assert response.status_code == 200
data = response.json()
assert "greeting" in data
print("βœ… Widget greeting working")
return True
except Exception as e:
print(f"❌ Widget greeting failed: {e}")
return False
def run_qa_tests():
"""Run all QA tests"""
print("πŸš€ Starting QA Test Suite...")
print("=" * 50)
# Test 1: Health check
if not test_health_endpoint():
print("❌ Critical: Health endpoint failed")
return False
# Test 2: User registration
user_data, token = test_user_registration()
if not token:
print("❌ Critical: User registration failed")
return False
# Test 3: Website creation
website_id = test_website_creation(token)
if not website_id:
print("❌ Critical: Website creation failed")
return False
# Test 4: Chat functionality
if not test_chat_functionality(website_id):
print("❌ Critical: Chat functionality failed")
return False
# Test 5: Widget greeting
if not test_widget_endpoint(website_id):
print("❌ Widget greeting failed")
print("=" * 50)
print("βœ… QA Tests Completed Successfully!")
return True
if __name__ == "__main__":
run_qa_tests()