mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 09:48:41 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96761cd02f |
No files matched your search
@@ -448,7 +448,19 @@ class OwnedObjectSerializer(
|
||||
|
||||
def validate_unique_together(self, validated_data, instance=None) -> None:
|
||||
# workaround for https://github.com/encode/django-rest-framework/issues/9358
|
||||
if "owner" in validated_data and "name" in self.Meta.fields:
|
||||
if "name" not in self.Meta.fields:
|
||||
return
|
||||
if instance is not None:
|
||||
owner = validated_data.get("owner", instance.owner)
|
||||
if owner == instance.owner and (
|
||||
validated_data.get("name", instance.name) == instance.name
|
||||
):
|
||||
# Nothing the constraint covers changes, and rows saved before it
|
||||
# was enforced can already be duplicates
|
||||
return
|
||||
# An update that does not mention the owner keeps the current one
|
||||
validated_data = {**validated_data, "owner": owner}
|
||||
if "owner" in validated_data:
|
||||
name = validated_data.get("name", instance.name if instance else None)
|
||||
objects = (
|
||||
self.Meta.model.objects.exclude(pk=instance.pk)
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
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,
|
||||
mocker: MockerFixture,
|
||||
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()
|
||||
|
||||
execute = mocker.patch("documents.views.execute_bulk_operation")
|
||||
# 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,
|
||||
mocker: MockerFixture,
|
||||
) -> 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")
|
||||
|
||||
execute = mocker.patch("documents.views.execute_bulk_operation")
|
||||
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]
|
||||
@@ -0,0 +1,130 @@
|
||||
import pytest
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from paperless_testing.factories import CorrespondentFactory
|
||||
from paperless_testing.factories import SavedViewFactory
|
||||
|
||||
|
||||
class TestUniqueNameOnUpdate:
|
||||
@pytest.mark.parametrize(
|
||||
"owner_kind",
|
||||
[
|
||||
pytest.param("owned", id="owned"),
|
||||
pytest.param("unowned", id="unowned"),
|
||||
],
|
||||
)
|
||||
def test_patching_to_an_existing_name_is_a_400(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
admin_user: User,
|
||||
owner_kind: str,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Two correspondents with the same owner, or none
|
||||
WHEN:
|
||||
- One is patched to the other's name without mentioning the owner
|
||||
THEN:
|
||||
- The response is a 400, as it is when creating a duplicate
|
||||
"""
|
||||
owner = admin_user if owner_kind == "owned" else None
|
||||
CorrespondentFactory.create(name="taken", owner=owner)
|
||||
other = CorrespondentFactory.create(name="free", owner=owner)
|
||||
|
||||
response = admin_client.patch(
|
||||
f"/api/correspondents/{other.pk}/",
|
||||
{"name": "taken"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
other.refresh_from_db()
|
||||
assert other.name == "free"
|
||||
|
||||
def test_the_same_name_under_a_different_owner_is_allowed(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
admin_user: User,
|
||||
regular_user: User,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Correspondents with the same name but different owners are allowed
|
||||
WHEN:
|
||||
- One correspondent is renamed to a name another owner already uses
|
||||
THEN:
|
||||
- The request succeeds
|
||||
"""
|
||||
CorrespondentFactory.create(name="shared name", owner=regular_user)
|
||||
mine = CorrespondentFactory.create(name="mine", owner=admin_user)
|
||||
|
||||
response = admin_client.patch(
|
||||
f"/api/correspondents/{mine.pk}/",
|
||||
{"name": "shared name"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_saving_another_field_of_a_legacy_duplicate_still_works(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
admin_user: User,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Two saved views with the same name and owner, which the database
|
||||
allows and an older version could create
|
||||
WHEN:
|
||||
- One is patched without changing its name, or with the name it already has
|
||||
THEN:
|
||||
- Both requests succeed, and renaming it to a free name works too
|
||||
"""
|
||||
view = SavedViewFactory.create(name="dup", owner=admin_user)
|
||||
SavedViewFactory.create(name="dup", owner=admin_user)
|
||||
|
||||
other_field = admin_client.patch(
|
||||
f"/api/saved_views/{view.pk}/",
|
||||
{"sort_reverse": True},
|
||||
format="json",
|
||||
)
|
||||
same_name = admin_client.patch(
|
||||
f"/api/saved_views/{view.pk}/",
|
||||
{"name": "dup", "sort_reverse": False},
|
||||
format="json",
|
||||
)
|
||||
renamed = admin_client.patch(
|
||||
f"/api/saved_views/{view.pk}/",
|
||||
{"name": "unique now"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert other_field.status_code == status.HTTP_200_OK
|
||||
assert same_name.status_code == status.HTTP_200_OK
|
||||
assert renamed.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_a_new_duplicate_saved_view_name_is_a_400(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
admin_user: User,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Two saved views with different names
|
||||
WHEN:
|
||||
- One is renamed to the other's name
|
||||
THEN:
|
||||
- The response is a 400
|
||||
"""
|
||||
SavedViewFactory.create(name="taken", owner=admin_user)
|
||||
other = SavedViewFactory.create(name="free", owner=admin_user)
|
||||
|
||||
response = admin_client.patch(
|
||||
f"/api/saved_views/{other.pk}/",
|
||||
{"name": "taken"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
+4
-13
@@ -72,7 +72,6 @@ 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
|
||||
@@ -3070,15 +3069,11 @@ class DocumentSelectionMixin:
|
||||
root_document__isnull=True,
|
||||
)
|
||||
# orm-filtered docs
|
||||
filterset = DocumentFilterSet(
|
||||
filtered_documents = DocumentFilterSet(
|
||||
data=orm_filters,
|
||||
queryset=permitted_documents,
|
||||
user=user,
|
||||
)
|
||||
# .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()
|
||||
).qs.distinct()
|
||||
# tantivy-filtered docs (if search params provided)
|
||||
search_filtered_ids = self._get_search_document_ids(
|
||||
user=user,
|
||||
@@ -5339,14 +5334,10 @@ class BulkEditObjectsView(PassUserMixin):
|
||||
user_permitted_objects = object_class.objects.filter(
|
||||
id__in=permitted_object_ids(user, object_class, perm_codename),
|
||||
)
|
||||
filterset = filterset_class(
|
||||
objs = filterset_class(
|
||||
data=filters,
|
||||
queryset=user_permitted_objects,
|
||||
)
|
||||
# .qs silently drops an invalid filter, which would select every object
|
||||
if not filterset.is_valid():
|
||||
raise translate_validation(filterset.errors)
|
||||
objs = filterset.qs
|
||||
).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))
|
||||
|
||||
@@ -16,6 +16,7 @@ from documents.models import MatchingModel
|
||||
from documents.models import OcrTemplate
|
||||
from documents.models import OcrTemplateZone
|
||||
from documents.models import PaperlessTask
|
||||
from documents.models import SavedView
|
||||
from documents.models import StoragePath
|
||||
from documents.models import Tag
|
||||
from paperless_testing.typed_factory import TypedModelFactory
|
||||
@@ -143,3 +144,11 @@ class OcrTemplateZoneFactory(TypedModelFactory[OcrTemplateZone]):
|
||||
y = 0
|
||||
width = 100
|
||||
height = 50
|
||||
|
||||
|
||||
class SavedViewFactory(TypedModelFactory[SavedView]):
|
||||
class Meta:
|
||||
model = SavedView
|
||||
|
||||
name = factory.Sequence(lambda n: f"Saved view {n}")
|
||||
sort_field = "created"
|
||||
Reference in new issue
Block a user