171 lines
6.3 KiB
Python
171 lines
6.3 KiB
Python
"""Пагинация истории и списка чатов по keyset-курсору."""
|
|
|
|
from urllib.parse import quote
|
|
|
|
from conftest import assert_error
|
|
|
|
|
|
def send_many(client, person, chat, count, prefix="msg"):
|
|
"""Отправить несколько сообщений в чат."""
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
for number in range(count):
|
|
reply = client.post(
|
|
url, json={"text": "%s-%02d" % (prefix, number)}, headers=person.headers
|
|
)
|
|
assert reply.status_code == 201
|
|
|
|
|
|
def walk(client, person, chat, limit):
|
|
"""Пройти историю страницами; вернуть элементы и число страниц."""
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
items, pages, cursor = [], 0, None
|
|
while True:
|
|
query = "?limit=%d" % limit + (
|
|
"&cursor=" + quote(cursor, safe="") if cursor else ""
|
|
)
|
|
data = client.get(url + query, headers=person.headers).json()
|
|
assert set(data) == {"items", "next_cursor"} and len(data["items"]) <= limit
|
|
items += data["items"]
|
|
pages += 1
|
|
cursor = data["next_cursor"]
|
|
if cursor is None:
|
|
return items, pages
|
|
assert pages < 1000, "бесконечный обход"
|
|
|
|
|
|
def test_walk_25_messages_by_7(client, alice, bob, chat):
|
|
send_many(client, alice, chat, 25)
|
|
items, pages = walk(client, bob, chat, 7)
|
|
assert pages == 4
|
|
assert [m["text"] for m in items] == ["msg-%02d" % n for n in range(25)]
|
|
assert len({m["id"] for m in items}) == 25
|
|
|
|
|
|
def test_exact_multiple_has_no_empty_extra_page(client, alice, chat):
|
|
send_many(client, alice, chat, 10)
|
|
items, pages = walk(client, alice, chat, 5)
|
|
assert len(items) == 10 and pages == 2
|
|
|
|
|
|
def test_limit_one_walks_everything(client, alice, chat):
|
|
send_many(client, alice, chat, 6)
|
|
items, pages = walk(client, alice, chat, 1)
|
|
assert len(items) == 6 and pages == 6
|
|
|
|
|
|
def test_default_limit_is_50_and_max_is_100(client, alice, chat):
|
|
send_many(client, alice, chat, 120)
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
default = client.get(url, headers=alice.headers).json()
|
|
assert len(default["items"]) == 50 and default["next_cursor"]
|
|
biggest = client.get(url + "?limit=100", headers=alice.headers).json()
|
|
assert len(biggest["items"]) == 100 and biggest["next_cursor"]
|
|
|
|
|
|
def test_empty_history(client, alice, chat):
|
|
data = client.get(
|
|
"/api/v1/chats/" + chat["id"] + "/messages", headers=alice.headers
|
|
).json()
|
|
assert data == {"items": [], "next_cursor": None}
|
|
|
|
|
|
def test_limit_can_change_between_pages(client, alice, chat):
|
|
send_many(client, alice, chat, 9)
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
first = client.get(url + "?limit=2", headers=alice.headers).json()
|
|
second = client.get(
|
|
url + "?limit=100&cursor=" + quote(first["next_cursor"], safe=""),
|
|
headers=alice.headers,
|
|
).json()
|
|
assert [m["text"] for m in first["items"] + second["items"]] == [
|
|
"msg-%02d" % n for n in range(9)
|
|
]
|
|
assert second["next_cursor"] is None
|
|
|
|
|
|
def test_messages_sent_during_walk_appear_at_the_end(client, alice, chat):
|
|
send_many(client, alice, chat, 4)
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
first = client.get(url + "?limit=2", headers=alice.headers).json()
|
|
send_many(client, alice, chat, 1, prefix="late")
|
|
rest = client.get(
|
|
url + "?limit=10&cursor=" + quote(first["next_cursor"], safe=""),
|
|
headers=alice.headers,
|
|
).json()
|
|
texts = [m["text"] for m in first["items"] + rest["items"]]
|
|
assert texts == ["msg-00", "msg-01", "msg-02", "msg-03", "late-00"]
|
|
|
|
|
|
def test_created_at_is_increasing_and_utc(client, alice, chat):
|
|
send_many(client, alice, chat, 30)
|
|
items, _ = walk(client, alice, chat, 100)
|
|
stamps = [m["created_at"] for m in items]
|
|
assert stamps == sorted(stamps) and len(set(stamps)) == 30
|
|
assert all(s.endswith("Z") and "T" in s for s in stamps)
|
|
|
|
|
|
def test_bad_limit_values(client, alice, chat):
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
for bad in ["0", "101", "-1", "abc", "1.5", "", "+5", " 5", "٣"]:
|
|
assert_error(
|
|
client.get(url + "?limit=" + quote(bad), headers=alice.headers),
|
|
400,
|
|
"invalid_request",
|
|
)
|
|
assert client.get(url + "?limit=1", headers=alice.headers).status_code == 200
|
|
assert client.get(url + "?limit=100", headers=alice.headers).status_code == 200
|
|
|
|
|
|
def test_garbage_cursor_is_400(client, alice, chat):
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
for bad in ["garbage", "!!!", "e30", "a" * 600, "eyJzIjoibWVzc2FnZXM6eCJ9"]:
|
|
assert_error(
|
|
client.get(url + "?cursor=" + quote(bad), headers=alice.headers),
|
|
400,
|
|
"invalid_request",
|
|
)
|
|
|
|
|
|
def test_cursor_from_another_chat_is_400(client, alice, bob, make_chat, chat):
|
|
other = make_chat(alice, bob, title="Второй чат")
|
|
send_many(client, alice, chat, 5)
|
|
send_many(client, alice, other, 5)
|
|
first = client.get(
|
|
"/api/v1/chats/" + chat["id"] + "/messages?limit=2", headers=alice.headers
|
|
).json()
|
|
reply = client.get(
|
|
"/api/v1/chats/"
|
|
+ other["id"]
|
|
+ "/messages?cursor="
|
|
+ quote(first["next_cursor"], safe=""),
|
|
headers=alice.headers,
|
|
)
|
|
assert_error(reply, 400, "invalid_request")
|
|
|
|
|
|
def test_chat_list_pagination(client, alice, bob, make_chat):
|
|
created = [make_chat(alice, bob, title="Чат %02d" % n)["id"] for n in range(5)]
|
|
seen, cursor = [], None
|
|
while True:
|
|
query = "?limit=2" + ("&cursor=" + quote(cursor, safe="") if cursor else "")
|
|
data = client.get("/api/v1/chats" + query, headers=bob.headers).json()
|
|
seen += [c["id"] for c in data["items"]]
|
|
cursor = data["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
assert seen == created
|
|
|
|
|
|
def test_chat_list_cursor_cannot_be_used_for_messages(
|
|
client, alice, bob, make_chat, chat
|
|
):
|
|
make_chat(alice, bob, title="ещё")
|
|
cursor = client.get("/api/v1/chats?limit=1", headers=alice.headers).json()[
|
|
"next_cursor"
|
|
]
|
|
reply = client.get(
|
|
"/api/v1/chats/" + chat["id"] + "/messages?cursor=" + quote(cursor, safe=""),
|
|
headers=alice.headers,
|
|
)
|
|
assert_error(reply, 400, "invalid_request")
|