forked from mtusi2026/mtusi2026lab4
95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
import pytest
|
|
import requests
|
|
import io
|
|
import uuid
|
|
from PIL import Image
|
|
|
|
BASE_URL = "http://localhost:8080"
|
|
|
|
|
|
def register_and_login(username):
|
|
requests.post(f"{BASE_URL}/api/v1/users", json={
|
|
"username": username, "display_name": "Test", "password": "valid_password_123"
|
|
})
|
|
resp = requests.post(f"{BASE_URL}/api/v1/auth/sessions", auth=(username, "valid_password_123"))
|
|
return resp.json()["token"]
|
|
|
|
|
|
def create_chat(token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
me = requests.get(f"{BASE_URL}/api/v1/users/me", headers=headers).json()
|
|
resp = requests.post(f"{BASE_URL}/api/v1/chats", headers=headers, json={
|
|
"title": "Test Chat", "member_ids": [me["id"]]
|
|
})
|
|
return resp.json()["id"]
|
|
|
|
|
|
def make_png(width, height):
|
|
img = Image.new('RGB', (width, height), color='red')
|
|
buf = io.BytesIO()
|
|
img.save(buf, format='PNG')
|
|
return buf.getvalue()
|
|
|
|
|
|
def upload_file(token, chat_id, content, filename="test.png", mime="image/png"):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
return requests.post(
|
|
f"{BASE_URL}/api/v1/chats/{chat_id}/attachments?filename={filename}",
|
|
headers={**headers, "Content-Type": mime},
|
|
data=content
|
|
)
|
|
|
|
|
|
|
|
def test_file_too_large():
|
|
token = register_and_login("test_large")
|
|
chat_id = create_chat(token)
|
|
large_content = b"0" * (11 * 1024 * 1024)
|
|
resp = upload_file(token, chat_id, large_content)
|
|
assert resp.status_code == 413, f"Ожидался 413, получен {resp.status_code}"
|
|
|
|
|
|
def test_corrupted_image():
|
|
token = register_and_login("test_corrupt")
|
|
chat_id = create_chat(token)
|
|
resp = upload_file(token, chat_id, b"this is not an image")
|
|
assert resp.status_code == 400, f"Ожидался 400, получен {resp.status_code}"
|
|
|
|
|
|
def test_mime_spoofing():
|
|
token = register_and_login("test_mime")
|
|
chat_id = create_chat(token)
|
|
resp = upload_file(token, chat_id, b"fake image content", mime="image/png")
|
|
assert resp.status_code == 400, f"Ожидался 400, получен {resp.status_code}"
|
|
|
|
|
|
def test_foreign_attachment_access():
|
|
token1 = register_and_login("test_foreign_1")
|
|
chat_id1 = create_chat(token1)
|
|
content = make_png(10, 10)
|
|
resp_upload = upload_file(token1, chat_id1, content)
|
|
att_id = resp_upload.json()["id"]
|
|
|
|
token2 = register_and_login("test_foreign_2")
|
|
headers2 = {"Authorization": f"Bearer {token2}"}
|
|
resp_download = requests.get(f"{BASE_URL}/api/v1/attachments/{att_id}/content", headers=headers2)
|
|
assert resp_download.status_code == 404, f"Ожидался 404, получен {resp_download.status_code}"
|
|
|
|
|
|
def test_attachment_from_other_chat():
|
|
token1 = register_and_login("test_chat_1")
|
|
chat_id1 = create_chat(token1)
|
|
content = make_png(10, 10)
|
|
resp_upload = upload_file(token1, chat_id1, content)
|
|
att_id = resp_upload.json()["id"]
|
|
|
|
token2 = register_and_login("test_chat_2")
|
|
chat_id2 = create_chat(token2)
|
|
|
|
headers2 = {"Authorization": f"Bearer {token2}"}
|
|
resp_msg = requests.post(
|
|
f"{BASE_URL}/api/v1/chats/{chat_id2}/messages",
|
|
headers={**headers2, "Idempotency-Key": str(uuid.uuid4())},
|
|
json={"text": "взлом", "attachment_ids": [att_id]}
|
|
)
|
|
assert resp_msg.status_code in (400, 403, 404), f"Ожидался отказ, получен {resp_msg.status_code}" |