mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-10-03 06:40:31 +00:00
Test modules in paperless, paperless_mail and documents imported filesystem assertions, the migration test base, the retry helper and the streaming-response reader out of documents/tests/utils.py, which kept each app's tests coupled to another app's test package. They now live in paperless_testing, and the progress manager fake is renamed FakeProgressManager and now subclasses the real ProgressManager, overriding only the transport, so the payload it records is built by the production code. The twenty places that patched documents.tasks.ProgressManager by hand now use a fake_progress_manager fixture.
105 lines
3.3 KiB
Python
105 lines
3.3 KiB
Python
import shutil
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from django.core.management import call_command
|
|
from django.test import TestCase
|
|
|
|
from documents.management.commands.document_thumbnails import _process_document
|
|
from documents.models import Document
|
|
from documents.parsers import get_default_thumbnail
|
|
from paperless_testing.assertions import FileSystemAssertsMixin
|
|
from paperless_testing.dirs import DirectoriesMixin
|
|
|
|
|
|
@pytest.mark.management
|
|
class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|
def make_models(self) -> None:
|
|
self.d1 = Document.objects.create(
|
|
checksum="A",
|
|
title="A",
|
|
content="first document",
|
|
mime_type="application/pdf",
|
|
filename="test.pdf",
|
|
)
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "simple.pdf",
|
|
self.d1.source_path,
|
|
)
|
|
|
|
self.d2 = Document.objects.create(
|
|
checksum="B",
|
|
title="B",
|
|
content="second document",
|
|
mime_type="application/pdf",
|
|
filename="test2.pdf",
|
|
)
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "simple.pdf",
|
|
self.d2.source_path,
|
|
)
|
|
|
|
self.d3 = Document.objects.create(
|
|
checksum="C",
|
|
title="C",
|
|
content="third document",
|
|
mime_type="application/pdf",
|
|
filename="test3.pdf",
|
|
)
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "password-is-test.pdf",
|
|
self.d3.source_path,
|
|
)
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
self.make_models()
|
|
|
|
def test_process_document(self) -> None:
|
|
self.assertIsNotFile(self.d1.thumbnail_path)
|
|
_process_document(self.d1.id)
|
|
self.assertIsFile(self.d1.thumbnail_path)
|
|
|
|
def test_process_document_password_protected(self) -> None:
|
|
self.assertIsFile(get_default_thumbnail())
|
|
self.assertIsNotFile(self.d3.thumbnail_path)
|
|
_process_document(self.d3.id)
|
|
# Ensure default thumbnail is still there
|
|
self.assertIsFile(get_default_thumbnail())
|
|
self.assertIsFile(self.d3.thumbnail_path)
|
|
|
|
@mock.patch("documents.management.commands.document_thumbnails.shutil.move")
|
|
def test_process_document_invalid_mime_type(self, m: mock.Mock) -> None:
|
|
self.d1.mime_type = "asdasdasd"
|
|
self.d1.save()
|
|
|
|
# .save() triggers filename handling
|
|
m.reset_mock()
|
|
|
|
_process_document(self.d1.id)
|
|
|
|
# Not called during processing of document
|
|
m.assert_not_called()
|
|
|
|
def test_command(self) -> None:
|
|
self.assertIsNotFile(self.d1.thumbnail_path)
|
|
self.assertIsNotFile(self.d2.thumbnail_path)
|
|
call_command("document_thumbnails", "--processes", "1", skip_checks=True)
|
|
self.assertIsFile(self.d1.thumbnail_path)
|
|
self.assertIsFile(self.d2.thumbnail_path)
|
|
|
|
def test_command_documentid(self) -> None:
|
|
self.assertIsNotFile(self.d1.thumbnail_path)
|
|
self.assertIsNotFile(self.d2.thumbnail_path)
|
|
call_command(
|
|
"document_thumbnails",
|
|
"--processes",
|
|
"1",
|
|
"-d",
|
|
f"{self.d1.id}",
|
|
skip_checks=True,
|
|
)
|
|
self.assertIsFile(self.d1.thumbnail_path)
|
|
self.assertIsNotFile(self.d2.thumbnail_path)
|