Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e. Contracts, public tests and evidence of lab 5 are not carried over. The compose project, image and CI prefix are renamed to lab6; the first broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the OpenSpec changes of lab 6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
5.1 KiB
Python
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"]
|