|
| 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 | + ) |
0 commit comments