Files
mtusi2026lab4/student-tests/test_messages.py
T
ricoromaandClaude Opus 5.5 5d5d8f9d2a chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov
  (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests,
  bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts,
  Dockerfile, compose, pyproject and uv lock
- merge Makefile, .gitignore and .dockerignore (the lab 3 versions only
  add lines to the skeleton, which is the same in lab 4) and
  .env.example (lab 4 skeleton names listed in the header)
- rename the compose project to mtusi-lab4 and the image to :lab4 so
  the stand does not reuse the lab 3 containers and volumes
- update AGENTS.md for lab 4: current lab, origin commit, contract and
  test files of the new skeleton

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:32:33 +03:00

107 lines
3.1 KiB
Python

"""Спек `messages`: отправка, валидация текста, история."""
from typing import Any
import httpx
import pytest
from api_client import Actor, Api, order_key
from contract import assert_error, assert_ok
def send_raw(
api: Api,
actor: Actor,
chat_id: str,
body: dict[str, Any],
) -> httpx.Response:
"""Отправка с новым ключом; ответ проверяет тест."""
return api.post_message(actor, chat_id, json=body)
@pytest.mark.spec("messages", "Участник отправляет сообщение")
def test_member_sends(
api: Api,
alice: Actor,
chat: dict[str, Any],
) -> None:
resp = send_raw(api, alice, chat["id"], {"text": "Привет, мир 👋"})
message = assert_ok(resp, 201, "Message")
assert message["sender_id"] == alice.id
assert message["chat_id"] == chat["id"]
assert message["text"] == "Привет, мир 👋"
@pytest.mark.spec("messages", "Граница длины")
def test_text_length(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
# кириллица: 2 байта на символ, считаются code points
text = "я" * 4000
message = assert_ok(
send_raw(api, alice, chat["id"], {"text": text}),
201,
"Message",
)
assert message["text"] == text
assert_error(send_raw(api, alice, chat["id"], {"text": text + "я"}), 400)
assert [item["id"] for item in api.all_messages(alice, chat["id"])] == [
message["id"],
]
@pytest.mark.spec("messages", "Попытка подменить отправителя")
def test_sender_id_in_body(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
body = {"text": "от Боба", "sender_id": bob.id}
assert_error(send_raw(api, alice, chat["id"], body), 400)
assert api.all_messages(bob, chat["id"]) == []
@pytest.mark.spec("messages", "Два участника, две страницы")
def test_two_members_two_pages(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
sent = [
api.send_message(alice, chat["id"], "от Алисы"),
api.send_message(bob, chat["id"], "от Боба"),
]
first = assert_ok(
api.messages_page(bob, chat["id"], limit=1),
200,
"MessagePage",
)
assert len(first["items"]) == 1
assert first["next_cursor"]
second = assert_ok(
api.messages_page(
bob,
chat["id"],
limit=1,
cursor=first["next_cursor"],
),
200,
"MessagePage",
)
assert len(second["items"]) == 1
assert second["next_cursor"] is None
assert first["items"] + second["items"] == sorted(sent, key=order_key)
@pytest.mark.spec("messages", "Чтение после записи")
def test_read_after_write(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
for number in range(5):
message = api.send_message(alice, chat["id"], f"сразу {number}")
history = api.all_messages(bob, chat["id"])
assert message in history