Add unit tests to cover archive-related methods

Additionally this adds the pytest-mock dev dependency to be able to mock
certain methods.
This commit is contained in:
deeplow 2022-11-22 18:50:37 +00:00
parent f54446f2fd
commit c6a0b59379
No known key found for this signature in database
GPG key ID: 577982871529A52A
3 changed files with 62 additions and 2 deletions

20
poetry.lock generated
View file

@ -386,6 +386,20 @@ python-versions = ">=3.6"
py = "*" py = "*"
pytest = ">=3.10" pytest = ">=3.10"
[[package]]
name = "pytest-mock"
version = "3.10.0"
description = "Thin-wrapper around the mock package for easier use with pytest"
category = "dev"
optional = false
python-versions = ">=3.7"
[package.dependencies]
pytest = ">=5.0"
[package.extras]
dev = ["pre-commit", "pytest-asyncio", "tox"]
[[package]] [[package]]
name = "pytest-xdist" name = "pytest-xdist"
version = "2.5.0" version = "2.5.0"
@ -488,7 +502,7 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools"
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = ">=3.7,<3.11" python-versions = ">=3.7,<3.11"
content-hash = "5ed0423136bff7b3208bab5cc90ef6e1aa03918b9cb3e81b10d60d9a84c47de9" content-hash = "098fe8820fcf719b946a0b92b29a64e93e68107e9fe0340b1d8503e53b2802fd"
[metadata.files] [metadata.files]
altgraph = [ altgraph = [
@ -783,6 +797,10 @@ pytest-forked = [
{file = "pytest-forked-1.4.0.tar.gz", hash = "sha256:8b67587c8f98cbbadfdd804539ed5455b6ed03802203485dd2f53c1422d7440e"}, {file = "pytest-forked-1.4.0.tar.gz", hash = "sha256:8b67587c8f98cbbadfdd804539ed5455b6ed03802203485dd2f53c1422d7440e"},
{file = "pytest_forked-1.4.0-py3-none-any.whl", hash = "sha256:bbbb6717efc886b9d64537b41fb1497cfaf3c9601276be8da2cccfea5a3c8ad8"}, {file = "pytest_forked-1.4.0-py3-none-any.whl", hash = "sha256:bbbb6717efc886b9d64537b41fb1497cfaf3c9601276be8da2cccfea5a3c8ad8"},
] ]
pytest-mock = [
{file = "pytest-mock-3.10.0.tar.gz", hash = "sha256:fbbdb085ef7c252a326fd8cdcac0aa3b1333d8811f131bdcc701002e1be7ed4f"},
{file = "pytest_mock-3.10.0-py3-none-any.whl", hash = "sha256:f4c973eeae0282963eb293eb173ce91b091a79c1334455acfac9ddee8a1c784b"},
]
pytest-xdist = [ pytest-xdist = [
{file = "pytest-xdist-2.5.0.tar.gz", hash = "sha256:4580deca3ff04ddb2ac53eba39d76cb5dd5edeac050cb6fbc768b0dd712b4edf"}, {file = "pytest-xdist-2.5.0.tar.gz", hash = "sha256:4580deca3ff04ddb2ac53eba39d76cb5dd5edeac050cb6fbc768b0dd712b4edf"},
{file = "pytest_xdist-2.5.0-py3-none-any.whl", hash = "sha256:6fe5c74fec98906deb8f2d2b616b5c782022744978e7bd4695d39c8f42d0ce65"}, {file = "pytest_xdist-2.5.0-py3-none-any.whl", hash = "sha256:6fe5c74fec98906deb8f2d2b616b5c782022744978e7bd4695d39c8f42d0ce65"},

View file

@ -33,6 +33,9 @@ dangerzone = 'dangerzone:main'
dangerzone-container = 'dangerzone:main' dangerzone-container = 'dangerzone:main'
dangerzone-cli = 'dangerzone:main' dangerzone-cli = 'dangerzone:main'
[tool.poetry.group.dev.dependencies]
pytest-mock = "^3.10.0"
[build-system] [build-system]
requires = ["poetry>=1.1.4"] requires = ["poetry>=1.1.4"]
build-backend = "poetry.masonry.api" build-backend = "poetry.masonry.api"

View file

@ -3,11 +3,12 @@ import platform
import stat import stat
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock
import pytest import pytest
from dangerzone import errors from dangerzone import errors
from dangerzone.document import SAFE_EXTENSION, Document from dangerzone.document import ARCHIVE_SUBDIR, SAFE_EXTENSION, Document
from . import sample_doc, unreadable_pdf from . import sample_doc, unreadable_pdf
@ -16,6 +17,10 @@ def test_input_sample_init(sample_doc: str) -> None:
Document(sample_doc) Document(sample_doc)
def test_input_sample_init_archive(sample_doc: str) -> None:
Document(sample_doc, archive=True)
def test_input_sample_after(sample_doc: str) -> None: def test_input_sample_after(sample_doc: str) -> None:
d = Document() d = Document()
d.input_filename = sample_doc d.input_filename = sample_doc
@ -78,6 +83,40 @@ def test_output_file_not_pdf(tmp_path: Path) -> None:
assert not os.path.exists(docx_file) assert not os.path.exists(docx_file)
@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific")
def test_archive_unwriteable_dir(sample_doc: str, tmp_path: Path) -> None:
doc = tmp_path / "doc.pdf"
Path.touch(doc)
d = Document(str(doc))
# make archive directory unreadable
os.chmod(tmp_path, 0o400)
with pytest.raises(errors.UnwriteableArchiveDirException) as e:
d.validate_default_archive_dir()
def test_archive(mocker: MagicMock, tmp_path: Path) -> None:
test_string = "original file"
original_doc_path = str(tmp_path / "doc.pdf")
archived_doc_path = str(tmp_path / ARCHIVE_SUBDIR / "doc.pdf")
# write some content for later verifying content integrity
with open(original_doc_path, "w") as f:
f.write(test_string)
d = Document(original_doc_path, archive=True)
d.archive()
# original document has been moved to unsafe/doc.pdf
assert not os.path.exists(original_doc_path)
assert os.path.exists(archived_doc_path)
# make sure it is the original file by comparing its content
with open(archived_doc_path) as f:
assert f.read() == test_string
def test_set_output_dir(sample_doc: str, tmp_path: Path) -> None: def test_set_output_dir(sample_doc: str, tmp_path: Path) -> None:
d = Document(sample_doc) d = Document(sample_doc)
d.set_output_dir(str(tmp_path)) d.set_output_dir(str(tmp_path))