Skip to content

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

Development: improve test portability (#12187)

Jan Kleine0bc032a67d64
diff --git a/src/documents/tests/test_api_uisettings.py b/src/documents/tests/test_api_uisettings.py
index ec973e2f1..811b77286 100644
--- a/src/documents/tests/test_api_uisettings.py
+++ b/src/documents/tests/test_api_uisettings.py
@@ -21,6 +21,16 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase):
self.test_user.save()
self.client.force_authenticate(user=self.test_user)
+ @override_settings(
+ APP_TITLE=None,
+ APP_LOGO=None,
+ AUDIT_LOG_ENABLED=True,
+ EMPTY_TRASH_DELAY=30,
+ ENABLE_UPDATE_CHECK="default",
+ EMAIL_ENABLED=False,
+ GMAIL_OAUTH_ENABLED=False,
+ OUTLOOK_OAUTH_ENABLED=False,
+ )
def test_api_get_ui_settings(self) -> None:
response = self.client.get(self.ENDPOINT, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
diff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.py
index 2d9ac58f0..b1847f2b4 100644
--- a/src/documents/tests/test_barcodes.py
+++ b/src/documents/tests/test_barcodes.py
@@ -919,6 +919,7 @@ class TestTagBarcode(DirectoriesMixin, SampleDirMixin, GetReaderPluginMixin, Tes
@override_settings(
CONSUMER_ENABLE_TAG_BARCODE=True,
CONSUMER_TAG_BARCODE_MAPPING={"ASN(.*)": "\\g<1>"},
+ CONSUMER_ENABLE_ASN_BARCODE=False,
)
def test_scan_file_for_many_custom_tags(self) -> None:
"""
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index 9b6f96ce1..1e35b96bc 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -329,14 +329,14 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
FILENAME_FORMAT="{added_year}-{added_month}-{added_day}",
)
def test_added_year_month_day(self) -> None:
- d1 = timezone.make_aware(datetime.datetime(232, 1, 9, 1, 1, 1))
+ d1 = timezone.make_aware(datetime.datetime(1232, 1, 9, 1, 1, 1))
doc1 = Document.objects.create(
title="doc1",
mime_type="application/pdf",
added=d1,
)
- self.assertEqual(generate_filename(doc1), Path("232-01-09.pdf"))
+ self.assertEqual(generate_filename(doc1), Path("1232-01-09.pdf"))
doc1.added = timezone.make_aware(datetime.datetime(2020, 11, 16, 1, 1, 1))
diff --git a/src/documents/tests/test_management_fuzzy.py b/src/documents/tests/test_management_fuzzy.py
index b3d03ecea..195a3450d 100644
--- a/src/documents/tests/test_management_fuzzy.py
+++ b/src/documents/tests/test_management_fuzzy.py
@@ -140,7 +140,7 @@ class TestFuzzyMatchCommand(TestCase):
mime_type="application/pdf",
filename="final_test.pdf",
)
- stdout, _ = self.call_command("--no-progress-bar")
+ stdout, _ = self.call_command("--no-progress-bar", "--processes", "1")
lines = [x.strip() for x in stdout.splitlines() if x.strip()]
self.assertEqual(len(lines), 3)
for line in lines:
@@ -183,7 +183,12 @@ class TestFuzzyMatchCommand(TestCase):
self.assertEqual(Document.objects.count(), 3)
- stdout, _ = self.call_command("--delete", "--no-progress-bar")
+ stdout, _ = self.call_command(
+ "--delete",
+ "--no-progress-bar",
+ "--processes",
+ "1",
+ )
self.assertIn(
"The command is configured to delete documents. Use with caution",
diff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.py
index f099cd92e..346d895aa 100644
--- a/src/documents/tests/utils.py
+++ b/src/documents/tests/utils.py
@@ -33,11 +33,11 @@ from documents.plugins.helpers import ProgressStatusOptions
def setup_directories():
dirs = namedtuple("Dirs", ())
- dirs.data_dir = Path(tempfile.mkdtemp())
- dirs.scratch_dir = Path(tempfile.mkdtemp())
- dirs.media_dir = Path(tempfile.mkdtemp())
- dirs.consumption_dir = Path(tempfile.mkdtemp())
- dirs.static_dir = Path(tempfile.mkdtemp())
+ dirs.data_dir = Path(tempfile.mkdtemp()).resolve()
+ dirs.scratch_dir = Path(tempfile.mkdtemp()).resolve()
+ dirs.media_dir = Path(tempfile.mkdtemp()).resolve()
+ dirs.consumption_dir = Path(tempfile.mkdtemp()).resolve()
+ dirs.static_dir = Path(tempfile.mkdtemp()).resolve()
dirs.index_dir = dirs.data_dir / "index"
dirs.originals_dir = dirs.media_dir / "documents" / "originals"
dirs.thumbnail_dir = dirs.media_dir / "documents" / "thumbnails"
diff --git a/src/paperless/tests/test_adapter.py b/src/paperless/tests/test_adapter.py
index 767749dd2..fcbd21b12 100644
--- a/src/paperless/tests/test_adapter.py
+++ b/src/paperless/tests/test_adapter.py
@@ -78,11 +78,15 @@ class TestCustomAccountAdapter(TestCase):
adapter = get_adapter()
# Test when PAPERLESS_URL is None
- expected_url = f"https://foo.org{reverse('account_reset_password_from_key', kwargs={'uidb36': 'UID', 'key': 'KEY'})}"
- self.assertEqual(
- adapter.get_reset_password_from_key_url("UID-KEY"),
- expected_url,
- )
+ with override_settings(
+ PAPERLESS_URL=None,
+ ACCOUNT_DEFAULT_HTTP_PROTOCOL="https",
+ ):
+ expected_url = f"https://foo.org{reverse('account_reset_password_from_key', kwargs={'uidb36': 'UID', 'key': 'KEY'})}"
+ self.assertEqual(
+ adapter.get_reset_password_from_key_url("UID-KEY"),
+ expected_url,
+ )
# Test when PAPERLESS_URL is not None
with override_settings(PAPERLESS_URL="https://bar.com"):
diff --git a/src/paperless/tests/test_views.py b/src/paperless/tests/test_views.py
index 76bf694fc..ab8941538 100644
--- a/src/paperless/tests/test_views.py
+++ b/src/paperless/tests/test_views.py
@@ -1,7 +1,7 @@
import tempfile
from pathlib import Path
-from django.conf import settings
+from django.test import override_settings
def test_favicon_view(client):
@@ -11,15 +11,14 @@ def test_favicon_view(client):
favicon_path.parent.mkdir(parents=True, exist_ok=True)
favicon_path.write_bytes(b"FAKE ICON DATA")
- settings.STATIC_ROOT = static_dir
-
- response = client.get("/favicon.ico")
- assert response.status_code == 200
- assert response["Content-Type"] == "image/x-icon"
- assert b"".join(response.streaming_content) == b"FAKE ICON DATA"
+ with override_settings(STATIC_ROOT=static_dir):
+ response = client.get("/favicon.ico")
+ assert response.status_code == 200
+ assert response["Content-Type"] == "image/x-icon"
+ assert b"".join(response.streaming_content) == b"FAKE ICON DATA"
def test_favicon_view_missing_file(client):
- settings.STATIC_ROOT = Path(tempfile.mkdtemp())
- response = client.get("/favicon.ico")
- assert response.status_code == 404
+ with override_settings(STATIC_ROOT=Path(tempfile.mkdtemp())):
+ response = client.get("/favicon.ico")
+ assert response.status_code == 404
diff --git a/src/paperless_mail/parsers.py b/src/paperless_mail/parsers.py
index 22835de89..86cba23ab 100644
--- a/src/paperless_mail/parsers.py
+++ b/src/paperless_mail/parsers.py
@@ -5,6 +5,7 @@ from pathlib import Path
from bleach import clean
from bleach import linkify
from django.conf import settings
+from django.utils import timezone
from django.utils.timezone import is_naive
from django.utils.timezone import make_aware
from gotenberg_client import GotenbergClient
@@ -332,7 +333,9 @@ class MailDocumentParser(DocumentParser):
if data["attachments"]:
data["attachments_label"] = "Attachments"
- data["date"] = clean_html(mail.date.astimezone().strftime("%Y-%m-%d %H:%M"))
+ data["date"] = clean_html(
+ timezone.localtime(mail.date).strftime("%Y-%m-%d %H:%M"),
+ )
data["content"] = clean_html(mail.text.strip())
from django.template.loader import render_to_string
diff --git a/src/paperless_mail/tests/test_parsers.py b/src/paperless_mail/tests/test_parsers.py
index 061771d47..9746731f8 100644
--- a/src/paperless_mail/tests/test_parsers.py
+++ b/src/paperless_mail/tests/test_parsers.py
@@ -6,6 +6,7 @@ from unittest import mock
import httpx
import pytest
from django.test.html import parse_html
+from django.utils import timezone
from pytest_django.fixtures import SettingsWrapper
from pytest_httpx import HTTPXMock
from pytest_mock import MockerFixture
@@ -634,13 +635,14 @@ class TestParser:
THEN:
- Resulting HTML is as expected
"""
- mail = mail_parser.parse_file_to_message(html_email_file)
- html_file = mail_parser.mail_to_html(mail)
+ with timezone.override("UTC"):
+ mail = mail_parser.parse_file_to_message(html_email_file)
+ html_file = mail_parser.mail_to_html(mail)
- expected_html = parse_html(html_email_html_file.read_text())
- actual_html = parse_html(html_file.read_text())
+ expected_html = parse_html(html_email_html_file.read_text())
+ actual_html = parse_html(html_file.read_text())
- assert expected_html == actual_html
+ assert expected_html == actual_html
def test_generate_pdf_from_mail(
self,
diff --git a/src/paperless_tesseract/tests/test_parser.py b/src/paperless_tesseract/tests/test_parser.py
index ce8ef4c9e..e9c05b03e 100644
--- a/src/paperless_tesseract/tests/test_parser.py
+++ b/src/paperless_tesseract/tests/test_parser.py
@@ -1,5 +1,6 @@
import shutil
import tempfile
+import unicodedata
import uuid
from pathlib import Path
from unittest import mock
@@ -847,8 +848,18 @@ class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
"application/pdf",
)
- # Copied from the PDF to here. Don't even look at it
- self.assertIn("ةﯾﻠﺧﺎدﻻ ةرازو", parser.get_text())
+ # OCR output for RTL text varies across platforms/versions due to
+ # bidi controls and presentation forms; normalize before assertion.
+ normalized_text = "".join(
+ char
+ for char in unicodedata.normalize("NFKC", parser.get_text())
+ if unicodedata.category(char) != "Cf" and not char.isspace()
+ )
+
+ self.assertIn("ةرازو", normalized_text)
+ self.assertTrue(
+ any(token in normalized_text for token in ("ةیلخادلا", "الاخليد")),
+ )
@mock.patch("ocrmypdf.ocr")
def test_gs_rendering_error(self, m) -> None: