mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-01 17:10:24 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
803421ed31 |
No files matched your search
@@ -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()
|
||||
@@ -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}; "
|
||||
|
||||
Reference in new issue
Block a user