Files
mtusi2026lab3/student-tests/test_messages_api.py
T
vest1ter cde3374d8a ЛР3: балансировка HAProxy, 2 реплики API, идемпотентность, отказоустойчивость (на 4)
- X-Instance-Id в каждом ответе; обязательный Idempotency-Key, таблица idempotency_keys
  (миграция 0002, одна транзакция, конкурентные повторы через уникальный индекс)
- graceful shutdown: SIGTERM -> ready=503 -> drain 3 с -> закрытие порта
- compose: lb (HAProxy) + api x2 без портов + разовый migrate + postgres, лимиты CPU/RAM
- хуки stop-one/restart/restore-replicas, scripts/experiments.py (failover, idempotency, rolling)
- свои тесты: 44 (13 новых); evidence: smoke, pytest, persistence, failover, эксперименты
2026-10-29 10:58:17 +03:00

67 lines
2.8 KiB
Python

from conftest import assert_error, with_key
def test_empty_message_returns_400(client, chat, alice):
path = "/api/v1/chats/" + chat["id"] + "/messages"
headers = with_key(alice["auth"])
assert_error(client.post(path, headers=headers, json={"text": ""}), 400, "invalid_request")
assert_error(client.post(path, headers=headers, json={"text": " \n\t"}), 400, "invalid_request")
def test_sender_is_taken_from_token(client, chat, alice, bob):
path = "/api/v1/chats/" + chat["id"] + "/messages"
response = client.post(path, headers=with_key(bob["auth"]), json={"text": "Привет 👋"})
assert response.status_code == 201
assert response.json()["sender_id"] == bob["id"]
assert response.json()["text"] == "Привет 👋"
spoof = client.post(path, headers=with_key(bob["auth"]), json={"text": "x", "sender_id": alice["id"]})
assert_error(spoof, 400, "invalid_request")
def test_pagination_over_several_pages(client, chat, alice):
path = "/api/v1/chats/" + chat["id"] + "/messages"
headers = alice["auth"]
sent = [client.post(path, headers=with_key(headers), json={"text": f"сообщение {i}"}).json() for i in range(7)]
received = []
pages = 0
cursor = None
while True:
params = {"limit": 3}
if cursor:
params["cursor"] = cursor
page = client.get(path, headers=headers, params=params).json()
received += page["items"]
pages += 1
cursor = page["next_cursor"]
if cursor is None:
break
assert pages == 3 # 3 + 3 + 1
assert received == sent # тот же порядок, ничего не потеряно
assert len({m["id"] for m in received}) == 7 # без повторов
def test_cursor_from_another_chat_returns_400(client, chat, alice):
headers = alice["auth"]
path = "/api/v1/chats/" + chat["id"] + "/messages"
for i in range(2):
client.post(path, headers=with_key(headers), json={"text": f"m{i}"})
cursor = client.get(path, headers=headers, params={"limit": 1}).json()["next_cursor"]
other = client.post("/api/v1/chats", headers=headers, json={"title": "Другой", "member_ids": [alice["id"]]}).json()
response = client.get("/api/v1/chats/" + other["id"] + "/messages", headers=headers, params={"cursor": cursor})
assert_error(response, 400, "invalid_request")
response = client.get(path, headers=headers, params={"cursor": "мусор"})
assert_error(response, 400, "invalid_request")
def test_invalid_limit_returns_400(client, chat, alice):
path = "/api/v1/chats/" + chat["id"] + "/messages"
for limit in (0, 101, "abc"):
response = client.get(path, headers=alice["auth"], params={"limit": limit})
assert_error(response, 400, "invalid_request")