Skip to content

Commit 90ef0bb

Browse files
tests: Add tests for sanitizing and replacing demo redacted values
- Added `test_query_pdf_info_demo_redacted_booleans_replaced` in `test_query_pdf_info.py` to validate detection and replacement of demo redacted boolean and integer fields with appropriate values. - Added `test_unzip_file_demo_redacted_id_replaced_and_logged` in `test_unzip_file.py` to verify sanitization of redacted file IDs and logging of replacements. - Ensured proper log messages were emitted for all demo value replacements. Assisted-by: Codex
1 parent 9f0da25 commit 90ef0bb

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

tests/test_query_pdf_info.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import logging
45
from collections.abc import Sequence
56

67
import httpx
@@ -238,3 +239,80 @@ def handler(request: httpx.Request) -> httpx.Response:
238239
assert isinstance(response, PdfRestInfoResponse)
239240
assert response.tagged is True
240241
assert response.all_queries_processed is True
242+
243+
244+
def test_query_pdf_info_demo_redacted_booleans_replaced(
245+
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
246+
) -> None:
247+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
248+
caplog.set_level(logging.WARNING, logger="pdfrest.models")
249+
input_file = make_pdf_file(str(PdfRestFileID.generate()))
250+
251+
def handler(request: httpx.Request) -> httpx.Response:
252+
if request.method != "POST" or request.url.path != "/pdf-info":
253+
msg = f"Unexpected request {request.method} {request.url}"
254+
raise AssertionError(msg)
255+
return httpx.Response(
256+
200,
257+
json={
258+
"inputId": str(input_file.id),
259+
"tagged": "fa***",
260+
"image_only": "fa***",
261+
"contains_annotations": "fa***",
262+
"contains_signature": "fa***",
263+
"file_size": "25***",
264+
"restrict_permissions_set": "fa***",
265+
"contains_xfa": "fa***",
266+
"contains_acroforms": "fa***",
267+
"contains_javascript": "fa***",
268+
"contains_transparency": "fa***",
269+
"contains_embedded_file": "fa***",
270+
"uses_embedded_fonts": "fa***",
271+
"uses_nonembedded_fonts": "fa***",
272+
"pdfa": "fa***",
273+
"pdfua_claim": "fa***",
274+
"pdfe_claim": "fa***",
275+
"pdfx_claim": "fa***",
276+
"requires_password_to_open": "fa***",
277+
"allQueriesProcessed": "tr**",
278+
},
279+
)
280+
281+
transport = httpx.MockTransport(handler)
282+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
283+
response = client.query_pdf_info(
284+
input_file,
285+
queries=ALL_PDF_INFO_QUERIES,
286+
)
287+
288+
assert response.tagged is False
289+
assert response.image_only is False
290+
assert response.contains_annotations is False
291+
assert response.contains_signature is False
292+
assert response.file_size == 0
293+
assert response.restrict_permissions_set is False
294+
assert response.contains_xfa is False
295+
assert response.contains_acroforms is False
296+
assert response.contains_javascript is False
297+
assert response.contains_transparency is False
298+
assert response.contains_embedded_file is False
299+
assert response.uses_embedded_fonts is False
300+
assert response.uses_nonembedded_fonts is False
301+
assert response.pdfa is False
302+
assert response.pdfua_claim is False
303+
assert response.pdfe_claim is False
304+
assert response.pdfx_claim is False
305+
assert response.requires_password_to_open is False
306+
assert response.all_queries_processed is True
307+
assert "Demo value fa*** detected in tagged; replaced with False" in caplog.text
308+
assert "Demo value 25*** detected in file_size; replaced with 0" in caplog.text
309+
assert "Demo value fa*** detected in pdfe_claim; replaced with False" in caplog.text
310+
assert "Demo value fa*** detected in pdfx_claim; replaced with False" in caplog.text
311+
assert (
312+
"Demo value fa*** detected in requires_password_to_open; replaced with False"
313+
in caplog.text
314+
)
315+
assert (
316+
"Demo value tr** detected in all_queries_processed; replaced with True"
317+
in caplog.text
318+
)

tests/test_unzip_file.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import logging
45

56
import httpx
67
import pytest
@@ -16,6 +17,8 @@
1617
build_file_info_payload,
1718
)
1819

20+
DEMO_REPLACEMENT_ID = "00000000-0000-4000-8000-000000000000"
21+
1922

2023
def make_zip_file(file_id: str, name: str = "archive.zip") -> PdfRestFile:
2124
return PdfRestFile.model_validate(
@@ -302,3 +305,55 @@ async def test_async_unzip_file_single_input(monkeypatch: pytest.MonkeyPatch) ->
302305
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
303306
with pytest.raises(ValidationError, match="at most 1 item"):
304307
await client.unzip_file([first, second])
308+
309+
310+
def test_unzip_file_demo_redacted_id_replaced_and_logged(
311+
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
312+
) -> None:
313+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
314+
caplog.set_level(logging.WARNING, logger="pdfrest.models")
315+
zip_file = make_zip_file(str(PdfRestFileID.generate()))
316+
redacted_id = "XXXXXXXXX-XXXXXXXXX-XXXX-XXXXXXXXXXXX"
317+
318+
def handler(request: httpx.Request) -> httpx.Response:
319+
if request.method == "POST" and request.url.path == "/unzip":
320+
return httpx.Response(
321+
200,
322+
json={
323+
"inputId": [zip_file.id],
324+
"files": [
325+
{
326+
"name": "inner.txt",
327+
"id": redacted_id,
328+
"outputUrl": (
329+
"https://api.pdfrest.com/resource/"
330+
f"{redacted_id}?format=file"
331+
),
332+
}
333+
],
334+
},
335+
)
336+
if (
337+
request.method == "GET"
338+
and request.url.path == f"/resource/{DEMO_REPLACEMENT_ID}"
339+
):
340+
return httpx.Response(
341+
200,
342+
json=build_file_info_payload(
343+
DEMO_REPLACEMENT_ID,
344+
"inner.txt",
345+
"text/plain",
346+
),
347+
)
348+
msg = f"Unexpected request {request.method} {request.url}"
349+
raise AssertionError(msg)
350+
351+
transport = httpx.MockTransport(handler)
352+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
353+
response = client.unzip_file(zip_file)
354+
355+
assert response.output_file.id == DEMO_REPLACEMENT_ID
356+
assert (
357+
"Demo value XXXXXXXXX-XXXXXXXXX-XXXX-XXXXXXXXXXXX detected in id; "
358+
"replaced with 00000000-0000-4000-8000-000000000000" in caplog.text
359+
)

0 commit comments

Comments
 (0)