"""Integration tests for todo endpoints.""" import pytest from fastapi.testclient import TestClient from src.models.user import User @pytest.fixture def auth_headers(client: TestClient, auth_token_factory, test_user: User): """Create auth headers for testing.""" token = auth_token_factory(test_user.id) return {"Authorization": f"Bearer {token}"} def test_create_todo_success(client: TestClient, auth_headers: dict): """Test creating a todo.""" response = client.post( "/todos", json={"title": "New Todo", "description": "Test description"}, headers=auth_headers, ) assert response.status_code == 201 data = response.json() assert data["success"] is True assert data["data"]["todo"]["title"] == "New Todo" assert data["data"]["todo"]["completed"] is False def test_create_todo_without_auth(client: TestClient): """Test creating todo without auth fails.""" response = client.post( "/todos", json={"title": "Unauthorized Todo"}, ) assert response.status_code == 403 def test_create_todo_missing_title(client: TestClient, auth_headers: dict): """Test creating todo without title fails.""" response = client.post( "/todos", json={"description": "No title"}, headers=auth_headers, ) assert response.status_code == 422 def test_list_todos(client: TestClient, auth_headers: dict): """Test listing todos.""" # Create some todos client.post( "/todos", json={"title": "Todo 1"}, headers=auth_headers, ) client.post( "/todos", json={"title": "Todo 2"}, headers=auth_headers, ) # List todos response = client.get("/todos", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["success"] is True assert len(data["data"]["todos"]) == 2 assert "pagination" in data["data"] def test_list_todos_with_status_filter(client: TestClient, auth_headers: dict): """Test listing todos with status filter.""" # Create and complete a todo create_response = client.post( "/todos", json={"title": "Completed Todo"}, headers=auth_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] client.patch(f"/todos/{todo_id}/toggle", headers=auth_headers) # Create a pending todo client.post( "/todos", json={"title": "Pending Todo"}, headers=auth_headers, ) # Filter for completed response = client.get("/todos?status=completed", headers=auth_headers) data = response.json() assert len(data["data"]["todos"]) == 1 assert data["data"]["todos"][0]["completed"] is True # Filter for pending response = client.get("/todos?status=pending", headers=auth_headers) data = response.json() assert len(data["data"]["todos"]) == 1 assert data["data"]["todos"][0]["completed"] is False def test_get_todo(client: TestClient, auth_headers: dict): """Test getting a specific todo.""" # Create a todo create_response = client.post( "/todos", json={"title": "Get Me"}, headers=auth_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] # Get the todo response = client.get(f"/todos/{todo_id}", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["data"]["todo"]["id"] == todo_id assert data["data"]["todo"]["title"] == "Get Me" def test_get_nonexistent_todo(client: TestClient, auth_headers: dict): """Test getting non-existent todo returns 404.""" response = client.get("/todos/99999", headers=auth_headers) assert response.status_code == 404 def test_update_todo(client: TestClient, auth_headers: dict): """Test updating a todo.""" # Create a todo create_response = client.post( "/todos", json={"title": "Original"}, headers=auth_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] # Update it response = client.put( f"/todos/{todo_id}", json={"title": "Updated", "description": "New description"}, headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["data"]["todo"]["title"] == "Updated" assert data["data"]["todo"]["description"] == "New description" def test_delete_todo(client: TestClient, auth_headers: dict): """Test deleting a todo.""" # Create a todo create_response = client.post( "/todos", json={"title": "Delete Me"}, headers=auth_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] # Delete it response = client.delete(f"/todos/{todo_id}", headers=auth_headers) assert response.status_code == 200 assert response.json()["success"] is True # Verify it's deleted get_response = client.get(f"/todos/{todo_id}", headers=auth_headers) assert get_response.status_code == 404 def test_toggle_todo(client: TestClient, auth_headers: dict): """Test toggling todo completion status.""" # Create a todo create_response = client.post( "/todos", json={"title": "Toggle Me"}, headers=auth_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] # Toggle to completed response = client.patch(f"/todos/{todo_id}/toggle", headers=auth_headers) assert response.status_code == 200 assert response.json()["data"]["todo"]["completed"] is True # Toggle back to pending response = client.patch(f"/todos/{todo_id}/toggle", headers=auth_headers) assert response.status_code == 200 assert response.json()["data"]["todo"]["completed"] is False def test_cross_user_isolation(client: TestClient, auth_token_factory): """Test that users cannot access each other's todos.""" # Create two users and their todos user1_token = auth_token_factory(1) user2_token = auth_token_factory(2) user1_headers = {"Authorization": f"Bearer {user1_token}"} user2_headers = {"Authorization": f"Bearer {user2_token}"} # User 1 creates a todo create_response = client.post( "/todos", json={"title": "User 1 Todo"}, headers=user1_headers, ) todo_id = create_response.json()["data"]["todo"]["id"] # User 2 tries to access User 1's todo response = client.get(f"/todos/{todo_id}", headers=user2_headers) # Should return 404 (not found) for security - don't reveal existence assert response.status_code == 404