Files
mtusi2026lab4/student-tests/test_attachments.py
T

63 lines
3.4 KiB
Python

"""Negative and access-control checks specific to lab4 attachments."""
from __future__ import annotations
import hashlib
from pathlib import Path
import unittest
import uuid
import common
PNG = (Path(__file__).resolve().parents[1] / "tests" / "fixtures" / "sample.png").read_bytes()
class Attachments(unittest.TestCase):
def upload(self, token: str, chat_id: str, raw: bytes = PNG, content_type: str = "image/png"):
return common.request(
"POST", "/api/v1/chats/" + chat_id + "/attachments?filename=sample.png",
raw=raw, token=token, content_type=content_type,
)
def test_upload_download_and_foreign_access_are_scoped_to_chat(self) -> None:
alice = common.actor("attachalice")
bob = common.actor("attachbob")
eve = common.actor("attacheve")
chat = common.create_chat(alice.token, [alice.id, bob.id], "Attachment scope")
uploaded = self.upload(alice.token, chat["id"])
self.assertEqual(uploaded.status, 201, uploaded.json)
attachment = uploaded.json
self.assertEqual(attachment["sha256"], hashlib.sha256(PNG).hexdigest())
downloaded = common.request("GET", "/api/v1/attachments/" + attachment["id"] + "/content", token=bob.token)
self.assertEqual(downloaded.status, 200)
self.assertEqual(downloaded.data, PNG)
common.expect_error(self, common.request("GET", "/api/v1/attachments/" + attachment["id"], token=eve.token), 404, "not_found")
common.expect_error(self, common.request("GET", "/api/v1/attachments/" + attachment["id"] + "/content", token=eve.token), 404, "not_found")
def test_rejects_bad_filename_mime_corruption_and_oversize_declaration(self) -> None:
alice = common.actor("attachinvalid")
chat = common.create_chat(alice.token, [alice.id], "Attachment validation")
base = "/api/v1/chats/" + chat["id"] + "/attachments"
common.expect_error(self, common.request("POST", base + "?filename=../secret.png", raw=PNG, token=alice.token, content_type="image/png"), 400, "invalid_request")
common.expect_error(self, common.request("POST", base + "?filename=x.png", raw=b"not an image", token=alice.token, content_type="image/png"), 415, "unsupported_media_type")
common.expect_error(self, common.request("POST", base + "?filename=x.jpg", raw=PNG, token=alice.token, content_type="image/jpeg"), 415, "unsupported_media_type")
too_large = common.request(
"POST", base + "?filename=large.png",
raw=PNG + b"x" * (10_485_761 - len(PNG)), token=alice.token,
content_type="image/png", timeout=60,
)
common.expect_error(self, too_large, 413, "payload_too_large")
def test_attachment_cannot_be_bound_to_another_chat(self) -> None:
alice = common.actor("attachcross")
source_chat = common.create_chat(alice.token, [alice.id], "Attachment source")
target_chat = common.create_chat(alice.token, [alice.id], "Attachment target")
uploaded = self.upload(alice.token, source_chat["id"])
self.assertEqual(uploaded.status, 201, uploaded.json)
rejected = common.request(
"POST", "/api/v1/chats/" + target_chat["id"] + "/messages",
body={"text": "", "attachment_ids": [uploaded.json["id"]]}, token=alice.token,
headers={"Idempotency-Key": uuid.uuid4().hex},
)
common.expect_error(self, rejected, 404, "not_found")