90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from io import BytesIO
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from app.jobs.pipeline import PermanentImageError, process_image
|
|
|
|
|
|
def encoded(width: int, height: int, mode: str = 'RGBA') -> bytes:
|
|
image = Image.new(mode, (width, height), (20, 80, 160, 128) if mode == 'RGBA' else 'navy')
|
|
output = BytesIO()
|
|
image.save(output, 'PNG', pnginfo=None)
|
|
return output.getvalue()
|
|
|
|
|
|
def run(source: bytes):
|
|
return process_image(
|
|
source,
|
|
'image/png',
|
|
hashlib.sha256(source).hexdigest(),
|
|
40_000_000,
|
|
85,
|
|
85,
|
|
82,
|
|
)
|
|
|
|
|
|
def test_pipeline_creates_all_grade4_variants() -> None:
|
|
"""Create every grade-4 variant with the required format, size and checksum."""
|
|
variants = {item.name: item for item in run(encoded(320, 200))}
|
|
assert set(variants) == {'thumbnail', 'crop', 'optimized'}
|
|
assert (variants['thumbnail'].width, variants['thumbnail'].height) == (256, 160)
|
|
assert variants['thumbnail'].content_type == 'image/jpeg'
|
|
assert (variants['crop'].width, variants['crop'].height) == (128, 128)
|
|
assert variants['crop'].content_type == 'image/jpeg'
|
|
assert (variants['optimized'].width, variants['optimized'].height) == (320, 200)
|
|
assert variants['optimized'].content_type == 'image/webp'
|
|
for item in variants.values():
|
|
assert hashlib.sha256(item.content).hexdigest() == item.sha256
|
|
|
|
|
|
def test_thumbnail_and_optimized_do_not_upscale_small_image() -> None:
|
|
"""Keep small images at source size except for the required 128x128 crop."""
|
|
variants = {item.name: item for item in run(encoded(40, 20, 'RGB'))}
|
|
assert (variants['thumbnail'].width, variants['thumbnail'].height) == (40, 20)
|
|
assert (variants['optimized'].width, variants['optimized'].height) == (40, 20)
|
|
assert (variants['crop'].width, variants['crop'].height) == (128, 128)
|
|
|
|
|
|
def test_jpeg_variants_composite_alpha_on_white() -> None:
|
|
"""Flatten transparent input before encoding JPEG variants without an alpha channel."""
|
|
variants = {item.name: item for item in run(encoded(2, 2))}
|
|
with Image.open(BytesIO(variants['thumbnail'].content)) as image:
|
|
assert image.mode == 'RGB'
|
|
|
|
|
|
def test_checksum_mismatch_is_permanent() -> None:
|
|
"""Reject changed staging bytes as a permanent checksum error."""
|
|
source = encoded(20, 20)
|
|
with pytest.raises(PermanentImageError, match='checksum_mismatch'):
|
|
process_image(source, 'image/png', '0' * 64, 40_000_000, 85, 85, 82)
|
|
|
|
|
|
def test_exif_orientation_is_applied_and_removed() -> None:
|
|
"""Apply EXIF orientation before resizing and omit metadata from generated output."""
|
|
image = Image.new('RGB', (320, 200), 'red')
|
|
exif = Image.Exif()
|
|
exif[274] = 6
|
|
output = BytesIO()
|
|
image.save(output, 'JPEG', exif=exif)
|
|
source = output.getvalue()
|
|
variants = {
|
|
item.name: item
|
|
for item in process_image(
|
|
source,
|
|
'image/jpeg',
|
|
hashlib.sha256(source).hexdigest(),
|
|
40_000_000,
|
|
85,
|
|
85,
|
|
82,
|
|
)
|
|
}
|
|
assert (variants['thumbnail'].width, variants['thumbnail'].height) == (160, 256)
|
|
with Image.open(BytesIO(variants['thumbnail'].content)) as transformed:
|
|
assert not transformed.getexif()
|