|
|
|
@@ -1,14 +1,13 @@
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
|
|
|
|
|
|
from ..file_handling import generate_filename, create_source_path_directory, delete_empty_directories
|
|
|
|
|
from ..models import Document, Correspondent
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
from ..signals.handlers import update_filename_and_move_files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -68,24 +67,18 @@ class TestDate(TestCase):
|
|
|
|
|
# test that creating dirs for the source_path creates the correct directory
|
|
|
|
|
create_source_path_directory(document.source_path)
|
|
|
|
|
Path(document.source_path).touch()
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), True)
|
|
|
|
|
|
|
|
|
|
# Set a correspondent and save the document
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(
|
|
|
|
|
name="test")[0]
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
|
|
|
|
document.save()
|
|
|
|
|
|
|
|
|
|
# Check proper handling of files
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/test"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR +
|
|
|
|
|
"/test/test-{:07d}.pdf.gpg".format(document.pk)), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/test"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/test/test-{:07d}.pdf.gpg".format(document.pk)), True)
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
|
|
|
|
def test_file_renaming_missing_permissions(self):
|
|
|
|
|
document = Document()
|
|
|
|
|
document.file_type = "pdf"
|
|
|
|
@@ -100,27 +93,22 @@ class TestDate(TestCase):
|
|
|
|
|
Path(document.source_path).touch()
|
|
|
|
|
|
|
|
|
|
# Test source_path
|
|
|
|
|
self.assertEqual(document.source_path, settings.ORIGINALS_DIR +
|
|
|
|
|
"/none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
self.assertEqual(document.source_path, settings.ORIGINALS_DIR + "/none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
|
|
|
|
|
# Make the folder read- and execute-only (no writing and no renaming)
|
|
|
|
|
os.chmod(settings.ORIGINALS_DIR + "/none", 0o555)
|
|
|
|
|
|
|
|
|
|
# Set a correspondent and save the document
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(
|
|
|
|
|
name="test")[0]
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
|
|
|
|
document.save()
|
|
|
|
|
|
|
|
|
|
# Check proper handling of files
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
|
|
|
|
|
"originals/none/none-{:07d}.pdf".format(document.pk)), True)
|
|
|
|
|
self.assertEqual(document.filename,
|
|
|
|
|
"none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/originals/none/none-{:07d}.pdf".format(document.pk)), True)
|
|
|
|
|
self.assertEqual(document.filename, "none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
|
|
|
|
|
os.chmod(settings.ORIGINALS_DIR + "/none", 0o777)
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
|
|
|
|
def test_file_renaming_database_error(self):
|
|
|
|
|
|
|
|
|
|
document1 = Document.objects.create(file_type="pdf", storage_type=Document.STORAGE_TYPE_UNENCRYPTED, checksum="AAAAA")
|
|
|
|
@@ -155,13 +143,10 @@ class TestDate(TestCase):
|
|
|
|
|
|
|
|
|
|
# Check proper handling of files
|
|
|
|
|
self.assertTrue(os.path.isfile(document.source_path))
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
|
|
|
|
|
"originals/none/none-{:07d}.pdf".format(document.pk)), True)
|
|
|
|
|
self.assertEqual(document.filename,
|
|
|
|
|
"none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/originals/none/none-{:07d}.pdf".format(document.pk)), True)
|
|
|
|
|
self.assertEqual(document.filename, "none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
|
|
|
|
def test_document_delete(self):
|
|
|
|
|
document = Document()
|
|
|
|
|
document.file_type = "pdf"
|
|
|
|
@@ -179,13 +164,10 @@ class TestDate(TestCase):
|
|
|
|
|
# Ensure file deletion after delete
|
|
|
|
|
pk = document.pk
|
|
|
|
|
document.delete()
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none/none-{:07d}.pdf".format(pk)), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none-{:07d}.pdf".format(pk)), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
|
|
|
|
def test_document_delete_nofile(self):
|
|
|
|
|
document = Document()
|
|
|
|
|
document.file_type = "pdf"
|
|
|
|
@@ -194,8 +176,7 @@ class TestDate(TestCase):
|
|
|
|
|
|
|
|
|
|
document.delete()
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
|
|
|
|
def test_directory_not_empty(self):
|
|
|
|
|
document = Document()
|
|
|
|
|
document.file_type = "pdf"
|
|
|
|
@@ -214,18 +195,14 @@ class TestDate(TestCase):
|
|
|
|
|
Path(important_file).touch()
|
|
|
|
|
|
|
|
|
|
# Set a correspondent and save the document
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(
|
|
|
|
|
name="test")[0]
|
|
|
|
|
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
|
|
|
|
document.save()
|
|
|
|
|
|
|
|
|
|
# Check proper handling of files
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
|
|
|
|
"/documents/originals/test"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
|
|
|
|
"/documents/originals/none"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT + "/documents/originals/test"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT + "/documents/originals/none"), True)
|
|
|
|
|
self.assertTrue(os.path.isfile(important_file))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")
|
|
|
|
|
def test_tags_with_underscore(self):
|
|
|
|
|
document = Document()
|
|
|
|
@@ -304,9 +281,7 @@ class TestDate(TestCase):
|
|
|
|
|
self.assertEqual(generate_filename(document),
|
|
|
|
|
"none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
|
|
|
|
"{correspondent}/{correspondent}")
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}")
|
|
|
|
|
def test_nested_directory_cleanup(self):
|
|
|
|
|
document = Document()
|
|
|
|
|
document.file_type = "pdf"
|
|
|
|
@@ -315,25 +290,19 @@ class TestDate(TestCase):
|
|
|
|
|
|
|
|
|
|
# Ensure that filename is properly generated
|
|
|
|
|
document.filename = generate_filename(document)
|
|
|
|
|
self.assertEqual(document.filename,
|
|
|
|
|
"none/none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
self.assertEqual(document.filename, "none/none/none-{:07d}.pdf".format(document.pk))
|
|
|
|
|
create_source_path_directory(document.source_path)
|
|
|
|
|
Path(document.source_path).touch()
|
|
|
|
|
|
|
|
|
|
# Check proper handling of files
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none/none"), True)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), True)
|
|
|
|
|
|
|
|
|
|
pk = document.pk
|
|
|
|
|
document.delete()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none/none/none-{:07d}.pdf".format(pk)),
|
|
|
|
|
False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR +
|
|
|
|
|
"/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none-{:07d}.pdf".format(pk)), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
|
|
|
|
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR), True)
|
|
|
|
|
|
|
|
|
|
@override_settings(PAPERLESS_FILENAME_FORMAT=None)
|
|
|
|
@@ -355,8 +324,7 @@ class TestDate(TestCase):
|
|
|
|
|
Path(os.path.join(tmp, "notempty", "file")).touch()
|
|
|
|
|
os.makedirs(os.path.join(tmp, "notempty", "empty"))
|
|
|
|
|
|
|
|
|
|
delete_empty_directories(
|
|
|
|
|
os.path.join(tmp, "notempty", "empty"))
|
|
|
|
|
delete_empty_directories(os.path.join(tmp, "notempty", "empty"))
|
|
|
|
|
self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty")), True)
|
|
|
|
|
self.assertEqual(os.path.isfile(
|
|
|
|
|
os.path.join(tmp, "notempty", "file")), True)
|
|
|
|
|