test_rest_invalid_cursor_308.py
python
sha256:0ee8d0cfba18a9463727bbdc1dec6d33eeeae3d1c6fbbeb283b39db7bfe6657a
Merge branch 'task/rest-308-312' into dev
Opus 5.5
patch
1 day ago
| 1 | """musehub#308: a malformed ``?cursor=`` is a 400 on every paginated REST list, never a 500 (or a 404 carrying the parser's message). |
| 2 | |
| 3 | Each list decodes its cursor through ``musehub.core.cursors``; ``musehub.main`` answers ``InvalidCursor`` with ``{"detail": "invalid cursor"}``. |
| 4 | A cursor the list itself issued still pages: the round-trip tests follow ``nextCursor`` so a strict decoder can never reject a real cursor. |
| 5 | """ |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import pytest |
| 9 | from httpx import AsyncClient |
| 10 | from sqlalchemy.ext.asyncio import AsyncSession |
| 11 | |
| 12 | from musehub.core.cursors import InvalidCursor, cursor_datetime, cursor_int |
| 13 | from tests.factories import create_issue, create_proposal, create_repo |
| 14 | |
| 15 | BAD_CURSORS = ["not-a-cursor", "2026-13-45T99:99:99", "2026-11-15T10:00:00", "-1", "1.5", "٣"] |
| 16 | |
| 17 | |
| 18 | @pytest.mark.parametrize("bad", ["not-a-cursor", "2026-13-45T99:99:99", "2026-11-15T10:00:00", ""]) |
| 19 | def test_timestamp_cursor_rejects_anything_the_api_never_issues(bad: str) -> None: |
| 20 | with pytest.raises(InvalidCursor): |
| 21 | cursor_datetime(bad) |
| 22 | |
| 23 | |
| 24 | def test_timestamp_cursor_accepts_issued_forms() -> None: |
| 25 | assert cursor_datetime("2026-11-15T10:00:00+00:00").utcoffset() is not None |
| 26 | assert cursor_datetime("2026-11-15T10:00:00 00:00").utcoffset() is not None # '+' decoded to a space |
| 27 | assert cursor_datetime("2026-11-15T10:00:00Z").utcoffset() is not None |
| 28 | |
| 29 | |
| 30 | @pytest.mark.parametrize("bad", ["not-a-cursor", "-1", "1.5", "٣", ""]) |
| 31 | def test_sequence_cursor_rejects_non_decimal(bad: str) -> None: |
| 32 | with pytest.raises(InvalidCursor): |
| 33 | cursor_int(bad) |
| 34 | |
| 35 | |
| 36 | @pytest.fixture |
| 37 | async def seeded(db_session: AsyncSession) -> dict[str, str]: |
| 38 | repo = await create_repo(db_session, owner="cursoruser", slug="cursor-308", visibility="public") |
| 39 | rid = str(repo.repo_id) |
| 40 | issue = await create_issue(db_session, rid) |
| 41 | proposal = await create_proposal(db_session, rid) |
| 42 | return {"rid": rid, "issue": str(issue.number), "proposal": str(proposal.proposal_id)} |
| 43 | |
| 44 | |
| 45 | _ROUTES = [ |
| 46 | "/api/repos/{rid}/issues", |
| 47 | "/api/repos/{rid}/issues/{issue}/comments", |
| 48 | "/api/repos/{rid}/proposals", |
| 49 | "/api/repos/{rid}/proposals/{proposal}/comments", |
| 50 | "/api/repos/{rid}/proposals/{proposal}/reviews", |
| 51 | "/api/repos/{rid}/releases", |
| 52 | "/api/repos/{rid}/commits", |
| 53 | "/api/repos/{rid}/sessions", |
| 54 | "/api/discover/repos", |
| 55 | ] |
| 56 | |
| 57 | |
| 58 | @pytest.mark.asyncio |
| 59 | @pytest.mark.parametrize("route", _ROUTES) |
| 60 | @pytest.mark.parametrize("bad", BAD_CURSORS) |
| 61 | async def test_every_list_route_answers_400_for_a_malformed_cursor( |
| 62 | client: AsyncClient, seeded: dict[str, str], route: str, bad: str |
| 63 | ) -> None: |
| 64 | if route.endswith("/issues") and bad.startswith("2026"): |
| 65 | pytest.skip("issue cursors are numbers: a timestamp is covered by the non-decimal cases") |
| 66 | r = await client.get(route.format(**seeded), params={"cursor": bad}) |
| 67 | assert r.status_code == 400, (route, bad, r.status_code, r.text[:200]) |
| 68 | assert r.json() == {"detail": "invalid cursor"} |
| 69 | |
| 70 | |
| 71 | @pytest.mark.asyncio |
| 72 | async def test_services_without_a_public_route_reject_malformed_cursors(db_session: AsyncSession, seeded: dict[str, str]) -> None: |
| 73 | from musehub.services import musehub_releases, musehub_snapshot, musehub_webhook_dispatcher |
| 74 | |
| 75 | for call in ( |
| 76 | musehub_snapshot.list_snapshots(db_session, seeded["rid"], cursor="nope"), |
| 77 | musehub_webhook_dispatcher.list_webhooks(db_session, seeded["rid"], cursor="nope"), |
| 78 | musehub_webhook_dispatcher.list_deliveries(db_session, "sha256:" + "0" * 64, cursor="nope"), |
| 79 | musehub_releases.list_release_assets(db_session, "sha256:" + "0" * 64, "v1.0.0", cursor="nope"), |
| 80 | ): |
| 81 | with pytest.raises(InvalidCursor): |
| 82 | await call |
| 83 | |
| 84 | |
| 85 | @pytest.mark.asyncio |
| 86 | async def test_issued_cursors_still_page(client: AsyncClient, db_session: AsyncSession, seeded: dict[str, str]) -> None: |
| 87 | rid = seeded["rid"] |
| 88 | for _ in range(2): |
| 89 | await create_issue(db_session, rid) |
| 90 | await create_proposal(db_session, rid) |
| 91 | for route in ("/api/repos/{rid}/issues", "/api/repos/{rid}/proposals"): |
| 92 | seen: list[str] = [] |
| 93 | cursor: str | None = None |
| 94 | for _ in range(5): |
| 95 | r = await client.get(route.format(rid=rid), params={"limit": 1, **({"cursor": cursor} if cursor else {})}) |
| 96 | assert r.status_code == 200, (route, cursor, r.text[:200]) |
| 97 | body = r.json() |
| 98 | seen.append(r.text) |
| 99 | cursor = body.get("nextCursor") |
| 100 | if not cursor: |
| 101 | break |
| 102 | assert len(seen) == 3, (route, len(seen)) |
File History
2 commits
sha256:0ee8d0cfba18a9463727bbdc1dec6d33eeeae3d1c6fbbeb283b39db7bfe6657a
Merge branch 'task/rest-308-312' into dev
Opus 5.5
patch
1 day ago
sha256:762ad834cc326a18fd5ecb62f465e1ed59dac9cb7418674fd2c279dacc9febf4
fix(rest): a malformed pagination cursor is a 400 on every …
Opus 5.5
patch
1 day ago