Short IDs become the canonical identifier in URLs (/i/:short_id), MinIO/R2 storage keys, and all API responses. Hash-based deduplication is preserved. Includes two-phase Alembic migration (003 adds nullable column, 004 enforces NOT NULL) with a backfill script to copy storage objects and populate short_id for existing images. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
43 lines
1015 B
Python
43 lines
1015 B
Python
import hashlib
|
|
|
|
from app.utils import compute_sha256, generate_short_id
|
|
|
|
|
|
def test_sha256_known_bytes():
|
|
data = b"hello world"
|
|
expected = hashlib.sha256(data).hexdigest()
|
|
assert compute_sha256(data) == expected
|
|
|
|
|
|
def test_sha256_empty_bytes():
|
|
data = b""
|
|
expected = hashlib.sha256(data).hexdigest()
|
|
assert compute_sha256(data) == expected
|
|
|
|
|
|
def test_sha256_returns_64_char_hex():
|
|
result = compute_sha256(b"test data")
|
|
assert len(result) == 64
|
|
assert all(c in "0123456789abcdef" for c in result)
|
|
|
|
|
|
def test_generate_short_id_length():
|
|
assert len(generate_short_id()) == 8
|
|
|
|
|
|
def test_generate_short_id_charset():
|
|
result = generate_short_id()
|
|
assert all(
|
|
c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for c in result
|
|
)
|
|
|
|
|
|
def test_generate_short_id_randomness():
|
|
assert generate_short_id() != generate_short_id()
|
|
|
|
|
|
def test_generate_short_id_importable():
|
|
from app.utils import generate_short_id as fn
|
|
|
|
assert callable(fn)
|