diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index c4cca8fdc..84b3bc185 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -149,19 +149,16 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): @override_settings(PAPERLESS_FILENAME_FORMAT="") def test_download_with_archive(self): - _, filename = tempfile.mkstemp(dir=self.dirs.originals_dir) - content = b"This is a test" content_archive = b"This is the same test but archived" - with open(filename, "wb") as f: - f.write(content) - - filename = os.path.basename(filename) - - doc = Document.objects.create(title="none", filename=filename, + doc = Document.objects.create(title="none", filename="my_document.pdf", + archive_filename="archived.pdf", mime_type="application/pdf") + with open(doc.source_path, "wb") as f: + f.write(content) + with open(doc.archive_path, "wb") as f: f.write(content_archive) @@ -578,7 +575,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): async_task.assert_not_called() def test_get_metadata(self): - doc = Document.objects.create(title="test", filename="file.pdf", mime_type="image/png", archive_checksum="A") + doc = Document.objects.create(title="test", filename="file.pdf", mime_type="image/png", archive_checksum="A", archive_filename="archive.pdf") shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png"), doc.source_path) shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), doc.archive_path) @@ -592,6 +589,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertTrue(meta['has_archive_version']) self.assertEqual(len(meta['original_metadata']), 0) self.assertGreater(len(meta['archive_metadata']), 0) + self.assertEqual(meta['media_filename'], "file.pdf") + self.assertEqual(meta['archive_media_filename'], "archive.pdf") def test_get_metadata_invalid_doc(self): response = self.client.get(f"/api/documents/34576/metadata/") @@ -611,6 +610,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertFalse(meta['has_archive_version']) self.assertGreater(len(meta['original_metadata']), 0) self.assertIsNone(meta['archive_metadata']) + self.assertIsNone(meta['archive_media_filename']) def test_get_empty_suggestions(self): doc = Document.objects.create(title="test", mime_type="application/pdf") diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index b96e4844b..1ed041187 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -296,6 +296,7 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertIsNone(document.correspondent) self.assertIsNone(document.document_type) self.assertEqual(document.filename, "0000001.pdf") + self.assertEqual(document.archive_filename, "0000001.pdf") self.assertTrue(os.path.isfile( document.source_path @@ -454,6 +455,7 @@ class TestConsumer(DirectoriesMixin, TestCase): 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() @@ -468,7 +470,7 @@ class TestConsumer(DirectoriesMixin, TestCase): filenames.insert(0, f) return f - m.side_effect = lambda f, root: get_filename() + m.side_effect = lambda f, archive_filename = False: get_filename() filename = self.get_test_file() @@ -479,6 +481,7 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertEqual(document.title, "new docs") self.assertIsNotNone(os.path.isfile(document.title)) self.assertTrue(os.path.isfile(document.source_path)) + self.assertTrue(os.path.isfile(document.archive_path)) self._assert_first_last_send_progress() @@ -553,8 +556,16 @@ class TestConsumer(DirectoriesMixin, TestCase): doc2 = self.consumer.try_consume_file(os.path.join(settings.CONSUMPTION_DIR, "simple.pdf")) doc3 = self.consumer.try_consume_file(os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf")) + 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() + class PreConsumeTestCase(TestCase): @mock.patch("documents.consumer.Popen") diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index ae25c0f12..fd74aad7d 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -448,7 +448,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", filename="0000001.pdf", checksum="A", archive_filename="0000001.pdf", archive_checksum="B") self.assertTrue(os.path.isfile(original)) self.assertTrue(os.path.isfile(archive)) @@ -461,7 +461,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") self.assertFalse(os.path.isfile(original)) self.assertFalse(os.path.isfile(archive)) @@ -475,7 +475,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf") archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") self.assertTrue(os.path.isfile(original)) self.assertFalse(os.path.isfile(archive)) @@ -486,16 +486,19 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): def test_move_archive_exists(self): original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf") archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") + existing_archive_file = os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf") Path(original).touch() Path(archive).touch() os.makedirs(os.path.join(settings.ARCHIVE_DIR, "none")) - Path(os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf")).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + Path(existing_archive_file).touch() + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") - self.assertTrue(os.path.isfile(original)) - self.assertTrue(os.path.isfile(archive)) + self.assertFalse(os.path.isfile(original)) + self.assertFalse(os.path.isfile(archive)) self.assertTrue(os.path.isfile(doc.source_path)) self.assertTrue(os.path.isfile(doc.archive_path)) + self.assertTrue(os.path.isfile(existing_archive_file)) + self.assertEqual(doc.archive_filename, "none/my_doc_01.pdf") @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}") @mock.patch("documents.signals.handlers.os.rename") @@ -514,7 +517,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") self.assertTrue(os.path.isfile(original)) self.assertTrue(os.path.isfile(archive)) @@ -527,7 +530,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") #Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", archive_filename="0000001.pdf", checksum="A", archive_checksum="B") self.assertFalse(os.path.isfile(original)) self.assertTrue(os.path.isfile(archive)) @@ -551,7 +554,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", archive_filename="0000001.pdf", checksum="A", archive_checksum="B") self.assertTrue(os.path.isfile(original)) self.assertTrue(os.path.isfile(archive)) @@ -564,7 +567,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") self.assertTrue(os.path.isfile(original)) self.assertTrue(os.path.isfile(archive)) @@ -578,6 +581,28 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): self.assertFalse(os.path.isfile(doc.source_path)) self.assertFalse(os.path.isfile(doc.archive_path)) + @override_settings(PAPERLESS_FILENAME_FORMAT="{title}") + def test_archive_deleted2(self): + original = os.path.join(settings.ORIGINALS_DIR, "document.png") + original2 = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf") + archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") + Path(original).touch() + Path(original2).touch() + Path(archive).touch() + + doc1 = Document.objects.create(mime_type="image/png", title="document", filename="document.png", checksum="A", archive_checksum="B", archive_filename="0000001.pdf") + doc2 = Document.objects.create(mime_type="application/pdf", title="0000001", filename="0000001.pdf", checksum="C") + + self.assertTrue(os.path.isfile(doc1.source_path)) + self.assertTrue(os.path.isfile(doc1.archive_path)) + self.assertTrue(os.path.isfile(doc2.source_path)) + + doc2.delete() + + self.assertTrue(os.path.isfile(doc1.source_path)) + self.assertTrue(os.path.isfile(doc1.archive_path)) + self.assertFalse(os.path.isfile(doc2.source_path)) + @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}") def test_database_error(self): @@ -585,7 +610,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf") Path(original).touch() Path(archive).touch() - doc = Document(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B") + doc = Document(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_filename="0000001.pdf", archive_checksum="B") with mock.patch("documents.signals.handlers.Document.objects.filter") as m: m.side_effect = DatabaseError() doc.save() @@ -595,6 +620,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase): self.assertTrue(os.path.isfile(doc.source_path)) self.assertTrue(os.path.isfile(doc.archive_path)) + class TestFilenameGeneration(TestCase): @override_settings( @@ -618,7 +644,7 @@ class TestFilenameGeneration(TestCase): def run(): doc = Document.objects.create(checksum=str(uuid.uuid4()), title=str(uuid.uuid4()), content="wow") - doc.filename = generate_unique_filename(doc, settings.ORIGINALS_DIR) + doc.filename = generate_unique_filename(doc) Path(doc.thumbnail_path).touch() with open(doc.source_path, "w") as f: f.write(str(uuid.uuid4())) diff --git a/src/documents/tests/test_management.py b/src/documents/tests/test_management.py index ac119a431..38425a0aa 100644 --- a/src/documents/tests/test_management.py +++ b/src/documents/tests/test_management.py @@ -20,6 +20,7 @@ from documents.tests.utils import DirectoriesMixin sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf") +@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}") class TestArchiver(DirectoriesMixin, TestCase): def make_models(self): @@ -45,6 +46,7 @@ class TestArchiver(DirectoriesMixin, TestCase): self.assertTrue(os.path.isfile(doc.archive_path)) self.assertTrue(os.path.isfile(doc.source_path)) self.assertTrue(filecmp.cmp(sample_file, doc.source_path)) + self.assertEqual(doc.archive_filename, "none/A.pdf") class TestDecryptDocuments(TestCase): diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py index 986e458b9..cdc745305 100644 --- a/src/documents/tests/test_management_exporter.py +++ b/src/documents/tests/test_management_exporter.py @@ -22,7 +22,7 @@ class TestExportImport(DirectoriesMixin, TestCase): self.target = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.target) - self.d1 = Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow1", filename="0000001.pdf", mime_type="application/pdf") + self.d1 = Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow1", filename="0000001.pdf", mime_type="application/pdf", archive_filename="0000001.pdf") self.d2 = Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow2", filename="0000002.pdf", mime_type="application/pdf") self.d3 = Document.objects.create(content="Content", checksum="d38d7ed02e988e072caf924e0f3fcb76", title="wow2", filename="0000003.pdf", mime_type="application/pdf") self.d4 = Document.objects.create(content="Content", checksum="82186aaa94f0b98697d704b90fd1c072", title="wow_dec", filename="0000004.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG) diff --git a/src/documents/tests/test_sanity_check.py b/src/documents/tests/test_sanity_check.py index 0554cd7cd..d7ab1e07f 100644 --- a/src/documents/tests/test_sanity_check.py +++ b/src/documents/tests/test_sanity_check.py @@ -21,7 +21,7 @@ class TestSanityCheck(DirectoriesMixin, TestCase): shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "archive", "0000001.pdf"), os.path.join(self.dirs.archive_dir, "0000001.pdf")) shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png"), os.path.join(self.dirs.thumbnail_dir, "0000001.png")) - return Document.objects.create(title="test", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", content="test", pk=1, filename="0000001.pdf", mime_type="application/pdf") + return Document.objects.create(title="test", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", content="test", pk=1, filename="0000001.pdf", mime_type="application/pdf", archive_filename="0000001.pdf") def test_no_docs(self): self.assertEqual(len(check_sanity()), 0)