feat: clips-api mit konflikterkennung
- GET/POST /api/clips, GET/PATCH/DELETE /api/clips/{id}
- angeheftete zuerst, dann zuletzt geändert; Seitenweise mit limit/offset
- PATCH mit version liefert 409 samt aktuellem Stand bei paralleler Änderung
- Inhalt bis CLIP_MAX_CHARS (Standard 200000), Sprache aus fester Liste
- fremde Clips sind nicht von fehlenden unterscheidbar (404), Tests dafür
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f1bfa27ba3
commit
4caab7e657
6 files changed
+478
-1
No files matched your search
@@ -0,0 +1,234 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.config import get_settings
|
||||
from app.models import Clip, Role, utcnow
|
||||
from tests.conftest import login
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def anna(client, make_user):
|
||||
make_user("anna")
|
||||
login(client, "anna")
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bert(client, make_user):
|
||||
"""Second logged-in user with own client, for data separation tests."""
|
||||
make_user("bert")
|
||||
with TestClient(client.app, base_url="https://testserver") as other:
|
||||
login(other, "bert")
|
||||
yield other
|
||||
|
||||
|
||||
def create(c, **fields):
|
||||
response = c.post("/api/clips", json={"content": "hello", **fields})
|
||||
assert response.status_code == 201, response.text
|
||||
return response.json()
|
||||
|
||||
|
||||
# --- CRUD -----------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_create_text_clip(anna):
|
||||
clip = create(anna, content="Some text", title=" Note ")
|
||||
|
||||
assert clip["kind"] == "text"
|
||||
assert clip["title"] == "Note"
|
||||
assert clip["language"] is None
|
||||
assert clip["pinned"] is False
|
||||
assert clip["version"] == 1
|
||||
|
||||
|
||||
def test_create_code_clip_defaults_to_plaintext(anna):
|
||||
assert create(anna, kind="code")["language"] == "plaintext"
|
||||
assert create(anna, kind="code", language="python")["language"] == "python"
|
||||
|
||||
|
||||
def test_text_clip_drops_language(anna):
|
||||
assert create(anna, kind="text", language="python")["language"] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fields",
|
||||
[
|
||||
{"content": " "},
|
||||
{"content": "x", "kind": "code", "language": "brainfuck"},
|
||||
{"content": "x", "title": "t" * 201},
|
||||
{"content": "x", "kind": "image"},
|
||||
],
|
||||
)
|
||||
def test_create_validation(anna, fields):
|
||||
assert anna.post("/api/clips", json=fields).status_code in (400, 422)
|
||||
|
||||
|
||||
def test_content_limit_is_configurable(anna, monkeypatch):
|
||||
monkeypatch.setenv("CLIP_MAX_CHARS", "10")
|
||||
get_settings.cache_clear()
|
||||
|
||||
assert anna.post("/api/clips", json={"content": "x" * 10}).status_code == 201
|
||||
response = anna.post("/api/clips", json={"content": "x" * 11})
|
||||
assert response.status_code == 400
|
||||
assert "zu lang" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_list_orders_pinned_first_then_recent(anna, db):
|
||||
old = create(anna, content="old")
|
||||
pinned = create(anna, content="pinned", pinned=True)
|
||||
new = create(anna, content="new")
|
||||
db.query(Clip).filter_by(id=old["id"]).update({Clip.updated_at: utcnow() - timedelta(days=1)})
|
||||
db.commit()
|
||||
|
||||
data = anna.get("/api/clips").json()
|
||||
|
||||
assert [c["id"] for c in data["items"]] == [pinned["id"], new["id"], old["id"]]
|
||||
assert data["total"] == 3
|
||||
|
||||
|
||||
def test_list_pagination(anna):
|
||||
for i in range(5):
|
||||
create(anna, content=f"clip {i}")
|
||||
|
||||
page = anna.get("/api/clips", params={"limit": 2, "offset": 2}).json()
|
||||
|
||||
assert len(page["items"]) == 2
|
||||
assert page["total"] == 5
|
||||
assert anna.get("/api/clips", params={"limit": 101}).status_code == 422
|
||||
|
||||
|
||||
def test_get_update_delete(anna):
|
||||
clip = create(anna)
|
||||
|
||||
assert anna.get(f"/api/clips/{clip['id']}").json()["content"] == "hello"
|
||||
|
||||
updated = anna.patch(
|
||||
f"/api/clips/{clip['id']}",
|
||||
json={"content": "print(1)", "kind": "code", "language": "python"},
|
||||
).json()
|
||||
assert updated["content"] == "print(1)"
|
||||
assert updated["language"] == "python"
|
||||
assert updated["version"] == 2
|
||||
|
||||
assert anna.delete(f"/api/clips/{clip['id']}").status_code == 204
|
||||
assert anna.get(f"/api/clips/{clip['id']}").status_code == 404
|
||||
|
||||
|
||||
def test_patch_only_changes_given_fields(anna):
|
||||
clip = create(anna, title="Keep", kind="code", language="go")
|
||||
|
||||
updated = anna.patch(f"/api/clips/{clip['id']}", json={"content": "new"}).json()
|
||||
|
||||
assert updated["title"] == "Keep"
|
||||
assert updated["language"] == "go"
|
||||
|
||||
|
||||
def test_patch_can_clear_title(anna):
|
||||
clip = create(anna, title="Remove me")
|
||||
|
||||
assert anna.patch(f"/api/clips/{clip['id']}", json={"title": None}).json()["title"] is None
|
||||
|
||||
|
||||
def test_pinning_does_not_count_as_edit(anna):
|
||||
clip = create(anna)
|
||||
|
||||
pinned = anna.patch(f"/api/clips/{clip['id']}", json={"pinned": True}).json()
|
||||
|
||||
assert pinned["pinned"] is True
|
||||
assert pinned["updated_at"] == clip["updated_at"]
|
||||
assert pinned["version"] == 2
|
||||
|
||||
|
||||
def test_version_conflict(anna):
|
||||
clip = create(anna)
|
||||
anna.patch(f"/api/clips/{clip['id']}", json={"content": "from phone", "version": 1})
|
||||
|
||||
response = anna.patch(f"/api/clips/{clip['id']}", json={"content": "from laptop", "version": 1})
|
||||
|
||||
assert response.status_code == 409
|
||||
assert response.json()["current"]["content"] == "from phone"
|
||||
assert response.json()["current"]["version"] == 2
|
||||
assert anna.get(f"/api/clips/{clip['id']}").json()["content"] == "from phone"
|
||||
|
||||
|
||||
def test_update_without_version_overwrites(anna):
|
||||
clip = create(anna)
|
||||
anna.patch(f"/api/clips/{clip['id']}", json={"content": "first"})
|
||||
|
||||
response = anna.patch(f"/api/clips/{clip['id']}", json={"content": "second"})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["version"] == 3
|
||||
|
||||
|
||||
def test_clips_require_login(client):
|
||||
assert client.get("/api/clips").status_code == 401
|
||||
assert client.post("/api/clips", json={"content": "x"}).status_code == 401
|
||||
|
||||
|
||||
def test_clip_changes_require_csrf(anna):
|
||||
clip = create(anna)
|
||||
del anna.headers["X-CSRF-Token"]
|
||||
|
||||
assert anna.post("/api/clips", json={"content": "x"}).status_code == 403
|
||||
assert anna.patch(f"/api/clips/{clip['id']}", json={"pinned": True}).status_code == 403
|
||||
assert anna.delete(f"/api/clips/{clip['id']}").status_code == 403
|
||||
|
||||
|
||||
# --- Data separation ------------------------------------------------------------
|
||||
|
||||
|
||||
def test_list_only_shows_own_clips(anna, bert):
|
||||
create(anna, content="anna's secret")
|
||||
create(bert, content="bert's clip")
|
||||
|
||||
anna_items = anna.get("/api/clips").json()
|
||||
bert_items = bert.get("/api/clips").json()
|
||||
|
||||
assert [c["content"] for c in anna_items["items"]] == ["anna's secret"]
|
||||
assert [c["content"] for c in bert_items["items"]] == ["bert's clip"]
|
||||
assert anna_items["total"] == bert_items["total"] == 1
|
||||
|
||||
|
||||
def test_foreign_clip_is_indistinguishable_from_missing(anna, bert):
|
||||
clip_id = create(anna, content="anna's secret")["id"]
|
||||
|
||||
for response in (
|
||||
bert.get(f"/api/clips/{clip_id}"),
|
||||
bert.patch(f"/api/clips/{clip_id}", json={"content": "hacked"}),
|
||||
bert.delete(f"/api/clips/{clip_id}"),
|
||||
):
|
||||
assert response.status_code == 404
|
||||
assert response.json() == bert.get("/api/clips/999999").json()
|
||||
|
||||
assert anna.get(f"/api/clips/{clip_id}").json()["content"] == "anna's secret"
|
||||
|
||||
|
||||
def test_admin_cannot_read_other_users_clips(client, make_user):
|
||||
make_user("boss", Role.ADMIN)
|
||||
make_user("anna")
|
||||
login(client, "anna")
|
||||
clip_id = create(client, content="private")["id"]
|
||||
client.post("/api/auth/logout")
|
||||
|
||||
login(client, "boss")
|
||||
|
||||
assert client.get(f"/api/clips/{clip_id}").status_code == 404
|
||||
assert client.get("/api/clips").json()["total"] == 0
|
||||
|
||||
|
||||
def test_deleting_user_deletes_their_clips(client, make_user, db):
|
||||
make_user("boss", Role.ADMIN)
|
||||
anna = make_user("anna")
|
||||
anna_id = anna.id
|
||||
with TestClient(client.app, base_url="https://testserver") as anna_client:
|
||||
login(anna_client, "anna")
|
||||
create(anna_client, content="bye")
|
||||
|
||||
login(client, "boss")
|
||||
client.delete(f"/api/admin/users/{anna_id}")
|
||||
|
||||
db.expire_all()
|
||||
assert db.query(Clip).filter_by(user_id=anna_id).count() == 0
|
||||
Reference in new issue
Block a user