Skip to content

Commit c5cfde8

Browse files
Add Sign PDF tests
1 parent 03ebf29 commit c5cfde8

8 files changed

Lines changed: 494 additions & 0 deletions

tests/live/test_live_sign_pdf.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient
6+
from pdfrest.models import PdfRestFile
7+
8+
from ..resources import get_test_resource_path
9+
10+
11+
@pytest.fixture(scope="module")
12+
def uploaded_pdf_for_signing(
13+
pdfrest_api_key: str,
14+
pdfrest_live_base_url: str,
15+
) -> PdfRestFile:
16+
resource = get_test_resource_path("report.pdf")
17+
with PdfRestClient(
18+
api_key=pdfrest_api_key,
19+
base_url=pdfrest_live_base_url,
20+
) as client:
21+
return client.files.create_from_paths([resource])[0]
22+
23+
24+
@pytest.fixture(scope="module")
25+
def uploaded_pfx_credential(
26+
pdfrest_api_key: str,
27+
pdfrest_live_base_url: str,
28+
) -> PdfRestFile:
29+
resource = get_test_resource_path("signing_credentials.pfx")
30+
with PdfRestClient(
31+
api_key=pdfrest_api_key,
32+
base_url=pdfrest_live_base_url,
33+
) as client:
34+
return client.files.create_from_paths([resource])[0]
35+
36+
37+
@pytest.fixture(scope="module")
38+
def uploaded_passphrase(
39+
pdfrest_api_key: str,
40+
pdfrest_live_base_url: str,
41+
) -> PdfRestFile:
42+
resource = get_test_resource_path("signing_passphrase.txt")
43+
with PdfRestClient(
44+
api_key=pdfrest_api_key,
45+
base_url=pdfrest_live_base_url,
46+
) as client:
47+
return client.files.create_from_paths([resource])[0]
48+
49+
50+
@pytest.fixture(scope="module")
51+
def uploaded_certificate(
52+
pdfrest_api_key: str,
53+
pdfrest_live_base_url: str,
54+
) -> PdfRestFile:
55+
resource = get_test_resource_path("signing_certificate.pem")
56+
with PdfRestClient(
57+
api_key=pdfrest_api_key,
58+
base_url=pdfrest_live_base_url,
59+
) as client:
60+
return client.files.create_from_paths([resource])[0]
61+
62+
63+
@pytest.fixture(scope="module")
64+
def uploaded_private_key(
65+
pdfrest_api_key: str,
66+
pdfrest_live_base_url: str,
67+
) -> PdfRestFile:
68+
resource = get_test_resource_path("signing_private_key.der")
69+
with PdfRestClient(
70+
api_key=pdfrest_api_key,
71+
base_url=pdfrest_live_base_url,
72+
) as client:
73+
return client.files.create_from_paths([resource])[0]
74+
75+
76+
@pytest.fixture(scope="module")
77+
def uploaded_logo(
78+
pdfrest_api_key: str,
79+
pdfrest_live_base_url: str,
80+
) -> PdfRestFile:
81+
resource = get_test_resource_path("signing_logo.png")
82+
with PdfRestClient(
83+
api_key=pdfrest_api_key,
84+
base_url=pdfrest_live_base_url,
85+
) as client:
86+
return client.files.create_from_paths([resource])[0]
87+
88+
89+
def test_live_sign_pdf_with_pfx_credentials(
90+
pdfrest_api_key: str,
91+
pdfrest_live_base_url: str,
92+
uploaded_pdf_for_signing: PdfRestFile,
93+
uploaded_pfx_credential: PdfRestFile,
94+
uploaded_passphrase: PdfRestFile,
95+
) -> None:
96+
signature_configuration = {
97+
"type": "new",
98+
"name": "pdfrest-live",
99+
}
100+
with PdfRestClient(
101+
api_key=pdfrest_api_key,
102+
base_url=pdfrest_live_base_url,
103+
) as client:
104+
response = client.sign_pdf(
105+
uploaded_pdf_for_signing,
106+
signature_configuration=signature_configuration,
107+
credentials={
108+
"pfx": uploaded_pfx_credential,
109+
"passphrase": uploaded_passphrase,
110+
},
111+
output="live-signed-pfx",
112+
)
113+
114+
assert response.output_file.type == "application/pdf"
115+
assert response.output_file.name == "live-signed-pfx.pdf"
116+
assert str(uploaded_pdf_for_signing.id) in response.input_ids
117+
118+
119+
@pytest.mark.asyncio
120+
async def test_live_async_sign_pdf_with_certificate(
121+
pdfrest_api_key: str,
122+
pdfrest_live_base_url: str,
123+
uploaded_pdf_for_signing: PdfRestFile,
124+
uploaded_certificate: PdfRestFile,
125+
uploaded_private_key: PdfRestFile,
126+
uploaded_logo: PdfRestFile,
127+
) -> None:
128+
signature_configuration = {
129+
"type": "new",
130+
"logo_opacity": 0.5,
131+
"location": {
132+
"bottom_left": {"x": 0, "y": 0},
133+
"top_right": {"x": 216, "y": 72},
134+
"page": 1,
135+
},
136+
}
137+
async with AsyncPdfRestClient(
138+
api_key=pdfrest_api_key,
139+
base_url=pdfrest_live_base_url,
140+
) as client:
141+
response = await client.sign_pdf(
142+
uploaded_pdf_for_signing,
143+
signature_configuration=signature_configuration,
144+
credentials={
145+
"certificate": uploaded_certificate,
146+
"private_key": uploaded_private_key,
147+
},
148+
logo=uploaded_logo,
149+
output="live-signed-cert",
150+
)
151+
152+
assert response.output_file.type == "application/pdf"
153+
assert response.output_file.name == "live-signed-cert.pdf"
154+
assert uploaded_logo.id in response.input_ids
155+
156+
157+
def test_live_sign_pdf_invalid_signature_configuration(
158+
pdfrest_api_key: str,
159+
pdfrest_live_base_url: str,
160+
uploaded_pdf_for_signing: PdfRestFile,
161+
uploaded_pfx_credential: PdfRestFile,
162+
uploaded_passphrase: PdfRestFile,
163+
) -> None:
164+
with (
165+
PdfRestClient(
166+
api_key=pdfrest_api_key,
167+
base_url=pdfrest_live_base_url,
168+
) as client,
169+
pytest.raises(PdfRestApiError),
170+
):
171+
client.sign_pdf(
172+
uploaded_pdf_for_signing,
173+
signature_configuration={"type": "new"},
174+
credentials={
175+
"pfx": uploaded_pfx_credential,
176+
"passphrase": uploaded_passphrase,
177+
},
178+
extra_body={"signature_configuration": "not-json"},
179+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDDzCCAfegAwIBAgIUF0/LIPW1an831Oa3vegdRgAcvZgwDQYJKoZIhvcNAQEL
3+
BQAwFzEVMBMGA1UEAwwMcGRmcmVzdC10ZXN0MB4XDTI2MDExNjIyNDQwMVoXDTM2
4+
MDExNDIyNDQwMVowFzEVMBMGA1UEAwwMcGRmcmVzdC10ZXN0MIIBIjANBgkqhkiG
5+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEApp1yT4py9K3TGZa7PFALZmfmkG8EBvK1WFg2
6+
jDO7p7jgyeM5a8m91fHdq7MJcTgti3UUoogMwj1wQKRGWzoVyGNLPL4f0UyV7JdW
7+
mzzMjSQhFbc0ZPmZTS2mHNbbf0BgVsmnmvkXzwueV52reZlTaOTwG3M2pJxyb/+f
8+
YXkv8C8CT4dygNXrBm9A6wlvWEX6z5UaniJzMaFTckK1kl2nHceE0W7kXGqQCGzb
9+
Sff8IRH7LAiQaQ29UFdCE3v6kXxaM+HQ7FrHdm80eIJ8YPvxxW2iO+BYYRYnrMeF
10+
5tSeFyDKYbazzjeWDYptnuySyrcQH9IpknqbRXq8L6bj8u21HQIDAQABo1MwUTAd
11+
BgNVHQ4EFgQUhV4AOiZwlC4Uiv3YZfG248wTyiAwHwYDVR0jBBgwFoAUhV4AOiZw
12+
lC4Uiv3YZfG248wTyiAwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC
13+
AQEAkxFvZkOBBpjxGf4lhSEMP3iCm12SMUtc4uDJsyxU3tjsFjIvnmyvr5LTcG0F
14+
pAl6BWaJj2Ex4/mQL6ztqJRy5GINI5ycBaXQAdEyUDY4+SEaYZKPNfCZ1lDAzD+C
15+
LotVxPAiYnxS2pI9n93Av+HThLtkbr8gRpYhND9mGIWxFGxMlIqgWYtmk2frF325
16+
e8oIKIMUluESo3GIGwHMW1SCx7oxxfbxsP3oOQzGQtAHxouaZjXtCAw7vdtZAuRy
17+
8YbSz1mt5G/S07to9O/NxsvvKum7zY1g38R9j823F4plhHwpyNUlE0yuofH3lxZA
18+
HES2QmYhPJPxHNtmXLFmGFYT8Q==
19+
-----END CERTIFICATE-----
2.5 KB
Binary file not shown.

tests/resources/signing_key.pem

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvAIBADANBg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WAyU3k2d/gatm57GHrUwe3fo0+9+D98klZpo8GNIjk
19+
xbp9cuLBBwKBgD17M0mrUQn+fU+RWyexhqKc9ad5JXs1vphupoyCWiBXnvZvGwDS
20+
rqdMSHRGvVlG4eXphWbjEbAJafR8TruttxieT2DOGBmlOG7hZoI3/HUZlEfbIK55
21+
SoeEBr27V2EnagZwI6ClDDb0uUN9e0dfuYocYnNmlS+IDnalYZBhSgz/AoGABiak
22+
g+7w+bndwO1/aCGXXiEnp2EDvqxMyIRh9bdyw2WtH3vg12koQ32rqyPY6Y9i24Yj
23+
u6qfFYzjp79FC+m+2ps04LAIoUGlWuQbvWYaZ+PF0AfggZDC9ZSh3+3L1n0bUMzV
24+
uc5WPhmAfg6CE/ETU5WOzBHABqernp+1FM1lyBcCgYBpv/G3nmuLGJxE7ZdEv21H
25+
oKQeMuzT74u3RbLuLYCjRXOa7XEGHN/ehl27MZvXc4Fl4VbPqR29tPVQ1dAyehfu
26+
0GQZiJuS4Z+o7P5/BVmK/W98NTOgUHCiF2BNO4WND3qRev7rZqMdKtZgJ+cJvZ98
27+
9rhoMDzdXrqa8CzDOfqEbw==
28+
-----END PRIVATE KEY-----

tests/resources/signing_logo.png

68 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
password
1.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)