67 lines
2.8 KiB
Python
67 lines
2.8 KiB
Python
from conftest import assert_error
|
|
|
|
|
|
def test_empty_message_returns_400(client, chat, alice):
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
headers = alice["auth"]
|
|
|
|
assert_error(client.post(path, headers=headers, json={"text": ""}), 400, "invalid_request")
|
|
assert_error(client.post(path, headers=headers, json={"text": " \n\t"}), 400, "invalid_request")
|
|
|
|
|
|
def test_sender_is_taken_from_token(client, chat, alice, bob):
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
response = client.post(path, headers=bob["auth"], json={"text": "Привет 👋"})
|
|
assert response.status_code == 201
|
|
assert response.json()["sender_id"] == bob["id"]
|
|
assert response.json()["text"] == "Привет 👋"
|
|
|
|
spoof = client.post(path, headers=bob["auth"], json={"text": "x", "sender_id": alice["id"]})
|
|
assert_error(spoof, 400, "invalid_request")
|
|
|
|
|
|
def test_pagination_over_several_pages(client, chat, alice):
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
headers = alice["auth"]
|
|
sent = [client.post(path, headers=headers, json={"text": f"сообщение {i}"}).json() for i in range(7)]
|
|
|
|
received = []
|
|
pages = 0
|
|
cursor = None
|
|
while True:
|
|
params = {"limit": 3}
|
|
if cursor:
|
|
params["cursor"] = cursor
|
|
page = client.get(path, headers=headers, params=params).json()
|
|
received += page["items"]
|
|
pages += 1
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
|
|
assert pages == 3 # 3 + 3 + 1
|
|
assert received == sent # тот же порядок, ничего не потеряно
|
|
assert len({m["id"] for m in received}) == 7 # без повторов
|
|
|
|
|
|
def test_cursor_from_another_chat_returns_400(client, chat, alice):
|
|
headers = alice["auth"]
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
for i in range(2):
|
|
client.post(path, headers=headers, json={"text": f"m{i}"})
|
|
cursor = client.get(path, headers=headers, params={"limit": 1}).json()["next_cursor"]
|
|
|
|
other = client.post("/api/v1/chats", headers=headers, json={"title": "Другой", "member_ids": [alice["id"]]}).json()
|
|
response = client.get("/api/v1/chats/" + other["id"] + "/messages", headers=headers, params={"cursor": cursor})
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
response = client.get(path, headers=headers, params={"cursor": "мусор"})
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
def test_invalid_limit_returns_400(client, chat, alice):
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
for limit in (0, 101, "abc"):
|
|
response = client.get(path, headers=alice["auth"], params={"limit": limit})
|
|
assert_error(response, 400, "invalid_request")
|