|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | +from pydantic import ValidationError |
| 8 | + |
| 9 | +from pdfrest import AsyncPdfRestClient, PdfRestClient |
| 10 | +from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID |
| 11 | +from pdfrest.models._internal import UnzipPayload |
| 12 | + |
| 13 | +from .graphics_test_helpers import ( |
| 14 | + ASYNC_API_KEY, |
| 15 | + VALID_API_KEY, |
| 16 | + build_file_info_payload, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def make_zip_file(file_id: str, name: str = "archive.zip") -> PdfRestFile: |
| 21 | + return PdfRestFile.model_validate( |
| 22 | + build_file_info_payload(file_id, name, "application/zip") |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def test_unzip_file_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 27 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 28 | + zip_file = make_zip_file(str(PdfRestFileID.generate())) |
| 29 | + output_id = str(PdfRestFileID.generate()) |
| 30 | + |
| 31 | + payload_dump = UnzipPayload.model_validate({"files": zip_file}).model_dump( |
| 32 | + mode="json", by_alias=True, exclude_none=True, exclude_unset=True |
| 33 | + ) |
| 34 | + |
| 35 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 36 | + |
| 37 | + def handler(request: httpx.Request) -> httpx.Response: |
| 38 | + if request.method == "POST" and request.url.path == "/unzip": |
| 39 | + seen["post"] += 1 |
| 40 | + payload = json.loads(request.content.decode("utf-8")) |
| 41 | + assert payload == payload_dump |
| 42 | + return httpx.Response( |
| 43 | + 200, |
| 44 | + json={ |
| 45 | + "inputId": [zip_file.id], |
| 46 | + "files": [ |
| 47 | + { |
| 48 | + "name": "inner.txt", |
| 49 | + "id": output_id, |
| 50 | + "outputUrl": f"https://api.pdfrest.com/resource/{output_id}", |
| 51 | + } |
| 52 | + ], |
| 53 | + }, |
| 54 | + ) |
| 55 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 56 | + seen["get"] += 1 |
| 57 | + assert request.url.params["format"] == "info" |
| 58 | + return httpx.Response( |
| 59 | + 200, |
| 60 | + json=build_file_info_payload( |
| 61 | + output_id, |
| 62 | + "inner.txt", |
| 63 | + "text/plain", |
| 64 | + ), |
| 65 | + ) |
| 66 | + msg = f"Unexpected request {request.method} {request.url}" |
| 67 | + raise AssertionError(msg) |
| 68 | + |
| 69 | + transport = httpx.MockTransport(handler) |
| 70 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 71 | + response = client.unzip_file(zip_file) |
| 72 | + |
| 73 | + assert seen == {"post": 1, "get": 1} |
| 74 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 75 | + assert response.output_file.name == "inner.txt" |
| 76 | + assert response.output_file.type == "text/plain" |
| 77 | + assert str(response.input_id) == str(zip_file.id) |
| 78 | + |
| 79 | + |
| 80 | +def test_unzip_file_with_password_and_customization( |
| 81 | + monkeypatch: pytest.MonkeyPatch, |
| 82 | +) -> None: |
| 83 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 84 | + zip_file = make_zip_file(str(PdfRestFileID.generate())) |
| 85 | + output_id = str(PdfRestFileID.generate()) |
| 86 | + captured_timeout: dict[str, float | dict[str, float] | None] = {} |
| 87 | + |
| 88 | + password = "Secret123" # noqa: S105 - test-only password fixture |
| 89 | + |
| 90 | + def handler(request: httpx.Request) -> httpx.Response: |
| 91 | + if request.method == "POST" and request.url.path == "/unzip": |
| 92 | + assert request.url.params["trace"] == "sync" |
| 93 | + assert request.headers["X-Debug"] == "sync" |
| 94 | + captured_timeout["value"] = request.extensions.get("timeout") |
| 95 | + payload = json.loads(request.content.decode("utf-8")) |
| 96 | + assert payload["id"] == str(zip_file.id) |
| 97 | + assert payload["password"] == password |
| 98 | + assert payload["diagnostics"] == "on" |
| 99 | + return httpx.Response( |
| 100 | + 200, |
| 101 | + json={ |
| 102 | + "inputId": [zip_file.id], |
| 103 | + "outputId": [output_id], |
| 104 | + }, |
| 105 | + ) |
| 106 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 107 | + assert request.url.params["trace"] == "sync" |
| 108 | + assert request.headers["X-Debug"] == "sync" |
| 109 | + return httpx.Response( |
| 110 | + 200, |
| 111 | + json=build_file_info_payload( |
| 112 | + output_id, |
| 113 | + "custom-unzip.txt", |
| 114 | + "text/plain", |
| 115 | + ), |
| 116 | + ) |
| 117 | + msg = f"Unexpected request {request.method} {request.url}" |
| 118 | + raise AssertionError(msg) |
| 119 | + |
| 120 | + transport = httpx.MockTransport(handler) |
| 121 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 122 | + response = client.unzip_file( |
| 123 | + zip_file, |
| 124 | + password=password, |
| 125 | + extra_query={"trace": "sync"}, |
| 126 | + extra_headers={"X-Debug": "sync"}, |
| 127 | + extra_body={"diagnostics": "on"}, |
| 128 | + timeout=0.25, |
| 129 | + ) |
| 130 | + |
| 131 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 132 | + assert response.output_file.name == "custom-unzip.txt" |
| 133 | + timeout_value = captured_timeout["value"] |
| 134 | + assert timeout_value is not None |
| 135 | + if isinstance(timeout_value, dict): |
| 136 | + assert all(pytest.approx(0.25) == value for value in timeout_value.values()) |
| 137 | + else: |
| 138 | + assert timeout_value == pytest.approx(0.25) |
| 139 | + |
| 140 | + |
| 141 | +def test_unzip_file_requires_zip(monkeypatch: pytest.MonkeyPatch) -> None: |
| 142 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 143 | + non_zip = PdfRestFile.model_validate( |
| 144 | + build_file_info_payload( |
| 145 | + str(PdfRestFileID.generate()), "document.pdf", "application/pdf" |
| 146 | + ) |
| 147 | + ) |
| 148 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 149 | + |
| 150 | + with ( |
| 151 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 152 | + pytest.raises(ValidationError, match="Must be a ZIP file"), |
| 153 | + ): |
| 154 | + client.unzip_file(non_zip) |
| 155 | + |
| 156 | + |
| 157 | +def test_unzip_file_single_input(monkeypatch: pytest.MonkeyPatch) -> None: |
| 158 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 159 | + first = make_zip_file(str(PdfRestFileID.generate())) |
| 160 | + second = make_zip_file(str(PdfRestFileID.generate())) |
| 161 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 162 | + |
| 163 | + with ( |
| 164 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 165 | + pytest.raises(ValidationError, match="at most 1 item"), |
| 166 | + ): |
| 167 | + client.unzip_file([first, second]) |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.asyncio |
| 171 | +async def test_async_unzip_file(monkeypatch: pytest.MonkeyPatch) -> None: |
| 172 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 173 | + zip_file = make_zip_file(str(PdfRestFileID.generate())) |
| 174 | + output_id = str(PdfRestFileID.generate()) |
| 175 | + |
| 176 | + payload_dump = UnzipPayload.model_validate({"files": zip_file}).model_dump( |
| 177 | + mode="json", by_alias=True, exclude_none=True, exclude_unset=True |
| 178 | + ) |
| 179 | + |
| 180 | + def handler(request: httpx.Request) -> httpx.Response: |
| 181 | + if request.method == "POST" and request.url.path == "/unzip": |
| 182 | + payload = json.loads(request.content.decode("utf-8")) |
| 183 | + assert payload == payload_dump |
| 184 | + return httpx.Response( |
| 185 | + 200, |
| 186 | + json={ |
| 187 | + "inputId": [zip_file.id], |
| 188 | + "files": [ |
| 189 | + { |
| 190 | + "name": "async.txt", |
| 191 | + "id": output_id, |
| 192 | + "outputUrl": f"https://api.pdfrest.com/resource/{output_id}", |
| 193 | + } |
| 194 | + ], |
| 195 | + }, |
| 196 | + ) |
| 197 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 198 | + return httpx.Response( |
| 199 | + 200, |
| 200 | + json=build_file_info_payload( |
| 201 | + output_id, |
| 202 | + "async.txt", |
| 203 | + "text/plain", |
| 204 | + ), |
| 205 | + ) |
| 206 | + msg = f"Unexpected request {request.method} {request.url}" |
| 207 | + raise AssertionError(msg) |
| 208 | + |
| 209 | + transport = httpx.MockTransport(handler) |
| 210 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 211 | + response = await client.unzip_file(zip_file) |
| 212 | + |
| 213 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 214 | + assert response.output_file.name == "async.txt" |
| 215 | + assert str(response.input_id) == str(zip_file.id) |
0 commit comments