from fastapi.testclient import TestClient
def test_health_check(client): """Test health endpoint.""" response = client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "healthy"}
def test_read_root(client): """Test root endpoint.""" response = client.get("/") assert response.status_code == 200
def test_create_user(client): """Test user registration.""" response = client.post("/users/", json={ "email": "newuser@example.com", "password": "newpass123" }) assert response.status_code == 201 data = response.json() assert data["email"] == "newuser@example.com" assert "id" in data assert "password" not in data
def test_create_user_duplicate_email(client, test_user): """Test duplicate email rejection.""" response = client.post("/users/", json={ "email": "test@example.com", "password": "newpass123" }) assert response.status_code == 400 assert "already registered" in response.json()["detail"]
def test_login_success(client, test_user): """Test successful login.""" response = client.post("/auth/token", data={ "username": "test@example.com", "password": "testpass123" }) assert response.status_code == 200 assert "access_token" in response.json()
def test_login_wrong_password(client, test_user): """Test login with wrong password.""" response = client.post("/auth/token", data={ "username": "test@example.com", "password": "wrongpassword" }) assert response.status_code == 401
class TestArticleAPI: """Test article CRUD operations."""
def test_list_articles(self, client): response = client.get("/articles/") assert response.status_code == 200 assert isinstance(response.json(), list)
def test_create_article_unauthenticated(self, client): response = client.post("/articles/", json={ "title": "Test", "content": "Content" }) assert response.status_code == 401
def test_create_article_authenticated(self, client, auth_headers): response = client.post( "/articles/", json={"title": "Test Article", "content": "Content"}, headers=auth_headers ) assert response.status_code == 201 data = response.json() assert data["title"] == "Test Article"
def test_get_article(self, client, auth_headers): create_response = client.post( "/articles/", json={"title": "Test", "content": "Content"}, headers=auth_headers ) article_id = create_response.json()["id"]
response = client.get(f"/articles/{article_id}") assert response.status_code == 200 assert response.json()["id"] == article_id
def test_get_article_not_found(self, client): response = client.get("/articles/99999") assert response.status_code == 404
def test_update_article(self, client, auth_headers): create_response = client.post( "/articles/", json={"title": "Original", "content": "Content"}, headers=auth_headers ) article_id = create_response.json()["id"]
response = client.patch( f"/articles/{article_id}", json={"title": "Updated Title"}, headers=auth_headers ) assert response.status_code == 200 assert response.json()["title"] == "Updated Title"
def test_delete_article(self, client, auth_headers): create_response = client.post( "/articles/", json={"title": "To Delete", "content": "Content"}, headers=auth_headers ) article_id = create_response.json()["id"]
response = client.delete( f"/articles/{article_id}", headers=auth_headers ) assert response.status_code == 204
get_response = client.get(f"/articles/{article_id}") assert get_response.status_code == 404
|