"""Attachment tests for lab4 (grade 4): roundtrip, access control, limits.""" from __future__ import annotations import hashlib import os from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SAMPLE = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes() def _assert_error(response, status, code): assert response.status == status, (response.status, response.body) assert response.content_type == "application/json" payload = response.json assert payload["error"]["code"] == code assert payload["error"]["request_id"] == response.request_id def _fresh_actor(client, label): from conftest import _register_and_login return _register_and_login(client, label) def _peer_chat(client, actor): peer = _fresh_actor(client, "att_peer") response = client.create_chat(actor["token"], [actor["id"], peer["id"]], title="Attachment chat") assert response.status == 201, response.body return response.json, peer def test_upload_download_roundtrip(client, alice, bob, chat): upload = client.upload_attachment(alice["token"], chat["id"], SAMPLE, filename="sample.png") assert upload.status == 201, upload.body att = upload.json assert att["status"] == "ready" assert att["error_code"] is None assert att["variants"] == [] assert att["chat_id"] == chat["id"] and att["owner_id"] == alice["id"] assert att["content_type"] == "image/png" assert att["size_bytes"] == len(SAMPLE) assert att["sha256"] == hashlib.sha256(SAMPLE).hexdigest() meta = client.get_attachment(bob["token"], att["id"]) assert meta.status == 200, meta.body assert meta.json["id"] == att["id"] download = client.download(bob["token"], att["id"]) assert download.status == 200, download.body assert download.body == SAMPLE assert download.content_type == "image/png" assert download.headers.get("Location") is None, "download must not redirect" # A message may reference the attachment of its own chat, even with empty text. message = client.send(alice["token"], chat["id"], "", attachments=[att["id"]]) assert message.status == 201, message.body assert message.json["attachment_ids"] == [att["id"]] assert message.json["text"] == "" def test_foreign_actor_gets_404(client, alice, bob, chat): att = client.upload_attachment(alice["token"], chat["id"], SAMPLE).json stranger = _fresh_actor(client, "foreign") _assert_error(client.get_attachment(stranger["token"], att["id"]), 404, "not_found") _assert_error(client.download(stranger["token"], att["id"]), 404, "not_found") def test_cross_chat_attachment_is_404(client, alice, bob, chat): other_chat, peer = _peer_chat(client, alice) foreign = client.upload_attachment(alice["token"], other_chat["id"], SAMPLE).json # Referencing an attachment that belongs to another chat is not allowed. response = client.send(alice["token"], chat["id"], "", attachments=[foreign["id"]]) _assert_error(response, 404, "not_found") def test_oversized_upload_is_413(client, alice, chat): too_big = b"\x89PNG\r\n\x1a\n" + b"\x00" * (10 * 1024 * 1024 + 1) response = client.upload_attachment(alice["token"], chat["id"], too_big) _assert_error(response, 413, "payload_too_large") def test_corrupt_image_is_415(client, alice, chat): corrupt = b"\x89PNG\r\n\x1a\n" + os.urandom(256) response = client.upload_attachment(alice["token"], chat["id"], corrupt) _assert_error(response, 415, "unsupported_media_type") def test_mime_spoof_is_415(client, alice, chat): # GIF bytes claimed to be a PNG: the real format decides. spoof = b"GIF89a" + os.urandom(64) response = client.upload_attachment( alice["token"], chat["id"], spoof, content_type="image/png" ) _assert_error(response, 415, "unsupported_media_type") def test_empty_upload_is_415(client, alice, chat): _assert_error(client.upload_attachment(alice["token"], chat["id"], b""), 415, "unsupported_media_type") def test_unsafe_filename_is_400(client, alice, chat): _assert_error( client.upload_attachment(alice["token"], chat["id"], SAMPLE, filename="a/b.png"), 400, "invalid_request", ) def test_empty_text_without_attachment_is_400(client, alice, chat): _assert_error(client.send(alice["token"], chat["id"], ""), 400, "invalid_request") def test_message_idempotency_includes_attachments(client, alice, chat): att = client.upload_attachment(alice["token"], chat["id"], SAMPLE).json key = os.urandom(16).hex() first = client.send(alice["token"], chat["id"], "with file", key=key, attachments=[att["id"]]) assert first.status == 201, first.body replay = client.send(alice["token"], chat["id"], "with file", key=key, attachments=[att["id"]]) assert replay.status == 201, replay.body assert replay.json == first.json, "idempotent replay differs" # Same key, different body -> conflict. from conftest import _register_and_login # noqa: F401 (kept for symmetry) conflict = client.request( "POST", f"/api/v1/chats/{chat['id']}/messages", body={"text": "different"}, token=alice["token"], headers={"Idempotency-Key": key}, ) _assert_error(conflict, 409, "conflict")