"""Пустые сообщения, границы длины и 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 = "Привет, мир 👋 — «кавычки» \t\n и 𝔘𝔫𝔦𝔠𝔬𝔡𝔢" 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_nul_character_is_rejected(api: httpx.Client, make_user, make_chat) -> None: """PostgreSQL text не хранит U+0000: отказ 400 до записи, а не 500""" alice = make_user("alice") chat = make_chat(alice, [alice]) response = api.post( f"/api/v1/chats/{chat['id']}/messages", json={"text": "до\u0000после"}, headers=auth(alice), ) assert_error(response, 400, "invalid_request") response = api.post( "/api/v1/chats", json={"title": "a\u0000b", "member_ids": [alice["id"]]}, headers=auth(alice), ) assert_error(response, 400, "invalid_request") def test_sender_is_taken_from_session(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")