2020-11-25 17:28:49 +01:00
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
2025-05-16 07:23:04 -07:00
|
|
|
from datetime import date
|
2020-11-25 17:28:49 +01:00
|
|
|
from pathlib import Path
|
2016-08-16 19:13:37 +01:00
|
|
|
from unittest import mock
|
|
|
|
|
|
2022-03-11 10:55:51 -08:00
|
|
|
from django.test import TestCase
|
2023-04-20 08:10:17 -07:00
|
|
|
from django.test import override_settings
|
2025-09-09 22:02:16 +02:00
|
|
|
from faker import Faker
|
2016-08-16 19:13:37 +01:00
|
|
|
|
2023-03-28 09:39:30 -07:00
|
|
|
from documents.models import Correspondent
|
|
|
|
|
from documents.models import Document
|
2024-06-17 08:07:08 -07:00
|
|
|
from documents.tasks import empty_trash
|
2016-08-16 19:13:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDocument(TestCase):
|
2020-11-25 17:28:49 +01:00
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.originals_dir = tempfile.mkdtemp()
|
|
|
|
|
self.thumb_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
|
override_settings(
|
|
|
|
|
ORIGINALS_DIR=self.originals_dir,
|
|
|
|
|
THUMBNAIL_DIR=self.thumb_dir,
|
|
|
|
|
).enable()
|
|
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
|
shutil.rmtree(self.originals_dir)
|
|
|
|
|
shutil.rmtree(self.thumb_dir)
|
|
|
|
|
|
2016-08-16 19:13:37 +01:00
|
|
|
def test_file_deletion(self):
|
|
|
|
|
document = Document.objects.create(
|
|
|
|
|
correspondent=Correspondent.objects.create(name="Test0"),
|
|
|
|
|
title="Title",
|
|
|
|
|
content="content",
|
|
|
|
|
checksum="checksum",
|
2022-02-27 15:26:41 +01:00
|
|
|
mime_type="application/pdf",
|
2016-08-16 19:13:37 +01:00
|
|
|
)
|
2020-11-20 13:31:03 +01:00
|
|
|
|
2016-08-16 19:13:37 +01:00
|
|
|
file_path = document.source_path
|
2016-08-20 14:03:42 +01:00
|
|
|
thumb_path = document.thumbnail_path
|
2020-11-20 13:31:03 +01:00
|
|
|
|
2020-11-25 17:28:49 +01:00
|
|
|
Path(file_path).touch()
|
|
|
|
|
Path(thumb_path).touch()
|
|
|
|
|
|
2025-08-06 19:50:42 +02:00
|
|
|
with mock.patch("documents.signals.handlers.Path.unlink") as mock_unlink:
|
2016-08-16 19:13:37 +01:00
|
|
|
document.delete()
|
2024-06-17 08:07:08 -07:00
|
|
|
empty_trash([document.pk])
|
2016-08-20 14:03:42 +01:00
|
|
|
self.assertEqual(mock_unlink.call_count, 2)
|
2020-12-02 18:00:49 +01:00
|
|
|
|
2024-06-17 08:07:08 -07:00
|
|
|
def test_document_soft_delete(self):
|
|
|
|
|
document = Document.objects.create(
|
|
|
|
|
correspondent=Correspondent.objects.create(name="Test0"),
|
|
|
|
|
title="Title",
|
|
|
|
|
content="content",
|
|
|
|
|
checksum="checksum",
|
|
|
|
|
mime_type="application/pdf",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
file_path = document.source_path
|
|
|
|
|
thumb_path = document.thumbnail_path
|
|
|
|
|
|
|
|
|
|
Path(file_path).touch()
|
|
|
|
|
Path(thumb_path).touch()
|
|
|
|
|
|
2025-08-06 19:50:42 +02:00
|
|
|
with mock.patch("documents.signals.handlers.Path.unlink") as mock_unlink:
|
2024-06-17 08:07:08 -07:00
|
|
|
document.delete()
|
|
|
|
|
self.assertEqual(mock_unlink.call_count, 0)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(Document.objects.count(), 0)
|
|
|
|
|
|
|
|
|
|
document.restore(strict=False)
|
|
|
|
|
self.assertEqual(Document.objects.count(), 1)
|
|
|
|
|
|
|
|
|
|
document.delete()
|
|
|
|
|
empty_trash([document.pk])
|
|
|
|
|
self.assertEqual(mock_unlink.call_count, 2)
|
|
|
|
|
|
2020-12-02 18:00:49 +01:00
|
|
|
def test_file_name(self):
|
2022-02-27 15:26:41 +01:00
|
|
|
doc = Document(
|
|
|
|
|
mime_type="application/pdf",
|
|
|
|
|
title="test",
|
2025-05-16 07:23:04 -07:00
|
|
|
created=date(2020, 12, 25),
|
2022-02-27 15:26:41 +01:00
|
|
|
)
|
2020-12-06 19:03:45 +01:00
|
|
|
self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
|
2020-12-02 18:00:49 +01:00
|
|
|
|
|
|
|
|
def test_file_name_jpg(self):
|
2022-02-27 15:26:41 +01:00
|
|
|
doc = Document(
|
|
|
|
|
mime_type="image/jpeg",
|
|
|
|
|
title="test",
|
2025-05-16 07:23:04 -07:00
|
|
|
created=date(2020, 12, 25),
|
2022-02-27 15:26:41 +01:00
|
|
|
)
|
2020-12-06 19:03:45 +01:00
|
|
|
self.assertEqual(doc.get_public_filename(), "2020-12-25 test.jpg")
|
2020-12-02 18:00:49 +01:00
|
|
|
|
|
|
|
|
def test_file_name_unknown(self):
|
2022-02-27 15:26:41 +01:00
|
|
|
doc = Document(
|
|
|
|
|
mime_type="application/zip",
|
|
|
|
|
title="test",
|
2025-05-16 07:23:04 -07:00
|
|
|
created=date(2020, 12, 25),
|
2022-02-27 15:26:41 +01:00
|
|
|
)
|
2020-12-06 19:03:45 +01:00
|
|
|
self.assertEqual(doc.get_public_filename(), "2020-12-25 test.zip")
|
2020-12-02 18:00:49 +01:00
|
|
|
|
2020-12-06 19:03:45 +01:00
|
|
|
def test_file_name_invalid_type(self):
|
2022-02-27 15:26:41 +01:00
|
|
|
doc = Document(
|
|
|
|
|
mime_type="image/jpegasd",
|
|
|
|
|
title="test",
|
2025-05-16 07:23:04 -07:00
|
|
|
created=date(2020, 12, 25),
|
2022-02-27 15:26:41 +01:00
|
|
|
)
|
2020-12-06 19:03:45 +01:00
|
|
|
self.assertEqual(doc.get_public_filename(), "2020-12-25 test")
|
2025-09-09 22:02:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_suggestion_content():
|
|
|
|
|
"""
|
|
|
|
|
Check that the document for suggestion is cropped, only if it exceeds the length limit.
|
|
|
|
|
"""
|
|
|
|
|
fake_text = Faker().text(max_nb_chars=1201000)
|
|
|
|
|
|
|
|
|
|
# Do not crop content under 1.2M chars
|
|
|
|
|
content_under_limit = fake_text[:1200000]
|
|
|
|
|
doc = Document(
|
|
|
|
|
title="test",
|
|
|
|
|
created=date(2025, 6, 1),
|
|
|
|
|
content=content_under_limit,
|
|
|
|
|
)
|
|
|
|
|
assert doc.suggestion_content == content_under_limit
|
|
|
|
|
|
|
|
|
|
# If over the limit, crop to 1M char (800K from the beginning, 200K from the end)
|
|
|
|
|
content_over_limit = fake_text[:1200001]
|
|
|
|
|
expected_cropped_content = (
|
|
|
|
|
content_over_limit[:800000] + " " + content_over_limit[-200000:]
|
|
|
|
|
)
|
|
|
|
|
doc.content = content_over_limit
|
|
|
|
|
assert doc.suggestion_content == expected_cropped_content
|