Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Add: more tests

Wolf-Bastian Poettner4772480c8349
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index 75ce80905..b00ff68c7 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -97,6 +97,45 @@ class TestDate(TestCase):
self.assertEqual(document.generate_source_filename(),
"test/test-0000001.pdf.gpg")
+ @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
+ format(str(uuid4())[:8]))
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
+ "{correspondent}")
+ def test_file_renaming_missing_permissions(self):
+ self.add_to_deletion_list(settings.MEDIA_ROOT)
+
+ document = Document()
+ document.file_type = "pdf"
+ document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+ document.save()
+
+ # Ensure that filename is properly generated
+ tmp = document.source_filename
+ self.assertEqual(document.generate_source_filename(),
+ "none/none-0000001.pdf")
+ document.create_source_directory()
+ Path(document.source_path).touch()
+
+ # Test source_path
+ self.assertEqual(document.source_path, settings.MEDIA_ROOT +
+ "/documents/originals/none/none-0000001.pdf")
+
+ # Make the folder read- and execute-only (no writing and no renaming)
+ os.chmod(settings.MEDIA_ROOT + "/documents/originals/none", 0o555)
+
+ # Set a correspondent and save the document
+ 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-0000001.pdf"), True)
+ self.assertEqual(document.source_filename,
+ "none/none-0000001.pdf")
+
+ os.chmod(settings.MEDIA_ROOT + "/documents/originals/none", 0o666)
+
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
@@ -123,6 +162,16 @@ class TestDate(TestCase):
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
"/documents/originals/none"), False)
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
+ "{correspondent}")
+ def test_document_delete_nofile(self):
+ document = Document()
+ document.file_type = "pdf"
+ document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+ document.save()
+
+ document.delete()
+
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
@@ -393,6 +442,8 @@ class TestDate(TestCase):
"test/test-0000001.pdf")
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
"originals/test/test-0000001.pdf"), True)
+ self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
+ "originals/none/none-0000001.pdf"), False)
# Set new correspondent and expect document to be saved properly
document.correspondent = Correspondent.objects.get_or_create(
@@ -442,6 +493,8 @@ class TestDate(TestCase):
"test/test-0000001.pdf.gpg")
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
"originals/test/test-0000001.pdf.gpg"), True)
+ self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
+ "originals/none/none-0000001.pdf"), False)
# Set new correspondent and expect document to be saved properly
document.correspondent = Correspondent.objects.get_or_create(
@@ -459,3 +512,105 @@ class TestDate(TestCase):
"/documents/originals/test"), False)
self.assertEqual(document.generate_source_filename(),
"foo/foo-0000001.pdf.gpg")
+
+ def test_delete_all_empty_subdirectories(self):
+ # Create our working directory
+ tmp = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
+ os.makedirs(tmp)
+ self.add_to_deletion_list(tmp)
+
+ os.makedirs(os.path.join(tmp, "empty"))
+ os.makedirs(os.path.join(tmp, "empty", "subdirectory"))
+
+ os.makedirs(os.path.join(tmp, "notempty"))
+ Path(os.path.join(tmp, "notempty", "file")).touch()
+
+ Document.delete_all_empty_subdirectories(tmp)
+
+ self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty")), True)
+ self.assertEqual(os.path.isdir(os.path.join(tmp, "empty")), False)
+ self.assertEqual(os.path.isfile(
+ os.path.join(tmp, "notempty", "file")), True)
+
+ def test_try_delete_empty_directories(self):
+ # Create our working directory
+ tmp = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
+ os.makedirs(tmp)
+ self.add_to_deletion_list(tmp)
+
+ os.makedirs(os.path.join(tmp, "notempty"))
+ Path(os.path.join(tmp, "notempty", "file")).touch()
+ os.makedirs(os.path.join(tmp, "notempty", "empty"))
+
+ Document.try_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)
+ self.assertEqual(os.path.isdir(
+ os.path.join(tmp, "notempty", "empty")), False)
+
+ @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
+ format(str(uuid4())[:8]))
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
+ "{correspondent}")
+ def test_document_accidentally_deleted(self):
+ self.add_to_deletion_list(settings.MEDIA_ROOT)
+
+ document = Document()
+ document.file_type = "pdf"
+ document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+ document.save()
+
+ # Ensure that filename is properly generated
+ tmp = document.source_filename
+ self.assertEqual(document.generate_source_filename(),
+ "none/none-0000001.pdf")
+ document.create_source_directory()
+ Path(document.source_path).touch()
+
+ # Test source_path
+ self.assertEqual(document.source_path, settings.MEDIA_ROOT +
+ "/documents/originals/none/none-0000001.pdf")
+
+ # Delete the document "illegaly"
+ os.remove(settings.MEDIA_ROOT + "/documents/originals/" +
+ "none/none-0000001.pdf")
+
+ # Set new correspondent and expect document to be saved properly
+ document.correspondent = Correspondent.objects.get_or_create(
+ name="foo")[0]
+ document.save()
+
+ # Check proper handling of files
+ self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
+ "/documents/originals/none"), True)
+ self.assertEqual(document.source_filename,
+ "none/none-0000001.pdf")
+
+ @override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
+ format(str(uuid4())[:8]))
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
+ "{correspondent}")
+ def test_set_filename(self):
+ self.add_to_deletion_list(settings.MEDIA_ROOT)
+
+ document = Document()
+ document.file_type = "pdf"
+ document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+ document.save()
+
+ # Ensure that filename is properly generated
+ tmp = document.source_filename
+ self.assertEqual(document.generate_source_filename(),
+ "none/none-0000001.pdf")
+ document.create_source_directory()
+ Path(document.source_path).touch()
+
+ # Set existing filename
+ document.set_filename(tmp)
+ self.assertEqual(document.source_filename, "none/none-0000001.pdf")
+
+ # Set non-existing filename
+ document.set_filename("doesnotexist")
+ self.assertEqual(document.source_filename, "none/none-0000001.pdf")