forked from mtusi2026/mtusi2026lab5
113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
"""Неверный JSON и неверная структура тела — 400 invalid_request"""
|
|
|
|
import httpx
|
|
import pytest
|
|
from helpers import assert_error, auth, message_headers
|
|
|
|
PASSWORD = "Valid-password-123"
|
|
|
|
MALFORMED_BODIES = [
|
|
b'{"username": "abc",',
|
|
b"not json at all",
|
|
b'{"text": "\xff\xfe"}',
|
|
b"",
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("raw", MALFORMED_BODIES)
|
|
def test_malformed_json_on_register(api: httpx.Client, raw: bytes) -> None:
|
|
response = api.post(
|
|
"/api/v1/users", content=raw, headers={"Content-Type": "application/json"}
|
|
)
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
@pytest.mark.parametrize("raw", MALFORMED_BODIES)
|
|
def test_malformed_json_on_create_chat_and_message(
|
|
api: httpx.Client, make_user, make_chat, raw: bytes
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
headers = {**auth(alice), "Content-Type": "application/json"}
|
|
assert_error(
|
|
api.post("/api/v1/chats", content=raw, headers=headers), 400, "invalid_request"
|
|
)
|
|
message_post_headers = {
|
|
**message_headers(alice),
|
|
"Content-Type": "application/json",
|
|
}
|
|
assert_error(
|
|
api.post(
|
|
f"/api/v1/chats/{chat['id']}/messages",
|
|
content=raw,
|
|
headers=message_post_headers,
|
|
),
|
|
400,
|
|
"invalid_request",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body",
|
|
[
|
|
[],
|
|
"alice",
|
|
{"username": "valid_name", "password": PASSWORD},
|
|
{"username": 123, "display_name": "x", "password": PASSWORD},
|
|
{"username": "valid_name", "display_name": None, "password": PASSWORD},
|
|
{
|
|
"username": "valid_name",
|
|
"display_name": "x",
|
|
"password": PASSWORD,
|
|
"is_admin": True,
|
|
},
|
|
{"username": "Upper_Case", "display_name": "x", "password": PASSWORD},
|
|
{"username": "ab", "display_name": "x", "password": PASSWORD},
|
|
{"username": "valid_name", "display_name": " ", "password": PASSWORD},
|
|
{"username": "valid_name", "display_name": "я" * 101, "password": PASSWORD},
|
|
{"username": "valid_name", "display_name": "a\u0000b", "password": PASSWORD},
|
|
],
|
|
)
|
|
def test_invalid_user_payload(api: httpx.Client, body: object) -> None:
|
|
assert_error(api.post("/api/v1/users", json=body), 400, "invalid_request")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body",
|
|
[
|
|
{"text": 42},
|
|
{"text": "привет", "sender_id": "11111111-1111-4111-8111-111111111111"},
|
|
{},
|
|
],
|
|
)
|
|
def test_invalid_message_payload(
|
|
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_invalid_chat_payload(api: httpx.Client, make_user) -> None:
|
|
alice = make_user("alice")
|
|
bob = make_user("bob")
|
|
cases = [
|
|
{"title": "t", "member_ids": [bob["id"]]},
|
|
{"title": "t", "member_ids": [alice["id"], alice["id"]]},
|
|
{
|
|
"title": "t",
|
|
"member_ids": [alice["id"], "11111111-1111-4111-8111-111111111111"],
|
|
},
|
|
{"title": "t", "member_ids": [alice["id"], "not-a-uuid"]},
|
|
{"title": "t", "member_ids": []},
|
|
{"title": " ", "member_ids": [alice["id"]]},
|
|
]
|
|
for body in cases:
|
|
response = api.post("/api/v1/chats", json=body, headers=auth(alice))
|
|
assert_error(response, 400, "invalid_request")
|