134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
"""Attachment tests for lab5: async accept (202), variants, access control.
|
|
|
|
Upload now returns ``202 queued``; readiness is polled via GET metadata.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import os
|
|
import time
|
|
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 wait_ready(client, token, attachment_id, timeout=60):
|
|
deadline = time.monotonic() + timeout
|
|
last = None
|
|
while time.monotonic() < deadline:
|
|
last = client.get_attachment(token, attachment_id).json
|
|
if last["status"] == "failed":
|
|
raise AssertionError(f"attachment failed: {last.get('error_code')}")
|
|
if last["status"] == "ready":
|
|
return last
|
|
time.sleep(0.3)
|
|
raise AssertionError(f"attachment not ready in time: {last}")
|
|
|
|
|
|
def test_upload_is_202_queued_then_ready(client, alice, bob, chat):
|
|
upload = client.upload_attachment(alice["token"], chat["id"], SAMPLE, filename="sample.png")
|
|
assert upload.status == 202, upload.body
|
|
att = upload.json
|
|
assert att["status"] == "queued"
|
|
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()
|
|
|
|
ready = wait_ready(client, bob["token"], att["id"])
|
|
assert ready["status"] == "ready" and ready["error_code"] is None
|
|
names = {v["name"] for v in ready["variants"]}
|
|
assert {"thumbnail", "crop", "optimized"} <= names, names
|
|
|
|
download = client.download(bob["token"], att["id"])
|
|
assert download.status == 200, download.body
|
|
assert download.body == SAMPLE
|
|
assert download.content_type == "image/png"
|
|
|
|
message = client.send(alice["token"], chat["id"], "", attachments=[att["id"]])
|
|
assert message.status == 201, message.body
|
|
assert message.json["attachment_ids"] == [att["id"]]
|
|
|
|
|
|
def test_content_409_while_queued(client, alice, chat):
|
|
# A fresh attachment is queued/processing for at least a moment; if the
|
|
# worker is fast it may already be ready, so accept either 409 or 200.
|
|
att = client.upload_attachment(alice["token"], chat["id"], SAMPLE).json
|
|
response = client.download(alice["token"], att["id"])
|
|
assert response.status in (200, 409), response.status
|
|
if response.status == 409:
|
|
_assert_error(response, 409, "conflict")
|
|
|
|
|
|
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
|
|
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)
|
|
_assert_error(client.upload_attachment(alice["token"], chat["id"], too_big), 413, "payload_too_large")
|
|
|
|
|
|
def test_corrupt_image_is_415(client, alice, chat):
|
|
corrupt = b"\x89PNG\r\n\x1a\n" + os.urandom(256)
|
|
_assert_error(client.upload_attachment(alice["token"], chat["id"], corrupt), 415, "unsupported_media_type")
|
|
|
|
|
|
def test_mime_spoof_is_415(client, alice, chat):
|
|
spoof = b"GIF89a" + os.urandom(64)
|
|
_assert_error(
|
|
client.upload_attachment(alice["token"], chat["id"], spoof, content_type="image/png"),
|
|
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")
|