forked from mtusi2026/mtusi2026lab4
feat: Саватеев А трек 4
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
"""
|
||||
Сообщения с attachment_ids: вложение только своего чата, порядок, пустой
|
||||
текст, идемпотентность; при любом отказе сообщение не появляется.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from helpers import (
|
||||
assert_error,
|
||||
assert_not_server_error,
|
||||
auth,
|
||||
message_headers,
|
||||
png_bytes,
|
||||
read_all_messages,
|
||||
)
|
||||
|
||||
|
||||
def history(api: httpx.Client, user: dict, chat: dict) -> list[dict]:
|
||||
items, _ = read_all_messages(api, user, chat["id"], 100)
|
||||
return items
|
||||
|
||||
|
||||
def test_message_with_attachment_is_readable_by_members(
|
||||
api: httpx.Client, make_user, make_chat, upload, send_attachments
|
||||
) -> None:
|
||||
alice = make_user("alice")
|
||||
bob = make_user("bob")
|
||||
chat = make_chat(alice, [alice, bob])
|
||||
attachment = upload(alice, chat)
|
||||
|
||||
response = send_attachments(alice, chat, [attachment["id"]])
|
||||
assert response.status_code == 201, response.text
|
||||
message = response.json()
|
||||
assert message["text"] == "" and message["attachment_ids"] == [attachment["id"]]
|
||||
|
||||
assert history(api, bob, chat) == [message]
|
||||
content = api.get(
|
||||
f"/api/v1/attachments/{attachment['id']}/content", headers=auth(bob)
|
||||
)
|
||||
assert content.status_code == 200
|
||||
|
||||
|
||||
def test_any_member_may_reference_chat_attachment_and_order_is_kept(
|
||||
make_user, make_chat, upload, send_attachments
|
||||
) -> None:
|
||||
alice = make_user("alice")
|
||||
bob = make_user("bob")
|
||||
chat = make_chat(alice, [alice, bob])
|
||||
ids = [upload(alice, chat, png_bytes(i + 1, i + 1))["id"] for i in range(10)]
|
||||
reordered = list(reversed(ids))
|
||||
|
||||
response = send_attachments(bob, chat, reordered, text="от Боба")
|
||||
assert response.status_code == 201, response.text
|
||||
assert response.json()["attachment_ids"] == reordered
|
||||
|
||||
|
||||
def test_text_message_has_empty_attachment_ids(make_user, make_chat, send_message) -> None:
|
||||
alice = make_user("alice")
|
||||
chat = make_chat(alice, [alice])
|
||||
assert send_message(alice, chat, "только текст")["attachment_ids"] == []
|
||||
|
||||
|
||||
def test_attachment_of_other_chat_is_rejected(
|
||||
api: httpx.Client, make_user, make_chat, upload, send_attachments
|
||||
) -> None:
|
||||
"""Алиса — участник обоих чатов, но файл привязан к другому чату"""
|
||||
alice = make_user("alice")
|
||||
chat = make_chat(alice, [alice], title="Чат A")
|
||||
other = make_chat(alice, [alice], title="Чат B")
|
||||
foreign = upload(alice, other)
|
||||
own = upload(alice, chat)
|
||||
|
||||
for ids in ([foreign["id"]], [own["id"], foreign["id"]]):
|
||||
response = send_attachments(alice, chat, ids, text="чужой файл")
|
||||
assert_not_server_error(response)
|
||||
assert_error(response, 404, "not_found")
|
||||
assert history(api, alice, chat) == []
|
||||
|
||||
|
||||
def test_foreign_attachment_id_is_rejected(
|
||||
api: httpx.Client, make_user, make_chat, upload, send_attachments
|
||||
) -> None:
|
||||
"""Вложение из чата, где пользователь не участник, и несуществующее — 404"""
|
||||
alice = make_user("alice")
|
||||
eve = make_user("eve")
|
||||
chat = make_chat(alice, [alice])
|
||||
eve_chat = make_chat(eve, [eve])
|
||||
eve_attachment = upload(eve, eve_chat)
|
||||
|
||||
foreign = send_attachments(alice, chat, [eve_attachment["id"]])
|
||||
missing = send_attachments(alice, chat, [str(uuid.uuid4())])
|
||||
assert_error(foreign, 404, "not_found")
|
||||
assert_error(missing, 404, "not_found")
|
||||
assert foreign.json()["error"]["message"] == missing.json()["error"]["message"]
|
||||
assert history(api, alice, chat) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"body",
|
||||
[
|
||||
{"text": ""},
|
||||
{"text": " "},
|
||||
{"text": "", "attachment_ids": []},
|
||||
{"text": "x", "attachment_ids": ["not-a-uuid"]},
|
||||
{"text": "x", "attachment_ids": "a"},
|
||||
{"text": "x", "attachment_ids": None},
|
||||
{"text": "x", "attachment_ids": [str(uuid.uuid4())] * 2},
|
||||
{"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]},
|
||||
],
|
||||
ids=[
|
||||
"empty-text",
|
||||
"blank-text",
|
||||
"empty-both",
|
||||
"bad-uuid",
|
||||
"not-array",
|
||||
"null",
|
||||
"duplicates",
|
||||
"too-many",
|
||||
],
|
||||
)
|
||||
def test_invalid_attachment_ids_are_rejected(
|
||||
api: httpx.Client, make_user, make_chat, body: dict
|
||||
) -> None:
|
||||
alice = make_user("alice")
|
||||
chat = make_chat(alice, [alice])
|
||||
response = api.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages", json=body, headers=message_headers(alice)
|
||||
)
|
||||
assert_error(response, 400, "invalid_request")
|
||||
|
||||
|
||||
def test_idempotent_replay_with_attachments(
|
||||
api: httpx.Client, make_user, make_chat, upload, send_attachments
|
||||
) -> None:
|
||||
alice = make_user("alice")
|
||||
chat = make_chat(alice, [alice])
|
||||
first, second = upload(alice, chat), upload(alice, chat)
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
created = send_attachments(alice, chat, [first["id"]], key=key)
|
||||
replay = send_attachments(alice, chat, [first["id"]], key=key)
|
||||
assert created.status_code == replay.status_code == 201
|
||||
assert created.json() == replay.json()
|
||||
|
||||
changed = send_attachments(alice, chat, [first["id"], second["id"]], key=key)
|
||||
assert_error(changed, 409, "conflict")
|
||||
assert history(api, alice, chat) == [created.json()]
|
||||
|
||||
|
||||
def test_missing_attachment_ids_equals_empty_list_for_idempotency(
|
||||
api: httpx.Client, make_user, make_chat
|
||||
) -> None:
|
||||
alice = make_user("alice")
|
||||
chat = make_chat(alice, [alice])
|
||||
key = uuid.uuid4().hex
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
first = api.post(path, json={"text": "a"}, headers=message_headers(alice, key))
|
||||
second = api.post(
|
||||
path,
|
||||
json={"attachment_ids": [], "text": "a"},
|
||||
headers=message_headers(alice, key),
|
||||
)
|
||||
assert first.status_code == second.status_code == 201
|
||||
assert first.json() == second.json()
|
||||
Reference in New Issue
Block a user