Skip to content

Commit 49efc15

Browse files
Add Encrypt/Decrypt PDF tests
Assisted-by: Codex
1 parent 4f742c9 commit 49efc15

2 files changed

Lines changed: 653 additions & 0 deletions

File tree

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
from __future__ import annotations
2+
3+
from typing import cast, get_args
4+
from uuid import uuid4
5+
6+
import pytest
7+
8+
from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient
9+
from pdfrest.models import PdfRestFile
10+
from pdfrest.types import PdfRestriction
11+
12+
from ..resources import get_test_resource_path
13+
14+
PDF_RESTRICTIONS: tuple[PdfRestriction, ...] = cast(
15+
tuple[PdfRestriction, ...], get_args(PdfRestriction)
16+
)
17+
18+
19+
def make_password(label: str) -> str:
20+
return f"{label}-{uuid4().hex}"
21+
22+
23+
@pytest.fixture(scope="module")
24+
def uploaded_pdf_for_encrypt(
25+
pdfrest_api_key: str,
26+
pdfrest_live_base_url: str,
27+
) -> PdfRestFile:
28+
resource = get_test_resource_path("report.pdf")
29+
with PdfRestClient(
30+
api_key=pdfrest_api_key,
31+
base_url=pdfrest_live_base_url,
32+
) as client:
33+
return client.files.create_from_paths([resource])[0]
34+
35+
36+
def test_live_add_open_password(
37+
pdfrest_api_key: str,
38+
pdfrest_live_base_url: str,
39+
uploaded_pdf_for_encrypt: PdfRestFile,
40+
) -> None:
41+
with PdfRestClient(
42+
api_key=pdfrest_api_key,
43+
base_url=pdfrest_live_base_url,
44+
) as client:
45+
response = client.add_open_password(
46+
uploaded_pdf_for_encrypt,
47+
new_open_password=make_password("live-open"),
48+
output="live-encrypted",
49+
)
50+
51+
assert response.output_files
52+
output_file = response.output_file
53+
assert output_file.type == "application/pdf"
54+
assert output_file.name.startswith("live-encrypted")
55+
assert str(response.input_id) == str(uploaded_pdf_for_encrypt.id)
56+
57+
58+
@pytest.mark.asyncio
59+
async def test_live_async_add_open_password(
60+
pdfrest_api_key: str,
61+
pdfrest_live_base_url: str,
62+
uploaded_pdf_for_encrypt: PdfRestFile,
63+
) -> None:
64+
async with AsyncPdfRestClient(
65+
api_key=pdfrest_api_key,
66+
base_url=pdfrest_live_base_url,
67+
) as client:
68+
response = await client.add_open_password(
69+
uploaded_pdf_for_encrypt,
70+
new_open_password=make_password("async-live-open"),
71+
output="async-live-encrypted",
72+
)
73+
74+
assert response.output_files
75+
output_file = response.output_file
76+
assert output_file.type == "application/pdf"
77+
assert output_file.name.startswith("async-live-encrypted")
78+
assert str(response.input_id) == str(uploaded_pdf_for_encrypt.id)
79+
80+
81+
def test_live_change_open_password(
82+
pdfrest_api_key: str,
83+
pdfrest_live_base_url: str,
84+
uploaded_pdf_for_encrypt: PdfRestFile,
85+
) -> None:
86+
with PdfRestClient(
87+
api_key=pdfrest_api_key,
88+
base_url=pdfrest_live_base_url,
89+
) as client:
90+
current_password = make_password("live-open-current")
91+
restricted = client.add_open_password(
92+
uploaded_pdf_for_encrypt,
93+
new_open_password=current_password,
94+
output="live-open-old",
95+
).output_file
96+
response = client.change_open_password(
97+
restricted,
98+
current_open_password=current_password,
99+
new_open_password=make_password("live-open-new"),
100+
output="live-open-new",
101+
)
102+
103+
assert response.output_files
104+
output_file = response.output_file
105+
assert output_file.name.startswith("live-open-new")
106+
assert output_file.type == "application/pdf"
107+
assert str(response.input_id) == str(restricted.id)
108+
109+
110+
@pytest.mark.asyncio
111+
async def test_live_async_change_open_password(
112+
pdfrest_api_key: str,
113+
pdfrest_live_base_url: str,
114+
uploaded_pdf_for_encrypt: PdfRestFile,
115+
) -> None:
116+
async with AsyncPdfRestClient(
117+
api_key=pdfrest_api_key,
118+
base_url=pdfrest_live_base_url,
119+
) as client:
120+
current_password = make_password("async-live-open-current")
121+
restricted = (
122+
await client.add_open_password(
123+
uploaded_pdf_for_encrypt,
124+
new_open_password=current_password,
125+
output="async-live-open-old",
126+
)
127+
).output_file
128+
response = await client.change_open_password(
129+
restricted,
130+
current_open_password=current_password,
131+
new_open_password=make_password("async-live-open-new"),
132+
output="async-live-open-new",
133+
)
134+
135+
assert response.output_files
136+
output_file = response.output_file
137+
assert output_file.name.startswith("async-live-open-new")
138+
assert output_file.type == "application/pdf"
139+
assert str(response.input_id) == str(restricted.id)
140+
141+
142+
def test_live_remove_open_password(
143+
pdfrest_api_key: str,
144+
pdfrest_live_base_url: str,
145+
uploaded_pdf_for_encrypt: PdfRestFile,
146+
) -> None:
147+
with PdfRestClient(
148+
api_key=pdfrest_api_key,
149+
base_url=pdfrest_live_base_url,
150+
) as client:
151+
current_password = make_password("live-open-remove")
152+
restricted = client.add_open_password(
153+
uploaded_pdf_for_encrypt,
154+
new_open_password=current_password,
155+
output="live-open-to-remove",
156+
).output_file
157+
response = client.remove_open_password(
158+
restricted,
159+
current_open_password=current_password,
160+
output="live-open-removed",
161+
)
162+
163+
assert response.output_files
164+
output_file = response.output_file
165+
assert output_file.type == "application/pdf"
166+
assert output_file.name.startswith("live-open-removed")
167+
assert str(response.input_id) == str(restricted.id)
168+
169+
170+
@pytest.mark.asyncio
171+
async def test_live_async_remove_open_password(
172+
pdfrest_api_key: str,
173+
pdfrest_live_base_url: str,
174+
uploaded_pdf_for_encrypt: PdfRestFile,
175+
) -> None:
176+
async with AsyncPdfRestClient(
177+
api_key=pdfrest_api_key,
178+
base_url=pdfrest_live_base_url,
179+
) as client:
180+
current_password = make_password("async-live-open-remove")
181+
restricted = (
182+
await client.add_open_password(
183+
uploaded_pdf_for_encrypt,
184+
new_open_password=current_password,
185+
output="async-live-open-to-remove",
186+
)
187+
).output_file
188+
response = await client.remove_open_password(
189+
restricted,
190+
current_open_password=current_password,
191+
output="async-live-open-removed",
192+
)
193+
194+
assert response.output_files
195+
output_file = response.output_file
196+
assert output_file.type == "application/pdf"
197+
assert output_file.name.startswith("async-live-open-removed")
198+
assert str(response.input_id) == str(restricted.id)
199+
200+
201+
def test_live_remove_open_password_invalid_password(
202+
pdfrest_api_key: str,
203+
pdfrest_live_base_url: str,
204+
uploaded_pdf_for_encrypt: PdfRestFile,
205+
) -> None:
206+
with PdfRestClient(
207+
api_key=pdfrest_api_key,
208+
base_url=pdfrest_live_base_url,
209+
) as client:
210+
correct_password = make_password("live-open-correct")
211+
wrong_password = make_password("live-open-wrong")
212+
restricted = client.add_open_password(
213+
uploaded_pdf_for_encrypt,
214+
new_open_password=correct_password,
215+
output="live-open-invalid",
216+
).output_file
217+
with pytest.raises(PdfRestApiError, match="open password"):
218+
client.remove_open_password(
219+
restricted,
220+
current_open_password=wrong_password,
221+
)
222+
223+
224+
@pytest.mark.asyncio
225+
async def test_live_async_remove_open_password_invalid_password(
226+
pdfrest_api_key: str,
227+
pdfrest_live_base_url: str,
228+
uploaded_pdf_for_encrypt: PdfRestFile,
229+
) -> None:
230+
async with AsyncPdfRestClient(
231+
api_key=pdfrest_api_key,
232+
base_url=pdfrest_live_base_url,
233+
) as client:
234+
correct_password = make_password("async-live-open-correct")
235+
wrong_password = make_password("async-live-open-wrong")
236+
restricted = (
237+
await client.add_open_password(
238+
uploaded_pdf_for_encrypt,
239+
new_open_password=correct_password,
240+
output="async-live-open-invalid",
241+
)
242+
).output_file
243+
with pytest.raises(PdfRestApiError, match="open password"):
244+
await client.remove_open_password(
245+
restricted,
246+
current_open_password=wrong_password,
247+
)

0 commit comments

Comments
 (0)