mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-05 10:50:23 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a563bd15a7 |
No files matched your search
@@ -1,8 +1,10 @@
|
||||
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
|
||||
@@ -507,6 +509,39 @@ 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:
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
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()
|
||||
@@ -1461,10 +1461,7 @@ class DocumentViewSet(
|
||||
use_archive=not self.original_requested(request)
|
||||
and file_doc.has_archive_version,
|
||||
disposition=disposition,
|
||||
# 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),
|
||||
),
|
||||
follow_formatting=request.query_params.get("follow_formatting", False),
|
||||
)
|
||||
|
||||
def get_metadata(self, file, mime_type):
|
||||
@@ -5275,9 +5272,6 @@ 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}; "
|
||||
|
||||
@@ -303,7 +303,9 @@ class ApplicationConfigurationSerializer(
|
||||
|
||||
return InMemoryUploadedFile(
|
||||
file=data,
|
||||
field_name=file.field_name,
|
||||
# An upload past the in memory limit is a temporary file, which has no
|
||||
# field name of its own
|
||||
field_name="app_logo",
|
||||
name=file.name,
|
||||
content_type=file.content_type,
|
||||
size=data.getbuffer().nbytes,
|
||||
|
||||
Reference in new issue
Block a user