|
| 1 | +"""Integration tests for image file watermark functionality.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from nutrient_dws import NutrientClient |
| 9 | + |
| 10 | +try: |
| 11 | + from . import integration_config # type: ignore[attr-defined] |
| 12 | + |
| 13 | + API_KEY: Optional[str] = integration_config.API_KEY |
| 14 | + BASE_URL: Optional[str] = getattr(integration_config, "BASE_URL", None) |
| 15 | + TIMEOUT: int = getattr(integration_config, "TIMEOUT", 60) |
| 16 | +except ImportError: |
| 17 | + API_KEY = None |
| 18 | + BASE_URL = None |
| 19 | + TIMEOUT = 60 |
| 20 | + |
| 21 | + |
| 22 | +def assert_is_pdf(file_path_or_bytes): |
| 23 | + """Assert that a file or bytes is a valid PDF.""" |
| 24 | + if isinstance(file_path_or_bytes, str): |
| 25 | + with open(file_path_or_bytes, "rb") as f: |
| 26 | + content = f.read(8) |
| 27 | + else: |
| 28 | + content = file_path_or_bytes[:8] |
| 29 | + |
| 30 | + assert content.startswith(b"%PDF-"), ( |
| 31 | + f"File does not start with PDF magic number, got: {content!r}" |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def create_test_image(tmp_path, filename="watermark.png"): |
| 36 | + """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) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.skipif(not API_KEY, reason="No API key configured in integration_config.py") |
| 50 | +class TestWatermarkImageFileIntegration: |
| 51 | + """Integration tests for image file watermark functionality.""" |
| 52 | + |
| 53 | + @pytest.fixture |
| 54 | + def client(self): |
| 55 | + """Create a client with the configured API key.""" |
| 56 | + client = NutrientClient(api_key=API_KEY, timeout=TIMEOUT) |
| 57 | + yield client |
| 58 | + client.close() |
| 59 | + |
| 60 | + @pytest.fixture |
| 61 | + def sample_pdf_path(self): |
| 62 | + """Get path to sample PDF file for testing.""" |
| 63 | + return os.path.join(os.path.dirname(__file__), "..", "data", "sample.pdf") |
| 64 | + |
| 65 | + def test_watermark_pdf_with_image_file_path(self, client, sample_pdf_path, tmp_path): |
| 66 | + """Test watermark_pdf with local image file path.""" |
| 67 | + # Create a test image |
| 68 | + image_path = create_test_image(tmp_path) |
| 69 | + |
| 70 | + result = client.watermark_pdf( |
| 71 | + sample_pdf_path, |
| 72 | + image_file=image_path, |
| 73 | + width=100, |
| 74 | + height=50, |
| 75 | + opacity=0.5, |
| 76 | + position="bottom-right" |
| 77 | + ) |
| 78 | + |
| 79 | + assert isinstance(result, bytes) |
| 80 | + assert len(result) > 0 |
| 81 | + assert_is_pdf(result) |
| 82 | + |
| 83 | + def test_watermark_pdf_with_image_bytes(self, client, sample_pdf_path): |
| 84 | + """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 | + ) |
| 91 | + |
| 92 | + result = client.watermark_pdf( |
| 93 | + sample_pdf_path, |
| 94 | + image_file=png_bytes, |
| 95 | + width=150, |
| 96 | + height=75, |
| 97 | + opacity=0.8, |
| 98 | + position="top-left" |
| 99 | + ) |
| 100 | + |
| 101 | + assert isinstance(result, bytes) |
| 102 | + assert len(result) > 0 |
| 103 | + assert_is_pdf(result) |
| 104 | + |
| 105 | + def test_watermark_pdf_with_image_file_output_path(self, client, sample_pdf_path, tmp_path): |
| 106 | + """Test watermark_pdf with image file saving to output path.""" |
| 107 | + # Create a test image |
| 108 | + image_path = create_test_image(tmp_path) |
| 109 | + output_path = str(tmp_path / "watermarked_with_image.pdf") |
| 110 | + |
| 111 | + result = client.watermark_pdf( |
| 112 | + sample_pdf_path, |
| 113 | + image_file=image_path, |
| 114 | + width=200, |
| 115 | + height=100, |
| 116 | + opacity=0.7, |
| 117 | + position="center", |
| 118 | + output_path=output_path |
| 119 | + ) |
| 120 | + |
| 121 | + assert result is None |
| 122 | + assert (tmp_path / "watermarked_with_image.pdf").exists() |
| 123 | + assert (tmp_path / "watermarked_with_image.pdf").stat().st_size > 0 |
| 124 | + assert_is_pdf(output_path) |
| 125 | + |
| 126 | + def test_watermark_pdf_with_file_like_object(self, client, sample_pdf_path, tmp_path): |
| 127 | + """Test watermark_pdf with image as file-like object.""" |
| 128 | + # Create a test image |
| 129 | + image_path = create_test_image(tmp_path) |
| 130 | + |
| 131 | + # Read as file-like object |
| 132 | + with open(image_path, "rb") as image_file: |
| 133 | + result = client.watermark_pdf( |
| 134 | + sample_pdf_path, |
| 135 | + image_file=image_file, |
| 136 | + width=120, |
| 137 | + height=60, |
| 138 | + opacity=0.6, |
| 139 | + position="top-center" |
| 140 | + ) |
| 141 | + |
| 142 | + assert isinstance(result, bytes) |
| 143 | + assert len(result) > 0 |
| 144 | + assert_is_pdf(result) |
| 145 | + |
| 146 | + def test_builder_api_with_image_file_watermark(self, client, sample_pdf_path, tmp_path): |
| 147 | + """Test Builder API with image file watermark.""" |
| 148 | + # Create a test image |
| 149 | + image_path = create_test_image(tmp_path) |
| 150 | + |
| 151 | + # Use builder API |
| 152 | + result = ( |
| 153 | + client.build(sample_pdf_path) |
| 154 | + .add_step("watermark-pdf", options={ |
| 155 | + "image_file": image_path, |
| 156 | + "width": 180, |
| 157 | + "height": 90, |
| 158 | + "opacity": 0.4, |
| 159 | + "position": "bottom-left" |
| 160 | + }) |
| 161 | + .execute() |
| 162 | + ) |
| 163 | + |
| 164 | + assert isinstance(result, bytes) |
| 165 | + assert len(result) > 0 |
| 166 | + assert_is_pdf(result) |
| 167 | + |
| 168 | + def test_multiple_watermarks_with_image_files(self, client, sample_pdf_path, tmp_path): |
| 169 | + """Test applying multiple watermarks including image files.""" |
| 170 | + # Create test images |
| 171 | + image1_path = create_test_image(tmp_path, "watermark1.png") |
| 172 | + |
| 173 | + # Chain multiple watermark operations |
| 174 | + result = ( |
| 175 | + client.build(sample_pdf_path) |
| 176 | + .add_step("watermark-pdf", options={ |
| 177 | + "text": "DRAFT", |
| 178 | + "width": 200, |
| 179 | + "height": 100, |
| 180 | + "opacity": 0.3, |
| 181 | + "position": "center" |
| 182 | + }) |
| 183 | + .add_step("watermark-pdf", options={ |
| 184 | + "image_file": image1_path, |
| 185 | + "width": 100, |
| 186 | + "height": 50, |
| 187 | + "opacity": 0.5, |
| 188 | + "position": "top-right" |
| 189 | + }) |
| 190 | + .execute() |
| 191 | + ) |
| 192 | + |
| 193 | + assert isinstance(result, bytes) |
| 194 | + assert len(result) > 0 |
| 195 | + assert_is_pdf(result) |
| 196 | + |
0 commit comments