forked from mtusi2026/mtusi2026lab5
86 lines
3.6 KiB
Python
86 lines
3.6 KiB
Python
import base64
|
|
import io
|
|
import time
|
|
import httpx
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
BASE_URL = "http://localhost:8080"
|
|
|
|
def get_client():
|
|
return httpx.Client(base_url=BASE_URL, trust_env=False, timeout=20.0)
|
|
|
|
def auth_user(client, username):
|
|
pw = "super_password_12345"
|
|
client.post("/users", json={"username": username, "display_name": "Test", "password": pw})
|
|
b64 = base64.b64encode(f"{username}:{pw}".encode()).decode()
|
|
res = client.post("/auth/sessions", headers={"Authorization": f"Basic {b64}"})
|
|
return res.json()["token"], res.json()["user"]["id"]
|
|
|
|
def make_test_png(w=400, h=300):
|
|
img = Image.new("RGBA", (w, h), (100, 150, 200, 128))
|
|
buf = io.BytesIO()
|
|
img.save(buf, format="PNG")
|
|
return buf.getvalue()
|
|
|
|
def test_async_upload_and_all_three_variants():
|
|
with get_client() as client:
|
|
ts = int(time.time() * 1000)
|
|
token, uid = auth_user(client, f"u_l5_{ts}")
|
|
h = {"Authorization": f"Bearer {token}"}
|
|
chat_id = client.post("/chats", json={"title": "Lab5 Chat", "member_ids": [uid]}, headers=h).json()["id"]
|
|
|
|
png_bytes = make_test_png(400, 300)
|
|
up = client.post(
|
|
f"/chats/{chat_id}/attachments?filename=pic.png",
|
|
content=png_bytes,
|
|
headers={**h, "Content-Type": "image/png"}
|
|
)
|
|
# 1. Должен вернуться 202 queued
|
|
assert up.status_code == 202
|
|
att = up.json()
|
|
assert att["status"] == "queued"
|
|
att_id = att["id"]
|
|
|
|
# 2. Ждем готовности (ready)
|
|
deadline = time.time() + 20
|
|
ready = None
|
|
while time.time() < deadline:
|
|
meta = client.get(f"/attachments/{att_id}", headers=h).json()
|
|
if meta["status"] == "ready":
|
|
ready = meta
|
|
break
|
|
time.sleep(0.3)
|
|
|
|
assert ready is not None, "Attachment did not become ready in time"
|
|
variants = {v["name"]: v for v in ready["variants"]}
|
|
|
|
# 3. Проверяем все 3 варианта: thumbnail, crop, optimized
|
|
assert "thumbnail" in variants
|
|
assert "crop" in variants
|
|
assert "optimized" in variants
|
|
|
|
# Проверяем thumbnail (400x300 -> вписан в 256x256 -> 256x192)
|
|
assert variants["thumbnail"]["content_type"] == "image/jpeg"
|
|
assert (variants["thumbnail"]["width"], variants["thumbnail"]["height"]) == (256, 192)
|
|
dl_thumb = client.get(f"/attachments/{att_id}/content?variant=thumbnail", headers=h)
|
|
assert dl_thumb.status_code == 200
|
|
assert dl_thumb.headers.get("Content-Type") == "image/jpeg"
|
|
|
|
# Проверяем crop (128x128 JPEG)
|
|
assert variants["crop"]["content_type"] == "image/jpeg"
|
|
assert (variants["crop"]["width"], variants["crop"]["height"]) == (128, 128)
|
|
dl_crop = client.get(f"/attachments/{att_id}/content?variant=crop", headers=h)
|
|
assert dl_crop.status_code == 200
|
|
|
|
# Проверяем optimized (WebP, 400x300 без апскейла)
|
|
assert variants["optimized"]["content_type"] == "image/webp"
|
|
assert (variants["optimized"]["width"], variants["optimized"]["height"]) == (400, 300)
|
|
dl_opt = client.get(f"/attachments/{att_id}/content?variant=optimized", headers=h)
|
|
assert dl_opt.status_code == 200
|
|
assert dl_opt.headers.get("Content-Type") == "image/webp"
|
|
|
|
# 4. Безопасный replay уже готового вложения (не дублирует и не портит)
|
|
rep = client.post(f"/internal/attachments/{att_id}/replay")
|
|
assert rep.status_code == 200
|
|
assert rep.json()["replayed"] is False |