Skip to content

Commit 85ff431

Browse files
tests: Complete encrypt customization coverage
Assisted-by: Codex
1 parent 988817d commit 85ff431

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

tests/test_encrypt_pdf.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,77 @@ def handler(request: httpx.Request) -> httpx.Response:
153153
)
154154

155155

156+
def test_add_open_password_request_customization(
157+
monkeypatch: pytest.MonkeyPatch,
158+
) -> None:
159+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
160+
input_file = make_pdf_file(PdfRestFileID.generate(1))
161+
output_id = str(PdfRestFileID.generate())
162+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
163+
new_password = make_password("open-custom")
164+
permissions_password = make_password("perm-custom")
165+
payload_dump = build_encrypt_payload(
166+
input_file,
167+
new_open_password=new_password,
168+
current_permissions_password=permissions_password,
169+
output="encrypted-custom",
170+
)
171+
172+
def handler(request: httpx.Request) -> httpx.Response:
173+
if request.method == "POST" and request.url.path == "/encrypted-pdf":
174+
assert request.url.params["trace"] == "sync"
175+
assert request.headers["X-Debug"] == "sync"
176+
captured_timeout["value"] = request.extensions.get("timeout")
177+
payload = json.loads(request.content.decode("utf-8"))
178+
assert payload == {**payload_dump, "diagnostics": "on"}
179+
return httpx.Response(
180+
200,
181+
json={
182+
"inputId": [input_file.id],
183+
"outputId": [output_id],
184+
},
185+
)
186+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
187+
assert request.url.params["format"] == "info"
188+
assert request.url.params["trace"] == "sync"
189+
assert request.headers["X-Debug"] == "sync"
190+
return httpx.Response(
191+
200,
192+
json=build_file_info_payload(
193+
output_id,
194+
"encrypted-custom.pdf",
195+
"application/pdf",
196+
),
197+
)
198+
msg = f"Unexpected request {request.method} {request.url}"
199+
raise AssertionError(msg)
200+
201+
transport = httpx.MockTransport(handler)
202+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
203+
response = client.add_open_password(
204+
input_file,
205+
new_open_password=new_password,
206+
current_permissions_password=permissions_password,
207+
output="encrypted-custom",
208+
extra_query={"trace": "sync"},
209+
extra_headers={"X-Debug": "sync"},
210+
extra_body={"diagnostics": "on"},
211+
timeout=0.71,
212+
)
213+
214+
assert_pdf_file_response(
215+
response,
216+
expected_name="encrypted-custom.pdf",
217+
input_file=input_file,
218+
)
219+
timeout_value = captured_timeout["value"]
220+
assert timeout_value is not None
221+
if isinstance(timeout_value, dict):
222+
assert all(pytest.approx(0.71) == value for value in timeout_value.values())
223+
else:
224+
assert timeout_value == pytest.approx(0.71)
225+
226+
156227
def test_change_open_password_request_customization(
157228
monkeypatch: pytest.MonkeyPatch,
158229
) -> None:
@@ -484,6 +555,81 @@ def handler(request: httpx.Request) -> httpx.Response:
484555
)
485556

486557

558+
@pytest.mark.asyncio
559+
async def test_async_change_open_password_request_customization(
560+
monkeypatch: pytest.MonkeyPatch,
561+
) -> None:
562+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
563+
input_file = make_pdf_file(PdfRestFileID.generate(2))
564+
output_id = str(PdfRestFileID.generate())
565+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
566+
current_password = make_password("async-open-current-custom")
567+
new_password = make_password("async-open-next-custom")
568+
permissions_password = make_password("async-open-perm-custom")
569+
payload_dump = build_encrypt_payload(
570+
input_file,
571+
current_open_password=current_password,
572+
new_open_password=new_password,
573+
current_permissions_password=permissions_password,
574+
output="async-rotated-open-custom",
575+
)
576+
577+
def handler(request: httpx.Request) -> httpx.Response:
578+
if request.method == "POST" and request.url.path == "/encrypted-pdf":
579+
assert request.url.params["trace"] == "async"
580+
assert request.headers["X-Debug"] == "async"
581+
captured_timeout["value"] = request.extensions.get("timeout")
582+
payload = json.loads(request.content.decode("utf-8"))
583+
assert payload == {**payload_dump, "audit": "yes"}
584+
return httpx.Response(
585+
200,
586+
json={
587+
"inputId": [input_file.id],
588+
"outputId": [output_id],
589+
},
590+
)
591+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
592+
assert request.url.params["format"] == "info"
593+
assert request.url.params["trace"] == "async"
594+
assert request.headers["X-Debug"] == "async"
595+
return httpx.Response(
596+
200,
597+
json=build_file_info_payload(
598+
output_id,
599+
"async-rotated-open-custom.pdf",
600+
"application/pdf",
601+
),
602+
)
603+
msg = f"Unexpected request {request.method} {request.url}"
604+
raise AssertionError(msg)
605+
606+
transport = httpx.MockTransport(handler)
607+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
608+
response = await client.change_open_password(
609+
input_file,
610+
current_open_password=current_password,
611+
new_open_password=new_password,
612+
current_permissions_password=permissions_password,
613+
output="async-rotated-open-custom",
614+
extra_query={"trace": "async"},
615+
extra_headers={"X-Debug": "async"},
616+
extra_body={"audit": "yes"},
617+
timeout=0.74,
618+
)
619+
620+
assert_pdf_file_response(
621+
response,
622+
expected_name="async-rotated-open-custom.pdf",
623+
input_file=input_file,
624+
)
625+
timeout_value = captured_timeout["value"]
626+
assert timeout_value is not None
627+
if isinstance(timeout_value, dict):
628+
assert all(pytest.approx(0.74) == value for value in timeout_value.values())
629+
else:
630+
assert timeout_value == pytest.approx(0.74)
631+
632+
487633
@pytest.mark.asyncio
488634
async def test_async_remove_open_password_success(
489635
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)