Chore: use shared user and client fixtures in zone OCR tests

This commit is contained in:
stumpylog committed 2026-11-18 12:45:11 -08:00
1 parent e03faab730
commit 1c6742723f
3 files changed
+128 -133

No files matched your search

+20 -29
View File
@@ -2876,15 +2876,6 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
@pytest.mark.django_db
@pytest.mark.usefixtures("paperless_dirs")
class TestRunZoneOcrBulkEditAPI:
@pytest.fixture
def superuser(self) -> User:
return UserFactory(username="temp_admin", superuser=True)
@pytest.fixture
def api_client(self, rest_api_client: APIClient, superuser: User) -> APIClient:
rest_api_client.force_authenticate(user=superuser)
return rest_api_client
@pytest.fixture
def run_zone_ocr_mock(self, mocker: MockerFixture) -> mock.MagicMock:
"""
@@ -2907,14 +2898,14 @@ class TestRunZoneOcrBulkEditAPI:
return method
@pytest.fixture
def doc1(self) -> Document:
def document(self) -> Document:
return DocumentFactory()
def test_run_zone_ocr_documents_endpoint(
self,
api_client: APIClient,
superuser: User,
doc1: Document,
admin_client: APIClient,
admin_user: User,
document: Document,
run_zone_ocr_mock: mock.MagicMock,
) -> None:
"""
@@ -2925,22 +2916,22 @@ class TestRunZoneOcrBulkEditAPI:
THEN:
- run_zone_ocr is called with the document
"""
response = api_client.post(
response = admin_client.post(
"/api/documents/run_zone_ocr/",
json.dumps({"documents": [doc1.id]}),
json.dumps({"documents": [document.id]}),
content_type="application/json",
)
assert response.status_code == status.HTTP_200_OK
run_zone_ocr_mock.assert_called_once()
args, kwargs = run_zone_ocr_mock.call_args
assert args[0] == [doc1.id]
assert kwargs == {"user": superuser}
assert args[0] == [document.id]
assert kwargs == {"user": admin_user}
def test_run_zone_ocr_documents_endpoint_with_template(
self,
api_client: APIClient,
superuser: User,
doc1: Document,
admin_client: APIClient,
admin_user: User,
document: Document,
run_zone_ocr_mock: mock.MagicMock,
) -> None:
"""
@@ -2952,21 +2943,21 @@ class TestRunZoneOcrBulkEditAPI:
- run_zone_ocr is called with the template
"""
template = OcrTemplateFactory()
response = api_client.post(
response = admin_client.post(
"/api/documents/run_zone_ocr/",
json.dumps({"documents": [doc1.id], "template": template.pk}),
json.dumps({"documents": [document.id], "template": template.pk}),
content_type="application/json",
)
assert response.status_code == status.HTTP_200_OK
run_zone_ocr_mock.assert_called_once()
args, kwargs = run_zone_ocr_mock.call_args
assert args[0] == [doc1.id]
assert kwargs == {"template": template.pk, "user": superuser}
assert args[0] == [document.id]
assert kwargs == {"template": template.pk, "user": admin_user}
def test_run_zone_ocr_documents_endpoint_rejects_private_template(
self,
api_client: APIClient,
doc1: Document,
rest_api_client: APIClient,
document: Document,
run_zone_ocr_mock: mock.MagicMock,
) -> None:
"""
@@ -2983,11 +2974,11 @@ class TestRunZoneOcrBulkEditAPI:
name="Private",
owner=UserFactory(username="other"),
)
api_client.force_authenticate(user=user)
rest_api_client.force_authenticate(user=user)
response = api_client.post(
response = rest_api_client.post(
"/api/documents/run_zone_ocr/",
json.dumps({"documents": [doc1.id], "template": template.pk}),
json.dumps({"documents": [document.id], "template": template.pk}),
content_type="application/json",
)
+66 -73
View File
@@ -480,7 +480,8 @@ class TestOcrTemplateCreate:
class TestOcrTemplatePermissions:
def test_list_only_shows_permitted_templates(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
) -> None:
"""
GIVEN:
@@ -492,16 +493,18 @@ class TestOcrTemplatePermissions:
- Only the unowned, owned and shared templates are returned
"""
other = UserFactory()
user = UserFactory()
grant_global(user, "view_ocrtemplate")
grant_global(regular_user, "view_ocrtemplate")
title_template("Unowned")
title_template("Mine", owner=user)
title_template("Mine", owner=regular_user)
title_template("Private", owner=other)
grant_object(user, title_template("Shared", owner=other), "view_ocrtemplate")
rest_api_client.force_authenticate(user=user)
grant_object(
regular_user,
title_template("Shared", owner=other),
"view_ocrtemplate",
)
response = rest_api_client.get(ENDPOINT)
response = user_client.get(ENDPOINT)
assert response.status_code == status.HTTP_200_OK
assert sorted(t["name"] for t in response.json()["results"]) == [
@@ -512,7 +515,8 @@ class TestOcrTemplatePermissions:
def test_cannot_change_another_users_template(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
) -> None:
"""
GIVEN:
@@ -523,22 +527,20 @@ class TestOcrTemplatePermissions:
- The update is refused until the user is granted change on it
"""
other = UserFactory()
user = UserFactory()
grant_global(user, "view_ocrtemplate", "change_ocrtemplate")
grant_global(regular_user, "view_ocrtemplate", "change_ocrtemplate")
template = OcrTemplateFactory(
name="Private",
match="Northwind",
owner=other,
)
grant_object(user, template, "view_ocrtemplate")
rest_api_client.force_authenticate(user=user)
grant_object(regular_user, template, "view_ocrtemplate")
url = f"{ENDPOINT}{template.pk}/"
response = rest_api_client.patch(url, {"name": "Renamed"}, format="json")
response = user_client.patch(url, {"name": "Renamed"}, format="json")
assert response.status_code == status.HTTP_403_FORBIDDEN
grant_object(user, template, "change_ocrtemplate")
response = rest_api_client.patch(url, {"name": "Renamed"}, format="json")
grant_object(regular_user, template, "change_ocrtemplate")
response = user_client.patch(url, {"name": "Renamed"}, format="json")
assert response.status_code == status.HTTP_200_OK
template.refresh_from_db()
assert template.name == "Renamed"
@@ -561,7 +563,8 @@ class TestOcrTemplatePermissions:
def test_document_page_image_requires_view_permission_on_the_document(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
) -> None:
"""
GIVEN:
@@ -574,23 +577,22 @@ class TestOcrTemplatePermissions:
- Afterwards it passes the permission check and reaches the missing file
"""
owner = UserFactory()
user = UserFactory()
grant_global(user, "view_ocrtemplate", "view_document")
grant_global(regular_user, "view_ocrtemplate", "view_document")
document = DocumentFactory(owner=owner)
rest_api_client.force_authenticate(user=user)
url = f"{ENDPOINT}document-page-image/{document.pk}/0/"
response = rest_api_client.get(url)
response = user_client.get(url)
assert response.status_code == status.HTTP_403_FORBIDDEN
grant_object(user, document, "view_document")
response = rest_api_client.get(url)
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,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_preview: MagicMock,
) -> None:
"""
@@ -605,20 +607,18 @@ class TestOcrTemplatePermissions:
- No preview extraction runs
"""
owner = UserFactory()
user = UserFactory()
grant_global(user, "add_ocrtemplate", "view_document")
grant_global(regular_user, "add_ocrtemplate", "view_document")
document = DocumentFactory(owner=owner)
rest_api_client.force_authenticate(user=user)
data = {
"document": document.pk,
"zone": {"x": 0, "y": 0, "width": 100, "height": 100, "page": 1},
}
response = rest_api_client.post(PREVIEW_ZONE_URL, data, format="json")
response = user_client.post(PREVIEW_ZONE_URL, data, format="json")
assert response.status_code == status.HTTP_403_FORBIDDEN
grant_object(user, document, "view_document")
response = rest_api_client.post(PREVIEW_ZONE_URL, data, format="json")
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()
@@ -635,7 +635,8 @@ class TestOcrTemplatePermissions:
)
def test_shared_editor_cannot_transfer_ownership_or_change_sharing(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
admin_user: User,
make_data: Callable[[User], dict[str, Any]],
) -> None:
@@ -647,8 +648,7 @@ class TestOcrTemplatePermissions:
THEN:
- The request is refused and the owner is unchanged
"""
editor = UserFactory()
grant_global(editor, "view_ocrtemplate", "change_ocrtemplate")
grant_global(regular_user, "view_ocrtemplate", "change_ocrtemplate")
template = OcrTemplateFactory(
name="Shared",
owner=admin_user,
@@ -656,12 +656,11 @@ class TestOcrTemplatePermissions:
source_width=100,
source_height=100,
)
grant_object(editor, template, "view_ocrtemplate", "change_ocrtemplate")
rest_api_client.force_authenticate(editor)
grant_object(regular_user, template, "view_ocrtemplate", "change_ocrtemplate")
response = rest_api_client.patch(
response = user_client.patch(
f"{ENDPOINT}{template.pk}/",
make_data(editor),
make_data(regular_user),
format="json",
)
@@ -1660,7 +1659,8 @@ class TestRunZoneOcr:
class TestRunZoneOcrPermissions:
def test_rejects_a_template_the_user_cannot_view(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_extract: MagicMock,
) -> None:
"""
@@ -1672,16 +1672,14 @@ class TestRunZoneOcrPermissions:
THEN:
- 403 is returned and nothing is extracted
"""
user = UserFactory()
grant_global(user, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=user)
grant_global(regular_user, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=regular_user)
template = OcrTemplateFactory(
name="Private",
owner=UserFactory(),
)
rest_api_client.force_authenticate(user=user)
response = rest_api_client.post(
response = user_client.post(
RUN_ZONE_OCR_URL.format(pk=document.pk),
{"template": template.pk},
format="json",
@@ -1692,7 +1690,8 @@ class TestRunZoneOcrPermissions:
def test_requires_change_permission_on_the_document(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_extract: MagicMock,
) -> None:
"""
@@ -1705,19 +1704,18 @@ class TestRunZoneOcrPermissions:
- 403 is returned and nothing is extracted
"""
owner = UserFactory()
user = UserFactory()
grant_global(user, "view_document", "change_document")
grant_global(regular_user, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=owner)
rest_api_client.force_authenticate(user=user)
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
response = user_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_403_FORBIDDEN
mock_extract.assert_not_called()
def test_requires_global_change_permission(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_extract: MagicMock,
) -> None:
"""
@@ -1729,19 +1727,18 @@ class TestRunZoneOcrPermissions:
THEN:
- 403 is returned and nothing is extracted
"""
user = UserFactory()
grant_global(user, "view_document", "add_document")
document = DocumentFactory(content=CONTENT, owner=user)
rest_api_client.force_authenticate(user=user)
grant_global(regular_user, "view_document", "add_document")
document = DocumentFactory(content=CONTENT, owner=regular_user)
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
response = user_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_403_FORBIDDEN
mock_extract.assert_not_called()
def test_does_not_require_document_upload_permission(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_extract: MagicMock,
) -> None:
"""
@@ -1753,20 +1750,18 @@ class TestRunZoneOcrPermissions:
THEN:
- The request succeeds and extraction runs
"""
user = UserFactory()
grant_global(user, "change_document")
document = DocumentFactory(owner=user, content=CONTENT)
grant_global(regular_user, "change_document")
document = DocumentFactory(owner=regular_user, content=CONTENT)
document.source_path.write_bytes(b"test PDF")
OcrTemplateFactory(
name="Matching",
owner=user,
owner=regular_user,
source_width=100,
source_height=100,
match="Northwind",
)
rest_api_client.force_authenticate(user)
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
response = user_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == 200
mock_extract.assert_called_once()
@@ -1849,7 +1844,8 @@ class TestRunZoneOcrPermissions:
def test_hides_the_owners_private_templates_from_an_editor(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
@@ -1862,10 +1858,9 @@ class TestRunZoneOcrPermissions:
- Only templates the editor can also view run and are reported
"""
owner = UserFactory()
editor = UserFactory()
grant_global(editor, "view_document", "change_document")
grant_global(regular_user, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=owner)
grant_object(editor, document, "view_document", "change_document")
grant_object(regular_user, document, "view_document", "change_document")
write_blank_png(document.source_path)
mock_zone_ocr_run.return_value.stdout = "extracted"
for name, template_owner in [
@@ -1880,10 +1875,9 @@ class TestRunZoneOcrPermissions:
source_height=100,
)
if name == "Shared":
grant_object(editor, template, "view_ocrtemplate")
rest_api_client.force_authenticate(editor)
grant_object(regular_user, template, "view_ocrtemplate")
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
response = user_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_200_OK
assert sorted(result["template"] for result in response.json()["results"]) == [
@@ -2264,7 +2258,8 @@ class TestOcrTemplatePreviewPermissions:
)
def test_previews_require_global_document_view_permission(
self,
rest_api_client: APIClient,
regular_user: User,
user_client: APIClient,
mock_preview: MagicMock,
action: str,
) -> None:
@@ -2278,17 +2273,15 @@ class TestOcrTemplatePreviewPermissions:
THEN:
- 403 is returned and no extraction runs
"""
user = UserFactory()
grant_global(user, "view_ocrtemplate", "add_ocrtemplate")
rest_api_client.force_authenticate(user)
grant_global(regular_user, "view_ocrtemplate", "add_ocrtemplate")
document = DocumentFactory(owner=None, mime_type="image/png")
document.source_path.write_bytes(b"document contents")
mock_preview.return_value = {"raw_text": "document contents", "value": None}
if action == "document-page-image":
response = rest_api_client.get(preview_url(document))
response = user_client.get(preview_url(document))
else:
response = rest_api_client.post(
response = user_client.post(
PREVIEW_ZONE_URL,
{"document": document.pk, "zone": {"width": 100, "height": 50}},
format="json",
+42 -31
View File
@@ -20,6 +20,7 @@ from documents.signals.handlers import before_task_publish_handler
from documents.signals.handlers import task_failure_handler
from documents.signals.handlers import task_postrun_handler
from documents.signals.handlers import task_revoked_handler
from paperless_testing.factories import DocumentFactory
from paperless_testing.factories import OcrTemplateFactory
from paperless_testing.factories import UserFactory
from paperless_testing.permissions import grant_global
@@ -68,13 +69,20 @@ def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
@pytest.mark.django_db
@pytest.mark.usefixtures("paperless_dirs")
class TestRunZoneOcrBulkOperation:
"""Zone OCR runs as a tracked bulk operation."""
@pytest.fixture
def document(self) -> Document:
return DocumentFactory()
def test_queues_only_template_id_and_returns_tracked_task(
self,
mocker: MockerFixture,
bulk_setup: tuple[User, APIClient, Document, Correspondent],
admin_client: APIClient,
admin_user: User,
document: Document,
) -> None:
"""
GIVEN:
@@ -85,15 +93,14 @@ class TestRunZoneOcrBulkOperation:
- Only the template id is queued, with a tracked task id returned
- Extraction does not run in the request
"""
user, client, doc, _ = bulk_setup
template = OcrTemplateFactory()
enqueue = mocker.patch("documents.views.execute_bulk_operation.apply_async")
extract = mocker.patch("documents.bulk_edit.extract_zones")
enqueue.return_value.id = "zone-ocr-task"
response = client.post(
response = admin_client.post(
"/api/documents/run_zone_ocr/",
{
"documents": [doc.pk],
"documents": [document.pk],
"template": template.pk,
"from_webui": True,
},
@@ -104,9 +111,9 @@ class TestRunZoneOcrBulkOperation:
kwargs = enqueue.call_args.kwargs["kwargs"]
assert json.loads(json.dumps(kwargs)) == {
"operation": "run_zone_ocr",
"document_ids": [doc.pk],
"document_ids": [document.pk],
"parameters": {"template": template.pk},
"user_id": user.pk,
"user_id": admin_user.pk,
"trigger_source": "web_ui",
}
extract.assert_not_called()
@@ -114,7 +121,8 @@ class TestRunZoneOcrBulkOperation:
def test_finishes_extraction_within_the_tracked_task(
self,
mocker: MockerFixture,
bulk_setup: tuple[User, APIClient, Document, Correspondent],
admin_user: User,
document: Document,
) -> None:
"""
GIVEN:
@@ -126,19 +134,18 @@ class TestRunZoneOcrBulkOperation:
- No further extraction task is enqueued
- The result reports the operation and document count
"""
user, _, doc, _ = bulk_setup
template = OcrTemplateFactory()
extract = mocker.patch("documents.bulk_edit.extract_zones")
result = execute_bulk_operation(
"run_zone_ocr",
[doc.pk],
[document.pk],
{"template": template.pk},
user.pk,
admin_user.pk,
)
extract.assert_called_once_with(
document_id=doc.pk,
document_id=document.pk,
template_id=template.pk,
requester=user,
requester=admin_user,
)
extract.apply_async.assert_not_called()
assert result == {"operation": "run_zone_ocr", "document_count": 1}
@@ -146,7 +153,8 @@ class TestRunZoneOcrBulkOperation:
def test_without_template_selects_for_the_requester(
self,
mocker: MockerFixture,
bulk_setup: tuple[User, APIClient, Document, Correspondent],
admin_user: User,
document: Document,
) -> None:
"""
GIVEN:
@@ -157,13 +165,12 @@ class TestRunZoneOcrBulkOperation:
- Extraction receives the requesting user so templates are limited to
those they can view
"""
user, _, doc, _ = bulk_setup
extract = mocker.patch("documents.bulk_edit.extract_zones")
execute_bulk_operation("run_zone_ocr", [doc.pk], {}, user.pk)
execute_bulk_operation("run_zone_ocr", [document.pk], {}, admin_user.pk)
extract.assert_called_once_with(
document_id=doc.pk,
document_id=document.pk,
template_id=None,
requester=user,
requester=admin_user,
)
@pytest.mark.parametrize(
@@ -177,7 +184,9 @@ class TestRunZoneOcrBulkOperation:
def test_rechecks_access_before_extraction(
self,
mocker: MockerFixture,
bulk_setup: tuple[User, APIClient, Document, Correspondent],
user_client: APIClient,
regular_user: User,
document: Document,
revoked: str,
) -> None:
"""
@@ -191,30 +200,27 @@ class TestRunZoneOcrBulkOperation:
- Execution raises PermissionDenied
- Extraction never runs
"""
_, client, doc, _ = bulk_setup
user = UserFactory()
grant_global(user, "change_document")
template = OcrTemplateFactory(owner=user)
client.force_authenticate(user)
grant_global(regular_user, "change_document")
template = OcrTemplateFactory(owner=regular_user)
enqueue = mocker.patch("documents.views.execute_bulk_operation.apply_async")
enqueue.return_value.id = "zone-ocr-task"
response = client.post(
response = user_client.post(
"/api/documents/run_zone_ocr/",
{
"documents": [doc.pk],
"documents": [document.pk],
"template": template.pk,
},
format="json",
)
assert response.status_code == 200
if revoked == "document":
doc.owner = UserFactory()
doc.save(update_fields=["owner"])
document.owner = UserFactory()
document.save(update_fields=["owner"])
elif revoked == "template":
template.owner = UserFactory()
template.save(update_fields=["owner"])
else:
User.objects.filter(pk=user.pk).update(is_active=False)
User.objects.filter(pk=regular_user.pk).update(is_active=False)
extract = mocker.patch("documents.bulk_edit.extract_zones")
with pytest.raises(PermissionDenied):
execute_bulk_operation(**enqueue.call_args.kwargs["kwargs"])
@@ -223,7 +229,8 @@ class TestRunZoneOcrBulkOperation:
def test_cannot_fall_back_to_matching_if_chosen_template_is_deleted(
self,
mocker: MockerFixture,
bulk_setup: tuple[User, APIClient, Document, Correspondent],
admin_user: User,
document: Document,
) -> None:
"""
GIVEN:
@@ -235,10 +242,14 @@ class TestRunZoneOcrBulkOperation:
template matching
- Extraction never runs
"""
user, _, doc, _ = bulk_setup
extract = mocker.patch("documents.bulk_edit.extract_zones")
with pytest.raises(OcrTemplate.DoesNotExist):
execute_bulk_operation("run_zone_ocr", [doc.pk], {"template": 999}, user.pk)
execute_bulk_operation(
"run_zone_ocr",
[document.pk],
{"template": 999},
admin_user.pk,
)
extract.assert_not_called()