Проверять формат cursor: неверный cursor даёт 400

This commit is contained in:
2026-10-09 13:55:19 +03:00
parent cd66e8cd53
commit 31530fcf57
3 changed files with 63 additions and 10 deletions
+43
View File
@@ -1,3 +1,5 @@
import base64
import json
import unittest
import urllib.parse
@@ -49,6 +51,47 @@ class MessagePaginationTest(unittest.TestCase):
with self.subTest(cursor=cursor):
self.assertEqual(call("GET", self.path + "?cursor=" + cursor, user=self.user)[0], 400)
def test_cursor_format_is_checked(self):
_, first, _ = call("GET", self.path + "?limit=3", user=self.user)
good = first["next_cursor"]
scope, created_at, item_id = json.loads(base64.urlsafe_b64decode(good))
def encode(value, **options):
return base64.urlsafe_b64encode(json.dumps(value, **options).encode()).decode()
# тест знает внутренний формат cursor: собранный заново правильный cursor совпадает с выданным
self.assertEqual(encode([scope, created_at, item_id]), good)
bad = {
"посторонний символ внутри base64": good[:5] + "!" + good[5:],
"пробел внутри base64": good[:5] + " " + good[5:],
"перевод строки в конце": good + "\n",
"лишний знак =": good + "=",
"обрезанный": good[:-4],
"время не в формате сервера": encode([scope, "вчера", item_id]),
"время без микросекунд": encode([scope, created_at[:19] + "Z", item_id]),
"время с неполными микросекундами": encode([scope, created_at[:21] + "Z", item_id]),
"время со смещением вместо Z": encode([scope, created_at[:-1] + "+00:00", item_id]),
"несуществующая дата": encode([scope, "2026-13-45T25:61:61.000000Z", item_id]),
"число вместо времени": encode([scope, 1234567890, item_id]),
"id не UUID": encode([scope, created_at, "abc"]),
"UUID в верхнем регистре": encode([scope, created_at, item_id.upper()]),
"UUID без дефисов": encode([scope, created_at, item_id.replace("-", "")]),
"null вместо id": encode([scope, created_at, None]),
"лишний элемент": encode([scope, created_at, item_id, "x"]),
"не хватает элемента": encode([scope, created_at]),
"объект вместо списка": encode({"scope": scope, "created_at": created_at, "id": item_id}),
"тот же JSON без пробелов": encode([scope, created_at, item_id], separators=(",", ":")),
"пустой список": encode([]),
}
for name, cursor in bad.items():
with self.subTest(name):
path = self.path + "?cursor=" + urllib.parse.quote(cursor, safe="")
status, body, _ = call("GET", path, user=self.user)
self.assertEqual(status, 400)
self.assertEqual(body["error"]["code"], "invalid_request")
# правильный cursor при этом продолжает работать
self.assertEqual(call("GET", self.path + "?cursor=" + urllib.parse.quote(good, safe=""), user=self.user)[0], 200)
def test_cursor_from_another_chat(self):
other = new_chat(self.user, title="Другой чат")
send(self.user, other, "раз")