"""Вложения lab4 через балансировщик (HTTP, только стандартная библиотека). BASE_URL=http://127.0.0.1:8080 python -m unittest discover -s student-tests -p test_attachments.py -v Покрывает уровень 4: слишком большой файл (с Content-Length и chunked без него), повреждённые изображения, подмену MIME, pixel bomb, опасные filename, чужое attachment_id, привязку файла другого чата, неизвестный вариант. Во всех негативных случаях дополнительно проверяется: ответ < 500, общий формат ошибки. """ import hashlib import http.client import sys import unittest import uuid from pathlib import Path from urllib.parse import quote, urlparse sys.path.insert(0, str(Path(__file__).resolve().parent)) import common # noqa: E402 import imagegen as img # noqa: E402 from common import BASE_URL, actor, create_chat, reachable, request # noqa: E402 MAX = 10 * 1024 * 1024 def upload(token, chat_id, data, content_type="image/png", filename="photo.png", headers=None): path = "/api/v1/chats/%s/attachments" % chat_id if filename is not None: path += "?filename=" + quote(filename, safe="") h = {"Content-Type": content_type} h.update(headers or {}) return request("POST", path, token=token, raw=data, headers=h, timeout=60) def message(token, chat_id, text, attachment_ids=None, key=None): body = {"text": text} if attachment_ids is not None: body["attachment_ids"] = attachment_ids return request("POST", "/api/v1/chats/%s/messages" % chat_id, token=token, payload=body, headers={"Idempotency-Key": key or uuid.uuid4().hex}) def raw_get(path, token): """GET, возвращающий байты (common.request декодирует только JSON).""" u = urlparse(BASE_URL) conn = http.client.HTTPConnection(u.hostname, u.port or 80, timeout=30) conn.request("GET", path, headers={"Authorization": "Bearer " + token}) resp = conn.getresponse() data = resp.read() conn.close() return resp.status, resp.headers, data def chunked_upload(token, chat_id, total, piece=256 * 1024): """Transfer-Encoding: chunked без Content-Length: лимит должен сработать по факту чтения.""" u = urlparse(BASE_URL) conn = http.client.HTTPConnection(u.hostname, u.port or 80, timeout=60) path = "/api/v1/chats/%s/attachments?filename=big.png" % chat_id conn.putrequest("POST", path) conn.putheader("Authorization", "Bearer " + token) conn.putheader("Content-Type", "image/png") conn.putheader("Transfer-Encoding", "chunked") conn.endheaders() first = img.PNG_SIG + b"\x00" * (piece - 8) sent = 0 try: while sent < total: part = first if sent == 0 else b"\x00" * min(piece, total - sent) conn.send(b"%x\r\n" % len(part) + part + b"\r\n") sent += len(part) conn.send(b"0\r\n\r\n") except (BrokenPipeError, ConnectionResetError): pass # сервер мог закрыть приём раньше; ответ всё равно читаем resp = conn.getresponse() status = resp.status resp.read() conn.close() return status @unittest.skipUnless(reachable(BASE_URL), "API is not reachable at %s" % BASE_URL) class AttachmentTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.alice = actor("att_a") cls.bob = actor("att_b") cls.eve = actor("att_e") cls.chat = create_chat(cls.alice, cls.bob) cls.eve_chat = create_chat(cls.eve) cls.other_chat = create_chat(cls.alice) # чат Алисы без Боба def assertError(self, r, status, code=None): self.assertEqual(r.status, status, (r.status, r.json)) self.assertLess(r.status, 500) self.assertIsInstance(r.json, dict) self.assertIn("error", r.json) if code: self.assertEqual(r.json["error"]["code"], code) self.assertEqual(r.json["error"]["request_id"], r.headers.get("X-Request-Id")) def upload_ok(self, data, content_type="image/png", filename="photo.png", who=None, chat=None): who = who or self.alice chat = chat or self.chat r = upload(who["_token"], chat["id"], data, content_type, filename) self.assertEqual(r.status, 201, (r.status, r.json)) att = r.json self.assertEqual(att["status"], "ready") self.assertEqual(att["variants"], []) self.assertIsNone(att["error_code"]) self.assertEqual(att["size_bytes"], len(data)) self.assertEqual(att["sha256"], hashlib.sha256(data).hexdigest()) self.assertEqual(att["content_type"], content_type) self.assertEqual(att["chat_id"], chat["id"]) self.assertEqual(att["owner_id"], who["id"]) self.assertNotIn("object_key", att) return att # ------------------------------------------------------------- happy path def test_png_roundtrip_member_reads_outsider_404(self): data = img.sample_png() att = self.upload_ok(data, filename="Отпуск 2026 (1).png") self.assertEqual(att["filename"], "Отпуск 2026 (1).png") meta = request("GET", "/api/v1/attachments/" + att["id"], token=self.bob["_token"]) self.assertEqual(meta.status, 200) self.assertEqual(meta.json, att) status, headers, body = raw_get("/api/v1/attachments/%s/content" % att["id"], self.bob["_token"]) self.assertEqual(status, 200) self.assertEqual(body, data) self.assertEqual(headers.get_content_type(), "image/png") self.assertEqual(int(headers["Content-Length"]), len(data)) self.assertEqual(headers.get("X-Content-Type-Options"), "nosniff") self.assertIn("no-store", headers.get("Cache-Control", "")) for suffix in ("", "/content"): self.assertError(request("GET", "/api/v1/attachments/" + att["id"] + suffix, token=self.eve["_token"]), 404, "not_found") self.assertError(request("GET", "/api/v1/attachments/" + att["id"] + suffix), 401, "unauthorized") def test_jpeg_baseline_and_progressive(self): for data in (img.sample_jpeg(), img.progressive_jpeg()): att = self.upload_ok(data, "image/jpeg", "photo.jpg") status, headers, body = raw_get("/api/v1/attachments/%s/content" % att["id"], self.alice["_token"]) self.assertEqual((status, body, headers.get_content_type()), (200, data, "image/jpeg")) def test_content_type_with_parameters_is_accepted(self): self.upload_ok(img.png(8, 8), "image/png", "x.png") r = upload(self.alice["_token"], self.chat["id"], img.png(8, 8), "image/png; charset=binary", "x.png") self.assertEqual(r.status, 201, r.json) def test_exactly_max_size_is_accepted(self): data = img.png_of_exact_size(MAX) att = self.upload_ok(data, filename="max.png") status, _, body = raw_get("/api/v1/attachments/%s/content" % att["id"], self.bob["_token"]) self.assertEqual(status, 200) self.assertEqual(hashlib.sha256(body).hexdigest(), att["sha256"]) def test_original_variant_and_unknown_variants(self): att = self.upload_ok(img.png(4, 4)) base = "/api/v1/attachments/%s/content" % att["id"] status, _, _ = raw_get(base + "?variant=original", self.bob["_token"]) self.assertEqual(status, 200) self.assertError(request("GET", base + "?variant=thumbnail", token=self.bob["_token"]), 404, "not_found") self.assertError(request("GET", base + "?variant=../../etc", token=self.bob["_token"]), 400, "invalid_request") # ------------------------------------------------------------- limits def test_too_large_with_content_length(self): r = upload(self.alice["_token"], self.chat["id"], img.png_of_exact_size(MAX + 1), filename="big.png") self.assertError(r, 413, "payload_too_large") def test_too_large_chunked_without_content_length(self): status = chunked_upload(self.alice["_token"], self.chat["id"], MAX + 512 * 1024) self.assertEqual(status, 413) def test_too_many_pixels(self): data = img.huge_dimensions_png(8000, 6000) # 48 Мп при размере файла ~50 КБ self.assertLess(len(data), MAX) self.assertError(upload(self.alice["_token"], self.chat["id"], data), 413, "payload_too_large") # ------------------------------------------------------------- content checks def test_corrupted_images_are_415(self): cases = { "truncated png": img.truncated_png(), "bad crc png": img.bad_crc_png(), "png header + random": img.PNG_SIG + uuid.uuid4().bytes * 64, "png with wrong data length": img.wrong_length_png(), "truncated jpeg": img.truncated_jpeg(), "empty body": b"", "plain text": "не картинка".encode(), "gif": b"GIF89a" + b"\x00" * 64, } for name, data in cases.items(): ctype = "image/jpeg" if "jpeg" in name else "image/png" with self.subTest(name): self.assertError(upload(self.alice["_token"], self.chat["id"], data, ctype), 415, "unsupported_media_type") def test_mime_spoofing_is_415(self): cases = [ (img.sample_png(), "image/jpeg"), (img.sample_jpeg(), "image/png"), (img.sample_png(), "image/gif"), (img.sample_png(), "application/octet-stream"), (img.sample_png(), "multipart/form-data; boundary=x"), (img.sample_png(), "text/html"), ] for data, ctype in cases: with self.subTest(ctype): self.assertError(upload(self.alice["_token"], self.chat["id"], data, ctype), 415, "unsupported_media_type") def test_dangerous_filenames_are_400(self): for name in ("../etc/passwd", "a/b.png", "a\\b.png", "nul\x00.png", "bell\x07.png", "new\nline.png", "", " ", ".", "..", "x" * 256, "rtl‮gnp.exe"): with self.subTest(repr(name)): self.assertError(upload(self.alice["_token"], self.chat["id"], img.png(2, 2), filename=name), 400, "invalid_request") self.assertError(upload(self.alice["_token"], self.chat["id"], img.png(2, 2), filename=None), 400, "invalid_request") self.upload_ok(img.png(2, 2), filename="x" * 255) # ------------------------------------------------------------- access def test_outsider_and_unknown_chat_upload_404(self): self.assertError(upload(self.eve["_token"], self.chat["id"], img.png(2, 2)), 404, "not_found") self.assertError(upload(self.alice["_token"], str(uuid.uuid4()), img.png(2, 2)), 404, "not_found") self.assertError(upload(self.alice["_token"], "not-a-uuid", img.png(2, 2)), 400, "invalid_request") self.assertError(upload(None, self.chat["id"], img.png(2, 2)), 401, "unauthorized") def test_invalid_and_unknown_attachment_ids(self): self.assertError(request("GET", "/api/v1/attachments/not-a-uuid", token=self.bob["_token"]), 400) self.assertError(request("GET", "/api/v1/attachments/%s" % uuid.uuid4(), token=self.bob["_token"]), 404) self.assertError(request("GET", "/api/v1/attachments/%s/content" % uuid.uuid4(), token=self.bob["_token"]), 404) # ------------------------------------------------------------- messages def test_message_with_attachments_visible_to_members(self): a1 = self.upload_ok(img.png(3, 3)) a2 = self.upload_ok(img.sample_jpeg(), "image/jpeg", "b.jpg") r = message(self.alice["_token"], self.chat["id"], "", [a2["id"], a1["id"]]) self.assertEqual(r.status, 201, r.json) self.assertEqual(r.json["text"], "") self.assertEqual(r.json["attachment_ids"], [a2["id"], a1["id"]]) # порядок сохранён # Любой участник может сослаться на вложение чата повторно. r2 = message(self.bob["_token"], self.chat["id"], "и снова", [a1["id"]]) self.assertEqual(r2.status, 201, r2.json) page = request("GET", "/api/v1/chats/%s/messages?limit=100" % self.chat["id"], token=self.bob["_token"]) by_id = {m["id"]: m for m in page.json["items"]} self.assertEqual(by_id[r.json["id"]]["attachment_ids"], [a2["id"], a1["id"]]) self.assertEqual(by_id[r2.json["id"]]["attachment_ids"], [a1["id"]]) text_only = message(self.alice["_token"], self.chat["id"], "без вложений") self.assertEqual(text_only.json["attachment_ids"], []) def test_attachment_from_other_chat_is_404_and_nothing_saved(self): foreign = self.upload_ok(img.png(2, 2), chat=self.other_chat) # чат Алисы без Боба key = uuid.uuid4().hex r = message(self.alice["_token"], self.chat["id"], "чужой файл", [foreign["id"]], key=key) self.assertError(r, 404, "not_found") # Отказ не «сжёг» ключ: тот же ключ с корректным телом создаёт сообщение. ok = message(self.alice["_token"], self.chat["id"], "чужой файл", [], key=key) self.assertEqual(ok.status, 201, ok.json) # Посторонняя Ева не может прикрепить файл Алисы в своём чате. a = self.upload_ok(img.png(2, 2)) self.assertError(message(self.eve["_token"], self.eve_chat["id"], "x", [a["id"]]), 404, "not_found") self.assertError(message(self.alice["_token"], self.chat["id"], "x", [str(uuid.uuid4())]), 404) def test_message_attachment_validation(self): a = self.upload_ok(img.png(2, 2)) cid, tok = self.chat["id"], self.alice["_token"] self.assertError(message(tok, cid, ""), 400) self.assertError(message(tok, cid, " "), 400) self.assertError(message(tok, cid, "dup", [a["id"], a["id"]]), 400) self.assertError(message(tok, cid, "many", [str(uuid.uuid4()) for _ in range(11)]), 400) self.assertError(message(tok, cid, "bad", ["not-a-uuid"]), 400) self.assertError(message(tok, cid, "bad", "not-a-list"), 400) r = message(tok, cid, " ", [a["id"]]) # пробельный текст допустим с вложением self.assertEqual(r.status, 201, r.json) def test_idempotency_includes_attachment_ids(self): a1 = self.upload_ok(img.png(2, 2)) a2 = self.upload_ok(img.png(3, 3)) key = uuid.uuid4().hex first = message(self.alice["_token"], self.chat["id"], "пара", [a1["id"], a2["id"]], key=key) self.assertEqual(first.status, 201) replay = message(self.alice["_token"], self.chat["id"], "пара", [a1["id"], a2["id"]], key=key) self.assertEqual((replay.status, replay.json), (201, first.json)) self.assertError(message(self.alice["_token"], self.chat["id"], "пара", [a2["id"], a1["id"]], key=key), 409) self.assertError(message(self.alice["_token"], self.chat["id"], "пара", [a1["id"]], key=key), 409) # Отсутствующее поле == [] для идемпотентности. k2 = uuid.uuid4().hex m1 = message(self.alice["_token"], self.chat["id"], "без поля", None, key=k2) m2 = message(self.alice["_token"], self.chat["id"], "без поля", [], key=k2) self.assertEqual((m2.status, m2.json), (201, m1.json)) if __name__ == "__main__": unittest.main()