|
15 | 15 | ASYNC_API_KEY, |
16 | 16 | VALID_API_KEY, |
17 | 17 | build_file_info_payload, |
| 18 | + make_image_file, |
18 | 19 | make_pdf_file, |
19 | 20 | ) |
20 | 21 |
|
@@ -216,6 +217,62 @@ def handler(_: httpx.Request) -> httpx.Response: |
216 | 217 | ) |
217 | 218 |
|
218 | 219 |
|
| 220 | +def test_add_text_to_pdf_pdf_mime_validation(monkeypatch: pytest.MonkeyPatch) -> None: |
| 221 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 222 | + |
| 223 | + def handler(_: httpx.Request) -> httpx.Response: |
| 224 | + pytest.fail("Request should not be sent when validation fails.") |
| 225 | + |
| 226 | + transport = httpx.MockTransport(handler) |
| 227 | + with ( |
| 228 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 229 | + pytest.raises(ValidationError, match="Must be a PDF file"), |
| 230 | + ): |
| 231 | + client.add_text_to_pdf( |
| 232 | + make_image_file(PdfRestFileID.generate(1)), |
| 233 | + text_objects=[make_text_object()], |
| 234 | + ) |
| 235 | + |
| 236 | + |
| 237 | +def test_add_text_to_pdf_page_minimum(monkeypatch: pytest.MonkeyPatch) -> None: |
| 238 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 239 | + |
| 240 | + def handler(_: httpx.Request) -> httpx.Response: |
| 241 | + pytest.fail("Request should not be sent when validation fails.") |
| 242 | + |
| 243 | + transport = httpx.MockTransport(handler) |
| 244 | + with ( |
| 245 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 246 | + pytest.raises(ValidationError, match="greater than or equal to 1"), |
| 247 | + ): |
| 248 | + client.add_text_to_pdf( |
| 249 | + make_pdf_file(PdfRestFileID.generate(1)), |
| 250 | + text_objects=[make_text_object(page=0)], |
| 251 | + ) |
| 252 | + |
| 253 | + |
| 254 | +def test_add_text_to_pdf_rejects_both_color_modes( |
| 255 | + monkeypatch: pytest.MonkeyPatch, |
| 256 | +) -> None: |
| 257 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 258 | + |
| 259 | + def handler(_: httpx.Request) -> httpx.Response: |
| 260 | + pytest.fail("Request should not be sent when validation fails.") |
| 261 | + |
| 262 | + transport = httpx.MockTransport(handler) |
| 263 | + with ( |
| 264 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 265 | + pytest.raises( |
| 266 | + ValidationError, |
| 267 | + match=re.escape("Provide only one of text_color_rgb or text_color_cmyk."), |
| 268 | + ), |
| 269 | + ): |
| 270 | + client.add_text_to_pdf( |
| 271 | + make_pdf_file(PdfRestFileID.generate(1)), |
| 272 | + text_objects=[make_text_object(text_color_cmyk=(0, 0, 0, 0))], |
| 273 | + ) |
| 274 | + |
| 275 | + |
219 | 276 | def test_add_text_to_pdf_rgb_bounds(monkeypatch: pytest.MonkeyPatch) -> None: |
220 | 277 | monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
221 | 278 |
|
@@ -429,6 +486,63 @@ def handler(_: httpx.Request) -> httpx.Response: |
429 | 486 | ) |
430 | 487 |
|
431 | 488 |
|
| 489 | +@pytest.mark.asyncio |
| 490 | +async def test_async_add_text_to_pdf_pdf_mime_validation( |
| 491 | + monkeypatch: pytest.MonkeyPatch, |
| 492 | +) -> None: |
| 493 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 494 | + |
| 495 | + def handler(_: httpx.Request) -> httpx.Response: |
| 496 | + pytest.fail("Request should not be sent when validation fails.") |
| 497 | + |
| 498 | + transport = httpx.MockTransport(handler) |
| 499 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 500 | + with pytest.raises(ValidationError, match="Must be a PDF file"): |
| 501 | + await client.add_text_to_pdf( |
| 502 | + make_image_file(PdfRestFileID.generate(1)), |
| 503 | + text_objects=[make_text_object()], |
| 504 | + ) |
| 505 | + |
| 506 | + |
| 507 | +@pytest.mark.asyncio |
| 508 | +async def test_async_add_text_to_pdf_page_minimum( |
| 509 | + monkeypatch: pytest.MonkeyPatch, |
| 510 | +) -> None: |
| 511 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 512 | + |
| 513 | + def handler(_: httpx.Request) -> httpx.Response: |
| 514 | + pytest.fail("Request should not be sent when validation fails.") |
| 515 | + |
| 516 | + transport = httpx.MockTransport(handler) |
| 517 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 518 | + with pytest.raises(ValidationError, match="greater than or equal to 1"): |
| 519 | + await client.add_text_to_pdf( |
| 520 | + make_pdf_file(PdfRestFileID.generate(1)), |
| 521 | + text_objects=[make_text_object(page=0)], |
| 522 | + ) |
| 523 | + |
| 524 | + |
| 525 | +@pytest.mark.asyncio |
| 526 | +async def test_async_add_text_to_pdf_rejects_both_color_modes( |
| 527 | + monkeypatch: pytest.MonkeyPatch, |
| 528 | +) -> None: |
| 529 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 530 | + |
| 531 | + def handler(_: httpx.Request) -> httpx.Response: |
| 532 | + pytest.fail("Request should not be sent when validation fails.") |
| 533 | + |
| 534 | + transport = httpx.MockTransport(handler) |
| 535 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 536 | + with pytest.raises( |
| 537 | + ValidationError, |
| 538 | + match=re.escape("Provide only one of text_color_rgb or text_color_cmyk."), |
| 539 | + ): |
| 540 | + await client.add_text_to_pdf( |
| 541 | + make_pdf_file(PdfRestFileID.generate(1)), |
| 542 | + text_objects=[make_text_object(text_color_cmyk=(0, 0, 0, 0))], |
| 543 | + ) |
| 544 | + |
| 545 | + |
432 | 546 | @pytest.mark.asyncio |
433 | 547 | async def test_async_add_text_to_pdf_requires_color( |
434 | 548 | monkeypatch: pytest.MonkeyPatch, |
|
0 commit comments