solution
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Текст сообщения: пустые строки, длина и Unicode."""
|
||||
|
||||
import pytest
|
||||
|
||||
from conftest import assert_error
|
||||
|
||||
|
||||
@pytest.mark.parametrize("text", ["", " ", " ", "\n", "\t \n", " ", " "])
|
||||
def test_blank_text_is_rejected(client, alice, chat, text):
|
||||
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
assert_error(
|
||||
client.post(url, json={"text": text}, headers=alice.headers),
|
||||
400,
|
||||
"invalid_request",
|
||||
)
|
||||
assert client.get(url, headers=alice.headers).json()["items"] == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"body", [{}, {"text": None}, {"text": 123}, {"text": ["a"]}, {"message": "hi"}]
|
||||
)
|
||||
def test_missing_or_wrong_type_text_is_rejected(client, alice, chat, body):
|
||||
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
assert_error(
|
||||
client.post(url, json=body, headers=alice.headers), 400, "invalid_request"
|
||||
)
|
||||
|
||||
|
||||
def test_text_length_is_counted_in_characters(client, alice, chat):
|
||||
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
assert (
|
||||
client.post(url, json={"text": "я" * 4000}, headers=alice.headers).status_code
|
||||
== 201
|
||||
)
|
||||
assert (
|
||||
client.post(url, json={"text": "😀" * 4000}, headers=alice.headers).status_code
|
||||
== 201
|
||||
)
|
||||
assert_error(
|
||||
client.post(url, json={"text": "я" * 4001}, headers=alice.headers),
|
||||
400,
|
||||
"invalid_request",
|
||||
)
|
||||
|
||||
|
||||
def test_nul_character_is_400_not_500(client, alice, chat):
|
||||
"""Символ NUL отклоняется с 400."""
|
||||
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
assert_error(
|
||||
client.post(url, json={"text": "a\x00b"}, headers=alice.headers),
|
||||
400,
|
||||
"invalid_request",
|
||||
)
|
||||
|
||||
|
||||
def test_unicode_survives_the_database(client, alice, bob, chat):
|
||||
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
text = " Привет, мир 👋 你好 ✓ é "
|
||||
sent = client.post(url, json={"text": text}, headers=alice.headers)
|
||||
assert sent.status_code == 201 and sent.json()["text"] == text
|
||||
assert client.get(url, headers=bob.headers).json()["items"][0]["text"] == text
|
||||
assert sent.headers["content-type"].split(";")[0] == "application/json"
|
||||
|
||||
|
||||
def test_message_has_correct_fields(client, alice, chat):
|
||||
data = client.post(
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
json={"text": "hi"},
|
||||
headers=alice.headers,
|
||||
).json()
|
||||
assert set(data) == {"id", "chat_id", "sender_id", "text", "created_at"}
|
||||
assert data["chat_id"] == chat["id"] and data["sender_id"] == alice.id
|
||||
assert data["created_at"].endswith("Z")
|
||||
Reference in New Issue
Block a user