mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 17:58:41 +00:00
* Fix: stop full document saves from writing file path columns A full save() of a Document instance loaded before another process moved its file wrote the old filename back. The DB then pointed at a path that no longer existed, and recovery only worked when the recomputed path happened to match the file's real location. Document.save() now leaves filename and archive_filename out of full saves on existing rows. Writers that own those columns opt in with write_file_paths=True or an explicit update_fields. A full save of a row deleted elsewhere now raises instead of re-inserting it. * Fix: make the global media lock reentrant within a thread Every caller built its own FileLock on media.lock, so code already holding it could not call into code that takes it again without blocking itself. That made it impossible to take media.lock before opening a transaction in paths that also attach files or move files inside that transaction. media_lock() hands out one FileLock instance per path with per-thread hold counts, so a thread re-enters while other threads and processes stay excluded. All media.lock call sites now use it. * Fix: extract document file moves out of the signal receiver The file move receiver refreshed the caller's own instance from the database, which wiped unsaved in-memory changes. It ran on every m2m_changed action including pre_add and pre_remove, wrote modified even when nothing moved, and its no-move branch skipped trashed documents. The move body now lives in documents.file_moves.move_files_for_document. It skips documents without a filename before taking media.lock, then takes the lock with a 300 second timeout, loads a fresh instance by pk, writes only when a filename column changes, and returns the instance. The receiver ignores reverse and pre_* m2m actions and copies just the final filename columns back onto the caller's instance. * Fix: collect document file moves per scope and move once on exit Each save of a document moved its files on the spot, so one logical change could move them several times, and moves could run inside another open transaction. defer_file_moves() opens a ContextVar-backed scope. Inside it, move requests are recorded and the outermost scope moves each document once on exit, or on commit if a transaction is still open. A failing move is logged and does not stop the others. Outside any scope moves stay immediate. * Fix: move document files once per request, after its writes commit A tag PATCH moved files from inside the tags.set() transaction while holding the document row lock. A concurrent save holding media.lock and waiting on that row deadlocked with it, and every later save on the instance queued behind media.lock until the webserver restarted. FileMoveMiddleware opens a file move scope around every request, in sync or async mode, so each changed document is moved once after the view returns and never while a transaction holds row locks. * Fix: move document files once per Celery task Tasks such as bulk_update_documents moved a document's files once for its manual post_save and again for every save its workflows made, each time taking the global media lock. All tasks now run through FileMoveScopedTask, which opens a file move scope around the task body, including when the task raises. bulk_update_documents flushes after each document so long bulk edits still make steady progress. * Fix: take media.lock before the archiver's transaction The archiver updated the document row inside a transaction and then waited on media.lock. A concurrent file move holding media.lock and updating the same row could deadlock with it, and the unique archive filename was generated outside the lock from an instance loaded before the parse. The archiver now acquires media.lock before opening its transaction, refreshes the document under the lock, and moves the archive and thumbnail inside that block, so it never waits on media.lock while holding the row. * Fix: take media.lock before emptying the trash Emptying the trash deleted documents inside Django's delete transaction, and the post_delete file cleanup waited on media.lock while the deleted rows were locked. A concurrent file move holding media.lock and updating one of those rows could deadlock with it. empty_trash now holds media.lock around the delete, so the cleanup handler re-enters the lock instead of waiting while rows are locked. * Fix: take media.lock before the consumer's storage transaction Consuming a new version locked the root document row with select_for_update and then waited on media.lock to place the file, the same lock ordering that let concurrent saves deadlock. On MariaDB even the version row's foreign key check holds a shared lock on the root. The consumer's storage phase now takes media.lock before opening its transaction and runs inside its own file move scope. File placement and workflow email actions inside the transaction re-enter the lock instead of waiting, and filename follow-ups run once after commit. * Fix: keep Celery's Django task base for file move scoped tasks Passing task_cls to Celery replaces the Django fixup's default DjangoTask, so FileMoveScopedTask, built on the plain Task, left every task without delay_on_commit. The workflow AI suggestions trigger failed with an AttributeError. FileMoveScopedTask now subclasses DjangoTask, which only needs django.db.transaction and is safe to import before apps load. The task scope test also asserts tasks are DjangoTask instances. * Fix: rename and retag documents one file move at a time document_renamer sent a fake post_save per document to trigger the move, and document_retagger moved a document once for each field it assigned. The renamer calls move_files_for_document directly for each document, so progress reflects real work. The retagger opens a file move scope per document, so each document is moved once after all of its assignments. * Fix: move files per document during scheduled workflow runs check_scheduled_workflows runs the workflow once per matched document inside the task-wide file move scope, so every rename it triggered was deferred to the end of the task. A long run killed by the Celery time limit would then discard all of the renames. The task now flushes the pending file moves after each document's workflow run and websocket notification, the same way bulk_update_documents does, so renames land document by document while the scope stays open. * Fix: place consumed files before running the post-consume script The consumer's storage phase opens its own file move scope, which nests under the consume task's scope. Moves requested while storing the document therefore stayed pending until the task ended, and the post-consume script could be handed a path from before the move. The consumer now flushes the pending file moves once after the storage block, outside the media lock and the transaction, and before the post-consume script runs. * Fix: skip the thread hop for async file move scopes with nothing to move adefer_file_moves hopped to a worker thread on every exit, even when no file move had been requested, which costs an executor round trip for most async requests. The collector is now closed on the event loop, which touches no database, and the flush only runs through sync_to_async when something was collected. The transaction check still happens inside the flush in the sync thread. The comment above the lock-free filename precheck in move_files_for_document now gives the real reason it uses values_list(): it keeps the check off Document.global_objects.filter(), which tests patch to inject database errors and observe the mover's writes. * Fix: return the post-move modified time from document updates Editing a document in a way that renames its file returned a response carrying the modified time from before the move. The deferred file move runs after the response is built and bumps modified, so the stored value was newer than the one the client received. The frontend then saw a newer server modified time and showed a spurious "document was updated" prompt whose reload discarded local edits. DocumentViewSet.update now flushes pending file moves right after saving and before reading the document back, so the response reflects the final filename and modified time. * Trimming and simpliciation * Fix: refresh the document after placing files, before the post-consume script * Fix: move documents when tags change through the reverse relation * Fix: move versions of trashed documents * Fix: rename only root documents in document_renamer * Improves test coverage even for the exception cases * Fix: move documents when a tag is cleared through the reverse relation * Fix: flush file moves before the scheduled workflow websocket notification * Fix: move files before the bulk update websocket notification * Fix: bump modified for documents changed by a bulk edit * Fix: defer unscoped file moves to commit inside a transaction * Fix: allow deferred file move callback in zone OCR reindex test * Fix: return 404 instead of 500 when a document is trashed during its own update DocumentViewSet.update saves the change and then re-reads the document through the default manager, which excludes soft deleted rows. A concurrent request that moved the document to the trash in between made that read raise DoesNotExist, so the client got a 500 for a document that no longer exists. Raise Http404 instead, the same answer get_object() gives when the document is already trashed at the start of the request. The committed change is kept. Found while hammering the API with concurrent PATCH and delete requests. * Fix: remove the superseded archive file when a reprocess changes its path Reprocessing a document writes the new archive to the path its current metadata generates and updates archive_filename, but never removed the file the old archive_filename pointed at. Normally both are the same path. They differ when a committed edit has not had its file move applied yet, which deferred file moves make a real window: the new archive lands at the new path and the old one stays on disk with no document referencing it. The deferred move then finds the archive already at its target and only moves the original. Delete the replaced archive, and its directory if that leaves it empty, once the new one is in place and only when the two paths differ. * Fix: keep the trash cleanup handler connected while any empty_trash call runs empty_trash connects cleanup_document_deletion to post_delete before deleting and disconnects it in a finally block. The signal is process wide and the trash endpoint calls empty_trash directly in the web process, so overlapping calls interfere. One caller finishing disconnects the handler while another is still waiting for media.lock; the waiter then hard deletes its documents with no handler attached and their files stay on disk, unreferenced. Hold the connection with a lock protected reference count, connecting for the first active caller and disconnecting after the last one finishes.
1948 lines
66 KiB
Python
1948 lines
66 KiB
Python
import datetime
|
|
import shutil
|
|
import stat
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
from unittest.mock import MagicMock
|
|
|
|
from django.conf import settings
|
|
from django.contrib.auth.models import Group
|
|
from django.contrib.auth.models import User
|
|
from django.test import TestCase
|
|
from django.test import override_settings
|
|
from django.utils import timezone
|
|
from guardian.core import ObjectPermissionChecker
|
|
|
|
from documents.barcodes import BarcodePlugin
|
|
from documents.consumer import ConsumerError
|
|
from documents.consumer import ConsumerPlugin
|
|
from documents.consumer import ConsumerPreflightPlugin
|
|
from documents.data_models import ConsumableDocument
|
|
from documents.data_models import DocumentMetadataOverrides
|
|
from documents.data_models import DocumentSource
|
|
from documents.models import Correspondent
|
|
from documents.models import CustomField
|
|
from documents.models import Document
|
|
from documents.models import DocumentType
|
|
from documents.models import StoragePath
|
|
from documents.models import Tag
|
|
from documents.models import Workflow
|
|
from documents.models import WorkflowAction
|
|
from documents.models import WorkflowActionEmail
|
|
from documents.models import WorkflowTrigger
|
|
from documents.parsers import ParseError
|
|
from documents.plugins.helpers import ProgressStatusOptions
|
|
from documents.tasks import sanity_check
|
|
from documents.tests.utils import GetConsumerMixin
|
|
from paperless_mail.models import MailRule
|
|
from paperless_testing.assertions import FileSystemAssertsMixin
|
|
from paperless_testing.dirs import DirectoriesMixin
|
|
from paperless_testing.factories import DocumentFactory
|
|
from paperless_testing.factories import TagFactory
|
|
from paperless_testing.factories import UserFactory
|
|
from paperless_testing.fakes.progress import FakeProgressManager
|
|
from paperless_testing.locks import record_media_lock_acquisitions
|
|
from paperless_testing.permissions import grant_object
|
|
from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF
|
|
from paperless_testing.samples import MULTI_PAGE_IMAGES_PDF
|
|
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
|
|
|
|
|
class _BaseNewStyleParser:
|
|
"""Minimal ParserProtocol implementation for use in consumer tests."""
|
|
|
|
name: str = "test-parser"
|
|
version: str = "0.1"
|
|
author: str = "test"
|
|
url: str = "test"
|
|
|
|
@classmethod
|
|
def supported_mime_types(cls) -> dict:
|
|
return {
|
|
"application/pdf": ".pdf",
|
|
"image/png": ".png",
|
|
"message/rfc822": ".eml",
|
|
}
|
|
|
|
@classmethod
|
|
def score(cls, mime_type: str, filename: str, path=None):
|
|
return 0 if mime_type in cls.supported_mime_types() else None
|
|
|
|
@property
|
|
def can_produce_archive(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def requires_pdf_rendition(self) -> bool:
|
|
return False
|
|
|
|
def __init__(self) -> None:
|
|
self._tmpdir: Path | None = None
|
|
self._text: str | None = None
|
|
self._archive: Path | None = None
|
|
self._thumb: Path | None = None
|
|
|
|
def __enter__(self):
|
|
self._tmpdir = Path(
|
|
tempfile.mkdtemp(prefix="paperless-test-", dir=settings.SCRATCH_DIR),
|
|
)
|
|
_, thumb = tempfile.mkstemp(suffix=".webp", dir=self._tmpdir)
|
|
self._thumb = Path(thumb)
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
if self._tmpdir and self._tmpdir.exists():
|
|
shutil.rmtree(self._tmpdir, ignore_errors=True)
|
|
|
|
def configure(self, context) -> None:
|
|
"""
|
|
Test parser doesn't do anything with context
|
|
"""
|
|
|
|
def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None:
|
|
raise NotImplementedError
|
|
|
|
def get_text(self) -> str | None:
|
|
return self._text
|
|
|
|
def get_date(self):
|
|
return None
|
|
|
|
def get_archive_path(self):
|
|
return self._archive
|
|
|
|
def get_thumbnail(self, document_path, mime_type) -> Path:
|
|
return self._thumb
|
|
|
|
def get_page_count(self, document_path, mime_type):
|
|
return None
|
|
|
|
def extract_metadata(self, document_path, mime_type) -> list:
|
|
return []
|
|
|
|
|
|
class DummyParser(_BaseNewStyleParser):
|
|
_ARCHIVE_SRC = MULTI_PAGE_IMAGES_PDF
|
|
|
|
def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None:
|
|
self._text = "The Text"
|
|
if produce_archive and self._tmpdir:
|
|
self._archive = self._tmpdir / "archive.pdf"
|
|
shutil.copy(self._ARCHIVE_SRC, self._archive)
|
|
|
|
|
|
class CopyParser(_BaseNewStyleParser):
|
|
def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None:
|
|
self._text = "The text"
|
|
if produce_archive and self._tmpdir:
|
|
self._archive = self._tmpdir / "archive.pdf"
|
|
shutil.copy(document_path, self._archive)
|
|
|
|
|
|
class FaultyParser(_BaseNewStyleParser):
|
|
def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None:
|
|
raise ParseError("Does not compute.")
|
|
|
|
|
|
class FaultyGenericExceptionParser(_BaseNewStyleParser):
|
|
def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None:
|
|
raise Exception("Generic exception.")
|
|
|
|
|
|
def fake_magic_from_file(file, *, mime=False): # NOSONAR
|
|
if mime:
|
|
filepath = Path(file)
|
|
if filepath.name.startswith("invalid_pdf"):
|
|
return "application/octet-stream"
|
|
if filepath.name.startswith("valid_pdf"):
|
|
return "application/pdf"
|
|
if filepath.suffix == ".pdf":
|
|
return "application/pdf"
|
|
elif filepath.suffix == ".png":
|
|
return "image/png"
|
|
elif filepath.suffix == ".webp":
|
|
return "image/webp"
|
|
elif filepath.suffix == ".eml":
|
|
return "message/rfc822"
|
|
else:
|
|
return "unknown"
|
|
else:
|
|
return "A verbose string that describes the contents of the file"
|
|
|
|
|
|
@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)
|
|
class TestConsumer(
|
|
DirectoriesMixin,
|
|
FileSystemAssertsMixin,
|
|
GetConsumerMixin,
|
|
TestCase,
|
|
):
|
|
def _assert_first_last_send_progress(
|
|
self,
|
|
first_status=ProgressStatusOptions.STARTED,
|
|
last_status=ProgressStatusOptions.SUCCESS,
|
|
first_progress=0,
|
|
first_progress_max=100,
|
|
last_progress=100,
|
|
last_progress_max=100,
|
|
) -> None:
|
|
self.assertGreaterEqual(len(self.status.payloads), 2)
|
|
|
|
payload = self.status.payloads[0]
|
|
self.assertEqual(payload["data"]["current_progress"], first_progress)
|
|
self.assertEqual(payload["data"]["max_progress"], first_progress_max)
|
|
self.assertEqual(payload["data"]["status"], first_status)
|
|
|
|
payload = self.status.payloads[-1]
|
|
|
|
self.assertEqual(payload["data"]["current_progress"], last_progress)
|
|
self.assertEqual(payload["data"]["max_progress"], last_progress_max)
|
|
self.assertEqual(payload["data"]["status"], last_status)
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
|
|
patcher = mock.patch("documents.consumer.get_parser_registry")
|
|
mock_registry = patcher.start()
|
|
mock_registry.return_value.get_parser_for_file.return_value = DummyParser
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def get_test_file(self):
|
|
src = SIMPLE_DIGITAL_PDF
|
|
dst = self.dirs.scratch_dir / "sample.pdf"
|
|
shutil.copy(src, dst)
|
|
return dst
|
|
|
|
def get_test_file2(self):
|
|
src = MULTI_PAGE_DIGITAL_PDF
|
|
dst = self.dirs.scratch_dir / "sample2.pdf"
|
|
shutil.copy(src, dst)
|
|
return dst
|
|
|
|
def get_test_archive_file(self):
|
|
src = MULTI_PAGE_IMAGES_PDF
|
|
dst = self.dirs.scratch_dir / "sample_archive.pdf"
|
|
shutil.copy(src, dst)
|
|
return dst
|
|
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_takes_media_lock_outside_transaction(self, m) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A new document to consume
|
|
WHEN:
|
|
- It is consumed
|
|
THEN:
|
|
- media.lock is acquired
|
|
- media.lock is never first acquired inside a transaction
|
|
- The document's file is in place
|
|
"""
|
|
m.return_value = MagicMock()
|
|
|
|
with record_media_lock_acquisitions() as acquisitions:
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertTrue(acquisitions)
|
|
self.assertFalse(any(a.in_user_transaction for a in acquisitions))
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
self.assertIsFile(document.source_path)
|
|
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_places_file_before_post_consume_script(self, m) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A new document to consume
|
|
WHEN:
|
|
- It is consumed
|
|
THEN:
|
|
- Pending file moves are flushed before the post-consume script runs
|
|
"""
|
|
m.return_value = MagicMock()
|
|
order = MagicMock()
|
|
|
|
with (
|
|
mock.patch(
|
|
"documents.consumer.flush_pending_file_moves",
|
|
order.flush,
|
|
),
|
|
mock.patch.object(
|
|
ConsumerPlugin,
|
|
"run_post_consume_script",
|
|
order.script,
|
|
),
|
|
):
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertEqual(
|
|
[c[0] for c in order.mock_calls],
|
|
["flush", "script"],
|
|
)
|
|
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_version_takes_media_lock_outside_transaction(self, m) -> None:
|
|
"""
|
|
GIVEN:
|
|
- An existing root document
|
|
WHEN:
|
|
- A new version of it is consumed (the root row is locked with
|
|
select_for_update inside the consumer's transaction)
|
|
THEN:
|
|
- media.lock is never first acquired inside a transaction
|
|
- The root's modified is bumped
|
|
"""
|
|
m.return_value = MagicMock()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
root_doc = Document.objects.first()
|
|
assert root_doc is not None
|
|
original_modified = timezone.now() - datetime.timedelta(days=1)
|
|
Document.objects.filter(pk=root_doc.pk).update(modified=original_modified)
|
|
|
|
version_file = self.get_test_file2()
|
|
status = FakeProgressManager(version_file.name, None)
|
|
overrides = DocumentMetadataOverrides(version_label="v2")
|
|
doc = ConsumableDocument(
|
|
DocumentSource.ApiUpload,
|
|
original_file=version_file,
|
|
root_document_id=root_doc.pk,
|
|
)
|
|
preflight = ConsumerPreflightPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
preflight.setup()
|
|
preflight.run()
|
|
consumer = ConsumerPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
consumer.setup()
|
|
|
|
try:
|
|
with record_media_lock_acquisitions() as acquisitions:
|
|
consumer.run()
|
|
finally:
|
|
consumer.cleanup()
|
|
|
|
self.assertTrue(acquisitions)
|
|
self.assertFalse(any(a.in_user_transaction for a in acquisitions))
|
|
root_doc.refresh_from_db()
|
|
self.assertGreater(root_doc.modified, original_modified)
|
|
|
|
@override_settings(
|
|
PAPERLESS_EMAIL_HOST="localhost",
|
|
EMAIL_ENABLED=True,
|
|
PAPERLESS_URL="http://localhost:8000",
|
|
)
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
@mock.patch("django.core.mail.message.EmailMessage.send", return_value=1)
|
|
def test_consumption_workflow_email_runs_under_held_lock(self, send, m) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A document added workflow with an email action
|
|
WHEN:
|
|
- A document is consumed (the consumer holds media.lock while
|
|
workflows run)
|
|
THEN:
|
|
- The email action re-enters media.lock and sends without blocking
|
|
"""
|
|
m.return_value = MagicMock()
|
|
trigger = WorkflowTrigger.objects.create(
|
|
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
|
|
)
|
|
email = WorkflowActionEmail.objects.create(
|
|
subject="added",
|
|
body="body",
|
|
to="someone@example.com",
|
|
include_document=True,
|
|
)
|
|
action = WorkflowAction.objects.create(
|
|
type=WorkflowAction.WorkflowActionType.EMAIL,
|
|
email=email,
|
|
)
|
|
workflow = Workflow.objects.create(name="email on add")
|
|
workflow.triggers.add(trigger)
|
|
workflow.actions.add(action)
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
send.assert_called_once()
|
|
|
|
@override_settings(
|
|
FILENAME_FORMAT=None,
|
|
TIME_ZONE="America/Chicago",
|
|
ARCHIVE_FILE_GENERATION="always",
|
|
)
|
|
def testNormalOperation(self) -> None:
|
|
filename = self.get_test_file()
|
|
|
|
# Get the local time, as an aware datetime
|
|
# Roughly equal to file modification time
|
|
rough_create_date_local = timezone.localtime(timezone.now())
|
|
|
|
with self.get_consumer(filename) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIsNotNone(document)
|
|
|
|
self.assertEqual(document.content, "The Text")
|
|
self.assertEqual(
|
|
document.title,
|
|
Path(filename).stem,
|
|
)
|
|
self.assertIsNone(document.correspondent)
|
|
self.assertIsNone(document.document_type)
|
|
self.assertEqual(document.filename, "0000001.pdf")
|
|
self.assertEqual(document.archive_filename, "0000001.pdf")
|
|
|
|
self.assertIsFile(document.source_path)
|
|
|
|
self.assertIsFile(document.thumbnail_path)
|
|
|
|
self.assertIsFile(document.archive_path)
|
|
|
|
self.assertEqual(
|
|
document.checksum,
|
|
"1093cf6e32adbd16b06969df09215d42c4a3a8938cc18b39455953f08d1ff2ab",
|
|
)
|
|
self.assertEqual(
|
|
document.archive_checksum,
|
|
"706124ecde3c31616992fa979caed17a726b1c9ccdba70e82a4ff796cea97ccf",
|
|
)
|
|
|
|
self.assertIsNotFile(filename)
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
self.assertEqual(document.created.year, rough_create_date_local.year)
|
|
self.assertEqual(document.created.month, rough_create_date_local.month)
|
|
self.assertEqual(document.created.day, rough_create_date_local.day)
|
|
|
|
@override_settings(FILENAME_FORMAT=None)
|
|
def testDeleteMacFiles(self) -> None:
|
|
# https://github.com/jonaswinkler/paperless-ng/discussions/1037
|
|
|
|
filename = self.get_test_file()
|
|
shadow_file = Path(self.dirs.scratch_dir) / "._sample.pdf"
|
|
|
|
shutil.copy(filename, shadow_file)
|
|
|
|
self.assertIsFile(shadow_file)
|
|
|
|
with self.get_consumer(filename) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIsNotNone(document)
|
|
|
|
self.assertIsFile(document.source_path)
|
|
|
|
self.assertIsNotFile(shadow_file)
|
|
self.assertIsNotFile(filename)
|
|
|
|
def testOverrideFilename(self) -> None:
|
|
filename = self.get_test_file()
|
|
override_filename = "Statement for November.pdf"
|
|
|
|
with self.get_consumer(
|
|
filename,
|
|
DocumentMetadataOverrides(filename=override_filename),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIsNotNone(document)
|
|
|
|
self.assertEqual(document.title, "Statement for November")
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideTitle(self) -> None:
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(title="Override Title"),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIsNotNone(document)
|
|
|
|
self.assertEqual(document.title, "Override Title")
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideCorrespondent(self) -> None:
|
|
c = Correspondent.objects.create(name="test")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(correspondent_id=c.pk),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIsNotNone(document)
|
|
|
|
self.assertEqual(document.correspondent.id, c.id)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideDocumentType(self) -> None:
|
|
dt = DocumentType.objects.create(name="test")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(document_type_id=dt.pk),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.document_type.id, dt.id)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideStoragePath(self) -> None:
|
|
sp = StoragePath.objects.create(name="test")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(storage_path_id=sp.pk),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.storage_path.id, sp.id)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideTags(self) -> None:
|
|
t1 = Tag.objects.create(name="t1")
|
|
t2 = Tag.objects.create(name="t2")
|
|
t3 = Tag.objects.create(name="t3")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(tag_ids=[t1.id, t3.id]),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertIn(t1, document.tags.all())
|
|
self.assertNotIn(t2, document.tags.all())
|
|
self.assertIn(t3, document.tags.all())
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideCustomFields(self) -> None:
|
|
cf1 = CustomField.objects.create(name="Custom Field 1", data_type="string")
|
|
cf2 = CustomField.objects.create(
|
|
name="Custom Field 2",
|
|
data_type="integer",
|
|
)
|
|
cf3 = CustomField.objects.create(
|
|
name="Custom Field 3",
|
|
data_type="url",
|
|
)
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(
|
|
custom_fields={cf1.id: "value1", cf3.id: "http://example.com"},
|
|
),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
fields_used = [
|
|
field_instance.field for field_instance in document.custom_fields.all()
|
|
]
|
|
self.assertIn(cf1, fields_used)
|
|
self.assertNotIn(cf2, fields_used)
|
|
self.assertIn(cf3, fields_used)
|
|
self.assertEqual(document.custom_fields.get(field=cf1).value, "value1")
|
|
self.assertEqual(
|
|
document.custom_fields.get(field=cf3).value,
|
|
"http://example.com",
|
|
)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideAsn(self) -> None:
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(asn=123),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.archive_serial_number, 123)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideTitlePlaceholders(self) -> None:
|
|
c = Correspondent.objects.create(name="Correspondent Name")
|
|
dt = DocumentType.objects.create(name="DocType Name")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(
|
|
correspondent_id=c.pk,
|
|
document_type_id=dt.pk,
|
|
title="{{correspondent}}{{document_type}} {{added_month}}-{{added_year_short}}",
|
|
),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
now = timezone.now()
|
|
self.assertEqual(document.title, f"{c.name}{dt.name} {now.strftime('%m-%y')}")
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverrideOwner(self) -> None:
|
|
testuser = User.objects.create(username="testuser")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(owner_id=testuser.pk),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.owner, testuser)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testOverridePermissions(self) -> None:
|
|
testuser = User.objects.create(username="testuser")
|
|
testgroup = Group.objects.create(name="testgroup")
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(
|
|
view_users=[testuser.pk],
|
|
view_groups=[testgroup.pk],
|
|
),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
user_checker = ObjectPermissionChecker(testuser)
|
|
self.assertTrue(user_checker.has_perm("view_document", document))
|
|
group_checker = ObjectPermissionChecker(testgroup)
|
|
self.assertTrue(group_checker.has_perm("view_document", document))
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testInboxTagsScopedToOwner(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Inbox tags that are owned by the document owner, unowned, owned by
|
|
another user but shared with the document owner, and owned by
|
|
another user and not shared
|
|
- A non-inbox tag owned by the document owner
|
|
WHEN:
|
|
- A document is consumed with that owner
|
|
THEN:
|
|
- The document gets the owned, unowned and shared inbox tags only
|
|
"""
|
|
owner = UserFactory()
|
|
other = UserFactory()
|
|
owned = TagFactory(is_inbox_tag=True, owner=owner)
|
|
unowned = TagFactory(is_inbox_tag=True, owner=None)
|
|
shared = TagFactory(is_inbox_tag=True, owner=other)
|
|
grant_object(owner, shared, "view_tag")
|
|
TagFactory(is_inbox_tag=True, owner=other)
|
|
TagFactory(is_inbox_tag=False, owner=owner)
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(owner_id=owner.pk),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertCountEqual(document.tags.all(), [owned, unowned, shared])
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testInboxTagsUnownedDocument(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Inbox tags owned by two different users, an unowned inbox tag and
|
|
a non-inbox tag
|
|
WHEN:
|
|
- A document is consumed with no owner
|
|
THEN:
|
|
- The document gets every inbox tag
|
|
"""
|
|
inbox_tags = [
|
|
TagFactory(is_inbox_tag=True, owner=UserFactory()),
|
|
TagFactory(is_inbox_tag=True, owner=UserFactory()),
|
|
TagFactory(is_inbox_tag=True, owner=None),
|
|
]
|
|
TagFactory(is_inbox_tag=False)
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertCountEqual(document.tags.all(), inbox_tags)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testNotAFile(self) -> None:
|
|
with self.assertRaisesMessage(ConsumerError, "File not found"):
|
|
with self.get_consumer(Path("non-existing-file")) as consumer:
|
|
consumer.run()
|
|
self._assert_first_last_send_progress(last_status="FAILED")
|
|
|
|
def testDuplicates1(self) -> None:
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertEqual(Document.objects.count(), 2)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testDuplicates2(self) -> None:
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
with self.get_consumer(self.get_test_archive_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertEqual(Document.objects.count(), 2)
|
|
self._assert_first_last_send_progress()
|
|
|
|
def testDuplicates3(self) -> None:
|
|
with self.get_consumer(self.get_test_archive_file()) as consumer:
|
|
consumer.run()
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
def testDuplicateInTrash(self) -> None:
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
Document.objects.all().delete()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertEqual(Document.objects.count(), 1)
|
|
|
|
def testAsnExists(self) -> None:
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(asn=123),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
with self.assertRaisesMessage(ConsumerError, "ASN 123 already exists"):
|
|
with self.get_consumer(
|
|
self.get_test_file2(),
|
|
DocumentMetadataOverrides(asn=123),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
def testAsnExistsInTrash(self) -> None:
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(asn=123),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
document.delete()
|
|
|
|
with self.assertRaisesMessage(ConsumerError, "document is in the trash"):
|
|
with self.get_consumer(
|
|
self.get_test_file2(),
|
|
DocumentMetadataOverrides(asn=123),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
def testNoParsers(self, m) -> None:
|
|
m.return_value.get_parser_for_file.return_value = None
|
|
|
|
with self.assertRaisesMessage(
|
|
ConsumerError,
|
|
"sample.pdf: Unsupported mime type application/pdf",
|
|
):
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
self._assert_first_last_send_progress(last_status="FAILED")
|
|
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
def testFaultyParser(self, m) -> None:
|
|
m.return_value.get_parser_for_file.return_value = FaultyParser
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
with self.assertRaisesMessage(
|
|
ConsumerError,
|
|
"sample.pdf: Error occurred while consuming document sample.pdf: Does not compute.",
|
|
):
|
|
consumer.run()
|
|
|
|
self._assert_first_last_send_progress(last_status="FAILED")
|
|
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
def testGenericParserException(self, m) -> None:
|
|
m.return_value.get_parser_for_file.return_value = FaultyGenericExceptionParser
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
with self.assertRaisesMessage(
|
|
ConsumerError,
|
|
"sample.pdf: Unexpected error while consuming document sample.pdf: Generic exception.",
|
|
):
|
|
consumer.run()
|
|
|
|
self._assert_first_last_send_progress(last_status="FAILED")
|
|
|
|
@mock.patch("documents.consumer.ConsumerPlugin._write")
|
|
def testPostSaveError(self, m) -> None:
|
|
filename = self.get_test_file()
|
|
m.side_effect = OSError("NO.")
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
with self.assertRaisesMessage(
|
|
ConsumerError,
|
|
"sample.pdf: The following error occurred while storing document sample.pdf after parsing: NO.",
|
|
):
|
|
consumer.run()
|
|
|
|
self._assert_first_last_send_progress(last_status="FAILED")
|
|
|
|
# file not deleted
|
|
self.assertIsFile(filename)
|
|
|
|
# Database empty
|
|
self.assertEqual(Document.objects.all().count(), 0)
|
|
|
|
@override_settings(
|
|
FILENAME_FORMAT="{correspondent}/{title}",
|
|
ARCHIVE_FILE_GENERATION="always",
|
|
)
|
|
def testFilenameHandling(self) -> None:
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(title="new docs"),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.title, "new docs")
|
|
self.assertEqual(document.filename, "none/new docs.pdf")
|
|
self.assertEqual(document.archive_filename, "none/new docs.pdf")
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
@mock.patch("documents.consumer.generate_unique_filename")
|
|
@override_settings(FILENAME_FORMAT="{pk}", ARCHIVE_FILE_GENERATION="always")
|
|
def testFilenameHandlingFallsBackWhenGeneratedPathExceedsDbLimit(self, m):
|
|
m.side_effect = lambda doc, archive_filename=False: Path(
|
|
("a" * 1100 + ".pdf") if not archive_filename else ("b" * 1100 + ".pdf"),
|
|
)
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(title="new docs"),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
self.assertIsNotNone(document)
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.filename, f"{document.pk:07d}.pdf")
|
|
self.assertLessEqual(len(document.filename), 1024)
|
|
self.assertLessEqual(
|
|
len(document.archive_filename),
|
|
1024,
|
|
)
|
|
self.assertIsFile(document.source_path)
|
|
self.assertIsFile(document.archive_path)
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
@override_settings(
|
|
FILENAME_FORMAT="{correspondent}/{title}",
|
|
ARCHIVE_FILE_GENERATION="always",
|
|
)
|
|
@mock.patch("documents.file_moves.generate_unique_filename")
|
|
def testFilenameHandlingUnstableFormat(self, m) -> None:
|
|
filenames = ["this", "that", "now this", "i cannot decide"]
|
|
|
|
def get_filename():
|
|
f = filenames.pop()
|
|
filenames.insert(0, f)
|
|
return f
|
|
|
|
m.side_effect = lambda f, archive_filename=False: get_filename()
|
|
|
|
Tag.objects.create(name="test", is_inbox_tag=True)
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(title="new docs"),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.title, "new docs")
|
|
self.assertIsNotNone(document.title)
|
|
self.assertIsFile(document.source_path)
|
|
self.assertIsFile(document.archive_path)
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_version_label_override_applies(self, m) -> None:
|
|
m.return_value = MagicMock()
|
|
|
|
with self.get_consumer(
|
|
self.get_test_file(),
|
|
DocumentMetadataOverrides(version_label="v1"),
|
|
) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.version_label, "v1")
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
@override_settings(AUDIT_LOG_ENABLED=True)
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_version_creates_new_version(self, m) -> None:
|
|
m.return_value = MagicMock()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
root_doc = Document.objects.first()
|
|
self.assertIsNotNone(root_doc)
|
|
assert root_doc is not None
|
|
|
|
root_storage_path = StoragePath.objects.create(
|
|
name="version-root-path",
|
|
path="root/{{title}}",
|
|
)
|
|
root_doc.storage_path = root_storage_path
|
|
root_doc.archive_serial_number = 42
|
|
root_doc.save()
|
|
|
|
original_modified = timezone.now() - datetime.timedelta(days=1)
|
|
Document.objects.filter(pk=root_doc.pk).update(modified=original_modified)
|
|
actor = UserFactory(
|
|
username="actor",
|
|
email="actor@example.com",
|
|
password="password",
|
|
)
|
|
|
|
version_file = self.get_test_file2()
|
|
status = FakeProgressManager(version_file.name, None)
|
|
overrides = DocumentMetadataOverrides(
|
|
version_label="v2",
|
|
actor_id=actor.pk,
|
|
)
|
|
doc = ConsumableDocument(
|
|
DocumentSource.ApiUpload,
|
|
original_file=version_file,
|
|
root_document_id=root_doc.pk,
|
|
)
|
|
preflight = ConsumerPreflightPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
preflight.setup()
|
|
preflight.run()
|
|
|
|
consumer = ConsumerPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
consumer.setup()
|
|
try:
|
|
self.assertEqual(consumer.filename, version_file.name)
|
|
consumer.run()
|
|
finally:
|
|
consumer.cleanup()
|
|
|
|
versions = Document.objects.filter(root_document=root_doc)
|
|
self.assertEqual(versions.count(), 1)
|
|
version = versions.first()
|
|
assert version is not None
|
|
assert version.original_filename is not None
|
|
self.assertEqual(version.version_index, 1)
|
|
self.assertEqual(version.version_label, "v2")
|
|
self.assertIsNone(version.archive_serial_number)
|
|
self.assertEqual(version.original_filename, version_file.name)
|
|
self.assertTrue(bool(version.content))
|
|
root_doc.refresh_from_db()
|
|
self.assertGreater(root_doc.modified, original_modified)
|
|
|
|
@override_settings(AUDIT_LOG_ENABLED=True)
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_version_with_missing_actor_and_filename_without_suffix(
|
|
self,
|
|
m: mock.Mock,
|
|
) -> None:
|
|
m.return_value = MagicMock()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
root_doc = Document.objects.first()
|
|
self.assertIsNotNone(root_doc)
|
|
assert root_doc is not None
|
|
|
|
version_file = self.get_test_file2()
|
|
status = FakeProgressManager(version_file.name, None)
|
|
overrides = DocumentMetadataOverrides(
|
|
filename="valid_pdf_version-upload",
|
|
actor_id=999999,
|
|
)
|
|
doc = ConsumableDocument(
|
|
DocumentSource.ApiUpload,
|
|
original_file=version_file,
|
|
root_document_id=root_doc.pk,
|
|
)
|
|
|
|
preflight = ConsumerPreflightPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
preflight.setup()
|
|
preflight.run()
|
|
|
|
consumer = ConsumerPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
consumer.setup()
|
|
try:
|
|
self.assertEqual(consumer.filename, "valid_pdf_version-upload")
|
|
consumer.run()
|
|
finally:
|
|
consumer.cleanup()
|
|
|
|
version = (
|
|
Document.objects.filter(root_document=root_doc).order_by("-id").first()
|
|
)
|
|
self.assertIsNotNone(version)
|
|
assert version is not None
|
|
self.assertEqual(version.version_index, 1)
|
|
self.assertEqual(version.original_filename, "valid_pdf_version-upload")
|
|
self.assertTrue(bool(version.content))
|
|
|
|
@override_settings(AUDIT_LOG_ENABLED=True)
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def test_consume_version_index_monotonic_after_version_deletion(self, m) -> None:
|
|
m.return_value = MagicMock()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
root_doc = Document.objects.first()
|
|
self.assertIsNotNone(root_doc)
|
|
assert root_doc is not None
|
|
|
|
def consume_version(version_file: Path) -> Document:
|
|
status = FakeProgressManager(version_file.name, None)
|
|
overrides = DocumentMetadataOverrides()
|
|
doc = ConsumableDocument(
|
|
DocumentSource.ApiUpload,
|
|
original_file=version_file,
|
|
root_document_id=root_doc.pk,
|
|
)
|
|
preflight = ConsumerPreflightPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
preflight.setup()
|
|
preflight.run()
|
|
|
|
consumer = ConsumerPlugin(
|
|
doc,
|
|
overrides,
|
|
status, # type: ignore[arg-type]
|
|
self.dirs.scratch_dir,
|
|
"task-id",
|
|
)
|
|
consumer.setup()
|
|
try:
|
|
consumer.run()
|
|
finally:
|
|
consumer.cleanup()
|
|
|
|
version = (
|
|
Document.objects.filter(root_document=root_doc).order_by("-id").first()
|
|
)
|
|
assert version is not None
|
|
return version
|
|
|
|
v1 = consume_version(self.get_test_file2())
|
|
self.assertEqual(v1.version_index, 1)
|
|
v1.delete()
|
|
|
|
# The next version should have version_index 2, even though version_index 1 was deleted
|
|
v2 = consume_version(self.get_test_file())
|
|
self.assertEqual(v2.version_index, 2)
|
|
|
|
@mock.patch("documents.consumer.load_classifier")
|
|
def testClassifyDocument(self, m) -> None:
|
|
correspondent = Correspondent.objects.create(
|
|
name="test",
|
|
matching_algorithm=Correspondent.MATCH_AUTO,
|
|
)
|
|
dtype = DocumentType.objects.create(
|
|
name="test",
|
|
matching_algorithm=DocumentType.MATCH_AUTO,
|
|
)
|
|
t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO)
|
|
t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_AUTO)
|
|
|
|
m.return_value = MagicMock()
|
|
m.return_value.predict_correspondent.return_value = correspondent.pk
|
|
m.return_value.predict_document_type.return_value = dtype.pk
|
|
m.return_value.predict_tags.return_value = [t1.pk]
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(document.correspondent, correspondent)
|
|
self.assertEqual(document.document_type, dtype)
|
|
self.assertIn(t1, document.tags.all())
|
|
self.assertNotIn(t2, document.tags.all())
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
@override_settings(CONSUMER_DELETE_DUPLICATES=True)
|
|
def test_delete_duplicate(self) -> None:
|
|
dst = self.get_test_file()
|
|
self.assertIsFile(dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
self.assertIsNotFile(dst)
|
|
self.assertIsNotNone(document)
|
|
|
|
dst = self.get_test_file()
|
|
self.assertIsFile(dst)
|
|
|
|
expected_message = (
|
|
f"{dst.name}: Not consuming {dst.name}: "
|
|
f"It is a duplicate of {document.title} (#{document.pk})"
|
|
)
|
|
|
|
with self.assertRaisesMessage(ConsumerError, expected_message):
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertIsNotFile(dst)
|
|
self.assertEqual(Document.objects.count(), 1)
|
|
self._assert_first_last_send_progress(last_status=ProgressStatusOptions.FAILED)
|
|
|
|
@override_settings(CONSUMER_DELETE_DUPLICATES=True)
|
|
def test_delete_duplicate_in_trash(self) -> None:
|
|
dst = self.get_test_file()
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
# Move the existing document to trash
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
document.delete()
|
|
|
|
dst = self.get_test_file()
|
|
self.assertIsFile(dst)
|
|
|
|
expected_message = (
|
|
f"{dst.name}: Not consuming {dst.name}: "
|
|
f"It is a duplicate of {document.title} (#{document.pk})"
|
|
f" Note: existing document is in the trash."
|
|
)
|
|
|
|
with self.assertRaisesMessage(ConsumerError, expected_message):
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertIsNotFile(dst)
|
|
self.assertEqual(Document.global_objects.count(), 1)
|
|
self.assertEqual(Document.objects.count(), 0)
|
|
|
|
@override_settings(CONSUMER_DELETE_DUPLICATES=False)
|
|
def test_no_delete_duplicate(self) -> None:
|
|
dst = self.get_test_file()
|
|
self.assertIsFile(dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self._assert_first_last_send_progress()
|
|
|
|
self.assertIsNotFile(dst)
|
|
self.assertIsNotNone(document)
|
|
|
|
dst = self.get_test_file()
|
|
self.assertIsFile(dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
self.assertIsNotFile(dst)
|
|
self.assertEqual(Document.objects.count(), 2)
|
|
self._assert_first_last_send_progress()
|
|
|
|
@override_settings(FILENAME_FORMAT="{title}", ARCHIVE_FILE_GENERATION="always")
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
def test_similar_filenames(self, m) -> None:
|
|
shutil.copy(
|
|
SIMPLE_DIGITAL_PDF,
|
|
settings.CONSUMPTION_DIR / "simple.pdf",
|
|
)
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "simple.png",
|
|
settings.CONSUMPTION_DIR / "simple.png",
|
|
)
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "simple-noalpha.png",
|
|
settings.CONSUMPTION_DIR / "simple.png.pdf",
|
|
)
|
|
m.return_value.get_parser_for_file.return_value = CopyParser
|
|
|
|
with self.get_consumer(settings.CONSUMPTION_DIR / "simple.png") as consumer:
|
|
consumer.run()
|
|
|
|
doc1 = Document.objects.filter(pk=1).first()
|
|
|
|
with self.get_consumer(settings.CONSUMPTION_DIR / "simple.pdf") as consumer:
|
|
consumer.run()
|
|
|
|
doc2 = Document.objects.filter(pk=2).first()
|
|
|
|
with self.get_consumer(settings.CONSUMPTION_DIR / "simple.png.pdf") as consumer:
|
|
consumer.run()
|
|
|
|
doc3 = Document.objects.filter(pk=3).first()
|
|
|
|
self.assertEqual(doc1.filename, "simple.png")
|
|
self.assertEqual(doc1.archive_filename, "simple.pdf")
|
|
|
|
self.assertEqual(doc2.filename, "simple.pdf")
|
|
self.assertEqual(doc2.archive_filename, "simple_01.pdf")
|
|
|
|
self.assertEqual(doc3.filename, "simple.png.pdf")
|
|
self.assertEqual(doc3.archive_filename, "simple.png.pdf")
|
|
|
|
sanity_check()
|
|
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
def test_try_to_clean_invalid_pdf(self, m, mock_registry) -> None:
|
|
mock_registry.return_value.get_parser_for_file.return_value = None
|
|
shutil.copy(
|
|
Path(__file__).parent / "samples" / "invalid_pdf.pdf",
|
|
settings.CONSUMPTION_DIR / "invalid_pdf.pdf",
|
|
)
|
|
with self.get_consumer(
|
|
settings.CONSUMPTION_DIR / "invalid_pdf.pdf",
|
|
) as consumer:
|
|
# fails because no qpdf
|
|
self.assertRaises(ConsumerError, consumer.run)
|
|
|
|
m.assert_called_once()
|
|
|
|
args, _ = m.call_args
|
|
|
|
command = args[0]
|
|
|
|
self.assertEqual(command[0], "qpdf")
|
|
self.assertEqual(command[1], "--replace-input")
|
|
|
|
@mock.patch("paperless_mail.models.MailRule.objects.get")
|
|
@mock.patch("paperless.parsers.mail.MailDocumentParser.get_thumbnail")
|
|
@mock.patch("paperless.parsers.mail.MailDocumentParser.parse")
|
|
@mock.patch("documents.consumer.get_parser_registry")
|
|
def test_mail_parser_receives_mailrule(
|
|
self,
|
|
mock_get_parser_registry: mock.Mock,
|
|
mock_mail_parser_parse: mock.Mock,
|
|
mock_get_thumbnail: mock.Mock,
|
|
mock_mailrule_get: mock.Mock,
|
|
) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A mail document from a mail rule
|
|
WHEN:
|
|
- The consumer is run
|
|
THEN:
|
|
- The mail parser should receive the mail rule
|
|
"""
|
|
from documents.parsers import ParseError
|
|
from paperless.parsers.mail import MailDocumentParser
|
|
|
|
mock_get_parser_registry.return_value.get_parser_for_file.return_value = (
|
|
MailDocumentParser
|
|
)
|
|
mock_mailrule_get.return_value = mock.Mock(
|
|
pdf_layout=MailRule.PdfLayout.HTML_ONLY,
|
|
)
|
|
mock_get_thumbnail.side_effect = ParseError("no thumbnail")
|
|
|
|
src = (
|
|
Path(__file__).parent.parent.parent
|
|
/ Path("paperless")
|
|
/ Path("tests")
|
|
/ Path("samples")
|
|
/ Path("mail")
|
|
/ "html.eml"
|
|
)
|
|
dst = self.dirs.scratch_dir / "html.eml"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(
|
|
filepath=dst,
|
|
source=DocumentSource.MailFetch,
|
|
mailrule_id=1,
|
|
) as consumer:
|
|
with self.assertRaises(
|
|
ConsumerError,
|
|
):
|
|
consumer.run()
|
|
mock_mail_parser_parse.assert_called_once_with(
|
|
consumer.working_copy,
|
|
"message/rfc822",
|
|
produce_archive=True,
|
|
)
|
|
|
|
@mock.patch("documents.consumer.generate_unique_filename")
|
|
def test_consume_refuses_to_write_outside_originals_dir(
|
|
self,
|
|
m: mock.Mock,
|
|
) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Filename generation produces a path outside of the originals directory
|
|
WHEN:
|
|
- The document is consumed
|
|
THEN:
|
|
- The consumption fails and no file is written outside of the root
|
|
"""
|
|
m.return_value = Path("../../pwned.pdf")
|
|
escaped = (settings.ORIGINALS_DIR / ".." / ".." / "pwned.pdf").resolve()
|
|
|
|
with self.get_consumer(self.get_test_file()) as consumer:
|
|
with self.assertRaises(ConsumerError):
|
|
consumer.run()
|
|
|
|
self.assertIsNotFile(escaped)
|
|
self.assertEqual(Document.objects.count(), 0)
|
|
|
|
|
|
@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)
|
|
class TestConsumerCreatedDate(DirectoriesMixin, GetConsumerMixin, TestCase):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
|
|
def test_consume_date_from_content(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- File content with date in DMY (default) format
|
|
|
|
THEN:
|
|
- Should parse the date from the file content
|
|
"""
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000005.pdf"
|
|
)
|
|
dst = self.dirs.scratch_dir / "sample.pdf"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(
|
|
document.created,
|
|
datetime.date(1996, 2, 20),
|
|
)
|
|
|
|
@override_settings(FILENAME_DATE_ORDER="YMD")
|
|
def test_consume_date_from_filename(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- File content with date in DMY (default) format
|
|
- Filename with date in YMD format
|
|
|
|
THEN:
|
|
- Should parse the date from the filename
|
|
"""
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000005.pdf"
|
|
)
|
|
dst = self.dirs.scratch_dir / "Scan - 2022-02-01.pdf"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(
|
|
document.created,
|
|
datetime.date(2022, 2, 1),
|
|
)
|
|
|
|
def test_consume_date_filename_date_use_content(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- File content with date in DMY (default) format
|
|
- Filename date parsing disabled
|
|
- Filename with date in YMD format
|
|
|
|
THEN:
|
|
- Should parse the date from the content
|
|
"""
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000005.pdf"
|
|
)
|
|
dst = self.dirs.scratch_dir / "Scan - 2022-02-01.pdf"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(
|
|
document.created,
|
|
datetime.date(1996, 2, 20),
|
|
)
|
|
|
|
@override_settings(
|
|
IGNORE_DATES=(datetime.date(2010, 12, 13), datetime.date(2011, 11, 12)),
|
|
)
|
|
def test_consume_date_use_content_with_ignore(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- File content with dates in DMY (default) format
|
|
- File content includes ignored dates
|
|
|
|
THEN:
|
|
- Should parse the date from the filename
|
|
"""
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000006.pdf"
|
|
)
|
|
dst = self.dirs.scratch_dir / "0000006.pdf"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(dst) as consumer:
|
|
consumer.run()
|
|
|
|
document = Document.objects.first()
|
|
assert document is not None
|
|
|
|
self.assertEqual(
|
|
document.created,
|
|
datetime.date(1997, 2, 20),
|
|
)
|
|
|
|
|
|
class PreConsumeTestCase(DirectoriesMixin, GetConsumerMixin, TestCase):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000005.pdf"
|
|
)
|
|
self.test_file = self.dirs.scratch_dir / "sample.pdf"
|
|
shutil.copy(src, self.test_file)
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
@override_settings(PRE_CONSUME_SCRIPT=None)
|
|
def test_no_pre_consume_script(self, m) -> None:
|
|
with self.get_consumer(self.test_file) as c:
|
|
c.run()
|
|
# Verify no pre-consume script subprocess was invoked
|
|
# (run_subprocess may still be called by pdf_born_digital_text via pdftotext)
|
|
script_calls = [
|
|
call
|
|
for call in m.call_args_list
|
|
if call.args and call.args[0] and call.args[0][0] not in ("pdftotext",)
|
|
]
|
|
self.assertEqual(script_calls, [])
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
@override_settings(PRE_CONSUME_SCRIPT="does-not-exist")
|
|
def test_pre_consume_script_not_found(self, m) -> None:
|
|
with self.get_consumer(self.test_file) as c:
|
|
self.assertRaises(ConsumerError, c.run)
|
|
m.assert_not_called()
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
def test_pre_consume_script(self, m) -> None:
|
|
with tempfile.NamedTemporaryFile() as script:
|
|
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
|
with self.get_consumer(self.test_file) as c:
|
|
c.run()
|
|
|
|
self.assertTrue(m.called)
|
|
|
|
# Find the call that invoked the pre-consume script
|
|
# (run_subprocess may also be called by pdf_born_digital_text via pdftotext)
|
|
script_call = next(
|
|
call
|
|
for call in m.call_args_list
|
|
if call.args and call.args[0] and call.args[0][0] == script.name
|
|
)
|
|
args, _ = script_call
|
|
|
|
command = args[0]
|
|
environment = args[1]
|
|
|
|
self.assertEqual(command[0], script.name)
|
|
self.assertEqual(len(command), 1)
|
|
|
|
subset = {
|
|
"DOCUMENT_SOURCE_PATH": str(c.input_doc.original_file),
|
|
"DOCUMENT_WORKING_PATH": str(c.working_copy),
|
|
"TASK_ID": c.task_id,
|
|
}
|
|
self.assertDictEqual(environment, {**environment, **subset})
|
|
|
|
def test_script_with_output(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A script which outputs to stdout and stderr
|
|
WHEN:
|
|
- The script is executed as a consume script
|
|
THEN:
|
|
- The script's outputs are logged
|
|
"""
|
|
with tempfile.NamedTemporaryFile(mode="w") as script:
|
|
# Write up a little script
|
|
with script.file as outfile:
|
|
outfile.write("#!/usr/bin/env bash\n")
|
|
outfile.write("echo This message goes to stdout\n")
|
|
outfile.write("echo This message goes to stderr >&2")
|
|
|
|
# Make the file executable
|
|
st = Path(script.name).stat()
|
|
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
|
|
|
|
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
|
with self.assertLogs("paperless.consumer", level="INFO") as cm:
|
|
with self.get_consumer(self.test_file) as c:
|
|
c.run()
|
|
self.assertIn(
|
|
"INFO:paperless.consumer:This message goes to stdout",
|
|
cm.output,
|
|
)
|
|
self.assertIn(
|
|
"WARNING:paperless.consumer:This message goes to stderr",
|
|
cm.output,
|
|
)
|
|
|
|
def test_script_exit_non_zero(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A script which exits with a non-zero exit code
|
|
WHEN:
|
|
- The script is executed as a pre-consume script
|
|
THEN:
|
|
- A ConsumerError is raised
|
|
"""
|
|
with tempfile.NamedTemporaryFile(mode="w") as script:
|
|
# Write up a little script
|
|
with script.file as outfile:
|
|
outfile.write("#!/usr/bin/env bash\n")
|
|
outfile.write("exit 100\n")
|
|
|
|
# Make the file executable
|
|
st = Path(script.name).stat()
|
|
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
|
|
|
|
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
|
with self.get_consumer(self.test_file) as c:
|
|
self.assertRaises(
|
|
ConsumerError,
|
|
c.run,
|
|
)
|
|
|
|
|
|
class PostConsumeTestCase(DirectoriesMixin, GetConsumerMixin, TestCase):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
src = (
|
|
Path(__file__).parent
|
|
/ "samples"
|
|
/ "documents"
|
|
/ "originals"
|
|
/ "0000005.pdf"
|
|
)
|
|
self.test_file = self.dirs.scratch_dir / "sample.pdf"
|
|
shutil.copy(src, self.test_file)
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
@override_settings(POST_CONSUME_SCRIPT=None)
|
|
def test_no_post_consume_script(self, m) -> None:
|
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
|
tag1 = Tag.objects.create(name="a")
|
|
tag2 = Tag.objects.create(name="b")
|
|
doc.tags.add(tag1)
|
|
doc.tags.add(tag2)
|
|
|
|
with self.get_consumer(self.test_file) as consumer:
|
|
consumer.run_post_consume_script(doc)
|
|
m.assert_not_called()
|
|
|
|
@override_settings(POST_CONSUME_SCRIPT="does-not-exist")
|
|
def test_post_consume_script_not_found(self) -> None:
|
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
|
|
|
with self.get_consumer(self.test_file) as consumer:
|
|
with self.assertRaisesMessage(
|
|
ConsumerError,
|
|
"sample.pdf: Configured post-consume script does-not-exist does not exist",
|
|
):
|
|
consumer.run_post_consume_script(doc)
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
def test_post_consume_script_simple(self, m: mock.MagicMock) -> None:
|
|
with tempfile.NamedTemporaryFile() as script:
|
|
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
|
|
|
with self.get_consumer(self.test_file) as consumer:
|
|
consumer.run_post_consume_script(doc)
|
|
|
|
m.assert_called_once()
|
|
|
|
@mock.patch("documents.consumer.run_subprocess")
|
|
def test_post_consume_script_with_correspondent_and_type(
|
|
self,
|
|
m: mock.MagicMock,
|
|
) -> None:
|
|
with tempfile.NamedTemporaryFile() as script:
|
|
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
|
c = Correspondent.objects.create(name="my_bank")
|
|
t = DocumentType.objects.create(
|
|
name="Test type",
|
|
)
|
|
doc = Document.objects.create(
|
|
title="Test",
|
|
document_type=t,
|
|
mime_type="application/pdf",
|
|
correspondent=c,
|
|
)
|
|
tag1 = Tag.objects.create(name="a")
|
|
tag2 = Tag.objects.create(name="b")
|
|
doc.tags.add(tag1)
|
|
doc.tags.add(tag2)
|
|
|
|
with self.get_consumer(self.test_file) as consumer:
|
|
consumer.run_post_consume_script(doc)
|
|
|
|
m.assert_called_once()
|
|
|
|
args, _ = m.call_args
|
|
|
|
command = args[0]
|
|
environment = args[1]
|
|
|
|
self.assertEqual(command[0], script.name)
|
|
self.assertEqual(len(command), 1)
|
|
|
|
subset = {
|
|
"DOCUMENT_ID": str(doc.pk),
|
|
"DOCUMENT_TYPE": "Test type",
|
|
"DOCUMENT_DOWNLOAD_URL": f"/api/documents/{doc.pk}/download/",
|
|
"DOCUMENT_THUMBNAIL_URL": f"/api/documents/{doc.pk}/thumb/",
|
|
"DOCUMENT_CORRESPONDENT": "my_bank",
|
|
"DOCUMENT_TAGS": "a,b",
|
|
"TASK_ID": consumer.task_id,
|
|
}
|
|
|
|
self.assertDictEqual(environment, {**environment, **subset})
|
|
|
|
def test_script_exit_non_zero(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A script which exits with a non-zero exit code
|
|
WHEN:
|
|
- The script is executed as a post-consume script
|
|
THEN:
|
|
- A ConsumerError is raised
|
|
"""
|
|
with tempfile.NamedTemporaryFile(mode="w") as script:
|
|
# Write up a little script
|
|
with script.file as outfile:
|
|
outfile.write("#!/usr/bin/env bash\n")
|
|
outfile.write("exit -500\n")
|
|
|
|
# Make the file executable
|
|
st = Path(script.name).stat()
|
|
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
|
|
|
|
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
|
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
|
with self.get_consumer(self.test_file) as consumer:
|
|
with self.assertRaisesRegex(
|
|
ConsumerError,
|
|
r"sample\.pdf: Error while executing post-consume script: Command '\[.*\]' returned non-zero exit status \d+\.",
|
|
):
|
|
consumer.run_post_consume_script(doc)
|
|
|
|
|
|
class TestConsumerRemoteOCR(
|
|
DirectoriesMixin,
|
|
FileSystemAssertsMixin,
|
|
GetConsumerMixin,
|
|
TestCase,
|
|
):
|
|
"""
|
|
The consumer resolves the remote OCR mode and the per-document request from
|
|
workflows into the allow_remote flag it hands to the parser registry.
|
|
"""
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
|
|
patcher = mock.patch("documents.consumer.get_parser_registry")
|
|
self.mock_registry = patcher.start()
|
|
self.mock_registry.return_value.get_parser_for_file.return_value = DummyParser
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def _consume(self, *, overrides: DocumentMetadataOverrides | None = None) -> bool:
|
|
src = SIMPLE_DIGITAL_PDF
|
|
dst = self.dirs.scratch_dir / "sample.pdf"
|
|
shutil.copy(src, dst)
|
|
|
|
with self.get_consumer(dst, overrides=overrides) as consumer:
|
|
consumer.run()
|
|
|
|
_, kwargs = self.mock_registry.return_value.get_parser_for_file.call_args
|
|
return kwargs["allow_remote"]
|
|
|
|
@override_settings(REMOTE_OCR_MODE="always")
|
|
def test_always_mode_allows_remote(self) -> None:
|
|
"""
|
|
GIVEN: Remote OCR mode is 'always'.
|
|
WHEN: A document is consumed without any workflow asking for it.
|
|
THEN: The registry is allowed to pick the remote parser.
|
|
"""
|
|
self.assertTrue(self._consume())
|
|
|
|
@override_settings(REMOTE_OCR_MODE="workflow_only")
|
|
def test_workflow_only_mode_denies_remote_by_default(self) -> None:
|
|
"""
|
|
GIVEN: Remote OCR mode is 'workflow_only'.
|
|
WHEN: A document is consumed and nothing asked for remote OCR.
|
|
THEN: The remote parser is excluded.
|
|
"""
|
|
self.assertFalse(self._consume())
|
|
|
|
@override_settings(REMOTE_OCR_MODE="workflow_only")
|
|
def test_workflow_only_mode_allows_remote_when_requested(self) -> None:
|
|
"""
|
|
GIVEN: Remote OCR mode is 'workflow_only'.
|
|
WHEN: A workflow set remote_ocr on the metadata overrides.
|
|
THEN: The registry is allowed to pick the remote parser.
|
|
"""
|
|
self.assertTrue(
|
|
self._consume(overrides=DocumentMetadataOverrides(remote_ocr=True)),
|
|
)
|
|
|
|
|
|
class TestMetadataOverrides(TestCase):
|
|
def test_update_skip_asn_if_exists(self) -> None:
|
|
base = DocumentMetadataOverrides()
|
|
incoming = DocumentMetadataOverrides(skip_asn_if_exists=True)
|
|
base.update(incoming)
|
|
self.assertTrue(base.skip_asn_if_exists)
|
|
|
|
def test_update_remote_ocr(self) -> None:
|
|
base = DocumentMetadataOverrides()
|
|
base.update(DocumentMetadataOverrides(remote_ocr=True))
|
|
self.assertTrue(base.remote_ocr)
|
|
|
|
def test_update_remote_ocr_is_not_unset(self) -> None:
|
|
"""
|
|
A later workflow that says nothing must not undo an earlier one that
|
|
asked for remote OCR.
|
|
"""
|
|
base = DocumentMetadataOverrides(remote_ocr=True)
|
|
base.update(DocumentMetadataOverrides())
|
|
self.assertTrue(base.remote_ocr)
|
|
|
|
def test_update_actor_and_version_label(self) -> None:
|
|
base = DocumentMetadataOverrides(
|
|
actor_id=1,
|
|
version_label="root",
|
|
)
|
|
incoming = DocumentMetadataOverrides(
|
|
actor_id=2,
|
|
version_label="v2",
|
|
)
|
|
base.update(incoming)
|
|
self.assertEqual(base.actor_id, 2)
|
|
self.assertEqual(base.version_label, "v2")
|
|
|
|
def test_from_document_copies_user_and_group_grants_separately(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- A document with view and change granted directly to one user and
|
|
to a group with another user as a member
|
|
WHEN:
|
|
- Overrides are built from the document
|
|
THEN:
|
|
- The user lists hold only the directly granted user
|
|
- The group lists hold the group
|
|
"""
|
|
direct_user = UserFactory()
|
|
group_member = UserFactory()
|
|
group = Group.objects.create(name="group")
|
|
group_member.groups.add(group)
|
|
document = DocumentFactory()
|
|
grant_object(direct_user, document, "view_document", "change_document")
|
|
grant_object(group, document, "view_document", "change_document")
|
|
|
|
overrides = DocumentMetadataOverrides.from_document(document)
|
|
|
|
self.assertEqual(overrides.view_users, [direct_user.pk])
|
|
self.assertEqual(overrides.change_users, [direct_user.pk])
|
|
self.assertEqual(overrides.view_groups, [group.pk])
|
|
self.assertEqual(overrides.change_groups, [group.pk])
|
|
|
|
|
|
class TestBarcodeApplyDetectedASN(TestCase):
|
|
"""
|
|
GIVEN:
|
|
- Existing Documents with ASN 123
|
|
WHEN:
|
|
- A BarcodePlugin which detected an ASN
|
|
THEN:
|
|
- If skip_asn_if_exists is set, and ASN exists, do not set ASN
|
|
- If skip_asn_if_exists is set, and ASN does not exist, set ASN
|
|
"""
|
|
|
|
def test_apply_detected_asn_skips_existing_when_flag_set(self) -> None:
|
|
doc = Document.objects.create(
|
|
checksum="X1",
|
|
title="D1",
|
|
archive_serial_number=123,
|
|
)
|
|
metadata = DocumentMetadataOverrides(skip_asn_if_exists=True)
|
|
plugin = BarcodePlugin(
|
|
input_doc=mock.Mock(),
|
|
metadata=metadata,
|
|
status_mgr=mock.Mock(),
|
|
base_tmp_dir=Path(tempfile.gettempdir()),
|
|
task_id="test-task",
|
|
)
|
|
|
|
plugin._apply_detected_asn(123)
|
|
self.assertIsNone(plugin.metadata.asn)
|
|
|
|
doc.hard_delete()
|
|
|
|
plugin._apply_detected_asn(123)
|
|
self.assertEqual(plugin.metadata.asn, 123)
|