Files
MincedPad/backend/tests/test_items.py
T
2026-07-14 12:43:53 +08:00

71 lines
2.4 KiB
Python

from datetime import datetime, timedelta
def test_create_text_default_public_and_24h(client):
r = client.post("/api/items", json={"body": "hello **md**"})
assert r.status_code == 200
data = r.json()
assert data["slug"]
assert data["is_public"] is True
assert data["expires_at"] is not None
assert data["kind"] == "text"
assert data["title"] == "hello **md**"
assert data["body"] == "hello **md**"
assert data["burn_after_read"] is False
assert data["view_count"] == 0
assert data["created_at"]
def test_link_only_not_on_wall(client):
r = client.post("/api/items", json={"body": "secret", "is_public": False})
slug = r.json()["slug"]
wall = client.get("/api/items/wall").json()["items"]
assert all(i["slug"] != slug for i in wall)
assert client.get(f"/api/items/{slug}").status_code == 200
def test_ttl_options(client):
never = client.post("/api/items", json={"body": "n", "ttl": "never"}).json()
assert never["expires_at"] is None
one_h = client.post("/api/items", json={"body": "h", "ttl": "1h"}).json()
expires = datetime.fromisoformat(one_h["expires_at"])
delta = expires - datetime.fromisoformat(one_h["created_at"])
assert timedelta(minutes=50) < delta < timedelta(hours=2)
week = client.post("/api/items", json={"body": "w", "ttl": "7d"}).json()
expires_w = datetime.fromisoformat(week["expires_at"])
delta_w = expires_w - datetime.fromisoformat(week["created_at"])
assert timedelta(days=6) < delta_w < timedelta(days=8)
def test_wall_pagination_shape(client):
for i in range(3):
client.post("/api/items", json={"body": f"item {i}", "is_public": True})
r = client.get("/api/items/wall", params={"page": 1, "page_size": 2})
assert r.status_code == 200
data = r.json()
assert "items" in data
assert data["page"] == 1
assert data["page_size"] == 2
assert data["total"] >= 3
assert len(data["items"]) == 2
# newest first
titles = [i["title"] for i in data["items"]]
assert titles[0] == "item 2"
def test_get_missing_404(client):
assert client.get("/api/items/does-not-exist").status_code == 404
def test_title_from_first_line(client):
r = client.post("/api/items", json={"body": "First line\nSecond line"})
assert r.json()["title"] == "First line"
def test_explicit_title(client):
r = client.post("/api/items", json={"body": "body text", "title": "Custom"})
assert r.json()["title"] == "Custom"