PYTHON
test_api.py🐍python
"""API tests for Task Management API."""
import pytest
class TestHealth:
"""Test health endpoints."""
def test_root(self, client):
response = client.get("/")
assert response.status_code == 200
assert "Task Management API" in response.json()["message"]
def test_health(self, client):
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
class TestUsers:
"""Test user endpoints."""
def test_register_user(self, client):
response = client.post("/api/users/register", json={
"email": "new@example.com",
"username": "newuser",
"password": "password123"
})
assert response.status_code == 201
assert response.json()["username"] == "newuser"
def test_register_duplicate_email(self, client):
# First registration
client.post("/api/users/register", json={
"email": "dup@example.com",
"username": "user1",
"password": "password123"
})
# Duplicate email
response = client.post("/api/users/register", json={
"email": "dup@example.com",
"username": "user2",
"password": "password123"
})
assert response.status_code == 400
def test_login(self, client):
# Register
client.post("/api/users/register", json={
"email": "login@example.com",
"username": "loginuser",
"password": "password123"
})
# Login
response = client.post("/api/users/login", data={
"username": "loginuser",
"password": "password123"
})
assert response.status_code == 200
assert "access_token" in response.json()
def test_get_current_user(self, client, auth_headers):
response = client.get("/api/users/me", headers=auth_headers)
assert response.status_code == 200
assert response.json()["username"] == "testuser"
class TestTasks:
"""Test task endpoints."""
def test_create_task(self, client, auth_headers):
response = client.post("/api/tasks/", headers=auth_headers, json={
"title": "Test Task",
"description": "A test task",
"priority": "high"
})
assert response.status_code == 201
assert response.json()["title"] == "Test Task"
def test_get_tasks(self, client, auth_headers):
# Create a task first
client.post("/api/tasks/", headers=auth_headers, json={
"title": "Task 1"
})
response = client.get("/api/tasks/", headers=auth_headers)
assert response.status_code == 200
assert len(response.json()) >= 1
def test_get_task_by_id(self, client, auth_headers):
# Create task
create_response = client.post("/api/tasks/", headers=auth_headers, json={
"title": "Specific Task"
})
task_id = create_response.json()["id"]
# Get task
response = client.get(f"/api/tasks/{task_id}", headers=auth_headers)
assert response.status_code == 200
assert response.json()["title"] == "Specific Task"
def test_update_task(self, client, auth_headers):
# Create task
create_response = client.post("/api/tasks/", headers=auth_headers, json={
"title": "Original Title"
})
task_id = create_response.json()["id"]
# Update task
response = client.put(f"/api/tasks/{task_id}", headers=auth_headers, json={
"title": "Updated Title"
})
assert response.status_code == 200
assert response.json()["title"] == "Updated Title"
def test_delete_task(self, client, auth_headers):
# Create task
create_response = client.post("/api/tasks/", headers=auth_headers, json={
"title": "To Delete"
})
task_id = create_response.json()["id"]
# Delete task
response = client.delete(f"/api/tasks/{task_id}", headers=auth_headers)
assert response.status_code == 204
# Verify deleted
get_response = client.get(f"/api/tasks/{task_id}", headers=auth_headers)
assert get_response.status_code == 404
def test_mark_task_complete(self, client, auth_headers):
# Create task
create_response = client.post("/api/tasks/", headers=auth_headers, json={
"title": "Incomplete Task"
})
task_id = create_response.json()["id"]
# Mark complete
response = client.patch(f"/api/tasks/{task_id}/complete", headers=auth_headers)
assert response.status_code == 200
assert response.json()["completed"] is True
def test_unauthorized_access(self, client):
response = client.get("/api/tasks/")
assert response.status_code == 401