fix(feed): a hand-made relevance cursor is refused, not a crash

The page halves compare the cursor's timestamp with the window's in Python;
a null or naive one raised there, past the route's 400. decode_cursor now
refuses a null non-nullable key and a naive timestamp.
This commit is contained in:
2026-10-03 02:25:14 +02:00
parent c74ceb73e3
commit c4c442bc5b
2 changed files with 36 additions and 5 deletions
@@ -5,6 +5,8 @@ The ranking now covers the newest RANK_WINDOW matches and the rest follow newest
rides the cursor, so a video ingested mid-scroll cannot push a row behind the cursor and repeat it.
"""
import base64
import json
from datetime import datetime, timezone
import pytest
@@ -112,3 +114,20 @@ def test_cursor_walk_matches_the_unpaged_order(db, as_user, monkeypatch, window,
page, cursor = _ids(client, cursor, limit=limit)
walked.extend(page)
assert walked == full
@pytest.mark.parametrize(
"payload",
[
{"k": [5, None], "id": "m1", "w": "2024-01-01T00:00:00+00:00"}, # null published_at
{"k": [5, "2024-01-03T00:00:00+00:00"], "id": "m1", "w": "2024-01-01T00:00:00"}, # naive w
{"k": [5, "2024-01-03T00:00:00"], "id": "m1", "w": "2024-01-01T00:00:00+00:00"}, # naive k
],
)
def test_a_hand_made_relevance_cursor_is_refused_not_a_crash(db, as_user, payload):
# The page halves compare the cursor's timestamp with the window's in Python: a null or a naive
# one raised there, past the route's 400 — a 500 (E31 review 2).
client = as_user(_seed(db))
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode()
resp = client.get(f"/api/feed?q=foo&sort=relevance&limit=2&cursor={cursor}")
assert resp.status_code == 400, resp.text