import pytest from fastapi.testclient import TestClient from main import app from database import get_session, engine from sqlmodel import Session, SQLModel from unittest.mock import patch import os # Create a test client client = TestClient(app) # For testing, use in-memory SQLite database @pytest.fixture(scope="module") def test_client(): # Create test database SQLModel.metadata.create_all(engine) with TestClient(app) as client: yield client # Test basic health endpoints def test_read_root(test_client): response = test_client.get("/") assert response.status_code == 200 assert "message" in response.json() def test_health_check(test_client): response = test_client.get("/health") assert response.status_code == 200 assert response.json()["status"] == "healthy" # Mock JWT token for testing authenticated endpoints MOCK_JWT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwidXNlcl9pZCI6InRlc3RAZXhhbXBsZS5jb20iLCJuYW1lIjoiVGVzdCBVc2VyIiwiZW1haWwiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" # Test task endpoints with mocked authentication def test_create_task(test_client): with patch("auth.jwt.get_current_user_id") as mock_get_user: mock_get_user.return_value = "test@example.com" response = test_client.post( "/api/tasks", headers={"Authorization": f"Bearer {MOCK_JWT_TOKEN}"}, json={"title": "Test task", "description": "Test description"} ) # Should return 401 or 200 depending on whether the token verification is mocked properly assert response.status_code in [200, 401, 422] # 422 for validation errors def test_get_tasks(test_client): with patch("auth.jwt.get_current_user_id") as mock_get_user: mock_get_user.return_value = "test@example.com" response = test_client.get( "/api/tasks", headers={"Authorization": f"Bearer {MOCK_JWT_TOKEN}"} ) assert response.status_code in [200, 401] def test_update_task(test_client): with patch("auth.jwt.get_current_user_id") as mock_get_user: mock_get_user.return_value = "test@example.com" response = test_client.put( "/api/tasks/1", headers={"Authorization": f"Bearer {MOCK_JWT_TOKEN}"}, json={"title": "Updated task"} ) assert response.status_code in [200, 401, 404, 422] def test_delete_task(test_client): with patch("auth.jwt.get_current_user_id") as mock_get_user: mock_get_user.return_value = "test@example.com" response = test_client.delete( "/api/tasks/1", headers={"Authorization": f"Bearer {MOCK_JWT_TOKEN}"} ) assert response.status_code in [200, 401, 404] def test_toggle_task_completion(test_client): with patch("auth.jwt.get_current_user_id") as mock_get_user: mock_get_user.return_value = "test@example.com" response = test_client.patch( "/api/tasks/1/complete", headers={"Authorization": f"Bearer {MOCK_JWT_TOKEN}"} ) assert response.status_code in [200, 401, 404]