Compare commits

...
2 changed files with 111 additions and 4 deletions

No files matched your search

@@ -0,0 +1,98 @@
from unittest import mock
import pytest
from rest_framework import status
from rest_framework.test import APIClient
from documents.models import Document
from documents.models import Tag
from paperless_testing.factories import DocumentFactory
from paperless_testing.factories import TagFactory
class TestBulkSelectionRejectsInvalidFilters:
@pytest.mark.parametrize(
"filters",
[{"created__year": "abc"}, {"correspondent__id": "abc"}],
)
def test_documents_are_not_selected(
self,
admin_client: APIClient,
filters: dict[str, str],
) -> None:
"""
GIVEN:
- Documents exist
WHEN:
- A bulk delete of all documents is requested with a filter that is invalid
THEN:
- The response is a 400 and nothing is queued, a filter that is dropped
would select every document
"""
DocumentFactory.create()
with mock.patch("documents.views.execute_bulk_operation") as execute:
# A real id, so a request that is wrongly accepted fails the assertions
# below instead of failing to render a mock as JSON
execute.apply_async.return_value.id = "task-id"
response = admin_client.post(
"/api/documents/delete/",
{"all": True, "filters": filters},
format="json",
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
execute.apply_async.assert_not_called()
assert Document.objects.count() == 1
def test_objects_are_not_selected(self, admin_client: APIClient) -> None:
"""
GIVEN:
- Tags exist
WHEN:
- A bulk delete of all tags is requested with a filter that is invalid
THEN:
- The response is a 400 and no tag is deleted
"""
TagFactory.create()
response = admin_client.post(
"/api/bulk_edit_objects/",
{
"object_type": "tags",
"operation": "delete",
"all": True,
"filters": {"id": "abc"},
},
format="json",
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert Tag.objects.count() == 1
def test_a_valid_filter_still_selects_its_documents(
self,
admin_client: APIClient,
) -> None:
"""
GIVEN:
- Two documents
WHEN:
- A bulk delete of all documents is requested with a valid filter
THEN:
- Only the matching document is queued
"""
keep = DocumentFactory.create(title="keep")
DocumentFactory.create(title="other")
with mock.patch("documents.views.execute_bulk_operation") as execute:
execute.apply_async.return_value.id = "task-id"
response = admin_client.post(
"/api/documents/delete/",
{"all": True, "filters": {"title__iexact": "keep"}},
format="json",
)
assert response.status_code == status.HTTP_200_OK
queued = execute.apply_async.call_args.kwargs["kwargs"]["document_ids"]
assert queued == [keep.pk]
+13 -4
View File
@@ -72,6 +72,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import condition
from django.views.generic import TemplateView
from django_filters.rest_framework import DjangoFilterBackend
from django_filters.utils import translate_validation
from drf_spectacular.openapi import AutoSchema
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter
@@ -3069,11 +3070,15 @@ class DocumentSelectionMixin:
root_document__isnull=True,
)
# orm-filtered docs
filtered_documents = DocumentFilterSet(
filterset = DocumentFilterSet(
data=orm_filters,
queryset=permitted_documents,
user=user,
).qs.distinct()
)
# .qs silently drops an invalid filter, which would select every document
if not filterset.is_valid():
raise translate_validation(filterset.errors)
filtered_documents = filterset.qs.distinct()
# tantivy-filtered docs (if search params provided)
search_filtered_ids = self._get_search_document_ids(
user=user,
@@ -5334,10 +5339,14 @@ class BulkEditObjectsView(PassUserMixin):
user_permitted_objects = object_class.objects.filter(
id__in=permitted_object_ids(user, object_class, perm_codename),
)
objs = filterset_class(
filterset = filterset_class(
data=filters,
queryset=user_permitted_objects,
).qs
)
# .qs silently drops an invalid filter, which would select every object
if not filterset.is_valid():
raise translate_validation(filterset.errors)
objs = filterset.qs
if object_type == "tags":
editable_ids = set(user_permitted_objects.values_list("pk", flat=True))
all_ids = set(objs.values_list("pk", flat=True))