Skip to content

Commit 590490f

Browse files
tests: Assert encryption/password payloads via public payload models
- add `pdfrest.models.payloads` as a public export surface for `PdfEncryptPayload`, `PdfDecryptPayload`, `PdfRestrictPayload`, and `PdfUnrestrictPayload` - re-export payload models from `pdfrest.models` for test usage without private imports - update encrypt/restrict unit tests to build expected JSON via `model_validate(...).model_dump(...)` instead of hand-built dicts - fix a circular import by importing `PdfRestFile` from `models.public` inside `_internal.py` Assisted-by: Codex
1 parent eff7194 commit 590490f

5 files changed

Lines changed: 79 additions & 50 deletions

File tree

src/pdfrest/models/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from .payloads import (
2+
PdfDecryptPayload,
3+
PdfEncryptPayload,
4+
PdfRestrictPayload,
5+
PdfUnrestrictPayload,
6+
)
17
from .public import (
28
PdfRestDeletionResponse,
39
PdfRestErrorResponse,
@@ -12,12 +18,16 @@
1218
)
1319

1420
__all__ = [
21+
"PdfDecryptPayload",
22+
"PdfEncryptPayload",
1523
"PdfRestDeletionResponse",
1624
"PdfRestErrorResponse",
1725
"PdfRestFile",
1826
"PdfRestFileBasedResponse",
1927
"PdfRestFileID",
2028
"PdfRestInfoResponse",
29+
"PdfRestrictPayload",
30+
"PdfUnrestrictPayload",
2131
"SummarizePdfTextResponse",
2232
"TranslatePdfTextFileResponse",
2333
"TranslatePdfTextResponse",

src/pdfrest/models/_internal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
SummaryOutputType,
3434
TranslateOutputFormat,
3535
)
36-
from . import PdfRestFile
37-
from .public import PdfRestFileID
36+
from .public import PdfRestFile, PdfRestFileID
3837

3938

4039
def _ensure_list(value: Any) -> Any:

src/pdfrest/models/payloads.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Public payload models used to validate request serialization in tests."""
2+
3+
from ._internal import (
4+
PdfDecryptPayload,
5+
PdfEncryptPayload,
6+
PdfRestrictPayload,
7+
PdfUnrestrictPayload,
8+
)
9+
10+
__all__ = (
11+
"PdfDecryptPayload",
12+
"PdfEncryptPayload",
13+
"PdfRestrictPayload",
14+
"PdfUnrestrictPayload",
15+
)

tests/test_encrypt_pdf.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from pydantic import ValidationError
99

1010
from pdfrest import AsyncPdfRestClient, PdfRestClient
11-
from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID
11+
from pdfrest.models import (
12+
PdfDecryptPayload,
13+
PdfEncryptPayload,
14+
PdfRestFile,
15+
PdfRestFileBasedResponse,
16+
PdfRestFileID,
17+
)
1218

1319
from .graphics_test_helpers import (
1420
ASYNC_API_KEY,
@@ -39,18 +45,16 @@ def build_encrypt_payload(
3945
current_open_password: str | None = None,
4046
current_permissions_password: str | None = None,
4147
output: str | None = None,
42-
) -> dict[str, str]:
43-
payload: dict[str, str] = {
44-
"id": str(input_file.id),
45-
"new_open_password": new_open_password,
46-
}
47-
if current_open_password is not None:
48-
payload["current_open_password"] = current_open_password
49-
if current_permissions_password is not None:
50-
payload["current_permissions_password"] = current_permissions_password
51-
if output is not None:
52-
payload["output"] = output
53-
return payload
48+
) -> dict[str, object]:
49+
return PdfEncryptPayload.model_validate(
50+
{
51+
"files": [input_file],
52+
"new_open_password": new_open_password,
53+
"current_open_password": current_open_password,
54+
"current_permissions_password": current_permissions_password,
55+
"output": output,
56+
}
57+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
5458

5559

5660
def build_decrypt_payload(
@@ -59,16 +63,15 @@ def build_decrypt_payload(
5963
current_open_password: str,
6064
current_permissions_password: str | None = None,
6165
output: str | None = None,
62-
) -> dict[str, str]:
63-
payload: dict[str, str] = {
64-
"id": str(input_file.id),
65-
"current_open_password": current_open_password,
66-
}
67-
if current_permissions_password is not None:
68-
payload["current_permissions_password"] = current_permissions_password
69-
if output is not None:
70-
payload["output"] = output
71-
return payload
66+
) -> dict[str, object]:
67+
return PdfDecryptPayload.model_validate(
68+
{
69+
"files": [input_file],
70+
"current_open_password": current_open_password,
71+
"current_permissions_password": current_permissions_password,
72+
"output": output,
73+
}
74+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
7275

7376

7477
@pytest.mark.parametrize(

tests/test_permissions_password.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from pydantic import ValidationError
99

1010
from pdfrest import AsyncPdfRestClient, PdfRestClient
11-
from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID
11+
from pdfrest.models import (
12+
PdfRestFile,
13+
PdfRestFileBasedResponse,
14+
PdfRestFileID,
15+
PdfRestrictPayload,
16+
PdfUnrestrictPayload,
17+
)
1218
from pdfrest.types import PdfRestriction
1319

1420
from .graphics_test_helpers import (
@@ -41,20 +47,17 @@ def build_restrict_payload(
4147
current_open_password: str | None = None,
4248
restrictions: list[PdfRestriction] | None = None,
4349
output: str | None = None,
44-
) -> dict[str, str | list[PdfRestriction]]:
45-
payload: dict[str, str | list[PdfRestriction]] = {
46-
"id": str(input_file.id),
47-
"new_permissions_password": new_permissions_password,
48-
}
49-
if current_permissions_password is not None:
50-
payload["current_permissions_password"] = current_permissions_password
51-
if current_open_password is not None:
52-
payload["current_open_password"] = current_open_password
53-
if restrictions is not None:
54-
payload["restrictions"] = restrictions
55-
if output is not None:
56-
payload["output"] = output
57-
return payload
50+
) -> dict[str, object]:
51+
return PdfRestrictPayload.model_validate(
52+
{
53+
"files": [input_file],
54+
"new_permissions_password": new_permissions_password,
55+
"current_permissions_password": current_permissions_password,
56+
"current_open_password": current_open_password,
57+
"restrictions": restrictions,
58+
"output": output,
59+
}
60+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
5861

5962

6063
def build_unrestrict_payload(
@@ -63,16 +66,15 @@ def build_unrestrict_payload(
6366
current_permissions_password: str,
6467
current_open_password: str | None = None,
6568
output: str | None = None,
66-
) -> dict[str, str]:
67-
payload: dict[str, str] = {
68-
"id": str(input_file.id),
69-
"current_permissions_password": current_permissions_password,
70-
}
71-
if current_open_password is not None:
72-
payload["current_open_password"] = current_open_password
73-
if output is not None:
74-
payload["output"] = output
75-
return payload
69+
) -> dict[str, object]:
70+
return PdfUnrestrictPayload.model_validate(
71+
{
72+
"files": [input_file],
73+
"current_permissions_password": current_permissions_password,
74+
"current_open_password": current_open_password,
75+
"output": output,
76+
}
77+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
7678

7779

7880
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)