"""Некорректный JSON и поля: 400, 413 и 415.""" import uuid import pytest from conftest import assert_error JSON = {"Content-Type": "application/json"} PASSWORD = "long-enough-password" def valid(**changes) -> dict: """Корректное тело регистрации с изменёнными полями.""" body = {"username": "j_" + uuid.uuid4().hex[:10], "display_name": "Имя", "password": PASSWORD} body.update(changes) return {key: value for key, value in body.items() if value is not None} @pytest.mark.parametrize( "raw", [ b"{", b"not json at all", b"", b'{"username": "abc",}', b"\xff\xfe\x00", b'{"username": NaN}', b'["username"]', b'"text"', b"null", ], ) def test_registration_rejects_broken_json(client, raw): assert_error(client.post("/api/v1/users", content=raw, headers=JSON), 400, "invalid_request") @pytest.mark.parametrize( "changes", [ {"username": None}, {"display_name": None}, {"password": None}, {"username": 123}, {"display_name": 5}, {"display_name": None, "extra_field": 1}, {"role": "admin"}, {"username": "ab"}, {"username": "a" * 33}, {"username": "Alice"}, {"username": "al ice"}, {"username": "алиса"}, {"username": "alice\n"}, {"display_name": ""}, {"display_name": " "}, {"display_name": "я" * 101}, {"display_name": "a\x00b"}, ], ) def test_registration_rejects_bad_fields(client, changes): assert_error(client.post("/api/v1/users", json=valid(**changes)), 400, "invalid_request") def test_border_values_are_accepted(client): assert client.post("/api/v1/users", json=valid(display_name="я" * 100)).status_code == 201 assert client.post("/api/v1/users", json=valid(username=uuid.uuid4().hex, display_name="😀")).status_code == 201 def test_lone_surrogate_is_400_not_500(client): raw = b'{"username": "abcdef", "display_name": "\\ud800", "password": "long-enough-password"}' assert_error(client.post("/api/v1/users", content=raw, headers=JSON), 400, "invalid_request") def test_wrong_or_missing_content_type_is_415(client): body = b'{"username":"abc","display_name":"x","password":"long-enough-password"}' assert_error(client.post("/api/v1/users", content=body, headers={"Content-Type": "text/plain"}), 415, "unsupported_media_type") assert_error(client.post("/api/v1/users", content=body), 415, "unsupported_media_type") def test_huge_body_is_413(client): big = b'{"username": "abc", "display_name": "' + b"x" * (300 * 1024) + b'"}' assert_error(client.post("/api/v1/users", content=big, headers=JSON), 413, "payload_too_large") def test_broken_json_in_message_and_chat(client, alice, chat): headers = {**JSON, **alice.headers} assert_error(client.post("/api/v1/chats/" + chat["id"] + "/messages", content=b"{oops", headers=headers), 400, "invalid_request") assert_error(client.post("/api/v1/chats", content=b"[", headers=headers), 400, "invalid_request") def test_sender_id_in_message_is_rejected(client, alice, bob, chat): """Поле sender_id в теле сообщения запрещено.""" reply = client.post( "/api/v1/chats/" + chat["id"] + "/messages", json={"text": "привет", "sender_id": bob.id}, headers=alice.headers ) assert_error(reply, 400, "invalid_request") def test_chat_body_validation(client, alice, bob): unknown = str(uuid.uuid4()) cases = [ {"title": "x", "member_ids": []}, {"title": "", "member_ids": [alice.id]}, {"title": " ", "member_ids": [alice.id]}, {"title": "x" * 101, "member_ids": [alice.id]}, {"title": "x", "member_ids": [alice.id, alice.id]}, {"title": "x", "member_ids": [alice.id, unknown]}, {"title": "x", "member_ids": [bob.id]}, {"title": "x", "member_ids": ["не-uuid"]}, {"title": "x", "member_ids": alice.id}, {"title": "x"}, {"title": "x", "member_ids": [alice.id], "extra": 1}, ] for body in cases: assert_error(client.post("/api/v1/chats", json=body, headers=alice.headers), 400, "invalid_request")