Files
mtusi2026lab4/student-tests/test_chats.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

156 lines
5.1 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_errors, assert_ok
def create_raw(
api: Api,
actor: Actor,
title: str,
member_ids: Any,
) -> 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_in_upper_case(api: Api, alice: Actor, bob: Actor) -> None:
resp = create_raw(api, alice, "Upper", [alice.id, bob.id.upper()])
chat = assert_ok(resp, 201, "Chat")
assert chat["member_ids"] == [alice.id, bob.id]
assert chat["id"] in listed_ids(api, bob)
@pytest.mark.spec("chats", "Повтор участника в другом регистре")
def test_duplicate_member_other_case(api: Api, alice: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
member_ids = [alice.id, alice.id.upper()]
assert_error(create_raw(api, alice, title, member_ids), 400)
assert title not in listed_titles(api, alice)
@pytest.mark.spec("chats", "member_ids не массив")
def test_member_ids_not_array(api: Api, alice: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
assert_errors(
{
repr(member_ids): create_raw(api, alice, title, member_ids)
for member_ids in ({}, 123, False, alice.id)
},
400,
)
assert title not in listed_titles(api, alice)
@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"]