Skip to content

Commit 5c8a681

Browse files
committed
fix: update test images to use valid non-transparent PNGs
The API was returning 500 errors when using 1x1 transparent PNGs for watermarks. Updated create_test_image() to generate proper 100x100 RGB images using PIL when available, with fallback to a 2x2 colored PNG. This fixes all watermark image file integration tests.
1 parent 0c369c0 commit 5c8a681

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

tests/integration/test_watermark_image_file_integration.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@ def assert_is_pdf(file_path_or_bytes: str | bytes) -> None:
3434

3535
def create_test_image(tmp_path: Path, filename: str = "watermark.png") -> str:
3636
"""Create a simple test PNG image."""
37-
# PNG header for a 1x1 transparent pixel
38-
png_data = (
39-
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
40-
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f"
41-
b"\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82"
42-
)
43-
44-
image_path = tmp_path / filename
45-
image_path.write_bytes(png_data)
46-
return str(image_path)
37+
try:
38+
# Try to use PIL to create a proper image
39+
from PIL import Image
40+
img = Image.new('RGB', (100, 100), color='red')
41+
image_path = tmp_path / filename
42+
img.save(str(image_path))
43+
return str(image_path)
44+
except ImportError:
45+
# Fallback to a simple but valid PNG if PIL is not available
46+
# This is a 2x2 red PNG image
47+
png_data = (
48+
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00\x02'
49+
b'\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf'
50+
b'\xc0\x00\x00\x03\x01\x01\x00\x18\xdd\x8d\xb4\x00\x00\x00\x00IEND\xaeB`\x82'
51+
)
52+
image_path = tmp_path / filename
53+
image_path.write_bytes(png_data)
54+
return str(image_path)
4755

4856

4957
@pytest.mark.skipif(not API_KEY, reason="No API key configured in integration_config.py")
@@ -82,12 +90,21 @@ def test_watermark_pdf_with_image_file_path(self, client, sample_pdf_path, tmp_p
8290

8391
def test_watermark_pdf_with_image_bytes(self, client, sample_pdf_path):
8492
"""Test watermark_pdf with image as bytes."""
85-
# PNG header for a 1x1 transparent pixel
86-
png_bytes = (
87-
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
88-
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f"
89-
b"\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82"
90-
)
93+
# Create a proper PNG image as bytes
94+
try:
95+
from PIL import Image
96+
import io
97+
img = Image.new('RGB', (100, 100), color='blue')
98+
img_buffer = io.BytesIO()
99+
img.save(img_buffer, format='PNG')
100+
png_bytes = img_buffer.getvalue()
101+
except ImportError:
102+
# Fallback to a 2x2 blue PNG if PIL is not available
103+
png_bytes = (
104+
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00\x02'
105+
b'\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00\x00\x0cIDATx\x9cc\x98\x00'
106+
b'\x00\x00\x05\x00\x01\x85\xb7\xb2\xf3\x00\x00\x00\x00IEND\xaeB`\x82'
107+
)
91108

92109
result = client.watermark_pdf(
93110
sample_pdf_path,

0 commit comments

Comments
 (0)