mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	fixed test case. fixed bug with the decryption logic.
This commit is contained in:
		@@ -82,7 +82,8 @@ class Command(BaseCommand):
 | 
			
		||||
            with open(document.thumbnail_path, "wb") as f:
 | 
			
		||||
                f.write(raw_thumb)
 | 
			
		||||
 | 
			
		||||
            document.save(update_fields=("storage_type", "filename"))
 | 
			
		||||
            Document.objects.filter(id=document.id).update(
 | 
			
		||||
                storage_type=document.storage_type, filename=document.filename)
 | 
			
		||||
 | 
			
		||||
            for path in old_paths:
 | 
			
		||||
                os.unlink(path)
 | 
			
		||||
 
 | 
			
		||||
@@ -16,25 +16,23 @@ sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
 | 
			
		||||
class TestArchiver(DirectoriesMixin, TestCase):
 | 
			
		||||
 | 
			
		||||
    def make_models(self):
 | 
			
		||||
        self.d1 = Document.objects.create(checksum="A", title="A", content="first document", pk=1, mime_type="application/pdf")
 | 
			
		||||
        #self.d2 = Document.objects.create(checksum="B", title="B", content="second document")
 | 
			
		||||
        #self.d3 = Document.objects.create(checksum="C", title="C", content="unrelated document")
 | 
			
		||||
        return Document.objects.create(checksum="A", title="A", content="first document", mime_type="application/pdf")
 | 
			
		||||
 | 
			
		||||
    def test_archiver(self):
 | 
			
		||||
 | 
			
		||||
        shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, "0000001.pdf"))
 | 
			
		||||
        self.make_models()
 | 
			
		||||
        doc = self.make_models()
 | 
			
		||||
        shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"))
 | 
			
		||||
 | 
			
		||||
        call_command('document_archiver')
 | 
			
		||||
 | 
			
		||||
    def test_handle_document(self):
 | 
			
		||||
 | 
			
		||||
        shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, "0000001.pdf"))
 | 
			
		||||
        self.make_models()
 | 
			
		||||
        doc = self.make_models()
 | 
			
		||||
        shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"))
 | 
			
		||||
 | 
			
		||||
        handle_document(self.d1.pk)
 | 
			
		||||
        handle_document(doc.pk)
 | 
			
		||||
 | 
			
		||||
        doc = Document.objects.get(id=self.d1.id)
 | 
			
		||||
        doc = Document.objects.get(id=doc.id)
 | 
			
		||||
 | 
			
		||||
        self.assertIsNotNone(doc.checksum)
 | 
			
		||||
        self.assertTrue(os.path.isfile(doc.archive_path))
 | 
			
		||||
 
 | 
			
		||||
@@ -35,20 +35,20 @@ class TestDecryptDocuments(TestCase):
 | 
			
		||||
            PASSPHRASE="test"
 | 
			
		||||
        ).enable()
 | 
			
		||||
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf.gpg"), os.path.join(originals_dir, "0000002.pdf.gpg"))
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000002.png.gpg"), os.path.join(thumb_dir, "0000002.png.gpg"))
 | 
			
		||||
        doc = Document.objects.create(checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg",  mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
 | 
			
		||||
 | 
			
		||||
        Document.objects.create(checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", id=2, mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf.gpg"), os.path.join(originals_dir, "0000002.pdf.gpg"))
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", f"0000002.png.gpg"), os.path.join(thumb_dir, f"{doc.id:07}.png.gpg"))
 | 
			
		||||
 | 
			
		||||
        call_command('decrypt_documents')
 | 
			
		||||
 | 
			
		||||
        doc = Document.objects.get(id=2)
 | 
			
		||||
        doc.refresh_from_db()
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(doc.storage_type, Document.STORAGE_TYPE_UNENCRYPTED)
 | 
			
		||||
        self.assertEqual(doc.filename, "0000002.pdf")
 | 
			
		||||
        self.assertTrue(os.path.isfile(os.path.join(originals_dir, "0000002.pdf")))
 | 
			
		||||
        self.assertTrue(os.path.isfile(doc.source_path))
 | 
			
		||||
        self.assertTrue(os.path.isfile(os.path.join(thumb_dir, "0000002.png")))
 | 
			
		||||
        self.assertTrue(os.path.isfile(os.path.join(thumb_dir, f"{doc.id:07}.png")))
 | 
			
		||||
        self.assertTrue(os.path.isfile(doc.thumbnail_path))
 | 
			
		||||
 | 
			
		||||
        with doc.source_file as f:
 | 
			
		||||
 
 | 
			
		||||
@@ -24,13 +24,14 @@ class TestExportImport(DirectoriesMixin, TestCase):
 | 
			
		||||
 | 
			
		||||
        file = os.path.join(self.dirs.originals_dir, "0000001.pdf")
 | 
			
		||||
 | 
			
		||||
        Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", id=1, mime_type="application/pdf")
 | 
			
		||||
        Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", id=2, mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
 | 
			
		||||
        Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", mime_type="application/pdf")
 | 
			
		||||
        Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
 | 
			
		||||
        Tag.objects.create(name="t")
 | 
			
		||||
        DocumentType.objects.create(name="dt")
 | 
			
		||||
        Correspondent.objects.create(name="c")
 | 
			
		||||
 | 
			
		||||
        target = tempfile.mkdtemp()
 | 
			
		||||
        self.addCleanup(shutil.rmtree, target)
 | 
			
		||||
 | 
			
		||||
        call_command('document_exporter', target)
 | 
			
		||||
 | 
			
		||||
@@ -66,9 +67,6 @@ class TestExportImport(DirectoriesMixin, TestCase):
 | 
			
		||||
    def test_export_missing_files(self):
 | 
			
		||||
 | 
			
		||||
        target = tempfile.mkdtemp()
 | 
			
		||||
        Document.objects.create(checksum="AAAAAAAAAAAAAAAAA", title="wow", filename="0000004.pdf", id=3, mime_type="application/pdf")
 | 
			
		||||
        self.addCleanup(shutil.rmtree, target)
 | 
			
		||||
        Document.objects.create(checksum="AAAAAAAAAAAAAAAAA", title="wow", filename="0000004.pdf", mime_type="application/pdf")
 | 
			
		||||
        self.assertRaises(FileNotFoundError, call_command, 'document_exporter', target)
 | 
			
		||||
 | 
			
		||||
    def test_duplicate_titles(self):
 | 
			
		||||
        # TODO
 | 
			
		||||
        pass
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user