Spaces:
Sleeping
Sleeping
File size: 9,251 Bytes
697c967 |
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 |
"""
Detailed API Endpoint Verification Script
This script tests each API endpoint individually to verify functionality.
"""
import requests
import json
from datetime import datetime
# Base URL for the backend API
BASE_URL = "http://localhost:8000"
def test_endpoint(endpoint, method="GET", data=None, headers=None, expected_status=200, description=""):
"""Test a specific endpoint"""
print(f"Testing {description} - {method} {endpoint}")
try:
url = f"{BASE_URL}{endpoint}"
response = requests.request(method, url, json=data, headers=headers)
print(f" Status: {response.status_code} (Expected: {expected_status})")
if response.status_code == expected_status:
print(f" Result: PASS")
if response.content: # Check if there's response content
try:
print(f" Response: {response.json()}")
except:
print(f" Response: {response.text[:100]}...") # First 100 chars
return True
else:
print(f" Result: FAIL - {response.text}")
return False
except Exception as e:
print(f" Result: FAIL - Error: {e}")
return False
def test_all_endpoints():
"""Test all API endpoints"""
print("=" * 70)
print("Detailed API Endpoint Verification")
print("=" * 70)
results = {}
# Test 1: Health check endpoint
results['health'] = test_endpoint("/", "GET", expected_status=200,
description="Health Check")
# Test 2: Documentation endpoint
results['docs'] = test_endpoint("/docs", "GET", expected_status=200,
description="API Documentation")
# Test 3: Register a user first for authentication tests
print("\n--- Setting up user for authentication tests ---")
timestamp = str(int(datetime.now().timestamp()))
user_data = {
"email": f"testuser_{timestamp}@example.com",
"password": "securepassword123",
"name": f"Test User {timestamp}"
}
register_response = requests.post(f"{BASE_URL}/api/auth/register",
json=user_data,
headers={"Content-Type": "application/json"})
if register_response.status_code == 201:
register_result = register_response.json()
token = register_result['token']
user_id = register_result['user']['id']
print(f"User registered successfully. Token: {token[:20]}..., User ID: {user_id}")
else:
print(f"Failed to register user: {register_response.status_code} - {register_response.text}")
token = None
user_id = None
# Test 4: Authentication endpoints
if token:
auth_headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
# Test login with the same credentials
login_data = {"email": user_data["email"], "password": user_data["password"]}
results['auth_login'] = test_endpoint("/api/auth/login", "POST",
data=login_data, expected_status=200,
description="User Login")
# Test logout
results['auth_logout'] = test_endpoint("/api/auth/logout", "POST",
headers={"Authorization": f"Bearer {token}"},
expected_status=200,
description="User Logout")
# Test 5: Task endpoints (need to register another user and get a fresh token)
print("\n--- Setting up user for task tests ---")
timestamp2 = str(int(datetime.now().timestamp()))
task_user_data = {
"email": f"taskuser_{timestamp2}@example.com",
"password": "securepassword123",
"name": f"Task User {timestamp2}"
}
task_register_response = requests.post(f"{BASE_URL}/api/auth/register",
json=task_user_data,
headers={"Content-Type": "application/json"})
if task_register_response.status_code == 201:
task_user_result = task_register_response.json()
task_token = task_user_result['token']
task_user_id = task_user_result['user']['id']
task_headers = {"Authorization": f"Bearer {task_token}", "Content-Type": "application/json"}
print(f"Task user registered. Token: {task_token[:20]}..., User ID: {task_user_id}")
else:
print(f"Failed to register task user: {task_register_response.status_code}")
task_token = None
task_user_id = None
task_headers = None
# Test 6: Task CRUD operations
if task_token and task_user_id and task_headers:
# Create a task
task_data = {
"title": "Test Task for Endpoint Verification",
"description": "This is a test task for endpoint verification",
"completed": False
}
# Create task
create_task_response = requests.post(f"{BASE_URL}/api/{task_user_id}/tasks",
json=task_data, headers=task_headers)
if create_task_response.status_code == 201:
created_task = create_task_response.json()
task_id = created_task['id']
print(f"Task created with ID: {task_id}")
# Test get all tasks
results['get_all_tasks'] = test_endpoint(f"/api/{task_user_id}/tasks", "GET",
headers=task_headers, expected_status=200,
description="Get All Tasks")
# Test get specific task
results['get_task'] = test_endpoint(f"/api/{task_user_id}/tasks/{task_id}", "GET",
headers=task_headers, expected_status=200,
description="Get Specific Task")
# Test update task
update_data = {
"title": "Updated Test Task",
"description": "This is an updated test task",
"completed": True
}
results['update_task'] = test_endpoint(f"/api/{task_user_id}/tasks/{task_id}", "PUT",
data=update_data, headers=task_headers, expected_status=200,
description="Update Task")
# Test update task completion
completion_data = {"completed": False}
results['update_completion'] = test_endpoint(f"/api/{task_user_id}/tasks/{task_id}/complete", "PATCH",
data=completion_data, headers=task_headers, expected_status=200,
description="Update Task Completion")
# Test delete task
results['delete_task'] = test_endpoint(f"/api/{task_user_id}/tasks/{task_id}", "DELETE",
headers=task_headers, expected_status=204,
description="Delete Task")
else:
print(f"Failed to create task: {create_task_response.status_code} - {create_task_response.text}")
# Mark all task tests as failed
results['get_all_tasks'] = False
results['get_task'] = False
results['update_task'] = False
results['update_completion'] = False
results['delete_task'] = False
else:
print("Skipping task tests - no valid user/token")
# Mark all task tests as failed
results['get_all_tasks'] = False
results['get_task'] = False
results['update_task'] = False
results['update_completion'] = False
results['delete_task'] = False
# Test 7: Test protected endpoint without authentication (should fail)
results['unauthorized_access'] = test_endpoint(f"/api/{task_user_id if task_user_id else 1}/tasks", "GET",
expected_status=401, # Should be 401 Unauthorized
description="Unauthorized Access (should fail)")
# Test 8: Test with invalid token (should fail)
invalid_headers = {"Authorization": "Bearer invalid_token_here", "Content-Type": "application/json"}
results['invalid_token'] = test_endpoint(f"/api/{task_user_id if task_user_id else 1}/tasks", "GET",
headers=invalid_headers, expected_status=401,
description="Invalid Token (should fail)")
print("\n" + "=" * 70)
print("Endpoint Verification Results:")
print("=" * 70)
all_passed = True
for endpoint, result in results.items():
status = "PASS" if result else "FAIL"
print(f"{endpoint:25}: {status}")
if not result:
all_passed = False
print("=" * 70)
print(f"Overall Result: {'ALL ENDPOINTS WORKING' if all_passed else 'SOME ENDPOINTS FAILED'}")
print("=" * 70)
return all_passed
if __name__ == "__main__":
test_all_endpoints() |