Spaces:
Sleeping
Sleeping
File size: 3,207 Bytes
4c94294 | 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 | 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] |