|
6 | 6 | import pytest |
7 | 7 | from pydantic import ValidationError |
8 | 8 |
|
9 | | -from pdfrest import PdfRestClient |
| 9 | +from pdfrest import AsyncPdfRestClient, PdfRestClient |
10 | 10 | from pdfrest.models import PdfRestFileBasedResponse, PdfRestFileID |
11 | 11 | from pdfrest.models._internal import PdfRedactionApplyPayload |
12 | 12 | from pdfrest.types import PdfRGBColor |
13 | 13 |
|
14 | | -from .graphics_test_helpers import VALID_API_KEY, build_file_info_payload, make_pdf_file |
| 14 | +from .graphics_test_helpers import ( |
| 15 | + ASYNC_API_KEY, |
| 16 | + VALID_API_KEY, |
| 17 | + build_file_info_payload, |
| 18 | + make_pdf_file, |
| 19 | +) |
15 | 20 |
|
16 | 21 |
|
17 | 22 | @pytest.mark.parametrize( |
@@ -87,3 +92,63 @@ def test_apply_redactions_invalid_color(monkeypatch: pytest.MonkeyPatch) -> None |
87 | 92 |
|
88 | 93 | with pytest.raises(ValidationError, match="greater than or equal to 0"): |
89 | 94 | client.apply_redactions(input_file, rgb_color=[-1, 0, 0]) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.asyncio |
| 98 | +async def test_async_apply_redactions_includes_rgb_color( |
| 99 | + monkeypatch: pytest.MonkeyPatch, |
| 100 | +) -> None: |
| 101 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 102 | + input_file = make_pdf_file(PdfRestFileID.generate(2)) |
| 103 | + output_id = str(PdfRestFileID.generate()) |
| 104 | + |
| 105 | + payload_data: dict[str, object] = { |
| 106 | + "files": [input_file], |
| 107 | + "rgb_color": (10, 20, 30), |
| 108 | + "output": "async-output", |
| 109 | + } |
| 110 | + |
| 111 | + payload_model_dump = PdfRedactionApplyPayload.model_validate( |
| 112 | + payload_data |
| 113 | + ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
| 114 | + |
| 115 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 116 | + |
| 117 | + def handler(request: httpx.Request) -> httpx.Response: |
| 118 | + if ( |
| 119 | + request.method == "POST" |
| 120 | + and request.url.path == "/pdf-with-redacted-text-applied" |
| 121 | + ): |
| 122 | + seen["post"] += 1 |
| 123 | + body = json.loads(request.content.decode("utf-8")) |
| 124 | + assert body == payload_model_dump |
| 125 | + return httpx.Response( |
| 126 | + 200, |
| 127 | + json={ |
| 128 | + "inputId": [input_file.id], |
| 129 | + "outputId": [output_id], |
| 130 | + }, |
| 131 | + ) |
| 132 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 133 | + seen["get"] += 1 |
| 134 | + return httpx.Response( |
| 135 | + 200, |
| 136 | + json=build_file_info_payload( |
| 137 | + output_id, "async-output.pdf", "application/pdf" |
| 138 | + ), |
| 139 | + ) |
| 140 | + msg = f"Unexpected request {request.method} {request.url}" |
| 141 | + raise AssertionError(msg) |
| 142 | + |
| 143 | + transport = httpx.MockTransport(handler) |
| 144 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 145 | + response = await client.apply_redactions( |
| 146 | + input_file, |
| 147 | + rgb_color=(10, 20, 30), |
| 148 | + output="async-output", |
| 149 | + ) |
| 150 | + |
| 151 | + assert seen == {"post": 1, "get": 1} |
| 152 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 153 | + assert response.output_files[0].name == "async-output.pdf" |
| 154 | + assert response.output_files[0].type == "application/pdf" |
0 commit comments