Skip to content

Commit 6426620

Browse files
tests: Add and enhance tests for signing PDF with invalid configurations
- Updated `test_live_sign_pdf_invalid_signature_configuration` to improve validation by matching against specific error messages. - Added `test_live_async_sign_pdf_invalid_signature_configuration` to test async behavior for invalid signature configurations. - Introduced `test_sign_pdf_request_customization` to validate request headers, query params, payload, and timeout customization during PDF signing. - Ensured stricter validation of malformed JSON in signature configuration. Assisted-by: Codex
1 parent fa764f9 commit 6426620

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

tests/live/test_live_sign_pdf.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ def test_live_sign_pdf_invalid_signature_configuration(
216216
api_key=pdfrest_api_key,
217217
base_url=pdfrest_live_base_url,
218218
) as client,
219-
pytest.raises(PdfRestApiError),
219+
pytest.raises(
220+
PdfRestApiError,
221+
match=r"JSON data provided is not properly formatted",
222+
),
220223
):
221224
client.sign_pdf(
222225
uploaded_pdf_for_signing,
@@ -230,3 +233,33 @@ def test_live_sign_pdf_invalid_signature_configuration(
230233
},
231234
extra_body={"signature_configuration": "not-json"},
232235
)
236+
237+
238+
@pytest.mark.asyncio
239+
async def test_live_async_sign_pdf_invalid_signature_configuration(
240+
pdfrest_api_key: str,
241+
pdfrest_live_base_url: str,
242+
uploaded_pdf_for_signing: PdfRestFile,
243+
uploaded_pfx_credential: PdfRestFile,
244+
uploaded_passphrase: PdfRestFile,
245+
) -> None:
246+
async with AsyncPdfRestClient(
247+
api_key=pdfrest_api_key,
248+
base_url=pdfrest_live_base_url,
249+
) as client:
250+
with pytest.raises(
251+
PdfRestApiError,
252+
match=r"JSON data provided is not properly formatted",
253+
):
254+
await client.sign_pdf(
255+
uploaded_pdf_for_signing,
256+
signature_configuration={
257+
"type": "new",
258+
"location": make_signature_location(),
259+
},
260+
credentials={
261+
"pfx": uploaded_pfx_credential,
262+
"passphrase": uploaded_passphrase,
263+
},
264+
extra_body={"signature_configuration": "not-json"},
265+
)

tests/test_sign_pdf.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,74 @@ def test_sign_pdf_requires_location_for_new_signature_type(
235235
)
236236

237237

238+
def test_sign_pdf_request_customization(
239+
monkeypatch: pytest.MonkeyPatch,
240+
) -> None:
241+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
242+
input_file = make_pdf_file(PdfRestFileID.generate())
243+
certificate_file = make_certificate_file(str(PdfRestFileID.generate()))
244+
private_key_file = make_private_key_file(str(PdfRestFileID.generate()))
245+
output_id = str(PdfRestFileID.generate())
246+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
247+
248+
def handler(request: httpx.Request) -> httpx.Response:
249+
if request.method == "POST" and request.url.path == "/signed-pdf":
250+
assert request.url.params["trace"] == "sync"
251+
assert request.headers["X-Debug"] == "sync-sign"
252+
captured_timeout["value"] = request.extensions.get("timeout")
253+
payload = json.loads(request.content.decode("utf-8"))
254+
assert payload["certificate_id"] == str(certificate_file.id)
255+
assert payload["private_key_id"] == str(private_key_file.id)
256+
assert json.loads(payload["signature_configuration"])["type"] == "new"
257+
assert payload["output"] == "sync-signed"
258+
assert payload["diagnostics"] == "on"
259+
return httpx.Response(
260+
200,
261+
json={"inputId": [input_file.id], "outputId": [output_id]},
262+
)
263+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
264+
assert request.url.params["trace"] == "sync"
265+
assert request.headers["X-Debug"] == "sync-sign"
266+
return httpx.Response(
267+
200,
268+
json=build_file_info_payload(
269+
output_id,
270+
"sync-signed.pdf",
271+
"application/pdf",
272+
),
273+
)
274+
msg = f"Unexpected request {request.method} {request.url}"
275+
raise AssertionError(msg)
276+
277+
transport = httpx.MockTransport(handler)
278+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
279+
response = client.sign_pdf(
280+
input_file,
281+
signature_configuration={
282+
"type": "new",
283+
"location": make_signature_location(),
284+
},
285+
credentials={
286+
"certificate": certificate_file,
287+
"private_key": private_key_file,
288+
},
289+
output="sync-signed",
290+
extra_query={"trace": "sync"},
291+
extra_headers={"X-Debug": "sync-sign"},
292+
extra_body={"diagnostics": "on"},
293+
timeout=0.5,
294+
)
295+
296+
assert isinstance(response, PdfRestFileBasedResponse)
297+
assert response.output_file.name == "sync-signed.pdf"
298+
timeout_value = captured_timeout["value"]
299+
assert timeout_value is not None
300+
if isinstance(timeout_value, dict):
301+
assert all(pytest.approx(0.5) == value for value in timeout_value.values())
302+
else:
303+
assert timeout_value == pytest.approx(0.5)
304+
305+
238306
@pytest.mark.asyncio
239307
async def test_async_sign_pdf_request_customization(
240308
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)