Merge branch 'dev' into feature/ocr-templates

This commit is contained in:
shamoon authored and GitHub committed 2026-10-25 10:29:32 -07:00
commit 800748d5ab
18 files changed
+539 -439

No files matched your search

+6 -5
View File
@@ -25,6 +25,7 @@ from documents.models import OcrTemplate
from documents.models import StoragePath
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
@@ -448,7 +449,7 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
grant_object(self.group1, self.doc1, "view_document")
permissions = {
permissions: PermissionsSpec = {
"view": {
"users": [self.user1.id, self.user2.id],
"groups": [self.group2.id],
@@ -491,7 +492,7 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
grant_object(self.user1, self.doc1, "view_document")
grant_object(self.group1, self.doc1, "view_document")
permissions = {
permissions: PermissionsSpec = {
"view": {
"users": [self.user2.id],
"groups": [self.group2.id],
@@ -542,7 +543,7 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
batched call rather than one call per (document, identity)
pair
"""
permissions = {
permissions: PermissionsSpec = {
"view": {
"users": [self.user1.id, self.user2.id],
"groups": [self.group2.id],
@@ -649,7 +650,7 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
"""
with self.assertRaises(Permission.DoesNotExist):
set_permissions_for_objects(
{"not_a_real_action": {"users": [self.user1.id], "groups": []}},
{"not_a_real_action": {"users": [self.user1.id], "groups": []}}, # type: ignore[bad-assignment]
Document,
[self.doc1.pk],
)
@@ -677,7 +678,7 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
set_permissions_for_objects(
{
"view": {"users": [self.user1.id], "groups": []},
"not_a_real_action": {"users": [self.user1.id], "groups": []},
"not_a_real_action": {"users": [self.user1.id], "groups": []}, # type: ignore[bad-assignment]
},
Document,
[self.doc1.pk],
@@ -15,9 +15,11 @@ 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 Document
from documents.models import DocumentType
from documents.models import StoragePath
from documents.models import Tag
from documents.permissions import get_permission_grantees
from documents.permissions import has_perms_owner_aware
from documents.permissions import permitted_document_ids
from documents.permissions import permitted_object_ids
@@ -1372,3 +1374,101 @@ class TestRestrictQuerysetToVisible:
assert own.pk in visible_ids
assert hidden.pk not in visible_ids
@pytest.mark.django_db
class TestGetPermissionGrantees:
def test_lists_direct_grants_per_object_and_action(self) -> None:
"""
GIVEN:
- Two documents, the first with view and change granted to a user
and a group, the second with only view granted to another user
- A user who is a member of the granted group
WHEN:
- get_permission_grantees() is called for both documents
THEN:
- Each document and action lists exactly its direct grants
- The group member is not listed as a user
"""
direct = UserFactory()
other = UserFactory()
member = UserFactory()
group = Group.objects.create(name="grantees")
member.groups.add(group)
first = DocumentFactory()
second = DocumentFactory()
grant_object(direct, first, "view_document", "change_document")
grant_object(group, first, "view_document", "change_document")
grant_object(other, second, "view_document")
grantees = get_permission_grantees(Document, [first.pk, second.pk])
assert grantees == {
first.pk: {
"view": {"users": [direct.pk], "groups": [group.pk]},
"change": {"users": [direct.pk], "groups": [group.pk]},
},
second.pk: {
"view": {"users": [other.pk], "groups": []},
"change": {"users": [], "groups": []},
},
}
def test_ids_are_sorted(self) -> None:
"""
GIVEN:
- A tag with view granted to three users in reverse creation order
WHEN:
- get_permission_grantees() is called for the tag
THEN:
- The user ids come back in ascending order
"""
users = [UserFactory() for _ in range(3)]
tag = TagFactory()
for user in reversed(users):
grant_object(user, tag, "view_tag")
grantees = get_permission_grantees(Tag, [tag.pk], actions=("view",))
assert grantees[tag.pk]["view"]["users"] == sorted(user.pk for user in users)
def test_only_requested_actions_are_returned(self) -> None:
"""
GIVEN:
- A tag with view and change granted to a user
WHEN:
- get_permission_grantees() is called for the view action only
THEN:
- Only the view action is returned
"""
user = UserFactory()
tag = TagFactory()
grant_object(user, tag, "view_tag", "change_tag")
grantees = get_permission_grantees(Tag, [tag.pk], actions=("view",))
assert grantees == {tag.pk: {"view": {"users": [user.pk], "groups": []}}}
def test_grants_on_another_model_with_the_same_pk_are_ignored(self) -> None:
"""
GIVEN:
- A tag and a correspondent sharing a primary key, with view
granted to a user on the tag only
WHEN:
- get_permission_grantees() is called for the correspondent
THEN:
- No grants are returned
"""
user = UserFactory()
tag = TagFactory()
correspondent = CorrespondentFactory(pk=tag.pk)
grant_object(user, tag, "view_tag")
grantees = get_permission_grantees(Correspondent, [correspondent.pk])
assert grantees == {
correspondent.pk: {
"view": {"users": [], "groups": []},
"change": {"users": [], "groups": []},
},
}
+27
View File
@@ -292,6 +292,33 @@ class TestViews(DirectoriesMixin, TestCase):
else:
assert False, f"Unexpected tag found: {tag['name']}"
def test_retrieve_with_full_permissions(self) -> None:
"""
GIVEN:
- A tag with view granted to two groups and change granted to only
one of them
WHEN:
- The single tag is requested with full permissions
THEN:
- Each action lists only the groups granted that action
"""
view_only = Group.objects.create(name="view_only")
view_and_change = Group.objects.create(name="view_and_change")
tag = Tag.objects.create(name="invoice")
grant_object(view_only, tag, "view_tag")
grant_object(view_and_change, tag, "view_tag", "change_tag")
grant_global(self.user, "view_tag")
self.client.force_login(self.user)
response = self.client.get(f"/api/tags/{tag.pk}/?full_perms=true")
assert response.status_code == 200
permissions = response.json()["permissions"]
assert sorted(permissions["view"]["groups"]) == sorted(
[view_only.pk, view_and_change.pk],
)
assert permissions["change"]["groups"] == [view_and_change.pk]
def test_list_no_n_plus_1_queries(self) -> None:
"""
GIVEN: