Files
mtusi2026lab3/student-tests/test_chats.py
T
ricoromaandClaude Opus 5.5 af4bca08c9 feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 19:42:36 +03:00

127 lines
4.0 KiB
Python

"""Спек `chats`: создание, чтение, список, неизменный состав."""
import uuid
from typing import Any
import httpx
import pytest
from api_client import API, Actor, Api
from contract import assert_error, assert_ok
def create_raw(
api: Api,
actor: Actor,
title: str,
member_ids: list[str],
) -> httpx.Response:
return api.request(
"POST",
f"{API}/chats",
actor=actor,
json={"title": title, "member_ids": member_ids},
)
def listed_ids(api: Api, actor: Actor) -> set[str]:
return {
chat["id"] for page in api.chat_pages(actor) for chat in page["items"]
}
def listed_titles(api: Api, actor: Actor) -> set[str]:
return {
chat["title"]
for page in api.chat_pages(actor)
for chat in page["items"]
}
@pytest.mark.spec("chats", "Успешное создание")
def test_create_chat(api: Api, alice: Actor, bob: Actor) -> None:
resp = create_raw(api, alice, "Проект", [alice.id, bob.id])
chat = assert_ok(resp, 201, "Chat")
assert chat["member_ids"] == [alice.id, bob.id]
assert chat["title"] == "Проект"
# порядок участников сохраняется, даже если создатель не первый
resp = create_raw(api, alice, "Проект 2", [bob.id, alice.id])
reversed_chat = assert_ok(resp, 201, "Chat")
assert reversed_chat["member_ids"] == [bob.id, alice.id]
assert reversed_chat["id"] != chat["id"]
@pytest.mark.spec("chats", "Вызывающего нет в списке")
def test_caller_not_in_members(api: Api, alice: Actor, bob: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
assert_error(create_raw(api, alice, title, [bob.id]), 400)
# отклонённый чат не появился ни в одном списке
assert title not in listed_titles(api, alice)
assert title not in listed_titles(api, bob)
@pytest.mark.spec("chats", "Повтор участника")
def test_duplicate_member(api: Api, alice: Actor) -> None:
assert_error(create_raw(api, alice, "Dup", [alice.id, alice.id]), 400)
@pytest.mark.spec("chats", "Неизвестный участник")
def test_unknown_member(api: Api, alice: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
member_ids = [alice.id, str(uuid.uuid4())]
assert_error(create_raw(api, alice, title, member_ids), 400)
assert title not in listed_titles(api, alice)
@pytest.mark.spec("chats", "Пустой список")
def test_empty_members(api: Api, alice: Actor) -> None:
assert_error(create_raw(api, alice, "Empty", []), 400)
@pytest.mark.spec("chats", "Участник")
def test_member_reads_chat(
api: Api,
bob: Actor,
chat: dict[str, Any],
) -> None:
resp = api.request("GET", f"{API}/chats/{chat['id']}", actor=bob)
assert assert_ok(resp, 200, "Chat") == chat
@pytest.mark.spec("chats", "Неверный chat_id")
def test_invalid_chat_id(api: Api, alice: Actor) -> None:
assert_error(api.request("GET", f"{API}/chats/nope", actor=alice), 400)
@pytest.mark.spec("chats", "Участник видит чат")
def test_member_lists_chat(
api: Api,
bob: Actor,
chat: dict[str, Any],
) -> None:
assert chat["id"] in listed_ids(api, bob)
@pytest.mark.spec("chats", "Посторонний не видит чат")
def test_outsider_does_not_list_chat(
api: Api,
eve: Actor,
chat: dict[str, Any],
) -> None:
assert chat["id"] not in listed_ids(api, eve)
@pytest.mark.spec("chats", "Повторное чтение")
def test_members_do_not_change(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
path = f"{API}/chats/{chat['id']}"
first = assert_ok(api.request("GET", path, actor=alice), 200, "Chat")
api.send_message(bob, chat["id"], "между чтениями")
api.create_chat(alice, bob)
second = assert_ok(api.request("GET", path, actor=bob), 200, "Chat")
assert first["member_ids"] == second["member_ids"] == chat["member_ids"]