Spaces:
Sleeping
Sleeping
File size: 7,633 Bytes
5be081e | 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 | """
Simple Test Script for Todo API Endpoints
This script tests the basic functionality of the Todo API without authentication.
For full integration tests with JWT, see tests/ directory.
Usage:
python test_api.py
Requirements:
- API server must be running on http://localhost:8000
- httpx library must be installed
"""
import asyncio
import sys
from datetime import datetime, timedelta
from jose import jwt
import httpx
# Test configuration
API_BASE_URL = "http://localhost:8000"
TEST_USER_ID = "test_user_123"
BETTER_AUTH_SECRET = "test-secret-for-development" # Replace with actual secret
def create_test_token(user_id: str, secret: str) -> str:
"""
Create a test JWT token for authentication.
Args:
user_id: User ID to include in token
secret: BETTER_AUTH_SECRET for signing
Returns:
JWT token string
"""
payload = {
"sub": user_id,
"email": f"{user_id}@example.com",
"iat": datetime.utcnow(),
"exp": datetime.utcnow() + timedelta(days=1),
"iss": "todoweb",
"aud": "todoweb-api"
}
return jwt.encode(payload, secret, algorithm="HS256")
async def test_health_check():
"""Test the health check endpoint (no auth required)."""
print("\n[TEST] Health Check")
async with httpx.AsyncClient() as client:
response = await client.get(f"{API_BASE_URL}/health")
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "Health check failed"
print("β Health check passed")
async def test_root_endpoint():
"""Test the root endpoint (no auth required)."""
print("\n[TEST] Root Endpoint")
async with httpx.AsyncClient() as client:
response = await client.get(f"{API_BASE_URL}/")
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "Root endpoint failed"
print("β Root endpoint passed")
async def test_create_todo(token: str):
"""Test creating a todo."""
print("\n[TEST] Create Todo")
async with httpx.AsyncClient() as client:
response = await client.post(
f"{API_BASE_URL}/api/todos",
json={
"title": "Test Todo",
"description": "This is a test todo",
"is_completed": False
},
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 201, "Create todo failed"
todo = response.json()
print(f"β Todo created with ID: {todo['id']}")
return todo["id"]
async def test_list_todos(token: str):
"""Test listing todos."""
print("\n[TEST] List Todos")
async with httpx.AsyncClient() as client:
response = await client.get(
f"{API_BASE_URL}/api/todos",
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "List todos failed"
todos = response.json()
print(f"β Retrieved {len(todos)} todos")
return todos
async def test_get_todo(token: str, todo_id: str):
"""Test getting a specific todo."""
print(f"\n[TEST] Get Todo {todo_id}")
async with httpx.AsyncClient() as client:
response = await client.get(
f"{API_BASE_URL}/api/todos/{todo_id}",
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "Get todo failed"
print("β Todo retrieved successfully")
async def test_update_todo(token: str, todo_id: str):
"""Test updating a todo."""
print(f"\n[TEST] Update Todo {todo_id}")
async with httpx.AsyncClient() as client:
response = await client.put(
f"{API_BASE_URL}/api/todos/{todo_id}",
json={
"title": "Updated Test Todo",
"description": "This todo has been updated"
},
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "Update todo failed"
print("β Todo updated successfully")
async def test_toggle_completion(token: str, todo_id: str):
"""Test toggling todo completion."""
print(f"\n[TEST] Toggle Todo Completion {todo_id}")
async with httpx.AsyncClient() as client:
response = await client.patch(
f"{API_BASE_URL}/api/todos/{todo_id}/complete",
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
assert response.status_code == 200, "Toggle completion failed"
todo = response.json()
print(f"β Todo completion toggled to: {todo['is_completed']}")
async def test_delete_todo(token: str, todo_id: str):
"""Test deleting a todo."""
print(f"\n[TEST] Delete Todo {todo_id}")
async with httpx.AsyncClient() as client:
response = await client.delete(
f"{API_BASE_URL}/api/todos/{todo_id}",
headers={"Authorization": f"Bearer {token}"}
)
print(f"Status: {response.status_code}")
assert response.status_code == 204, "Delete todo failed"
print("β Todo deleted successfully")
async def test_unauthorized_access():
"""Test that endpoints reject requests without authentication."""
print("\n[TEST] Unauthorized Access")
async with httpx.AsyncClient() as client:
response = await client.get(f"{API_BASE_URL}/api/todos")
print(f"Status: {response.status_code}")
assert response.status_code == 403, "Should reject unauthorized requests"
print("β Unauthorized access correctly rejected")
async def main():
"""Run all tests."""
print("=" * 60)
print("Todo API Test Suite")
print("=" * 60)
print(f"API URL: {API_BASE_URL}")
print(f"Test User: {TEST_USER_ID}")
print("=" * 60)
try:
# Test public endpoints
await test_health_check()
await test_root_endpoint()
# Test unauthorized access
await test_unauthorized_access()
# Create test token
print("\n[SETUP] Creating test JWT token...")
token = create_test_token(TEST_USER_ID, BETTER_AUTH_SECRET)
print(f"Token created for user: {TEST_USER_ID}")
# Test authenticated endpoints
todo_id = await test_create_todo(token)
await test_list_todos(token)
await test_get_todo(token, todo_id)
await test_update_todo(token, todo_id)
await test_toggle_completion(token, todo_id)
await test_delete_todo(token, todo_id)
print("\n" + "=" * 60)
print("β All tests passed!")
print("=" * 60)
except AssertionError as e:
print(f"\nβ Test failed: {e}")
sys.exit(1)
except httpx.ConnectError:
print("\nβ Cannot connect to API server")
print(f"Make sure the server is running at {API_BASE_URL}")
sys.exit(1)
except Exception as e:
print(f"\nβ Unexpected error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())
|