#!/usr/bin/env python3 """Negative attachment checks required for the level-4 assessment.""" import hashlib from pathlib import Path import uuid from client import Client, arguments, check, run def main(): args = arguments("lab4 level-4 attachment checks") client = Client(args.base_url, args.ca_file) alice, bob, eve, chat = client.bootstrap() second_chat = client.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] sample = Path(__file__).with_name("fixtures").joinpath("sample.png").read_bytes() client.call( "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=large.png", 413, actor=alice, raw=b"x" * (10 * 1024 * 1024 + 1), headers={"Content-Type": "image/png"}, ) client.call( "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=broken.png", 415, actor=alice, raw=b"not an image", headers={"Content-Type": "image/png"}, ) client.call( "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=spoof.jpg", 415, actor=alice, raw=sample, headers={"Content-Type": "image/jpeg"}, ) client.call( "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=..%2Fsecret.png", 400, actor=alice, raw=sample, headers={"Content-Type": "image/png"}, ) attachment, content = client.attachment(alice, chat) check(hashlib.sha256(content).hexdigest() == attachment["sha256"], "fixture checksum changed") client.call( "GET", f"/api/v1/attachments/{attachment['id']}", 404, actor=eve, ) client.call( "POST", f"/api/v1/chats/{second_chat['id']}/messages", 404, actor=alice, body={"text": "wrong chat", "attachment_ids": [attachment["id"]]}, headers={"Idempotency-Key": uuid.uuid4().hex}, ) print("PASS: lab4 level-4 attachment validation and access checks") if __name__ == "__main__": run(main)