mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	more tests.
This commit is contained in:
		@@ -74,7 +74,7 @@ class Command(BaseCommand):
 | 
			
		||||
                    f"Abort: encrypted file {document.source_path} does not "
 | 
			
		||||
                    f"end with .gpg")
 | 
			
		||||
 | 
			
		||||
            document.filename = os.path.splitext(document.source_path)[0]
 | 
			
		||||
            document.filename = os.path.splitext(document.filename)[0]
 | 
			
		||||
 | 
			
		||||
            with open(document.source_path, "wb") as f:
 | 
			
		||||
                f.write(raw_document)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								src/documents/tests/samples/originals/0000001.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/documents/tests/samples/originals/0000001.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/documents/tests/samples/originals/0000002.pdf.gpg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/documents/tests/samples/originals/0000002.pdf.gpg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/documents/tests/samples/thumb/0000001.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/documents/tests/samples/thumb/0000001.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 7.7 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								src/documents/tests/samples/thumb/0000002.png.gpg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/documents/tests/samples/thumb/0000002.png.gpg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										56
									
								
								src/documents/tests/test_management_decrypt.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/documents/tests/test_management_decrypt.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
import hashlib
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
import shutil
 | 
			
		||||
import tempfile
 | 
			
		||||
from unittest import mock
 | 
			
		||||
 | 
			
		||||
from django.core.management import call_command
 | 
			
		||||
from django.test import TestCase, override_settings
 | 
			
		||||
 | 
			
		||||
from documents.management.commands import document_exporter
 | 
			
		||||
from documents.models import Document, Tag, DocumentType, Correspondent
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestDecryptDocuments(TestCase):
 | 
			
		||||
 | 
			
		||||
    @override_settings(
 | 
			
		||||
        ORIGINALS_DIR=os.path.join(os.path.dirname(__file__), "samples", "originals"),
 | 
			
		||||
        THUMBNAIL_DIR=os.path.join(os.path.dirname(__file__), "samples", "thumb"),
 | 
			
		||||
        PASSPHRASE="test"
 | 
			
		||||
    )
 | 
			
		||||
    @mock.patch("documents.management.commands.decrypt_documents.input")
 | 
			
		||||
    def test_decrypt(self, m):
 | 
			
		||||
 | 
			
		||||
        media_dir = tempfile.mkdtemp()
 | 
			
		||||
        originals_dir = os.path.join(media_dir, "documents", "originals")
 | 
			
		||||
        thumb_dir = os.path.join(media_dir, "documents", "thumbnails")
 | 
			
		||||
        os.makedirs(originals_dir, exist_ok=True)
 | 
			
		||||
        os.makedirs(thumb_dir, exist_ok=True)
 | 
			
		||||
 | 
			
		||||
        override_settings(
 | 
			
		||||
            ORIGINALS_DIR=originals_dir,
 | 
			
		||||
            THUMBNAIL_DIR=thumb_dir,
 | 
			
		||||
            PASSPHRASE="test"
 | 
			
		||||
        ).enable()
 | 
			
		||||
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "originals", "0000002.pdf.gpg"), os.path.join(originals_dir, "0000002.pdf.gpg"))
 | 
			
		||||
        shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "thumb", "0000002.png.gpg"), os.path.join(thumb_dir, "0000002.png.gpg"))
 | 
			
		||||
 | 
			
		||||
        Document.objects.create(checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", id=2, mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
 | 
			
		||||
 | 
			
		||||
        call_command('decrypt_documents')
 | 
			
		||||
 | 
			
		||||
        doc = Document.objects.get(id=2)
 | 
			
		||||
 | 
			
		||||
        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(doc.thumbnail_path))
 | 
			
		||||
 | 
			
		||||
        with doc.source_file as f:
 | 
			
		||||
            checksum = hashlib.md5(f.read()).hexdigest()
 | 
			
		||||
            self.assertEqual(checksum, doc.checksum)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										53
									
								
								src/documents/tests/test_management_exporter.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/documents/tests/test_management_exporter.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
import hashlib
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
import tempfile
 | 
			
		||||
 | 
			
		||||
from django.core.management import call_command
 | 
			
		||||
from django.test import TestCase, override_settings
 | 
			
		||||
 | 
			
		||||
from documents.management.commands import document_exporter
 | 
			
		||||
from documents.models import Document, Tag, DocumentType, Correspondent
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestExporter(TestCase):
 | 
			
		||||
 | 
			
		||||
    @override_settings(
 | 
			
		||||
        ORIGINALS_DIR=os.path.join(os.path.dirname(__file__), "samples", "originals"),
 | 
			
		||||
        THUMBNAIL_DIR=os.path.join(os.path.dirname(__file__), "samples", "thumb"),
 | 
			
		||||
        PASSPHRASE="test"
 | 
			
		||||
    )
 | 
			
		||||
    def test_exporter(self):
 | 
			
		||||
        file = os.path.join(os.path.dirname(__file__), "samples", "originals", "0000001.pdf")
 | 
			
		||||
 | 
			
		||||
        with open(file, "rb") as f:
 | 
			
		||||
            checksum = hashlib.md5(f.read()).hexdigest()
 | 
			
		||||
 | 
			
		||||
        Document.objects.create(checksum=checksum, title="wow", filename="0000001.pdf", id=1, mime_type="application/pdf")
 | 
			
		||||
        Document.objects.create(checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", id=2, 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()
 | 
			
		||||
 | 
			
		||||
        call_command('document_exporter', target)
 | 
			
		||||
 | 
			
		||||
        with open(os.path.join(target, "manifest.json")) as f:
 | 
			
		||||
            manifest = json.load(f)
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(len(manifest), 5)
 | 
			
		||||
 | 
			
		||||
        for element in manifest:
 | 
			
		||||
            if element['model'] == 'documents.document':
 | 
			
		||||
                fname = os.path.join(target, element[document_exporter.EXPORTER_FILE_NAME])
 | 
			
		||||
                self.assertTrue(os.path.exists(fname))
 | 
			
		||||
                self.assertTrue(os.path.exists(os.path.join(target, element[document_exporter.EXPORTER_THUMBNAIL_NAME])))
 | 
			
		||||
 | 
			
		||||
                with open(fname, "rb") as f:
 | 
			
		||||
                    checksum = hashlib.md5(f.read()).hexdigest()
 | 
			
		||||
                self.assertEqual(checksum, element['fields']['checksum'])
 | 
			
		||||
 | 
			
		||||
        Document.objects.create(checksum="AAAAAAAAAAAAAAAAA", title="wow", filename="0000004.pdf", id=3, mime_type="application/pdf")
 | 
			
		||||
 | 
			
		||||
        self.assertRaises(FileNotFoundError, call_command, 'document_exporter', target)
 | 
			
		||||
@@ -3,7 +3,7 @@ exclude = migrations, paperless/settings.py, .tox, */tests/*
 | 
			
		||||
 | 
			
		||||
[tool:pytest]
 | 
			
		||||
DJANGO_SETTINGS_MODULE=paperless.settings
 | 
			
		||||
addopts = --pythonwarnings=all
 | 
			
		||||
addopts = --pythonwarnings=all --cov --cov-report=html
 | 
			
		||||
env =
 | 
			
		||||
  PAPERLESS_SECRET=paperless
 | 
			
		||||
  PAPERLESS_EMAIL_SECRET=paperless
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user