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,10 +1,8 @@
|
||||
import json
|
||||
import os
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test import override_settings
|
||||
from PIL import Image
|
||||
@@ -509,39 +507,6 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
|
||||
|
||||
self.assertEqual(stored_text, {})
|
||||
|
||||
def test_api_accepts_a_logo_too_large_to_hold_in_memory(self) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A PNG logo larger than Django's in memory upload limit, which is
|
||||
stored in a temporary file while it is received
|
||||
WHEN:
|
||||
- Uploaded via PATCH to app config
|
||||
THEN:
|
||||
- The upload is accepted and the stored logo is a valid image
|
||||
"""
|
||||
# Random pixels do not compress, so this is a few megabytes as a PNG
|
||||
image = Image.frombytes("RGB", (1100, 1100), os.urandom(1100 * 1100 * 3))
|
||||
logo = BytesIO()
|
||||
image.save(logo, format="PNG")
|
||||
assert logo.getbuffer().nbytes > settings.FILE_UPLOAD_MAX_MEMORY_SIZE
|
||||
|
||||
response = self.client.patch(
|
||||
f"{self.ENDPOINT}1/",
|
||||
{
|
||||
"app_logo": SimpleUploadedFile(
|
||||
name="large-logo.png",
|
||||
content=logo.getvalue(),
|
||||
content_type="image/png",
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
config = ApplicationConfiguration.objects.first()
|
||||
assert config is not None
|
||||
with Image.open(config.app_logo.path) as stored_logo:
|
||||
self.assertEqual(stored_logo.size, (1100, 1100))
|
||||
|
||||
def test_api_accepts_valid_gif_logo(self) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
|
||||
@@ -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
|
||||
@@ -303,9 +303,7 @@ class ApplicationConfigurationSerializer(
|
||||
|
||||
return InMemoryUploadedFile(
|
||||
file=data,
|
||||
# An upload past the in memory limit is a temporary file, which has no
|
||||
# field name of its own
|
||||
field_name="app_logo",
|
||||
field_name=file.field_name,
|
||||
name=file.name,
|
||||
content_type=file.content_type,
|
||||
size=data.getbuffer().nbytes,
|
||||
|
||||
@@ -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