Files
mtusi2026lab2/student-tests/helpers.py
T

44 lines
1.5 KiB
Python

"""Общие проверки для собственных black-box тестов"""
import uuid
import httpx
def unique_username(label: str) -> str:
return f"st_{label}_{uuid.uuid4().hex[:12]}"
def auth(user: dict) -> dict[str, str]:
return {"X-User-Id": user["id"]}
def assert_error(response: httpx.Response, status: int, code: str) -> None:
"""Код ответа и единый формат ошибки: request_id совпадает с заголовком"""
assert response.status_code == status, response.text
assert response.headers["content-type"].startswith("application/json")
error = response.json()["error"]
assert error["code"] == code
assert error["message"]
assert error["request_id"] == response.headers["X-Request-Id"]
def read_all_messages(
api: httpx.Client, user: dict, chat_id: str, limit: int
) -> tuple[list[dict], int]:
"""Обход истории по next_cursor; возвращает сообщения и число страниц"""
items: list[dict] = []
pages = 0
params: dict[str, str | int] = {"limit": limit}
while True:
response = api.get(
f"/api/v1/chats/{chat_id}/messages", params=params, headers=auth(user)
)
assert response.status_code == 200, response.text
page = response.json()
items.extend(page["items"])
pages += 1
if page["next_cursor"] is None:
return items, pages
params = {"limit": limit, "cursor": page["next_cursor"]}