Files
mtusi2026lab2/student-tests/test_messages.py
T

60 lines
2.1 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Пустые сообщения, границы длины и Unicode"""
import httpx
import pytest
from helpers import assert_error, auth
@pytest.mark.parametrize("text", ["", " ", " \n\t ", " "])
def test_empty_or_blank_message_is_rejected(
api: httpx.Client, make_user, make_chat, text: str
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = api.post(
f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=auth(alice)
)
assert_error(response, 400, "invalid_request")
history = api.get(f"/api/v1/chats/{chat['id']}/messages", headers=auth(alice))
assert history.json()["items"] == []
def test_message_length_is_counted_in_code_points(
api: httpx.Client, make_user, make_chat, send_message
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
longest = "😀" * 4000
assert send_message(alice, chat, longest)["text"] == longest
response = api.post(
f"/api/v1/chats/{chat['id']}/messages",
json={"text": "😀" * 4001},
headers=auth(alice),
)
assert_error(response, 400, "invalid_request")
def test_unicode_is_preserved(
api: httpx.Client, make_user, make_chat, send_message
) -> None:
name = "Алиса 👩‍💻 é"
alice = make_user("alice", display_name=name)
assert alice["display_name"] == name
chat = make_chat(alice, [alice], title="Чат 🚀 中文")
assert chat["title"] == "Чат 🚀 中文"
text = "Привет, мир 👋 — «кавычки» \u0000 и 𝔘𝔫𝔦𝔠𝔬𝔡𝔢"
message = send_message(alice, chat, text)
assert message["text"] == text
page = api.get(f"/api/v1/chats/{chat['id']}/messages", headers=auth(alice)).json()
assert page["items"][0]["text"] == text
def test_sender_is_taken_from_x_user_id(make_user, make_chat, send_message) -> None:
alice = make_user("alice")
bob = make_user("bob")
chat = make_chat(alice, [alice, bob])
message = send_message(bob, chat, "от Боба")
assert message["sender_id"] == bob["id"]
assert message["chat_id"] == chat["id"]
assert message["created_at"].endswith("Z")