forked from mtusi2026/mtusi2026lab4
Стенд: PostgreSQL 16 + две API-реплики за HAProxy + SeaweedFS S3 в приватном bucket. Метаданные вложений в PostgreSQL, байты в S3, object key назначает сервер. Лимиты 10 MiB и 40 млн пикселей, MIME по магическим байтам, проверка filename. Добавлены smoke/негативные тесты, хуки restart/stop-one, сценарий отказа S3 и отчёт с доказательствами.
72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
"""Small dependency-light checks for pure validation helpers.
|
|
|
|
Run with: python -m unittest discover -s student-tests -v
|
|
"""
|
|
import sys
|
|
import unittest
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from app.images import ImageTooLarge, InvalidImage, inspect_image # noqa: E402
|
|
from app.security import ( # noqa: E402
|
|
decode_cursor,
|
|
encode_cursor,
|
|
hash_password,
|
|
verify_password,
|
|
)
|
|
from app.server import Handler # noqa: E402
|
|
|
|
|
|
class PasswordTests(unittest.TestCase):
|
|
def test_password_roundtrip(self):
|
|
stored = hash_password("T3st-abcdefghijkl")
|
|
self.assertTrue(verify_password("T3st-abcdefghijkl", stored))
|
|
self.assertFalse(verify_password("wrong-password", stored))
|
|
self.assertNotIn("T3st-abcdefghijkl", stored)
|
|
|
|
|
|
class CursorTests(unittest.TestCase):
|
|
def test_signed_cursor_roundtrip_and_tampering(self):
|
|
value = encode_cursor("secret", {"kind": "messages", "id": "1"})
|
|
self.assertEqual(decode_cursor("secret", value)["id"], "1")
|
|
with self.assertRaises(ValueError):
|
|
decode_cursor("other-secret", value)
|
|
with self.assertRaises(ValueError):
|
|
decode_cursor("secret", value + "x")
|
|
|
|
|
|
class ImageTests(unittest.TestCase):
|
|
def test_fixture_png(self):
|
|
data = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
|
self.assertEqual(inspect_image(data, "image/png", 40_000_000), ("image/png", 320, 200))
|
|
|
|
def test_jpeg_and_mime_mismatch(self):
|
|
buffer = BytesIO()
|
|
Image.new("RGB", (10, 10), "red").save(buffer, "JPEG")
|
|
data = buffer.getvalue()
|
|
self.assertEqual(inspect_image(data, "image/jpeg", 40_000_000)[0], "image/jpeg")
|
|
with self.assertRaises(InvalidImage):
|
|
inspect_image(data, "image/png", 40_000_000)
|
|
|
|
def test_corrupt_and_oversized(self):
|
|
with self.assertRaises(InvalidImage):
|
|
inspect_image(b"not an image", "image/png", 40_000_000)
|
|
buffer = BytesIO()
|
|
Image.new("RGB", (10, 10), "red").save(buffer, "PNG")
|
|
with self.assertRaises(ImageTooLarge):
|
|
inspect_image(buffer.getvalue(), "image/png", 99)
|
|
|
|
def test_filename_rules(self):
|
|
self.assertTrue(Handler._valid_filename("photo.png"))
|
|
for bad in ("", "a/b.png", "a\\b.png", "a\x00b", "a\nb"):
|
|
self.assertFalse(Handler._valid_filename(bad), repr(bad))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|