Files
FabledCurator/tests/test_api_posts.py
T
2026-05-21 18:55:18 -04:00

120 lines
3.4 KiB
Python

"""FC-3e: /api/posts API tests.
Validates list shape, cursor handling, filter validation, and detail
endpoint. Service-level fixtures are exercised by test_post_feed_service
— here we focus on the HTTP surface (validation, status codes, dict shape).
"""
from datetime import UTC, datetime
import pytest
from backend.app import create_app
from backend.app.models import Artist, Post, Source
pytestmark = pytest.mark.integration
@pytest.fixture
async def app():
return create_app()
@pytest.fixture
async def client(app):
async with app.test_client() as c:
yield c
@pytest.fixture
async def seeded_post(db):
artist = Artist(name="alice-api", slug="alice-api")
db.add(artist)
await db.flush()
source = Source(
artist_id=artist.id, platform="patreon",
url="https://p/alice-api", enabled=True,
)
db.add(source)
await db.flush()
post = Post(
source_id=source.id, external_post_id="API1",
post_title="Hello", post_url="https://p/alice-api/1",
post_date=datetime.now(UTC),
description="<p>hi</p>",
)
db.add(post)
await db.commit()
return artist, source, post
@pytest.mark.asyncio
async def test_list_returns_items_and_cursor_keys(client, seeded_post):
resp = await client.get("/api/posts")
assert resp.status_code == 200
body = await resp.get_json()
assert set(body.keys()) == {"items", "next_cursor"}
assert isinstance(body["items"], list)
assert body["items"][0]["post_title"] == "Hello"
assert body["items"][0]["description_plain"] == "hi"
@pytest.mark.asyncio
async def test_list_rejects_malformed_cursor(client):
resp = await client.get("/api/posts?cursor=garbage!!!")
assert resp.status_code == 400
body = await resp.get_json()
assert body["error"] == "invalid_cursor"
@pytest.mark.asyncio
async def test_list_rejects_unknown_platform(client):
resp = await client.get("/api/posts?platform=myspace")
assert resp.status_code == 400
body = await resp.get_json()
assert body["error"] == "unknown_platform"
@pytest.mark.asyncio
async def test_list_rejects_non_int_artist_id(client):
resp = await client.get("/api/posts?artist_id=notanint")
assert resp.status_code == 400
body = await resp.get_json()
assert body["error"] == "invalid_artist_id"
@pytest.mark.asyncio
async def test_list_rejects_limit_out_of_range(client):
resp = await client.get("/api/posts?limit=0")
assert resp.status_code == 400
resp = await client.get("/api/posts?limit=500")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_list_filter_propagates_artist(client, seeded_post):
artist, _, post = seeded_post
resp = await client.get(f"/api/posts?artist_id={artist.id}")
assert resp.status_code == 200
body = await resp.get_json()
assert len(body["items"]) == 1
assert body["items"][0]["id"] == post.id
@pytest.mark.asyncio
async def test_detail_200_for_known(client, seeded_post):
_, _, post = seeded_post
resp = await client.get(f"/api/posts/{post.id}")
assert resp.status_code == 200
body = await resp.get_json()
assert body["id"] == post.id
assert "description_full" in body
assert body["description_full"] == "hi"
@pytest.mark.asyncio
async def test_detail_404_for_unknown(client):
resp = await client.get("/api/posts/999999")
assert resp.status_code == 404
body = await resp.get_json()
assert body["error"] == "not_found"