- add verbatim GPLv3 text as LICENSE - declare license and license-files in pyproject - add SPDX headers to Python sources - document licensing in README
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
# Copyright (C) 2026 kawaiipunk
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
|
|
import httpx
|
|
from PIL import Image
|
|
|
|
from signal_cal_bot.config import ImagesConfig
|
|
from signal_cal_bot.images import ImageService
|
|
|
|
|
|
def make_service(tmp_path, **overrides) -> ImageService:
|
|
return ImageService(ImagesConfig(**overrides), tmp_path, httpx.Client())
|
|
|
|
|
|
def test_transparent_png_is_flattened_onto_white(tmp_path):
|
|
buffer = io.BytesIO()
|
|
Image.new("RGBA", (120, 120), (255, 0, 0, 0)).save(buffer, "PNG")
|
|
|
|
service = make_service(tmp_path)
|
|
result = service._resize(buffer.getvalue())
|
|
assert result
|
|
|
|
image = Image.open(io.BytesIO(result))
|
|
assert image.mode == "RGB"
|
|
red, green, blue = image.getpixel((60, 60))
|
|
assert min(red, green, blue) > 240
|
|
|
|
|
|
def test_oversized_image_is_downscaled(tmp_path):
|
|
buffer = io.BytesIO()
|
|
Image.new("RGB", (4000, 3000), (10, 80, 200)).save(buffer, "PNG")
|
|
service = make_service(tmp_path, max_pixels=400, max_bytes=50_000)
|
|
result = service._resize(buffer.getvalue())
|
|
assert result
|
|
assert len(result) <= 50_000
|
|
image = Image.open(io.BytesIO(result))
|
|
assert max(image.size) <= 400
|
|
|
|
|
|
def test_decompression_bomb_does_not_propagate(tmp_path):
|
|
service = make_service(tmp_path)
|
|
|
|
def boom(url):
|
|
raise Image.DecompressionBombError("too many pixels")
|
|
|
|
service._cached_or_download = boom # type: ignore[method-assign]
|
|
assert service.data_uri("https://example.test/huge.jpg") is None
|