forked from mtusi2026/mtusi2026lab5
Отделить обработку вложений от приёма файла.
Upload отвечает 202 после staging и записи задания, worker через RabbitMQ собирает thumbnail, crop и optimized и переживает разрыв до publish. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative attachment cases. Prints statuses only, never tokens."""
|
||||
|
||||
import os
|
||||
import base64
|
||||
from urllib.parse import urlparse
|
||||
import hashlib
|
||||
import json
|
||||
import socket
|
||||
import struct
|
||||
import time
|
||||
import uuid
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080")
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
JPEG = base64.b64decode(
|
||||
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5soooroMD/9k="
|
||||
@@ -69,6 +72,19 @@ class Api:
|
||||
return response.status, response.headers, parsed, payload
|
||||
|
||||
|
||||
def wait_ready(api: Api, attachment_id: str, token: str):
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
status, _, body, _ = api.call("GET", "/api/v1/attachments/" + attachment_id, token=token)
|
||||
expect(200, status, "poll attachment")
|
||||
if body["status"] == "failed":
|
||||
raise SystemExit("FAIL attachment failed")
|
||||
if body["status"] == "ready":
|
||||
return body
|
||||
time.sleep(0.3)
|
||||
raise SystemExit("FAIL attachment did not become ready")
|
||||
|
||||
|
||||
def expect(status, got, label):
|
||||
if got == 500:
|
||||
raise SystemExit("FAIL %s returned 500" % label)
|
||||
@@ -93,15 +109,17 @@ def user(api: Api, label: str):
|
||||
|
||||
|
||||
def declared_length(token, chat_id):
|
||||
parsed = urlparse(BASE)
|
||||
host = parsed.netloc or "127.0.0.1:8080"
|
||||
request = (
|
||||
"POST /api/v1/chats/%s/attachments?filename=big.png HTTP/1.1\r\n"
|
||||
"Host: 127.0.0.1:8080\r\n"
|
||||
"Host: %s\r\n"
|
||||
"Authorization: Bearer %s\r\n"
|
||||
"Content-Type: image/png\r\n"
|
||||
"Content-Length: 10485761\r\n"
|
||||
"Connection: close\r\n\r\n" % (chat_id, token)
|
||||
"Connection: close\r\n\r\n" % (chat_id, host, token)
|
||||
).encode() + b"\x89PNG"
|
||||
sock = socket.create_connection(("127.0.0.1", 8080), timeout=5)
|
||||
sock = socket.create_connection((parsed.hostname or "127.0.0.1", parsed.port or 80), timeout=5)
|
||||
try:
|
||||
sock.sendall(request)
|
||||
data = b""
|
||||
@@ -157,9 +175,10 @@ def main():
|
||||
raw=good,
|
||||
content_type="image/png",
|
||||
)
|
||||
expect(201, status, "png upload")
|
||||
if attachment["sha256"] != hashlib.sha256(good).hexdigest() or attachment["status"] != "ready":
|
||||
expect(202, status, "png upload")
|
||||
if attachment["sha256"] != hashlib.sha256(good).hexdigest() or attachment["status"] != "queued":
|
||||
raise SystemExit("FAIL png metadata")
|
||||
attachment = wait_ready(api, attachment["id"], bob_token)
|
||||
status, headers, _, downloaded = api.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/%s/content" % attachment["id"],
|
||||
@@ -178,7 +197,7 @@ def main():
|
||||
raw=JPEG,
|
||||
content_type="image/jpeg",
|
||||
)
|
||||
expect(201, status, "jpeg upload")
|
||||
expect(202, status, "jpeg upload")
|
||||
if jpeg_meta["content_type"] != "image/jpeg" or jpeg_meta["sha256"] != hashlib.sha256(JPEG).hexdigest():
|
||||
raise SystemExit("FAIL jpeg metadata")
|
||||
|
||||
@@ -269,7 +288,7 @@ def main():
|
||||
raw=good,
|
||||
content_type="image/png",
|
||||
)
|
||||
expect(201, status, "upload into other chat")
|
||||
expect(202, status, "upload into other chat")
|
||||
status, _, _, _ = api.call(
|
||||
"POST",
|
||||
"/api/v1/chats/%s/messages" % chat_id,
|
||||
|
||||
Reference in New Issue
Block a user