Skip to content

Commit cde8bb2

Browse files
Add Restrict/Unrestrict PDF tests
Assisted-by: Codex
1 parent 3d293fa commit cde8bb2

2 files changed

Lines changed: 964 additions & 0 deletions

File tree

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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+
15+
def make_password(label: str) -> str:
16+
return f"{label}-{uuid4().hex}"
17+
18+
19+
PDF_RESTRICTIONS: tuple[PdfRestriction, ...] = cast(
20+
tuple[PdfRestriction, ...], get_args(PdfRestriction)
21+
)
22+
RESTRICTION_PARAMS = [
23+
pytest.param(restriction, id=restriction) for restriction in PDF_RESTRICTIONS
24+
]
25+
26+
27+
@pytest.fixture(scope="module")
28+
def uploaded_pdf_for_permissions(
29+
pdfrest_api_key: str,
30+
pdfrest_live_base_url: str,
31+
) -> PdfRestFile:
32+
resource = get_test_resource_path("report.pdf")
33+
with PdfRestClient(
34+
api_key=pdfrest_api_key,
35+
base_url=pdfrest_live_base_url,
36+
) as client:
37+
return client.files.create_from_paths([resource])[0]
38+
39+
40+
@pytest.mark.parametrize("restriction", RESTRICTION_PARAMS)
41+
def test_live_add_permissions_password(
42+
pdfrest_api_key: str,
43+
pdfrest_live_base_url: str,
44+
uploaded_pdf_for_permissions: PdfRestFile,
45+
restriction: PdfRestriction,
46+
) -> None:
47+
with PdfRestClient(
48+
api_key=pdfrest_api_key,
49+
base_url=pdfrest_live_base_url,
50+
) as client:
51+
new_password = make_password("live-perm")
52+
response = client.add_permissions_password(
53+
uploaded_pdf_for_permissions,
54+
new_permissions_password=new_password,
55+
restrictions=[restriction],
56+
output="live-restrict",
57+
)
58+
59+
assert response.output_files
60+
output_file = response.output_file
61+
assert output_file.type == "application/pdf"
62+
assert output_file.name.startswith("live-restrict")
63+
assert str(response.input_id) == str(uploaded_pdf_for_permissions.id)
64+
65+
66+
@pytest.mark.asyncio
67+
@pytest.mark.parametrize("restriction", RESTRICTION_PARAMS)
68+
async def test_live_async_add_permissions_password(
69+
pdfrest_api_key: str,
70+
pdfrest_live_base_url: str,
71+
uploaded_pdf_for_permissions: PdfRestFile,
72+
restriction: PdfRestriction,
73+
) -> None:
74+
async with AsyncPdfRestClient(
75+
api_key=pdfrest_api_key,
76+
base_url=pdfrest_live_base_url,
77+
) as client:
78+
new_password = make_password("async-live-perm")
79+
response = await client.add_permissions_password(
80+
uploaded_pdf_for_permissions,
81+
new_permissions_password=new_password,
82+
restrictions=[restriction],
83+
output="async-restrict",
84+
)
85+
86+
assert response.output_files
87+
output_file = response.output_file
88+
assert output_file.type == "application/pdf"
89+
assert output_file.name.startswith("async-restrict")
90+
assert str(response.input_id) == str(uploaded_pdf_for_permissions.id)
91+
92+
93+
def test_live_change_permissions_password(
94+
pdfrest_api_key: str,
95+
pdfrest_live_base_url: str,
96+
uploaded_pdf_for_permissions: PdfRestFile,
97+
) -> None:
98+
with PdfRestClient(
99+
api_key=pdfrest_api_key,
100+
base_url=pdfrest_live_base_url,
101+
) as client:
102+
current_password = make_password("old-live")
103+
new_password = make_password("new-live")
104+
restricted_response = client.add_permissions_password(
105+
uploaded_pdf_for_permissions,
106+
new_permissions_password=current_password,
107+
restrictions=["print_low"],
108+
output="live-restrict-old",
109+
)
110+
restricted_file = restricted_response.output_file
111+
response = client.change_permissions_password(
112+
restricted_file,
113+
current_permissions_password=current_password,
114+
new_permissions_password=new_password,
115+
restrictions=["copy_content"],
116+
output="live-restrict-new",
117+
)
118+
119+
assert response.output_files
120+
output_file = response.output_file
121+
assert output_file.name.startswith("live-restrict-new")
122+
assert output_file.type == "application/pdf"
123+
assert str(response.input_id) == str(restricted_file.id)
124+
125+
126+
@pytest.mark.asyncio
127+
async def test_live_async_change_permissions_password(
128+
pdfrest_api_key: str,
129+
pdfrest_live_base_url: str,
130+
uploaded_pdf_for_permissions: PdfRestFile,
131+
) -> None:
132+
async with AsyncPdfRestClient(
133+
api_key=pdfrest_api_key,
134+
base_url=pdfrest_live_base_url,
135+
) as client:
136+
current_password = make_password("async-old")
137+
new_password = make_password("async-new")
138+
restricted_response = await client.add_permissions_password(
139+
uploaded_pdf_for_permissions,
140+
new_permissions_password=current_password,
141+
restrictions=["edit_content"],
142+
output="async-live-restrict-old",
143+
)
144+
restricted_file = restricted_response.output_file
145+
response = await client.change_permissions_password(
146+
restricted_file,
147+
current_permissions_password=current_password,
148+
new_permissions_password=new_password,
149+
restrictions=["edit_annotations"],
150+
output="async-live-restrict-new",
151+
)
152+
153+
assert response.output_files
154+
output_file = response.output_file
155+
assert output_file.name.startswith("async-live-restrict-new")
156+
assert output_file.type == "application/pdf"
157+
assert str(response.input_id) == str(restricted_file.id)
158+
159+
160+
def test_live_remove_permissions_password(
161+
pdfrest_api_key: str,
162+
pdfrest_live_base_url: str,
163+
uploaded_pdf_for_permissions: PdfRestFile,
164+
) -> None:
165+
with PdfRestClient(
166+
api_key=pdfrest_api_key,
167+
base_url=pdfrest_live_base_url,
168+
) as client:
169+
current_password = make_password("remove-live")
170+
restricted_response = client.add_permissions_password(
171+
uploaded_pdf_for_permissions,
172+
new_permissions_password=current_password,
173+
restrictions=["print_high"],
174+
output="live-to-remove",
175+
)
176+
restricted_file = restricted_response.output_file
177+
response = client.remove_permissions_password(
178+
restricted_file,
179+
current_permissions_password=current_password,
180+
output="live-removed",
181+
)
182+
183+
assert response.output_files
184+
output_file = response.output_file
185+
assert output_file.name.startswith("live-removed")
186+
assert output_file.type == "application/pdf"
187+
assert str(response.input_id) == str(restricted_file.id)
188+
189+
190+
@pytest.mark.asyncio
191+
async def test_live_async_remove_permissions_password(
192+
pdfrest_api_key: str,
193+
pdfrest_live_base_url: str,
194+
uploaded_pdf_for_permissions: PdfRestFile,
195+
) -> None:
196+
async with AsyncPdfRestClient(
197+
api_key=pdfrest_api_key,
198+
base_url=pdfrest_live_base_url,
199+
) as client:
200+
current_password = make_password("async-remove")
201+
restricted_response = await client.add_permissions_password(
202+
uploaded_pdf_for_permissions,
203+
new_permissions_password=current_password,
204+
restrictions=["accessibility_off"],
205+
output="async-live-to-remove",
206+
)
207+
restricted_file = restricted_response.output_file
208+
response = await client.remove_permissions_password(
209+
restricted_file,
210+
current_permissions_password=current_password,
211+
output="async-live-removed",
212+
)
213+
214+
assert response.output_files
215+
output_file = response.output_file
216+
assert output_file.name.startswith("async-live-removed")
217+
assert output_file.type == "application/pdf"
218+
assert str(response.input_id) == str(restricted_file.id)
219+
220+
221+
def test_live_remove_permissions_password_invalid_password(
222+
pdfrest_api_key: str,
223+
pdfrest_live_base_url: str,
224+
uploaded_pdf_for_permissions: PdfRestFile,
225+
) -> None:
226+
with PdfRestClient(
227+
api_key=pdfrest_api_key,
228+
base_url=pdfrest_live_base_url,
229+
) as client:
230+
correct_password = make_password("live-wrong")
231+
wrong_password = make_password("incorrect")
232+
restricted_response = client.add_permissions_password(
233+
uploaded_pdf_for_permissions,
234+
new_permissions_password=correct_password,
235+
restrictions=["edit_annotations"],
236+
output="live-invalid-remove",
237+
)
238+
restricted_file = restricted_response.output_file
239+
with pytest.raises(PdfRestApiError, match="permissions password"):
240+
client.remove_permissions_password(
241+
restricted_file,
242+
current_permissions_password=wrong_password,
243+
)
244+
245+
246+
@pytest.mark.asyncio
247+
async def test_live_async_remove_permissions_password_invalid_password(
248+
pdfrest_api_key: str,
249+
pdfrest_live_base_url: str,
250+
uploaded_pdf_for_permissions: PdfRestFile,
251+
) -> None:
252+
async with AsyncPdfRestClient(
253+
api_key=pdfrest_api_key,
254+
base_url=pdfrest_live_base_url,
255+
) as client:
256+
correct_password = make_password("async-live-wrong")
257+
wrong_password = make_password("async-wrong")
258+
restricted_response = await client.add_permissions_password(
259+
uploaded_pdf_for_permissions,
260+
new_permissions_password=correct_password,
261+
restrictions=["edit_content"],
262+
output="async-live-invalid-remove",
263+
)
264+
restricted_file = restricted_response.output_file
265+
with pytest.raises(PdfRestApiError, match="permissions password"):
266+
await client.remove_permissions_password(
267+
restricted_file,
268+
current_permissions_password=wrong_password,
269+
)

0 commit comments

Comments
 (0)