mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-19 20:25:21 +00:00
Chore: move misplaced zone OCR tests
This commit is contained in:
1 parent
093c18e68b
commit
044da9a627
4 files changed
+132
-120
No files matched your search
@@ -561,68 +561,6 @@ class TestOcrTemplatePermissions:
|
||||
status.HTTP_403_FORBIDDEN,
|
||||
)
|
||||
|
||||
def test_document_page_image_requires_view_permission_on_the_document(
|
||||
self,
|
||||
regular_user: User,
|
||||
user_client: APIClient,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A user with global view permissions only
|
||||
WHEN:
|
||||
- The user requests a page image of the document
|
||||
THEN:
|
||||
- The request is refused until the user is granted view on the document
|
||||
- Afterwards it passes the permission check and reaches the missing file
|
||||
"""
|
||||
owner = UserFactory()
|
||||
grant_global(regular_user, "view_ocrtemplate", "view_document")
|
||||
document = DocumentFactory(owner=owner)
|
||||
url = f"{ENDPOINT}document-page-image/{document.pk}/0/"
|
||||
|
||||
response = user_client.get(url)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
grant_object(regular_user, document, "view_document")
|
||||
response = user_client.get(url)
|
||||
# Past the permission check; the factory document has no file on disk.
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
def test_test_zone_requires_view_permission_on_the_document(
|
||||
self,
|
||||
regular_user: User,
|
||||
user_client: APIClient,
|
||||
mock_preview: MagicMock,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A user with global add template and view document permissions
|
||||
WHEN:
|
||||
- The user previews a zone on the document
|
||||
THEN:
|
||||
- The request is refused until the user is granted view on the document
|
||||
- Afterwards it passes the permission check and reaches the missing file
|
||||
- No preview extraction runs
|
||||
"""
|
||||
owner = UserFactory()
|
||||
grant_global(regular_user, "add_ocrtemplate", "view_document")
|
||||
document = DocumentFactory(owner=owner)
|
||||
data = {
|
||||
"document": document.pk,
|
||||
"zone": {"x": 0, "y": 0, "width": 100, "height": 100, "page": 1},
|
||||
}
|
||||
|
||||
response = user_client.post(PREVIEW_ZONE_URL, data, format="json")
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
grant_object(regular_user, document, "view_document")
|
||||
response = user_client.post(PREVIEW_ZONE_URL, data, format="json")
|
||||
# Past the permission check; the factory document has no file on disk.
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
mock_preview.assert_not_called()
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"make_data",
|
||||
[
|
||||
@@ -2288,3 +2226,65 @@ class TestOcrTemplatePreviewPermissions:
|
||||
preview_client.post(PREVIEW_ZONE_URL, data, format="json").status_code
|
||||
== 200
|
||||
)
|
||||
|
||||
def test_document_page_image_requires_view_permission_on_the_document(
|
||||
self,
|
||||
regular_user: User,
|
||||
user_client: APIClient,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A user with global view permissions only
|
||||
WHEN:
|
||||
- The user requests a page image of the document
|
||||
THEN:
|
||||
- The request is refused until the user is granted view on the document
|
||||
- Afterwards it passes the permission check and reaches the missing file
|
||||
"""
|
||||
owner = UserFactory()
|
||||
grant_global(regular_user, "view_ocrtemplate", "view_document")
|
||||
document = DocumentFactory(owner=owner)
|
||||
url = f"{ENDPOINT}document-page-image/{document.pk}/0/"
|
||||
|
||||
response = user_client.get(url)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
grant_object(regular_user, document, "view_document")
|
||||
response = user_client.get(url)
|
||||
# Past the permission check; the factory document has no file on disk.
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
def test_test_zone_requires_view_permission_on_the_document(
|
||||
self,
|
||||
regular_user: User,
|
||||
user_client: APIClient,
|
||||
mock_preview: MagicMock,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A user with global add template and view document permissions
|
||||
WHEN:
|
||||
- The user previews a zone on the document
|
||||
THEN:
|
||||
- The request is refused until the user is granted view on the document
|
||||
- Afterwards it passes the permission check and reaches the missing file
|
||||
- No preview extraction runs
|
||||
"""
|
||||
owner = UserFactory()
|
||||
grant_global(regular_user, "add_ocrtemplate", "view_document")
|
||||
document = DocumentFactory(owner=owner)
|
||||
data = {
|
||||
"document": document.pk,
|
||||
"zone": {"x": 0, "y": 0, "width": 100, "height": 100, "page": 1},
|
||||
}
|
||||
|
||||
response = user_client.post(PREVIEW_ZONE_URL, data, format="json")
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
grant_object(regular_user, document, "view_document")
|
||||
response = user_client.post(PREVIEW_ZONE_URL, data, format="json")
|
||||
# Past the permission check; the factory document has no file on disk.
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
mock_preview.assert_not_called()
|
||||
@@ -26,7 +26,6 @@ from documents.models import Tag
|
||||
from documents.pdf_ops import PageSpec
|
||||
from documents.permissions import PermissionsSpec
|
||||
from documents.permissions import set_permissions_for_objects
|
||||
from documents.tasks import extract_zones
|
||||
from paperless_testing.dirs import DirectoriesMixin
|
||||
from paperless_testing.factories import DocumentFactory
|
||||
from paperless_testing.factories import OcrTemplateFactory
|
||||
@@ -1973,63 +1972,6 @@ class TestBulkEditRunZoneOcr:
|
||||
)
|
||||
mock_task.apply_async.assert_not_called()
|
||||
|
||||
def test_task_runs_extraction(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A queued zone OCR task
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Templates are selected for the requester and extraction runs on
|
||||
the stored document
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
select = mocker.patch("documents.tasks.get_templates_for_document")
|
||||
doc = DocumentFactory()
|
||||
user = UserFactory()
|
||||
|
||||
extract_zones(doc.id, requester=user)
|
||||
|
||||
select.assert_called_once_with(doc, requester=user)
|
||||
extract.assert_called_once_with(
|
||||
doc,
|
||||
templates=select.return_value,
|
||||
reindex=True,
|
||||
)
|
||||
|
||||
def test_task_runs_the_chosen_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A queued zone OCR task with a chosen template
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Zone extraction runs with only that template
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
doc = DocumentFactory()
|
||||
template = OcrTemplateFactory()
|
||||
|
||||
extract_zones(doc.id, template.pk)
|
||||
|
||||
extract.assert_called_once_with(doc, templates=[template], reindex=True)
|
||||
|
||||
def test_task_skips_a_deleted_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A template deleted between queueing and running
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Nothing is extracted
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
doc = DocumentFactory()
|
||||
|
||||
extract_zones(doc.id, 999)
|
||||
|
||||
extract.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestBulkEditReprocess:
|
||||
|
||||
@@ -11,6 +11,7 @@ from django.test import TestCase
|
||||
from django.test import override_settings
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
from django.utils import timezone
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from documents import tasks
|
||||
from documents.models import Correspondent
|
||||
@@ -20,10 +21,13 @@ from documents.models import Tag
|
||||
from documents.models import WorkflowAction
|
||||
from documents.sanity_checker import SanityCheckFailedException
|
||||
from documents.sanity_checker import SanityCheckMessages
|
||||
from documents.tasks import extract_zones
|
||||
from documents.tests.helpers import dummy_preprocess
|
||||
from paperless_testing.assertions import FileSystemAssertsMixin
|
||||
from paperless_testing.dirs import DirectoriesMixin
|
||||
from paperless_testing.factories import DocumentFactory
|
||||
from paperless_testing.factories import OcrTemplateFactory
|
||||
from paperless_testing.factories import UserFactory
|
||||
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
||||
|
||||
|
||||
@@ -648,3 +652,65 @@ class TestClearExpiredSessions:
|
||||
|
||||
assert set(Session.objects.values_list("session_key", flat=True)) == {"valid"}
|
||||
assert sum(query["sql"].startswith("DELETE FROM") for query in queries) == 2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestExtractZonesTask:
|
||||
"""Tests for the extract_zones task."""
|
||||
|
||||
def test_task_runs_extraction(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A queued zone OCR task
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Templates are selected for the requester and extraction runs on
|
||||
the stored document
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
select = mocker.patch("documents.tasks.get_templates_for_document")
|
||||
doc = DocumentFactory()
|
||||
user = UserFactory()
|
||||
|
||||
extract_zones(doc.id, requester=user)
|
||||
|
||||
select.assert_called_once_with(doc, requester=user)
|
||||
extract.assert_called_once_with(
|
||||
doc,
|
||||
templates=select.return_value,
|
||||
reindex=True,
|
||||
)
|
||||
|
||||
def test_task_runs_the_chosen_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A queued zone OCR task with a chosen template
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Zone extraction runs with only that template
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
doc = DocumentFactory()
|
||||
template = OcrTemplateFactory()
|
||||
|
||||
extract_zones(doc.id, template.pk)
|
||||
|
||||
extract.assert_called_once_with(doc, templates=[template], reindex=True)
|
||||
|
||||
def test_task_skips_a_deleted_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A template deleted between queueing and running
|
||||
WHEN:
|
||||
- The task runs
|
||||
THEN:
|
||||
- Nothing is extracted
|
||||
"""
|
||||
extract = mocker.patch("documents.tasks.run_zone_extraction")
|
||||
doc = DocumentFactory()
|
||||
|
||||
extract_zones(doc.id, 999)
|
||||
|
||||
extract.assert_not_called()
|
||||
@@ -1456,6 +1456,10 @@ class TestApplyTransform:
|
||||
mocker.patch("documents.zone_ocr.dateparser.parse", side_effect=OverflowError)
|
||||
assert _apply_transform("unparsable", "date") == "unparsable"
|
||||
|
||||
|
||||
class TestPrepareFieldUpdate:
|
||||
"""Tests for converting extracted values into field updates."""
|
||||
|
||||
def test_unknown_target_cannot_produce_a_field_update(self) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
|
||||
Reference in new issue
Block a user