# tests/integration/test_posts.py
import pytest
from httpx import AsyncClient, ASGITransport
from sqlalchemy.ext.asyncio import AsyncSession
from src.snackbase.infrastructure.api.app import app
from src.snackbase.infrastructure.persistence.models.post import Post
from src.snackbase.core.id_generator import generate_id
@pytest.mark.asyncio
class TestPostsAPI:
"""Test posts API endpoints."""
async def test_create_post_success(
self,
client: AsyncClient,
superadmin_token: str
):
"""Creating a post with valid data should return 201."""
response = await client.post(
"/api/v1/posts",
headers={"Authorization": f"Bearer {superadmin_token}"},
json={
"title": "Test Post",
"content": "This is test content",
"status": "draft"
}
)
assert response.status_code == 201
data = response.json()
assert data["title"] == "Test Post"
assert data["content"] == "This is test content"
assert data["status"] == "draft"
assert "id" in data
assert "created_at" in data
async def test_create_post_requires_auth(
self,
client: AsyncClient
):
"""Creating a post without auth should return 401."""
response = await client.post(
"/api/v1/posts",
json={"title": "Test Post"}
)
assert response.status_code == 401
async def test_create_post_validates_required_fields(
self,
client: AsyncClient,
superadmin_token: str
):
"""Creating a post without required fields should return 422."""
response = await client.post(
"/api/v1/posts",
headers={"Authorization": f"Bearer {superadmin_token}"},
json={} # Missing required fields
)
assert response.status_code == 422
data = response.json()
assert "detail" in data