Spaces:
Running
Running
File size: 12,626 Bytes
431214c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
#!/usr/bin/env python3
# filepath: test_crawlgpt_api.py
import requests
import time
import json
import random
import string
import argparse
import sys
from pprint import pprint
class CrawlGPTTester:
def __init__(self, base_url="http://127.0.0.1:5000"):
self.base_url = base_url
self.token = None
self.user_id = None
self.username = None
self.test_data = {}
def generate_random_string(self, length=8):
"""Generate a random string for test data"""
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
def print_response(self, response, label):
"""Pretty print API responses"""
print(f"\n{'=' * 80}")
print(f"β
{label} - Status Code: {response.status_code}")
print(f"{'=' * 80}")
try:
pprint(response.json())
except:
print(response.text)
print(f"{'=' * 80}\n")
sys.stdout.flush()
def print_error(self, response, label):
"""Pretty print API error responses"""
print(f"\n{'=' * 80}")
print(f"β {label} - Status Code: {response.status_code}")
print(f"{'=' * 80}")
try:
pprint(response.json())
except:
print(response.text)
print(f"{'=' * 80}\n")
sys.stdout.flush()
def test_welcome(self):
"""Test welcome endpoint"""
response = requests.get(f"{self.base_url}/")
if response.status_code == 200:
self.print_response(response, "Welcome Endpoint")
return True
else:
self.print_error(response, "Welcome Endpoint")
return False
def test_register(self):
"""Test user registration"""
self.username = f"testuser_{self.generate_random_string()}"
password = "Test123!"
email = f"{self.username}@test.com"
data = {
"username": self.username,
"password": password,
"email": email
}
response = requests.post(f"{self.base_url}/api/register", json=data)
if response.status_code == 200:
self.print_response(response, "User Registration")
self.test_data["registration"] = data
return True
else:
self.print_error(response, "User Registration")
return False
def test_login(self):
"""Test user login"""
if not self.username:
print("Error: No user registered to log in with")
return False
data = {
"username": self.username,
"password": self.test_data["registration"]["password"]
}
response = requests.post(f"{self.base_url}/api/login", json=data)
if response.status_code == 200:
self.print_response(response, "User Login")
response_data = response.json()
self.token = response_data.get("token")
self.user_id = response_data.get("user", {}).get("id")
if not self.token:
print("Error: No token received from login")
return False
return True
else:
self.print_error(response, "User Login")
return False
def test_process_url(self, url="https://www.teachermagazine.com/in_en/articles/research-news-disability-inclusion-in-classroom-assessments"):
"""Test URL processing"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
data = {"url": url}
print(f"Processing URL: {url}")
response = requests.post(f"{self.base_url}/api/process-url", headers=headers, json=data)
if response.status_code == 200:
self.print_response(response, "Process URL")
return True
else:
self.print_error(response, "Process URL")
return False
def test_chat(self, message="What is the main topic of this page?"):
"""Test chat endpoint"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
data = {
"message": message,
"temperature": 0.7,
"max_tokens": 1000,
"model_id": "llama-3.1-8b-instant",
"use_summary": False
}
print(f"Sending chat message: {message}")
response = requests.post(f"{self.base_url}/api/chat", headers=headers, json=data)
if response.status_code == 200:
self.print_response(response, "Chat")
return True
else:
self.print_error(response, "Chat")
return False
def test_get_history(self):
"""Test getting chat history"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
response = requests.get(f"{self.base_url}/api/chat/history", headers=headers)
if response.status_code == 200:
self.print_response(response, "Get History")
return True
else:
self.print_error(response, "Get History")
return False
def test_export_data(self):
"""Test data export"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
response = requests.get(f"{self.base_url}/api/export", headers=headers)
if response.status_code == 200:
self.print_response(response, "Export Data")
self.test_data["export"] = response.json().get("data")
return True
else:
self.print_error(response, "Export Data")
return False
def test_clear_history(self):
"""Test clearing chat history"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
response = requests.post(f"{self.base_url}/api/chat/clear", headers=headers)
if response.status_code == 200:
self.print_response(response, "Clear History")
return True
else:
self.print_error(response, "Clear History")
return False
def test_import_data(self):
"""Test data import"""
if not self.token or "export" not in self.test_data:
print("Error: No exported data to import")
return False
headers = {"Authorization": f"Bearer {self.token}"}
data = {"data": self.test_data["export"]}
response = requests.post(f"{self.base_url}/api/import", headers=headers, json=data)
if response.status_code == 200:
self.print_response(response, "Import Data")
return True
else:
self.print_error(response, "Import Data")
return False
def test_metrics(self):
"""Test getting metrics"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
response = requests.get(f"{self.base_url}/api/metrics", headers=headers)
if response.status_code == 200:
self.print_response(response, "Get Metrics")
return True
else:
self.print_error(response, "Get Metrics")
return False
def test_update_settings(self):
"""Test updating settings"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
data = {"use_summary": True}
response = requests.post(f"{self.base_url}/api/settings", headers=headers, json=data)
if response.status_code == 200:
self.print_response(response, "Update Settings")
return True
else:
self.print_error(response, "Update Settings")
return False
def test_clear_all(self):
"""Test clearing all data"""
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
response = requests.post(f"{self.base_url}/api/clear-all", headers=headers)
if response.status_code == 200:
self.print_response(response, "Clear All Data")
return True
else:
self.print_error(response, "Clear All Data")
return False
def test_error_cases(self):
"""Test various error cases"""
results = []
# Test invalid URL
if not self.token:
print("Error: Not logged in")
return False
headers = {"Authorization": f"Bearer {self.token}"}
print("\nTesting error cases...")
# Invalid URL
data = {"url": "not-a-valid-url"}
response = requests.post(f"{self.base_url}/api/process-url", headers=headers, json=data)
self.print_response(response, "Invalid URL Test")
results.append(response.status_code == 400)
# Chat without processing URL
await_clear = requests.post(f"{self.base_url}/api/clear-all", headers=headers)
data = {"message": "This should fail"}
response = requests.post(f"{self.base_url}/api/chat", headers=headers, json=data)
self.print_response(response, "Chat Without URL Test")
results.append(response.status_code == 400)
# Invalid token
bad_headers = {"Authorization": "Bearer invalid-token"}
response = requests.get(f"{self.base_url}/api/chat/history", headers=bad_headers)
self.print_response(response, "Invalid Token Test")
results.append(response.status_code == 401)
return all(results)
def run_all_tests(self):
"""Run all tests in sequence"""
print("Starting CRAWLGPT API Tests...")
tests = [
("Welcome Endpoint", self.test_welcome),
("User Registration", self.test_register),
("User Login", self.test_login),
("Process URL", self.test_process_url),
("Chat", self.test_chat),
("Get History", self.test_get_history),
("Export Data", self.test_export_data),
("Clear History", self.test_clear_history),
("Import Data", self.test_import_data),
("Get Metrics", self.test_metrics),
("Update Settings", self.test_update_settings),
("Error Cases", self.test_error_cases),
("Clear All Data", self.test_clear_all)
]
results = {}
for name, test_func in tests:
print(f"\n{'=' * 80}")
print(f"Running test: {name}")
print(f"{'=' * 80}")
try:
success = test_func()
results[name] = success
if not success:
print(f"β Test '{name}' failed.")
time.sleep(1) # Brief pause between tests
except Exception as e:
print(f"β Test '{name}' threw an exception: {str(e)}")
results[name] = False
time.sleep(1) # Brief pause between tests
# Print summary
print("\n" + "=" * 80)
print("TEST SUMMARY")
print("=" * 80)
for name, success in results.items():
status = "β
PASSED" if success else "β FAILED"
print(f"{status} - {name}")
print("=" * 80)
success_rate = sum(1 for success in results.values() if success) / len(results) * 100
print(f"Overall success rate: {success_rate:.2f}%")
return all(results.values())
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Test the CRAWLGPT API")
parser.add_argument("--url", default="http://127.0.0.1:5000", help="Base URL for the API")
args = parser.parse_args()
tester = CrawlGPTTester(base_url=args.url)
success = tester.run_all_tests()
sys.exit(0 if success else 1) |