diff --git a/src/documents/tests/test_zone_ocr.py b/src/documents/tests/test_zone_ocr.py index 709eaf7191..c1e9617ab8 100644 --- a/src/documents/tests/test_zone_ocr.py +++ b/src/documents/tests/test_zone_ocr.py @@ -1534,22 +1534,33 @@ class TestTemplateVisibility: assert private in get_templates_for_document(document) @pytest.mark.usefixtures("mine", "private") - def test_unowned_documents_only_use_unowned_templates( + def test_unowned_documents_consider_every_template( self, + other: User, document: Document, + mine: OcrTemplate, unowned: OcrTemplate, + private: OcrTemplate, ) -> None: """ GIVEN: - A document with no owner + - Templates owned by two users and an unowned template WHEN: - - Templates are selected for the document + - Templates are selected for consumption, and for a manual run by + one of the users THEN: - - Only unowned templates apply + - Consumption considers every template + - The manual run is limited to templates the requester can view """ document.owner = None - assert get_templates_for_document(document) == [unowned] + assert Counter(get_templates_for_document(document)) == Counter( + [mine, unowned, private], + ) + assert Counter( + get_templates_for_document(document, requester=other), + ) == Counter([unowned, private]) @pytest.mark.usefixtures("private") def test_versions_use_the_root_documents_owner( diff --git a/src/documents/zone_ocr.py b/src/documents/zone_ocr.py index b7d30ea427..c8e5f8d8d2 100644 --- a/src/documents/zone_ocr.py +++ b/src/documents/zone_ocr.py @@ -128,15 +128,18 @@ def get_templates_for_document( ) -> list[OcrTemplate]: """ The enabled templates that match the selected file's content and that the - root document's owner can view. For a manual run, the templates are further - limited to those the requester can view, so a user editing someone else's - document never runs or sees that owner's private templates. + root document's owner can view. A document without an owner considers every + template, as matching does for tags, correspondents and the like. For a + manual run, the templates are further limited to those the requester can + view, so a user editing someone else's document never runs or sees that + owner's private templates. """ owner = get_root_document(document).owner - templates = OcrTemplate.objects.filter( - enabled=True, - id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"), - ) + templates = OcrTemplate.objects.filter(enabled=True) + if owner is not None: + templates = templates.filter( + id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"), + ) if requester is not None: templates = templates.filter( id__in=permitted_object_ids(requester, OcrTemplate, "view_ocrtemplate"),