from io import BytesIO import pytest from PIL import Image from messenger.attachments.application.ports import Rendition from messenger.attachments.domain.errors import ImageTooDemanding, UnreadableImage from messenger.attachments.infrastructure.images import RenderLimits, render_variants from pictures import SAMPLE LIMITS = RenderLimits(max_bytes=10_485_760, max_pixels=40_000_000) FORMATS = { "thumbnail": ("image/jpeg", "jpg", "JPEG"), "crop": ("image/jpeg", "jpg", "JPEG"), "optimized": ("image/webp", "webp", "WEBP"), } def encoded(image: Image.Image, kind: str = "PNG", **options: object) -> bytes: buffer = BytesIO() image.save(buffer, kind, **options) return buffer.getvalue() def rendered(data: bytes) -> dict[str, Image.Image]: renditions: list[Rendition] = render_variants(data, LIMITS) assert [rendition.name for rendition in renditions] == ["thumbnail", "crop", "optimized"] images = {} for rendition in renditions: content_type, extension, kind = FORMATS[rendition.name] assert (rendition.content_type, rendition.extension) == (content_type, extension) image = Image.open(BytesIO(rendition.data)) assert image.format == kind assert image.size == (rendition.width, rendition.height) assert not image.getexif() assert not {"icc_profile", "exif", "xmp"} & set(image.info) images[rendition.name] = image return images def sizes(data: bytes) -> tuple[tuple[int, int], ...]: images = rendered(data) return tuple(images[name].size for name in ("thumbnail", "crop", "optimized")) def test_fixture_gives_three_variants() -> None: assert sizes(SAMPLE) == ((256, 160), (128, 128), (320, 200)) @pytest.mark.parametrize( ("mode", "size", "thumbnail", "optimized"), [ ("L", (17, 1000), (4, 256), (17, 1000)), ("RGB", (64, 64), (64, 64), (64, 64)), ("RGB", (3000, 2), (256, 1), (1280, 1)), ("I;16", (300, 200), (256, 170), (300, 200)), ("CMYK", (801, 601), (256, 192), (801, 601)), ("RGB", (4000, 3000), (256, 192), (1280, 960)), ], ) def test_sizes_round_down_never_upscale_and_crop_is_square( mode: str, size: tuple[int, int], thumbnail: tuple[int, int], optimized: tuple[int, int] ) -> None: kind = "JPEG" if mode == "CMYK" or size == (4000, 3000) else "PNG" assert sizes(encoded(Image.new(mode, size), kind)) == (thumbnail, (128, 128), optimized) def test_crop_takes_the_centre() -> None: image = Image.new("RGB", (300, 100), (255, 0, 0)) image.paste((0, 0, 255), (100, 0, 200, 100)) crop = rendered(encoded(image))["crop"].convert("RGB") pixels = [crop.getpixel((x, 64)) for x in (2, 64, 125)] assert all(isinstance(p, tuple) and p[0] < 60 and p[2] > 200 for p in pixels) @pytest.mark.parametrize("mode", ["RGBA", "LA"]) def test_transparency_is_composed_on_white(mode: str) -> None: transparent = Image.new(mode, (300, 300), (0,) * len(mode)) for image in rendered(encoded(transparent)).values(): assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255) def test_palette_transparency_is_composed_on_white() -> None: palette = Image.new("P", (200, 100)) image = rendered(encoded(palette, transparency=0))["thumbnail"] assert image.size == (200, 100) assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255) @pytest.mark.parametrize( ("orientation", "thumbnail", "optimized"), [(6, (128, 256), (200, 400)), (3, (256, 128), (400, 200))], ) def test_exif_orientation_is_applied_before_sizing( orientation: int, thumbnail: tuple[int, int], optimized: tuple[int, int] ) -> None: exif = Image.Exif() exif[0x0112] = orientation data = encoded( Image.new("RGB", (400, 200), (10, 200, 10)), "JPEG", exif=exif, icc_profile=b"x" * 64 ) assert sizes(data) == (thumbnail, (128, 128), optimized) def test_same_bytes_give_same_variants() -> None: assert render_variants(SAMPLE, LIMITS) == render_variants(SAMPLE, LIMITS) @pytest.mark.parametrize( "data", [b"", b"garbage", SAMPLE[: len(SAMPLE) // 2], b"GIF89a" + bytes(20)], ids=["empty", "garbage", "truncated", "gif"], ) def test_undecodable_data_is_a_permanent_error(data: bytes) -> None: with pytest.raises(UnreadableImage): render_variants(data, LIMITS) def test_pixel_limit_is_checked_before_decoding() -> None: with pytest.raises(UnreadableImage): render_variants(SAMPLE, RenderLimits(LIMITS.max_bytes, 320 * 200 - 1)) def test_byte_limit_is_checked_before_decoding() -> None: with pytest.raises(ImageTooDemanding): render_variants(SAMPLE, RenderLimits(len(SAMPLE) - 1, LIMITS.max_pixels))