2025-05-30 17:39:08 +00:00

171 lines
4.9 KiB
Python

"""
Test script for the Task Manager API
"""
import os
import unittest
from fastapi.testclient import TestClient
from main import app
from app.db.database import Base, engine
# Create test client
client = TestClient(app)
class TestAPI(unittest.TestCase):
"""
Test class for API endpoints
"""
def setUp(self):
# Create tables
Base.metadata.create_all(bind=engine)
def tearDown(self):
# Drop tables
Base.metadata.drop_all(bind=engine)
def test_health_endpoint(self):
"""
Test the health endpoint
"""
response = client.get("/health")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"status": "healthy"})
def test_user_registration(self):
"""
Test user registration
"""
user_data = {
"email": "test@example.com",
"username": "testuser",
"password": "Password123"
}
response = client.post(
"/api/v1/auth/register",
json=user_data
)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data["email"], user_data["email"])
self.assertEqual(data["username"], user_data["username"])
self.assertTrue(data["is_active"])
self.assertFalse(data["is_superuser"])
def test_login(self):
"""
Test user login
"""
# First register a user
user_data = {
"email": "test@example.com",
"username": "testuser",
"password": "Password123"
}
client.post(
"/api/v1/auth/register",
json=user_data
)
# Then try to login
login_data = {
"username": user_data["email"],
"password": user_data["password"]
}
response = client.post(
"/api/v1/auth/login",
data=login_data
)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn("access_token", data)
self.assertEqual(data["token_type"], "bearer")
def test_task_crud(self):
"""
Test task CRUD operations
"""
# Register a user
user_data = {
"email": "test@example.com",
"username": "testuser",
"password": "Password123"
}
client.post(
"/api/v1/auth/register",
json=user_data
)
# Login to get token
login_data = {
"username": user_data["email"],
"password": user_data["password"]
}
login_response = client.post(
"/api/v1/auth/login",
data=login_data
)
token = login_response.json()["access_token"]
headers = {"Authorization": f"Bearer {token}"}
# Create a task
task_data = {
"title": "Test Task",
"description": "Test Description",
"priority": "high"
}
response = client.post(
"/api/v1/tasks",
json=task_data,
headers=headers
)
self.assertEqual(response.status_code, 200)
created_task = response.json()
self.assertEqual(created_task["title"], task_data["title"])
self.assertEqual(created_task["description"], task_data["description"])
self.assertEqual(created_task["priority"], task_data["priority"])
task_id = created_task["id"]
# Read the task
response = client.get(
f"/api/v1/tasks/{task_id}",
headers=headers
)
self.assertEqual(response.status_code, 200)
read_task = response.json()
self.assertEqual(read_task["id"], task_id)
self.assertEqual(read_task["title"], task_data["title"])
# Update the task
update_data = {
"title": "Updated Task",
"status": "in_progress"
}
response = client.put(
f"/api/v1/tasks/{task_id}",
json=update_data,
headers=headers
)
self.assertEqual(response.status_code, 200)
updated_task = response.json()
self.assertEqual(updated_task["title"], update_data["title"])
self.assertEqual(updated_task["status"], update_data["status"])
# Delete the task
response = client.delete(
f"/api/v1/tasks/{task_id}",
headers=headers
)
self.assertEqual(response.status_code, 200)
deleted_task = response.json()
self.assertEqual(deleted_task["id"], task_id)
self.assertTrue(deleted_task["is_deleted"])
# Try to get the deleted task
response = client.get(
f"/api/v1/tasks/{task_id}",
headers=headers
)
self.assertEqual(response.status_code, 404)
if __name__ == "__main__":
unittest.main()