Files
mtusi2026lab4/student-tests/test_attachments.py
T

195 lines
5.8 KiB
Python

import hashlib
import io
from pathlib import Path
from uuid import uuid4
import pytest
from conftest import check, headers
from PIL import Image
PNG = (Path(__file__).parents[1] / "tests/fixtures/sample.png").read_bytes()
def upload(
client, actor, chat, *, data=PNG, filename="Фото 🌍.png", mime="image/png"
):
return client.post(
f"/api/v1/chats/{chat['id']}/attachments",
headers={**headers(actor), "Content-Type": mime},
params={"filename": filename},
content=data,
)
def test_attachment_roundtrip_acl_and_message(client, actors, chat):
attachment = check(upload(client, actors[0], chat), 201, "Attachment")
assert attachment["status"] == "ready" and attachment["variants"] == []
assert attachment["sha256"] == hashlib.sha256(PNG).hexdigest()
assert attachment["size_bytes"] == len(PNG)
path = f"/api/v1/attachments/{attachment['id']}"
assert (
check(client.get(path, headers=headers(actors[1])), 200, "Attachment")
== attachment
)
downloaded = client.get(path + "/content", headers=headers(actors[1]))
assert downloaded.status_code == 200 and downloaded.content == PNG
assert downloaded.headers["Content-Type"] == "image/png"
assert not downloaded.history
for suffix in ["", "/content"]:
check(
client.get(path + suffix, headers=headers(actors[2])), 404, "Error"
)
check(client.get(path + suffix), 401, "Error")
check(
client.get(
path + "/content",
headers=headers(actors[1]),
params={"variant": "thumbnail"},
),
404,
"Error",
)
sent = check(
client.post(
f"/api/v1/chats/{chat['id']}/messages",
headers=headers(actors[1]),
json={"text": "", "attachment_ids": [attachment["id"]]},
),
201,
"Message",
)
assert sent["attachment_ids"] == [attachment["id"]]
assert sent["sender_id"] == actors[1]["id"]
@pytest.mark.parametrize(
"name",
[
"../x.png",
"x/y.png",
"x\\y.png",
"x\x00.png",
"x\x7f.png",
"x\n.png",
"a" * 256,
"",
],
)
def test_filename_guard(client, actors, chat, name):
check(upload(client, actors[0], chat, filename=name), 400, "Error")
@pytest.mark.parametrize(
"data,mime,status",
[
(b"not an image", "image/png", 415),
(PNG, "image/jpeg", 415),
(PNG, "application/octet-stream", 415),
(PNG[:25], "image/png", 415),
(b"", "image/png", 415),
(b"x" * (10 * 1024 * 1024 + 1), "image/png", 413),
],
)
def test_image_guards(client, actors, chat, data, mime, status):
r = upload(client, actors[0], chat, data=data, mime=mime)
assert r.status_code == status
assert r.headers["x-instance-id"] and r.headers["x-request-id"]
assert r.json()["error"]["request_id"] == r.headers["x-request-id"]
def test_jpeg_original_unchanged(client, actors, chat):
stream = io.BytesIO()
Image.new("RGB", (320, 200), (30, 60, 90)).save(stream, format="JPEG")
jpeg = stream.getvalue()
attachment = check(
upload(
client,
actors[0],
chat,
data=jpeg,
filename="camera.jpg",
mime="image/jpeg",
),
201,
"Attachment",
)
r = client.get(
f"/api/v1/attachments/{attachment['id']}/content",
headers=headers(actors[1]),
)
assert r.content == jpeg
def test_cross_chat_link_and_unknown(client, actors, chat):
a = check(upload(client, actors[0], chat), 201, "Attachment")
other = check(
client.post(
"/api/v1/chats",
headers=headers(actors[0]),
json={"title": "Other", "member_ids": [actors[0]["id"]]},
),
201,
"Chat",
)
for identity in [a["id"], str(uuid4())]:
check(
client.post(
f"/api/v1/chats/{other['id']}/messages",
headers=headers(actors[0]),
json={"text": "", "attachment_ids": [identity]},
),
404,
"Error",
)
check(
client.post(
f"/api/v1/chats/{chat['id']}/messages",
headers=headers(actors[0]),
json={"text": "", "attachment_ids": [a["id"], a["id"]]},
),
400,
"Error",
)
def test_default_attachments_are_semantically_equal(client, actors, chat):
h = headers(actors[0])
path = f"/api/v1/chats/{chat['id']}/messages"
first = check(
client.post(path, headers=h, json={"text": "Semantic default"}),
201,
"Message",
)
second = check(
client.post(
path,
headers=h,
json={"attachment_ids": [], "text": "Semantic default"},
),
201,
"Message",
)
assert first == second and first["attachment_ids"] == []
def test_pixel_limit_before_decode(client, actors, chat):
# Compressible synthetic image stays below10MiB but exceeds40M pixels.
stream = io.BytesIO()
Image.new("1", (8000, 5001)).save(stream, format="PNG")
assert len(stream.getvalue()) < 10 * 1024 * 1024
r = upload(client, actors[0], chat, data=stream.getvalue())
assert r.status_code == 413
def test_chunked_body_limit_without_content_length(client, actors, chat):
r = client.post(
f"/api/v1/chats/{chat['id']}/attachments",
headers={**headers(actors[0]), "Content-Type": "image/png"},
params={"filename": "chunked.png"},
content=iter([b"x" * (10 * 1024 * 1024 + 1)]),
)
assert r.request.headers.get("content-length") is None
assert r.status_code == 413
assert r.json()["error"]["code"] == "payload_too_large"
assert r.json()["error"]["request_id"] == r.headers["x-request-id"]