summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJames R. Barlow <jim@purplerock.ca>2019-06-18 16:04:05 -0700
committerJames R. Barlow <jim@purplerock.ca>2019-06-18 16:04:05 -0700
commita1df4a83510cbcc7ae05bc832985589c9d0feb9b (patch)
tree4dd20251f08ca9b3ccc3afd170be8382e7f6a7ba /tests
parentff85997ef280ed948c686d036e89eebf720c31be (diff)
Encryption: add saving
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py9
-rw-r--r--tests/test_encrypt.py50
2 files changed, 57 insertions, 2 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 6694c77..8887415 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -20,5 +20,10 @@ def resources():
@pytest.fixture(scope="function")
-def outdir(tmpdir):
- return Path(str(tmpdir))
+def outdir(tmp_path):
+ return tmp_path
+
+
+@pytest.fixture(scope="function")
+def outpdf(tmp_path):
+ return tmp_path / 'out.pdf'
diff --git a/tests/test_encrypt.py b/tests/test_encrypt.py
new file mode 100644
index 0000000..89aebf3
--- /dev/null
+++ b/tests/test_encrypt.py
@@ -0,0 +1,50 @@
+import pytest
+
+import pikepdf
+
+
+@pytest.fixture
+def trivial(resources):
+ return pikepdf.open(resources / 'pal-1bit-trivial.pdf')
+
+
+@pytest.mark.parametrize(
+ "R,owner,user",
+ [
+ (6, "foo", "bar"),
+ (4, "password", "password"),
+ (3, "12345678", "secret"),
+ (2, "qwerty", "123456"),
+ ],
+)
+def test_encrypt_basic(trivial, outpdf, R, owner, user):
+ trivial.save(outpdf, encryption=dict(R=R, owner=owner, user=user))
+ pdf_owner = pikepdf.open(outpdf, password=owner)
+ assert pdf_owner.is_encrypted
+ pdf_user = pikepdf.open(outpdf, password=user)
+ assert pdf_user.is_encrypted
+
+
+def test_encrypt_R5(trivial, outpdf):
+ with pytest.warns(UserWarning):
+ trivial.save(outpdf, encryption=dict(R=5, owner='foo', user='foo'))
+
+
+@pytest.mark.parametrize("R", [-1, 0, 1, 7, 9, 42])
+def test_encrypt_invalid_level_value(trivial, outpdf, R):
+ with pytest.raises(ValueError):
+ trivial.save(outpdf, encryption=dict(R=R, owner='foo', user='foo'))
+
+
+@pytest.mark.parametrize("R", [3.14, '6', b'6', None])
+def test_encrypt_invalid_level(trivial, outpdf, R):
+ with pytest.raises(TypeError):
+ trivial.save(outpdf, encryption=dict(R=R, owner='foo', user='foo'))
+
+
+def test_encrypt_without_owner(trivial, outpdf):
+ trivial.save(outpdf, encryption=dict(user='foo'))
+
+
+def test_encrypt_no_passwords(trivial, outpdf):
+ trivial.save(outpdf, encryption=dict(R=6))