forked from mtusi2026/mtusi2026lab4
97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
import os
|
|
from uuid import uuid4
|
|
import httpx
|
|
import pytest
|
|
from helpers import auth, message_headers
|
|
|
|
@pytest.fixture
|
|
def sample_image_path() -> str:
|
|
path = "test_image.png"
|
|
# Валидный 1x1 PNG файл
|
|
with open(path, "wb") as f:
|
|
f.write(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\xff\xff?\x00\x05\xfe\x02\xfe\xa74\x00\x00\x00\x00IEND\xaeB`\x82')
|
|
yield path
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
|
|
@pytest.fixture
|
|
def fake_txt_path() -> str:
|
|
path = "fake.png"
|
|
with open(path, "w") as f:
|
|
f.write("Not a real image file!")
|
|
yield path
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
|
|
|
|
def test_upload_valid_attachment(api: httpx.Client, make_user, make_chat, sample_image_path):
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
|
|
with open(sample_image_path, "rb") as f:
|
|
response = api.post(
|
|
f"/api/v1/chats/{chat['id']}/attachments",
|
|
params={"filename": "photo.png"},
|
|
content=f.read(),
|
|
headers={**auth(alice), "Content-Type": "image/png"}
|
|
)
|
|
|
|
assert response.status_code == 201, response.text
|
|
data = response.json()
|
|
assert data["status"] == "ready"
|
|
assert data["filename"] == "photo.png"
|
|
assert data["content_type"] == "image/png"
|
|
|
|
|
|
def test_upload_invalid_mime(api: httpx.Client, make_user, make_chat, fake_txt_path):
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
|
|
with open(fake_txt_path, "rb") as f:
|
|
response = api.post(
|
|
f"/api/v1/chats/{chat['id']}/attachments",
|
|
params={"filename": "photo.png"},
|
|
content=f.read(),
|
|
headers={**auth(alice), "Content-Type": "image/png"}
|
|
)
|
|
|
|
assert response.status_code == 415, response.text
|
|
|
|
|
|
def test_upload_invalid_filename(api: httpx.Client, make_user, make_chat, sample_image_path):
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
|
|
with open(sample_image_path, "rb") as f:
|
|
response = api.post(
|
|
f"/api/v1/chats/{chat['id']}/attachments",
|
|
params={"filename": "../../../etc/passwd"},
|
|
content=f.read(),
|
|
headers={**auth(alice), "Content-Type": "image/png"}
|
|
)
|
|
|
|
assert response.status_code == 400, response.text
|
|
|
|
|
|
def test_attach_file_to_message(api: httpx.Client, make_user, make_chat, sample_image_path):
|
|
alice = make_user("alice")
|
|
chat = make_chat(alice, [alice])
|
|
|
|
with open(sample_image_path, "rb") as f:
|
|
upload_resp = api.post(
|
|
f"/api/v1/chats/{chat['id']}/attachments",
|
|
params={"filename": "photo.png"},
|
|
content=f.read(),
|
|
headers={**auth(alice), "Content-Type": "image/png"}
|
|
)
|
|
assert upload_resp.status_code == 201, upload_resp.text
|
|
attachment_id = upload_resp.json()["id"]
|
|
|
|
msg_resp = api.post(
|
|
f"/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": "Смотри фото!", "attachment_ids": [attachment_id]},
|
|
headers=message_headers(alice)
|
|
)
|
|
|
|
assert msg_resp.status_code == 201, msg_resp.text
|
|
assert attachment_id in msg_resp.json()["attachment_ids"] |