diff --git a/dangerzone/document.py b/dangerzone/document.py index 681202f54..267dea7ee 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -64,5 +64,5 @@ def output_filename(self, filename: str) -> None: class DocumentFilenameException(Exception): - def __init__(self, message): + def __init__(self, message: str) -> None: super().__init__(message) diff --git a/tests/__init__.py b/tests/__init__.py index cad40abc7..bb7683f58 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -20,3 +20,8 @@ class TestBase: sample_doc = str(test_docs_dir.joinpath(BASIC_SAMPLE)) + + +@pytest.fixture +def sample_doc(): + return str(test_docs_dir.joinpath(BASIC_SAMPLE)) diff --git a/tests/test_cli.py b/tests/test_cli.py index f39a98d50..c4d5dec39 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,8 +1,8 @@ from __future__ import annotations -import os import shutil import tempfile +from pathlib import Path import pytest from click.testing import CliRunner, Result @@ -92,7 +92,7 @@ def test_lang_eng(self): ) def test_filenames(self, filename): tempdir = tempfile.mkdtemp(prefix="dangerzone-") - doc_path = os.path.join(filename) + doc_path = str(Path(tempdir).joinpath(filename)) shutil.copyfile(self.sample_doc, doc_path) result = self.run_cli(doc_path) shutil.rmtree(tempdir) diff --git a/tests/test_document.py b/tests/test_document.py new file mode 100644 index 000000000..a00b10c64 --- /dev/null +++ b/tests/test_document.py @@ -0,0 +1,87 @@ +import os +import tempfile + +import pytest + +from dangerzone.document import DocumentFilenameException, DocumentHolder + +from . import sample_doc + + +@pytest.fixture +def unwriteable_pdf(): + file_path = tempfile.mktemp(suffix=".pdf") + with open(file_path, "w+"): + # create file + pass + os.chmod(file_path, mode=600) + return file_path + + +@pytest.fixture +def unreadable_pdf(): + file_path = tempfile.mktemp(suffix=".pdf") + with open(file_path, "w+"): + # create file + pass + os.chmod(file_path, mode=0) + return file_path + + +def test_input_sample_init(sample_doc): + DocumentHolder(sample_doc) + + +def test_input_sample_after(sample_doc): + d = DocumentHolder() + d.input_filename = sample_doc + + +def test_input_file_none(): + """ + Attempts to read a document's filename when no doc has been set + """ + d = DocumentHolder() + with pytest.raises(DocumentFilenameException): + d.input_filename + + +def test_input_file_non_existing(): + with pytest.raises(DocumentFilenameException): + DocumentHolder("fake-dir/non-existing-file.pdf") + + +def test_input_file_unreadable(unreadable_pdf): + with pytest.raises(DocumentFilenameException): + DocumentHolder(unreadable_pdf) + + +def test_output_file_unwriteable(unwriteable_pdf): + d = DocumentHolder() + with pytest.raises(DocumentFilenameException): + d.output_filename = unwriteable_pdf + + +def test_output(): + pdf_file = tempfile.mktemp(suffix=".pdf") + d = DocumentHolder() + d.output_filename = pdf_file + + +def test_output_file_none(): + """ + Attempts to read a document's filename when no doc has been set + """ + d = DocumentHolder() + with pytest.raises(DocumentFilenameException): + d.output_filename(self, filename) + + +def test_output_file_not_pdf(): + docx_file = tempfile.mktemp(suffix=".docx") + d = DocumentHolder() + + with pytest.raises(DocumentFilenameException): + d.output_filename = docx_file + + assert not os.path.exists(docx_file)