mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Add unit tests for document.py
This commit is contained in:
parent
a068770ab4
commit
be5a942a73
3 changed files with 103 additions and 4 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -20,3 +21,22 @@ for_each_doc = pytest.mark.parametrize("doc", test_docs)
|
||||||
|
|
||||||
class TestBase:
|
class TestBase:
|
||||||
sample_doc = str(test_docs_dir.joinpath(BASIC_SAMPLE))
|
sample_doc = str(test_docs_dir.joinpath(BASIC_SAMPLE))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_doc() -> str:
|
||||||
|
return str(test_docs_dir.joinpath(BASIC_SAMPLE))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def unwriteable_pdf(tmp_path: Path) -> str:
|
||||||
|
file_path = tmp_path / "document.pdf"
|
||||||
|
file_path.touch(mode=0o400)
|
||||||
|
return str(file_path)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def unreadable_pdf(tmp_path: Path) -> str:
|
||||||
|
file_path = tmp_path / "document.pdf"
|
||||||
|
file_path.touch(mode=0o000)
|
||||||
|
return str(file_path)
|
||||||
|
|
|
@ -191,10 +191,9 @@ class TestCliConversion(TestCliBasic):
|
||||||
"spaces test.pdf",
|
"spaces test.pdf",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_filenames(self, filename: str) -> None:
|
def test_filenames(self, filename: str, tmp_path: Path) -> None:
|
||||||
tempdir = tempfile.mkdtemp(prefix="dangerzone-")
|
doc_path = str(Path(tmp_path).joinpath(filename))
|
||||||
doc_path = os.path.join(filename)
|
|
||||||
shutil.copyfile(self.sample_doc, doc_path)
|
shutil.copyfile(self.sample_doc, doc_path)
|
||||||
result = self.run_cli(doc_path)
|
result = self.run_cli(doc_path)
|
||||||
shutil.rmtree(tempdir)
|
|
||||||
result.assert_success()
|
result.assert_success()
|
||||||
|
assert len(os.listdir(tmp_path)) == 2
|
||||||
|
|
80
tests/test_document.py
Normal file
80
tests/test_document.py
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from dangerzone.document import Document
|
||||||
|
from dangerzone.errors import DocumentFilenameException
|
||||||
|
|
||||||
|
from . import sample_doc, unreadable_pdf, unwriteable_pdf
|
||||||
|
|
||||||
|
|
||||||
|
def test_input_sample_init(sample_doc: str) -> None:
|
||||||
|
Document(sample_doc)
|
||||||
|
|
||||||
|
|
||||||
|
def test_input_sample_after(sample_doc: str) -> None:
|
||||||
|
d = Document()
|
||||||
|
d.input_filename = sample_doc
|
||||||
|
|
||||||
|
|
||||||
|
def test_input_file_none() -> None:
|
||||||
|
"""
|
||||||
|
Attempts to read a document's filename when no doc has been set
|
||||||
|
"""
|
||||||
|
d = Document()
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
d.input_filename
|
||||||
|
assert "Input filename has not been set yet" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_input_file_non_existing() -> None:
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
Document("non-existing-file.pdf")
|
||||||
|
assert "Input file not found" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
# XXX: This is not easy to test on Windows, as the file owner can always read it.
|
||||||
|
# See also:
|
||||||
|
# https://stackoverflow.com/questions/72528318/what-file-permissions-make-a-file-unreadable-by-owner-in-windows
|
||||||
|
@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific")
|
||||||
|
def test_input_file_unreadable(unreadable_pdf: str) -> None:
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
Document(unreadable_pdf)
|
||||||
|
assert "don't have permission to open the input file" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_output_file_unwriteable(unwriteable_pdf: str) -> None:
|
||||||
|
d = Document()
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
d.output_filename = unwriteable_pdf
|
||||||
|
assert "Safe PDF filename is not writable" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_output(tmp_path: Path) -> None:
|
||||||
|
pdf_file = str(tmp_path.joinpath("document.pdf"))
|
||||||
|
d = Document()
|
||||||
|
d.output_filename = pdf_file
|
||||||
|
|
||||||
|
|
||||||
|
def test_output_file_none() -> None:
|
||||||
|
"""
|
||||||
|
Attempts to read a document's filename when no doc has been set
|
||||||
|
"""
|
||||||
|
d = Document()
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
d.output_filename
|
||||||
|
assert "Output filename has not been set yet" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_output_file_not_pdf(tmp_path: Path) -> None:
|
||||||
|
docx_file = str(tmp_path.joinpath("document.docx"))
|
||||||
|
d = Document()
|
||||||
|
|
||||||
|
with pytest.raises(DocumentFilenameException) as e:
|
||||||
|
d.output_filename = docx_file
|
||||||
|
assert "Safe PDF filename must end in '.pdf'" in str(e.value)
|
||||||
|
|
||||||
|
assert not os.path.exists(docx_file)
|
Loading…
Reference in a new issue