Spaces:
Sleeping
Sleeping
File size: 6,990 Bytes
0d7bef5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import tempfile
import unittest
import zipfile
from pathlib import Path
try:
from PIL import Image
except ImportError: # pragma: no cover
Image = None
from app.processor import (
ZipSlipError,
_save_kwargs_for,
parse_bg_color,
process_zip,
safe_extract_zip,
sanitize_folder_name,
validate_output_mode,
validate_quality_profile,
)
class ProcessorValidationTests(unittest.TestCase):
def test_parse_bg_color_accepts_rgb_and_hex(self):
self.assertEqual(parse_bg_color("241, 242, 242"), (241, 242, 242))
self.assertEqual(parse_bg_color("#F1F2F2"), (241, 242, 242))
def test_parse_bg_color_rejects_invalid_values(self):
with self.assertRaises(ValueError):
parse_bg_color("300,0,0")
with self.assertRaises(ValueError):
parse_bg_color("#XYZ123")
def test_sanitize_folder_name(self):
self.assertEqual(sanitize_folder_name(" Grey "), "Grey")
for unsafe in ("", "../Grey", "Grey/Blue", "Grey\\Blue", "C:Grey"):
with self.subTest(unsafe=unsafe):
with self.assertRaises(ValueError):
sanitize_folder_name(unsafe)
def test_validates_quality_profile_and_output_mode(self):
self.assertEqual(validate_quality_profile("Pro"), "pro")
self.assertEqual(validate_output_mode("lossless_png"), "lossless_png")
with self.assertRaises(ValueError):
validate_quality_profile("ultra")
with self.assertRaises(ValueError):
validate_output_mode("gif")
class ZipSafetyTests(unittest.TestCase):
def test_safe_extract_blocks_zip_slip(self):
with tempfile.TemporaryDirectory() as temp_dir_name:
temp_dir = Path(temp_dir_name)
zip_path = temp_dir / "bad.zip"
extract_dir = temp_dir / "extract"
with zipfile.ZipFile(zip_path, "w") as archive:
archive.writestr("../escape.txt", "nope")
with self.assertRaises(ZipSlipError):
safe_extract_zip(zip_path, extract_dir)
self.assertFalse((temp_dir / "escape.txt").exists())
@unittest.skipIf(Image is None, "Pillow is not installed")
class ProcessZipTests(unittest.TestCase):
def test_process_zip_preserves_filename_dimensions_and_reports_failures(self):
with tempfile.TemporaryDirectory() as temp_dir_name:
temp_dir = Path(temp_dir_name)
zip_path = temp_dir / "input.zip"
output_zip = temp_dir / "result.zip"
image_path = temp_dir / "SKU-100.jpg"
Image.new("RGB", (12, 8), (255, 255, 255)).save(image_path, "JPEG")
with zipfile.ZipFile(zip_path, "w") as archive:
archive.write(image_path, "SKU-100.jpg")
archive.writestr("broken.png", b"not an image")
archive.writestr("notes.txt", "ignore me")
progress_events = []
def fake_remove(image):
return image.convert("RGBA")
result = process_zip(
zip_path,
output_zip,
"Grey",
(241, 242, 242),
remover=fake_remove,
progress_callback=lambda total, completed, failed: progress_events.append(
(total, completed, failed)
),
)
self.assertEqual(result.total, 2)
self.assertEqual(result.completed, 2)
self.assertEqual(result.failed_files, ["broken.png"])
self.assertTrue(output_zip.exists())
self.assertEqual(progress_events[0], (2, 0, []))
self.assertEqual(progress_events[-1], (2, 2, ["broken.png"]))
with zipfile.ZipFile(output_zip) as archive:
names = set(archive.namelist())
self.assertIn("Grey/SKU-100.jpg", names)
self.assertNotIn("Grey/notes.txt", names)
extracted = temp_dir / "out"
archive.extract("Grey/SKU-100.jpg", extracted)
with Image.open(extracted / "Grey" / "SKU-100.jpg") as output_image:
self.assertEqual(output_image.size, (12, 8))
def test_lossless_png_output_has_exact_background_pixel_and_png_name(self):
with tempfile.TemporaryDirectory() as temp_dir_name:
temp_dir = Path(temp_dir_name)
zip_path = temp_dir / "input.zip"
output_zip = temp_dir / "result.zip"
image_path = temp_dir / "SKU-200.jpg"
Image.new("RGB", (4, 4), (255, 255, 255)).save(image_path, "JPEG")
with zipfile.ZipFile(zip_path, "w") as archive:
archive.write(image_path, "nested/SKU-200.jpg")
def fake_remove(_image):
cutout = Image.new("RGBA", (4, 4), (0, 0, 0, 0))
cutout.putpixel((1, 1), (20, 40, 60, 255))
cutout.putpixel((2, 1), (20, 40, 60, 255))
return cutout
result = process_zip(
zip_path,
output_zip,
"Grey",
(241, 242, 242),
quality_profile="pro",
output_mode="lossless_png",
remover=fake_remove,
)
self.assertEqual(result.failed_files, [])
with zipfile.ZipFile(output_zip) as archive:
names = set(archive.namelist())
self.assertIn("Grey/SKU-200.png", names)
extracted = temp_dir / "png-out"
archive.extract("Grey/SKU-200.png", extracted)
with Image.open(extracted / "Grey" / "SKU-200.png") as output_image:
self.assertEqual(output_image.size, (4, 4))
self.assertEqual(output_image.getpixel((0, 0)), (241, 242, 242))
def test_all_failed_images_raise_clear_error(self):
with tempfile.TemporaryDirectory() as temp_dir_name:
temp_dir = Path(temp_dir_name)
zip_path = temp_dir / "input.zip"
output_zip = temp_dir / "result.zip"
with zipfile.ZipFile(zip_path, "w") as archive:
archive.writestr("broken.png", b"not an image")
with self.assertRaisesRegex(RuntimeError, "All 1 image files failed"):
process_zip(
zip_path,
output_zip,
"Grey",
(241, 242, 242),
remover=lambda image: image.convert("RGBA"),
)
def test_jpeg_save_kwargs_use_maximum_practical_quality(self):
kwargs = _save_kwargs_for(Path("SKU-100.jpg"), {"icc_profile": b"icc", "exif": b"exif"})
self.assertEqual(kwargs["quality"], 100)
self.assertEqual(kwargs["subsampling"], 0)
self.assertTrue(kwargs["optimize"])
self.assertEqual(kwargs["icc_profile"], b"icc")
self.assertEqual(kwargs["exif"], b"exif")
if __name__ == "__main__":
unittest.main()
|