Spec-first port of the Flask app (still on main): - schema.arch: signed arch-llm spec (single source of truth) - paste_bin/: generated FastAPI backend, verified 51/51 frozen owner tests, 92.5% coverage (build r3, 2026-11-18) - ui/: SPA + emitted OpenAPI contract (openapi.json + generated client.js); the app calls only window.PasteBinAPI - 7 old bugs fixed (file raw 500, silent expiry, AVI-as-webp, stripped content, limiter leak, trusted mimetype, stale tmp) - 62/62 live probe green; /health + JSON index added; JSON API Full report on the wiki: 'FastAPI port (arch-llm pipeline)'.
107 lines
4.1 KiB
Python
107 lines
4.1 KiB
Python
# tests for module validate (paste_bin/validate.py)
|
|
|
|
import datetime
|
|
import re
|
|
|
|
import pytest
|
|
|
|
from paste_bin import validate
|
|
|
|
|
|
def test_generate_paste_id_shape_and_uniqueness():
|
|
a = validate.generate_paste_id()
|
|
b = validate.generate_paste_id()
|
|
assert re.fullmatch(r"[a-f0-9]{16}", a)
|
|
assert re.fullmatch(r"[a-f0-9]{16}", b)
|
|
assert a != b
|
|
|
|
|
|
def test_is_valid_paste_id():
|
|
assert validate.is_valid_paste_id("0123456789abcdef") is True
|
|
assert validate.is_valid_paste_id("a" * 16) is True
|
|
assert validate.is_valid_paste_id("0123456789abcde") is False # 15 chars
|
|
assert validate.is_valid_paste_id("0123456789abcdef0") is False # 17 chars
|
|
assert validate.is_valid_paste_id("0123456789ABCDEF") is False # uppercase
|
|
assert validate.is_valid_paste_id("../etc/passwd") is False
|
|
assert validate.is_valid_paste_id("") is False
|
|
|
|
|
|
def test_sanitize_filename():
|
|
assert validate.sanitize_filename("../../etc/passwd") == "passwd"
|
|
assert validate.sanitize_filename("/a/b/my file.txt") == "my_file.txt"
|
|
assert validate.sanitize_filename("???") == "___"
|
|
assert validate.sanitize_filename("...") == ""
|
|
assert len(validate.sanitize_filename("a" * 300)) <= 255
|
|
assert not validate.sanitize_filename("..hidden.txt").startswith(".")
|
|
|
|
|
|
def test_parse_expiry_known_keys():
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
for key, hours in [("1h", 1), ("1d", 24), ("1w", 24 * 7), ("1m", 24 * 30)]:
|
|
ts = datetime.datetime.fromisoformat(validate.parse_expiry(key))
|
|
delta = (ts - now).total_seconds()
|
|
assert abs(delta - hours * 3600) < 120
|
|
|
|
|
|
def test_parse_expiry_forever_is_none():
|
|
assert validate.parse_expiry("forever") is None
|
|
|
|
|
|
def test_parse_expiry_unknown_key_raises():
|
|
# old app silently defaulted unknown keys to 1 day — must now reject
|
|
with pytest.raises(ValueError):
|
|
validate.parse_expiry("2 weeks")
|
|
with pytest.raises(ValueError):
|
|
validate.parse_expiry("")
|
|
|
|
|
|
def _png() -> bytes:
|
|
return b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
|
|
|
|
|
|
def _riff(kind: bytes) -> bytes:
|
|
# RIFF container: 0..3 'RIFF', 4..7 size, 8..11 type tag
|
|
return b"RIFF" + b"\x00\x00\x00\x00" + kind + b"\x00" * 8
|
|
|
|
|
|
def test_check_image_magic():
|
|
assert validate.check_image_magic(_png(), "png") is True
|
|
assert validate.check_image_magic(b"\xff\xd8\xff" + b"0" * 10, "jpg") is True
|
|
assert validate.check_image_magic(b"GIF87a" + b"0" * 10, "gif") is True
|
|
assert validate.check_image_magic(b"GIF89a" + b"0" * 10, "gif") is True
|
|
assert validate.check_image_magic(b"BM" + b"0" * 10, "bmp") is True
|
|
assert validate.check_image_magic(b"II*\x00" + b"0" * 10, "tiff") is True
|
|
# webp needs the WEBP tag at offset 8, not just any RIFF
|
|
assert validate.check_image_magic(_riff(b"WEBP"), "webp") is True
|
|
# RIFF+AVI must be rejected (old bug: accepted any RIFF as webp)
|
|
assert validate.check_image_magic(_riff(b"AVI "), "webp") is False
|
|
# magic must match the requested extension
|
|
assert validate.check_image_magic(_png(), "jpg") is False
|
|
assert validate.check_image_magic(_png(), "txt") is False
|
|
|
|
|
|
def test_derive_mimetype():
|
|
assert validate.derive_mimetype("png") == "image/png"
|
|
assert validate.derive_mimetype("jpg") == "image/jpeg"
|
|
assert validate.derive_mimetype("jpeg") == "image/jpeg"
|
|
assert validate.derive_mimetype("gif") == "image/gif"
|
|
assert validate.derive_mimetype("bmp") == "image/bmp"
|
|
assert validate.derive_mimetype("webp") == "image/webp"
|
|
assert validate.derive_mimetype("tiff") == "image/tiff"
|
|
assert validate.derive_mimetype("txt") == "application/octet-stream"
|
|
assert validate.derive_mimetype("bin") == "application/octet-stream"
|
|
|
|
|
|
def test_is_image_ext():
|
|
assert validate.is_image_ext("photo.png") is True
|
|
assert validate.is_image_ext("PHOTO.PNG") is True
|
|
assert validate.is_image_ext("photo.pngx") is False
|
|
assert validate.is_image_ext("noext") is False
|
|
|
|
|
|
def test_is_text_ext():
|
|
assert validate.is_text_ext("script.py") is True
|
|
assert validate.is_text_ext("notes.md") is True
|
|
assert validate.is_text_ext("photo.png") is False
|
|
assert validate.is_text_ext("noext") is False
|