forked from mtusi2026/mtusi2026lab4
139 lines
4.4 KiB
Python
139 lines
4.4 KiB
Python
"""Обход нескольких страниц истории и списка чатов по cursor"""
|
|
|
|
import httpx
|
|
import pytest
|
|
from helpers import assert_error, auth, read_all_messages
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("total", "limit", "expected_pages"),
|
|
[(7, 3, 3), (6, 3, 2), (5, 1, 5), (3, 100, 1)],
|
|
)
|
|
def test_walk_message_pages(
|
|
api: httpx.Client,
|
|
make_user,
|
|
make_chat,
|
|
send_message,
|
|
total: int,
|
|
limit: int,
|
|
expected_pages: int,
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
bob = make_user("bob")
|
|
chat = make_chat(alice, [alice, bob])
|
|
sent = [send_message(alice if n % 2 else bob, chat, f"m{n}") for n in range(total)]
|
|
|
|
items, pages = read_all_messages(api, bob, chat["id"], limit)
|
|
|
|
assert pages == expected_pages
|
|
assert items == sent
|
|
assert len({item["id"] for item in items}) == total
|
|
keys = [(item["created_at"], item["id"]) for item in items]
|
|
assert keys == sorted(keys)
|
|
|
|
|
|
def test_limit_can_change_between_pages(
|
|
api: httpx.Client, make_user, make_chat, send_message
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
sent = [send_message(alice, chat, f"m{n}") for n in range(6)]
|
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
|
|
|
first = api.get(path, params={"limit": 2}, headers=auth(alice)).json()
|
|
second = api.get(
|
|
path, params={"limit": 4, "cursor": first["next_cursor"]}, headers=auth(alice)
|
|
).json()
|
|
|
|
assert first["items"] + second["items"] == sent
|
|
assert second["next_cursor"] is None
|
|
|
|
|
|
def test_new_message_appears_after_cursor(
|
|
api: httpx.Client, make_user, make_chat, send_message
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
send_message(alice, chat, "m0")
|
|
send_message(alice, chat, "m1")
|
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
|
first = api.get(path, params={"limit": 1}, headers=auth(alice)).json()
|
|
late = send_message(alice, chat, "m2")
|
|
rest = api.get(
|
|
path, params={"limit": 10, "cursor": first["next_cursor"]}, headers=auth(alice)
|
|
).json()
|
|
assert [item["text"] for item in rest["items"]] == ["m1", "m2"]
|
|
assert rest["items"][-1] == late
|
|
|
|
|
|
def test_cursor_is_bound_to_chat(
|
|
api: httpx.Client, make_user, make_chat, send_message
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
chat_a = make_chat(alice, [alice], title="A")
|
|
chat_b = make_chat(alice, [alice], title="B")
|
|
for n in range(3):
|
|
send_message(alice, chat_a, f"a{n}")
|
|
cursor = api.get(
|
|
f"/api/v1/chats/{chat_a['id']}/messages",
|
|
params={"limit": 1},
|
|
headers=auth(alice),
|
|
).json()["next_cursor"]
|
|
response = api.get(
|
|
f"/api/v1/chats/{chat_b['id']}/messages",
|
|
params={"cursor": cursor},
|
|
headers=auth(alice),
|
|
)
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"params",
|
|
[
|
|
{"limit": 0},
|
|
{"limit": 101},
|
|
{"limit": "abc"},
|
|
{"cursor": ""},
|
|
{"cursor": "not-a-cursor"},
|
|
{"cursor": "eyJzY29wZSI6IDF9"},
|
|
],
|
|
)
|
|
def test_invalid_limit_or_cursor(
|
|
api: httpx.Client, make_user, make_chat, params: dict
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
response = api.get(
|
|
f"/api/v1/chats/{chat['id']}/messages", params=params, headers=auth(alice)
|
|
)
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
def test_walk_chat_list_pages(api: httpx.Client, make_user, make_chat) -> None:
|
|
alice = make_user("alice")
|
|
created = [make_chat(alice, [alice], title=f"chat {n}") for n in range(5)]
|
|
listed: list[dict] = []
|
|
params: dict[str, str | int] = {"limit": 2}
|
|
pages = 0
|
|
while True:
|
|
page = api.get("/api/v1/chats", params=params, headers=auth(alice)).json()
|
|
listed.extend(page["items"])
|
|
pages += 1
|
|
if page["next_cursor"] is None:
|
|
break
|
|
params = {"limit": 2, "cursor": page["next_cursor"]}
|
|
assert pages == 3
|
|
assert listed == created
|
|
|
|
|
|
def test_chat_cursor_is_bound_to_user(api: httpx.Client, make_user, make_chat) -> None:
|
|
alice = make_user("alice")
|
|
bob = make_user("bob")
|
|
for n in range(2):
|
|
make_chat(alice, [alice, bob], title=f"chat {n}")
|
|
cursor = api.get("/api/v1/chats", params={"limit": 1}, headers=auth(alice)).json()[
|
|
"next_cursor"
|
|
]
|
|
response = api.get("/api/v1/chats", params={"cursor": cursor}, headers=auth(bob))
|
|
assert_error(response, 400, "invalid_request")
|