Copy the committed lab 1 sources as-is: src/, student-tests/, bench/, Dockerfile, docker-compose.yml, pyproject.toml, uv.lock, .python-version, config_example.toml, AGENTS.md, CLAUDE.md, .claude/, openspec/, .graphifyignore and KNOWN_ISSUES.md. Lab 1 evidence stays in the lab 1 repository. Merge the new skeleton files with own sections: Makefile (lab 2 targets plus own run, docker, bench and common targets), .gitignore, .dockerignore and .env.example. Rename the image to mtusi-messenger:lab2 and point AGENTS.md at lab 2. The application is still the in-memory lab 1 implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.3 KiB
Python
105 lines
3.3 KiB
Python
"""Обязательные сценарии уровня «4» (README.md, рубрика)."""
|
|
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from api_client import API, Actor, Api, order_key
|
|
from contract import assert_error
|
|
|
|
|
|
@pytest.mark.spec("api-conventions", "Битый JSON")
|
|
def test_malformed_json(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
|
|
headers = {"Content-Type": "application/json"}
|
|
for path, actor in (
|
|
(f"{API}/users", None),
|
|
(f"{API}/chats", alice),
|
|
(f"{API}/chats/{chat['id']}/messages", alice),
|
|
):
|
|
resp = api.request(
|
|
"POST",
|
|
path,
|
|
actor=actor,
|
|
headers=headers,
|
|
content=b"{bad",
|
|
)
|
|
assert_error(resp, 400)
|
|
|
|
|
|
@pytest.mark.spec("chats", "Посторонний")
|
|
@pytest.mark.spec("messages", "Посторонний пишет")
|
|
@pytest.mark.spec("messages", "Посторонний читает")
|
|
def test_foreign_chat(
|
|
api: Api,
|
|
alice: Actor,
|
|
eve: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
path = f"{API}/chats/{chat['id']}"
|
|
missing = f"{API}/chats/{uuid.uuid4()}"
|
|
requests = (
|
|
("GET", "", None),
|
|
("GET", "/messages", None),
|
|
("POST", "/messages", {"text": "чужое"}),
|
|
)
|
|
for method, suffix, body in requests:
|
|
foreign = assert_error(
|
|
api.request(method, path + suffix, actor=eve, json=body),
|
|
404,
|
|
)
|
|
absent = assert_error(
|
|
api.request(method, missing + suffix, actor=eve, json=body),
|
|
404,
|
|
)
|
|
# чужой чат неотличим от несуществующего
|
|
assert (foreign["code"], foreign["message"]) == (
|
|
absent["code"],
|
|
absent["message"],
|
|
)
|
|
assert api.all_messages(alice, chat["id"]) == []
|
|
|
|
|
|
@pytest.mark.spec("messages", "Пустой текст")
|
|
@pytest.mark.spec("api-conventions", "Строка из пробелов")
|
|
def test_empty_message(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
|
|
path = f"{API}/chats/{chat['id']}/messages"
|
|
for text in ("", " ", "\t\n "):
|
|
resp = api.request("POST", path, actor=alice, json={"text": text})
|
|
assert_error(resp, 400)
|
|
assert api.all_messages(alice, chat["id"]) == []
|
|
|
|
|
|
@pytest.mark.spec("users", "Повтор username")
|
|
def test_duplicate_username(api: Api, alice: Actor) -> None:
|
|
resp = api.request(
|
|
"POST",
|
|
f"{API}/users",
|
|
json={
|
|
"username": alice.user["username"],
|
|
"display_name": "Другая Алиса",
|
|
},
|
|
)
|
|
assert_error(resp, 409)
|
|
|
|
|
|
def test_history_walk_across_pages(
|
|
api: Api,
|
|
alice: Actor,
|
|
bob: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
"""12 сообщений, страницы по 5, 3, 4: `limit` меняется между ними."""
|
|
sent = [
|
|
api.send_message(sender, chat["id"], f"walk-{number}")
|
|
for number, sender in enumerate([alice, bob] * 6)
|
|
]
|
|
pages = api.message_pages(bob, chat["id"], limits=(5, 3, 4))
|
|
|
|
assert [len(page["items"]) for page in pages] == [5, 3, 4]
|
|
assert all(page["next_cursor"] for page in pages[:-1])
|
|
assert pages[-1]["next_cursor"] is None
|
|
items = [item for page in pages for item in page["items"]]
|
|
assert len({item["id"] for item in items}) == len(sent)
|
|
assert items == sorted(sent, key=order_key)
|