feat: крролик, по тщ 5 лаба

This commit is contained in:
Enterxx2
2026-11-10 12:38:09 +03:00
parent 1d8dfb7ea4
commit 595a959b96
65 changed files with 2692 additions and 216 deletions
+41 -16
View File
@@ -63,7 +63,12 @@ def test_upload_and_download_byte_exact(
assert set(attachment) == ATTACHMENT_FIELDS
assert attachment["status"] == "ready"
assert attachment["variants"] == [] and attachment["error_code"] is None
assert {v["name"] for v in attachment["variants"]} == {
"thumbnail",
"crop",
"optimized",
}
assert attachment["error_code"] is None
assert attachment["chat_id"] == chat["id"] and attachment["owner_id"] == alice["id"]
assert attachment["content_type"] == content_type
assert attachment["size_bytes"] == len(content)
@@ -118,8 +123,12 @@ def test_outsider_and_unknown_get_indistinguishable_404(
attachment = upload(alice, chat)
for suffix in ("", "/content"):
foreign = api.get(f"/api/v1/attachments/{attachment['id']}{suffix}", headers=auth(eve))
missing = api.get(f"/api/v1/attachments/{uuid.uuid4()}{suffix}", headers=auth(eve))
foreign = api.get(
f"/api/v1/attachments/{attachment['id']}{suffix}", headers=auth(eve)
)
missing = api.get(
f"/api/v1/attachments/{uuid.uuid4()}{suffix}", headers=auth(eve)
)
assert_error(foreign, 404, "not_found")
assert_error(missing, 404, "not_found")
assert foreign.json()["error"]["message"] == missing.json()["error"]["message"]
@@ -135,7 +144,9 @@ def test_outsider_cannot_upload_into_foreign_chat(
assert_error(upload_raw(api, eve, str(uuid.uuid4()), png_bytes()), 404, "not_found")
def test_authentication_is_required(api: httpx.Client, make_user, make_chat, upload) -> None:
def test_authentication_is_required(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
@@ -153,7 +164,9 @@ def test_authentication_is_required(api: httpx.Client, make_user, make_chat, upl
assert_error(response, 401, "unauthorized")
def test_variants_and_malformed_ids(api: httpx.Client, make_user, make_chat, upload) -> None:
def test_variants_and_malformed_ids(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
@@ -161,9 +174,10 @@ def test_variants_and_malformed_ids(api: httpx.Client, make_user, make_chat, upl
original = api.get(path, params={"variant": "original"}, headers=auth(alice))
assert original.status_code == 200
# Вариант из контракта, но в lab4 его нет у ready-вложения
missing = api.get(path, params={"variant": "thumbnail"}, headers=auth(alice))
assert_error(missing, 404, "not_found")
# Производный вариант pipeline v1 доступен через авторизованный API
thumbnail = api.get(path, params={"variant": "thumbnail"}, headers=auth(alice))
assert thumbnail.status_code == 200
assert thumbnail.headers["content-type"] == "image/jpeg"
unknown = api.get(path, params={"variant": "../../etc"}, headers=auth(alice))
assert_error(unknown, 400, "invalid_request")
malformed = api.get("/api/v1/attachments/not-a-uuid", headers=auth(alice))
@@ -173,7 +187,9 @@ def test_variants_and_malformed_ids(api: httpx.Client, make_user, make_chat, upl
# --- Размер ---
def test_exactly_max_bytes_is_accepted(api: httpx.Client, make_user, make_chat, upload) -> None:
def test_exactly_max_bytes_is_accepted(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
content = png_of_size(MAX_UPLOAD_BYTES)
@@ -182,7 +198,9 @@ def test_exactly_max_bytes_is_accepted(api: httpx.Client, make_user, make_chat,
assert download(api, alice, attachment["id"]).content == content
def test_too_large_file_is_rejected_by_api(api: httpx.Client, make_user, make_chat) -> None:
def test_too_large_file_is_rejected_by_api(
api: httpx.Client, make_user, make_chat
) -> None:
"""10 MiB + 1 байт отклоняет реплика (X-Instance-Id), а не только LB"""
alice = make_user("alice")
chat = make_chat(alice, [alice])
@@ -191,7 +209,9 @@ def test_too_large_file_is_rejected_by_api(api: httpx.Client, make_user, make_ch
assert response.headers["X-Instance-Id"]
def test_huge_body_is_rejected_by_balancer(api: httpx.Client, make_user, make_chat) -> None:
def test_huge_body_is_rejected_by_balancer(
api: httpx.Client, make_user, make_chat
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], b"\x00" * (12 * 1024 * 1024))
@@ -207,7 +227,7 @@ def test_empty_body_is_rejected(api: httpx.Client, make_user, make_chat) -> None
@pytest.mark.parametrize(
("width", "height", "status"),
[
(8000, 5000, 201),
(8000, 5000, 202),
(8000, 5001, 413),
(20000, 20000, 413),
],
@@ -221,9 +241,9 @@ def test_pixel_limit_applies_before_decoding(
content = bilevel_png(width, height)
assert len(content) < 1024 * 1024
response = upload_raw(api, alice, chat["id"], content)
if status == 201:
if status == 202:
assert width * height == MAX_IMAGE_PIXELS
assert response.status_code == 201, response.text
assert response.status_code == 202, response.text
else:
assert_error(response, 413, "payload_too_large")
@@ -252,7 +272,12 @@ def corrupted_payloads() -> list[tuple[str, bytes, str]]:
ids=[item[0] for item in corrupted_payloads()],
)
def test_corrupted_image_is_rejected(
api: httpx.Client, make_user, make_chat, name: str, content: bytes, content_type: str
api: httpx.Client,
make_user,
make_chat,
name: str,
content: bytes,
content_type: str,
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
@@ -302,7 +327,7 @@ def test_content_type_parameters_are_ignored(
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], png_bytes(), "Image/PNG; foo=bar")
assert response.status_code == 201, response.text
assert response.status_code == 202, response.text
assert response.json()["content_type"] == "image/png"