"""Негативные проверки API. Токены и пароли не печатаются.""" import json import struct import sys import uuid import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure # noqa: E402 def png(width, height, payload=b""): def chunk(tag, data): return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF) ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + payload + chunk(b"IEND", b"") def main(): base = sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:8080" client = Client(base) alice = client.user("n1") bob = client.user("n2") eve = client.user("n3") chat = client.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "Негативные", "member_ids": [alice["id"].upper(), bob["id"]]}, schema="Chat", )["json"] other = client.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "Другой", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] upper = client.call("GET", "/api/v1/chats/" + chat["id"].upper(), actor=bob, schema="Chat")["json"] results = [] def record(name, status): results.append({"name": name, "status": status}) record("chat_uuid_case", 200 if upper["id"] == chat["id"] else 0) path = "/api/v1/chats/" + chat["id"] + "/messages" record("blank_text", client.call("POST", path, 400, actor=alice, body={"text": " "}, headers={"Idempotency-Key": "blank-1"})["status"]) record("bad_json", client.call("POST", path, 400, actor=alice, raw=b"{", headers={"Content-Type": "application/json", "Idempotency-Key": "bad-json"})["status"]) record("short_password", client.call("POST", "/api/v1/users", 400, body={"username": "abc", "display_name": "Имя", "password": "short"})["status"]) key = uuid.uuid4().hex first = client.call("POST", path, 201, actor=alice, body={"text": "одно"}, headers={"Idempotency-Key": key}, schema="Message")["json"] second = client.call( "POST", path, 201, actor=alice, body={"text": "одно", "attachment_ids": []}, headers={"Idempotency-Key": key}, schema="Message", )["json"] record("omitted_equals_empty", 201 if first == second else 0) record("same_key_other_body", client.call("POST", path, 409, actor=alice, body={"text": "другое"}, headers={"Idempotency-Key": key})["status"]) history = client.call("GET", path.replace("/messages", "/messages?limit=100"), actor=bob, schema="MessagePage")["json"] record("no_second_row", 200 if sum(1 for item in history["items"] if item["text"] in ("одно", "другое")) == 1 else 0) other_path = "/api/v1/chats/" + other["id"] + "/messages" other_message = client.call( "POST", other_path, 201, actor=alice, body={"text": "одно"}, headers={"Idempotency-Key": key}, schema="Message", )["json"] record("same_key_other_chat", 201 if other_message["id"] != first["id"] else 0) record("foreign_chat", client.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)["status"]) record("bad_cursor", client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?cursor=not-a-cursor", 400, actor=alice)["status"]) upload = "/api/v1/chats/" + chat["id"] + "/attachments" record("slash_name", client.call("POST", upload + "?filename=a/b.png", 400, actor=alice, raw=png(1, 1), headers={"Content-Type": "image/png"})["status"]) record("huge_pixels", client.call("POST", upload + "?filename=big.png", 413, actor=alice, raw=png(8000, 8000), headers={"Content-Type": "image/png"})["status"]) record("bad_png", client.call("POST", upload + "?filename=bad.png", 415, actor=alice, raw=b"\x89PNG\r\n\x1a\nbroken", headers={"Content-Type": "image/png"})["status"]) record("type_mismatch", client.call("POST", upload + "?filename=no.jpg", 415, actor=alice, raw=png(1, 1), headers={"Content-Type": "image/jpeg"})["status"]) record("oversize", client.call("POST", upload + "?filename=over.png", 413, actor=alice, raw=b"x" * (10 * 1024 * 1024 + 1), headers={"Content-Type": "image/png"})["status"]) missing = str(uuid.uuid4()) record("unknown_attachment", client.call("GET", "/api/v1/attachments/" + missing.upper(), 404, actor=alice)["status"]) record("no_token", client.call("GET", "/api/v1/users/me", 401)["status"]) body = json.dumps({"results": results}, ensure_ascii=False, indent=2) print(body) failed = [item for item in results if item["status"] not in (200, 201, 400, 401, 404, 409, 413, 415)] if failed: return 1 expected = { "chat_uuid_case": 200, "blank_text": 400, "bad_json": 400, "short_password": 400, "omitted_equals_empty": 201, "same_key_other_body": 409, "no_second_row": 200, "same_key_other_chat": 201, "foreign_chat": 404, "bad_cursor": 400, "slash_name": 400, "huge_pixels": 413, "bad_png": 415, "type_mismatch": 415, "oversize": 413, "unknown_attachment": 404, "no_token": 401, } for name, status in expected.items(): got = next(item["status"] for item in results if item["name"] == name) if got != status: print("mismatch " + name, file=sys.stderr) return 1 print("PASS negative") return 0 if __name__ == "__main__": try: raise SystemExit(main()) except Failure as exc: print("FAIL " + str(exc), file=sys.stderr) raise SystemExit(1)