mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 17:58:41 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
803421ed31 |
No files matched your search
@@ -448,19 +448,7 @@ class OwnedObjectSerializer(
|
||||
|
||||
def validate_unique_together(self, validated_data, instance=None) -> None:
|
||||
# workaround for https://github.com/encode/django-rest-framework/issues/9358
|
||||
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:
|
||||
if "owner" in validated_data and "name" in self.Meta.fields:
|
||||
name = validated_data.get("name", instance.name if instance else None)
|
||||
objects = (
|
||||
self.Meta.model.objects.exclude(pk=instance.pk)
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import pytest
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from documents.models import Document
|
||||
from paperless_testing.dirs import PaperlessDirs
|
||||
from paperless_testing.factories import DocumentFactory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def archived_document(paperless_dirs: PaperlessDirs) -> Document:
|
||||
document = DocumentFactory.create(
|
||||
title="title",
|
||||
filename="source.pdf",
|
||||
archive_filename="archived.pdf",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
document.source_path.write_bytes(b"source")
|
||||
document.archive_path.write_bytes(b"archive")
|
||||
return document
|
||||
|
||||
|
||||
class TestDownloadFollowFormatting:
|
||||
@pytest.mark.parametrize("value", ["false", "0", "no", "off"])
|
||||
def test_false_values_use_the_public_filename(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
archived_document: Document,
|
||||
value: str,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document whose name on disk differs from its public name
|
||||
WHEN:
|
||||
- It is downloaded with follow_formatting set to a false value
|
||||
THEN:
|
||||
- The public name is used, the string "false" is not true
|
||||
"""
|
||||
response = admin_client.get(
|
||||
f"/api/documents/{archived_document.pk}/download/?follow_formatting={value}",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert "title.pdf" in response["Content-Disposition"]
|
||||
response.close()
|
||||
|
||||
@pytest.mark.parametrize("value", ["true", "True", "1", "yes", "on"])
|
||||
def test_true_values_use_the_name_on_disk(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
archived_document: Document,
|
||||
value: str,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document whose name on disk differs from its public name
|
||||
WHEN:
|
||||
- It is downloaded with follow_formatting set to a true value
|
||||
THEN:
|
||||
- The name on disk is used
|
||||
"""
|
||||
response = admin_client.get(
|
||||
f"/api/documents/{archived_document.pk}/download/?follow_formatting={value}",
|
||||
)
|
||||
|
||||
assert "archived.pdf" in response["Content-Disposition"]
|
||||
response.close()
|
||||
|
||||
def test_a_value_that_is_not_a_boolean_is_a_400(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
archived_document: Document,
|
||||
) -> None:
|
||||
response = admin_client.get(
|
||||
f"/api/documents/{archived_document.pk}/download/?follow_formatting=maybe",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
|
||||
|
||||
class TestDownloadFilenameControlCharacters:
|
||||
def test_control_characters_do_not_fail_the_download(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
paperless_dirs: PaperlessDirs,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document whose name on disk holds control characters, a title can
|
||||
WHEN:
|
||||
- It is downloaded following the name on disk
|
||||
THEN:
|
||||
- The download works and no control character is in the header
|
||||
"""
|
||||
document = DocumentFactory.create(
|
||||
filename="source.pdf",
|
||||
archive_filename="ar\r\nchi\x12ved.pdf",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
document.source_path.write_bytes(b"source")
|
||||
document.archive_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
document.archive_path.write_bytes(b"archive")
|
||||
|
||||
response = admin_client.get(
|
||||
f"/api/documents/{document.pk}/download/?follow_formatting=true",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert 'filename="archived.pdf"' in response["Content-Disposition"]
|
||||
assert not any(
|
||||
ord(char) < 32 or ord(char) == 127
|
||||
for char in response["Content-Disposition"]
|
||||
)
|
||||
response.close()
|
||||
@@ -1,130 +0,0 @@
|
||||
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
|
||||
@@ -1461,7 +1461,10 @@ class DocumentViewSet(
|
||||
use_archive=not self.original_requested(request)
|
||||
and file_doc.has_archive_version,
|
||||
disposition=disposition,
|
||||
follow_formatting=request.query_params.get("follow_formatting", False),
|
||||
# The query value is a string, "false" must not count as true
|
||||
follow_formatting=serializers.BooleanField(default=False).run_validation(
|
||||
request.query_params.get("follow_formatting", serializers.empty),
|
||||
),
|
||||
)
|
||||
|
||||
def get_metadata(self, file, mime_type):
|
||||
@@ -5272,6 +5275,9 @@ def serve_file(
|
||||
.replace("\\", "_")
|
||||
.replace('"', "_")
|
||||
)
|
||||
# A name can hold control characters, which a header cannot carry. The encoded
|
||||
# form below percent encodes them, only this plain fallback needs them removed
|
||||
filename_normalized = "".join(c for c in filename_normalized if c.isprintable())
|
||||
filename_encoded = quote(filename)
|
||||
content_disposition = (
|
||||
f"{disposition}; "
|
||||
|
||||
@@ -16,7 +16,6 @@ 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
|
||||
@@ -144,11 +143,3 @@ 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