2022-06-13 11:33:30 -07:00
|
|
|
import textwrap
|
2020-12-22 13:04:08 +01:00
|
|
|
from unittest import mock
|
2018-03-18 15:42:51 +00:00
|
|
|
|
2020-12-22 13:04:08 +01:00
|
|
|
from django.core.checks import Error
|
2024-10-06 12:54:01 -07:00
|
|
|
from django.core.checks import Warning
|
2018-03-18 15:42:51 +00:00
|
|
|
from django.test import TestCase
|
2023-04-20 08:10:17 -07:00
|
|
|
from django.test import override_settings
|
|
|
|
|
|
2022-06-13 11:33:30 -07:00
|
|
|
from documents.checks import changed_password_check
|
2024-10-06 12:54:01 -07:00
|
|
|
from documents.checks import filename_format_check
|
2022-06-13 11:33:30 -07:00
|
|
|
from documents.checks import parser_check
|
|
|
|
|
from documents.models import Document
|
2023-09-23 20:17:01 -07:00
|
|
|
from documents.tests.factories import DocumentFactory
|
2018-03-18 15:42:51 +00:00
|
|
|
|
|
|
|
|
|
2022-06-13 11:33:30 -07:00
|
|
|
class TestDocumentChecks(TestCase):
|
2018-03-18 15:42:51 +00:00
|
|
|
def test_changed_password_check_empty_db(self):
|
2022-06-13 11:33:30 -07:00
|
|
|
self.assertListEqual(changed_password_check(None), [])
|
2018-03-18 15:42:51 +00:00
|
|
|
|
|
|
|
|
def test_changed_password_check_no_encryption(self):
|
|
|
|
|
DocumentFactory.create(storage_type=Document.STORAGE_TYPE_UNENCRYPTED)
|
2022-06-13 11:33:30 -07:00
|
|
|
self.assertListEqual(changed_password_check(None), [])
|
|
|
|
|
|
|
|
|
|
def test_encrypted_missing_passphrase(self):
|
|
|
|
|
DocumentFactory.create(storage_type=Document.STORAGE_TYPE_GPG)
|
|
|
|
|
msgs = changed_password_check(None)
|
|
|
|
|
self.assertEqual(len(msgs), 1)
|
|
|
|
|
msg_text = msgs[0].msg
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg_text,
|
|
|
|
|
"The database contains encrypted documents but no password is set.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
|
PASSPHRASE="test",
|
|
|
|
|
)
|
|
|
|
|
@mock.patch("paperless.db.GnuPG.decrypted")
|
|
|
|
|
@mock.patch("documents.models.Document.source_file")
|
|
|
|
|
def test_encrypted_decrypt_fails(self, mock_decrypted, mock_source_file):
|
|
|
|
|
mock_decrypted.return_value = None
|
|
|
|
|
mock_source_file.return_value = b""
|
|
|
|
|
|
|
|
|
|
DocumentFactory.create(storage_type=Document.STORAGE_TYPE_GPG)
|
|
|
|
|
|
|
|
|
|
msgs = changed_password_check(None)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(len(msgs), 1)
|
|
|
|
|
msg_text = msgs[0].msg
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg_text,
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
|
|
|
|
The current password doesn't match the password of the
|
|
|
|
|
existing documents.
|
|
|
|
|
|
|
|
|
|
If you intend to change your password, you must first export
|
|
|
|
|
all of the old documents, start fresh with the new password
|
|
|
|
|
and then re-import them."
|
|
|
|
|
""",
|
|
|
|
|
),
|
|
|
|
|
)
|
2020-12-22 13:04:08 +01:00
|
|
|
|
|
|
|
|
def test_parser_check(self):
|
|
|
|
|
self.assertEqual(parser_check(None), [])
|
|
|
|
|
|
2022-02-27 15:26:41 +01:00
|
|
|
with mock.patch("documents.checks.document_consumer_declaration.send") as m:
|
2020-12-22 13:04:08 +01:00
|
|
|
m.return_value = []
|
|
|
|
|
|
2022-02-27 15:26:41 +01:00
|
|
|
self.assertEqual(
|
|
|
|
|
parser_check(None),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
|
|
|
|
"No parsers found. This is a bug. The consumer won't be "
|
2022-03-11 10:55:51 -08:00
|
|
|
"able to consume any documents without parsers.",
|
|
|
|
|
),
|
2022-02-27 15:26:41 +01:00
|
|
|
],
|
|
|
|
|
)
|
2024-10-06 12:54:01 -07:00
|
|
|
|
|
|
|
|
def test_filename_format_check(self):
|
|
|
|
|
self.assertEqual(filename_format_check(None), [])
|
|
|
|
|
|
|
|
|
|
with override_settings(FILENAME_FORMAT="{created}/{title}"):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
filename_format_check(None),
|
|
|
|
|
[
|
|
|
|
|
Warning(
|
|
|
|
|
"Filename format {created}/{title} is using the old style, please update to use double curly brackets",
|
|
|
|
|
hint="{{ created }}/{{ title }}",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|