From a0324755f7dc374181d7b77c751be24bd790072e Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Mon, 3 Aug 2026 11:31:26 -0700 Subject: [PATCH] test: add matching.py permission coverage for correspondents, document types, storage paths Completes the parametrized coverage started for tags -- proves all 4 matching.py lookups migrated to permitted_object_ids respect per-object view permissions, not just the tag case. Co-Authored-By: Claude Sonnet 5 --- .../test_permission_filtering_security.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/documents/tests/test_permission_filtering_security.py b/src/documents/tests/test_permission_filtering_security.py index be72b6e8f..36390f5ac 100644 --- a/src/documents/tests/test_permission_filtering_security.py +++ b/src/documents/tests/test_permission_filtering_security.py @@ -13,6 +13,9 @@ from django.test import override_settings from guardian.shortcuts import assign_perm from rest_framework.test import APIClient +from documents.matching import match_correspondents +from documents.matching import match_document_types +from documents.matching import match_storage_paths from documents.matching import match_tags from documents.models import Correspondent from documents.models import DocumentType @@ -579,3 +582,66 @@ class TestMatchingRespectsObjectPermissions: matched_ids = {t.pk for t in matched} assert visible_tag.pk in matched_ids assert hidden_tag.pk not in matched_ids + + def test_match_correspondents_only_considers_correspondents_visible_to_user(self): + owner = User.objects.create_user(username="correspondent_owner") + classifying_user = User.objects.create_user(username="classifier_user2") + visible_correspondent = CorrespondentFactory( + owner=owner, + match="invoice", + matching_algorithm=Correspondent.MATCH_LITERAL, + ) + hidden_correspondent = CorrespondentFactory( + owner=owner, + match="invoice", + matching_algorithm=Correspondent.MATCH_LITERAL, + ) + assign_perm("view_correspondent", classifying_user, visible_correspondent) + doc = DocumentFactory(owner=classifying_user, content="an invoice document") + + matched = match_correspondents(doc, classifier=None, user=classifying_user) + matched_ids = {c.pk for c in matched} + assert visible_correspondent.pk in matched_ids + assert hidden_correspondent.pk not in matched_ids + + def test_match_document_types_only_considers_document_types_visible_to_user(self): + owner = User.objects.create_user(username="document_type_owner") + classifying_user = User.objects.create_user(username="classifier_user3") + visible_document_type = DocumentTypeFactory( + owner=owner, + match="invoice", + matching_algorithm=DocumentType.MATCH_LITERAL, + ) + hidden_document_type = DocumentTypeFactory( + owner=owner, + match="invoice", + matching_algorithm=DocumentType.MATCH_LITERAL, + ) + assign_perm("view_documenttype", classifying_user, visible_document_type) + doc = DocumentFactory(owner=classifying_user, content="an invoice document") + + matched = match_document_types(doc, classifier=None, user=classifying_user) + matched_ids = {dt.pk for dt in matched} + assert visible_document_type.pk in matched_ids + assert hidden_document_type.pk not in matched_ids + + def test_match_storage_paths_only_considers_storage_paths_visible_to_user(self): + owner = User.objects.create_user(username="storage_path_owner") + classifying_user = User.objects.create_user(username="classifier_user4") + visible_storage_path = StoragePathFactory( + owner=owner, + match="invoice", + matching_algorithm=StoragePath.MATCH_LITERAL, + ) + hidden_storage_path = StoragePathFactory( + owner=owner, + match="invoice", + matching_algorithm=StoragePath.MATCH_LITERAL, + ) + assign_perm("view_storagepath", classifying_user, visible_storage_path) + doc = DocumentFactory(owner=classifying_user, content="an invoice document") + + matched = match_storage_paths(doc, classifier=None, user=classifying_user) + matched_ids = {sp.pk for sp in matched} + assert visible_storage_path.pk in matched_ids + assert hidden_storage_path.pk not in matched_ids