Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

views: signposting: files: fix filename encoding issues for downloads #1965

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions invenio_rdm_records/resources/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
all.
"""

import unicodedata
from urllib.parse import quote

from flask import current_app


Expand All @@ -49,6 +52,13 @@ def download_url_for(pid_value="", filename=""):
"""Return url for download route."""
url_prefix = current_app.config.get("SITE_UI_URL", "")

# see https://github.com/pallets/werkzeug/blob/main/src/werkzeug/utils.py#L456-L465
try:
filename.encode("ascii")
except UnicodeEncodeError:
# safe = RFC 5987 attr-char
filename = quote(filename, safe="!#$&+-.^_`|~")

# We use [] so that this fails and brings to attention the configuration
# problem if APP_RDM_ROUTES.record_file_download is missing
url_path = (
Expand Down
41 changes: 34 additions & 7 deletions tests/resources/serializers/test_signposting_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,38 @@

"""Resources serializers tests."""

import pytest

from invenio_rdm_records.resources.serializers import (
FAIRSignpostingProfileLvl1Serializer,
FAIRSignpostingProfileLvl2Serializer,
)


def test_signposting_serializer_full(running_app, full_record_to_dict):
@pytest.fixture
def full_record_to_dict_signposting(full_record_to_dict):
# raise ValueError("boom boom")
full_record_to_dict["files"] = {
"count": 1,
"enabled": True,
"entries": {
"testé.txt": {
"checksum": "md5:e795abeef2c38de2b064be9f6364ceae",
"ext": "txt",
"id": "d22bde05-5a36-48a3-86a7-acf2c4bb6f64",
"key": "testé.txt",
"metadata": None,
"mimetype": "text/plain",
"size": 9,
},
},
"order": [],
"total_bytes": 9,
}
return full_record_to_dict


def test_signposting_serializer_full(running_app, full_record_to_dict_signposting):
expected = {
"linkset": [
# Landing page Link Context Object
Expand Down Expand Up @@ -82,7 +107,7 @@ def test_signposting_serializer_full(running_app, full_record_to_dict):
],
"item": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test.txt", # noqa
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9.txt", # noqa
"type": "text/plain",
}
],
Expand All @@ -97,7 +122,7 @@ def test_signposting_serializer_full(running_app, full_record_to_dict):
},
# Content Resource (file) Link Context Object
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test.txt",
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
Expand All @@ -118,15 +143,17 @@ def test_signposting_serializer_full(running_app, full_record_to_dict):
]
}

serialized = FAIRSignpostingProfileLvl2Serializer().dump_obj(full_record_to_dict)
serialized = FAIRSignpostingProfileLvl2Serializer().dump_obj(
full_record_to_dict_signposting
)

assert expected == serialized


def test_signposting_lvl1_serializer_full(running_app, full_record_to_dict):
def test_signposting_lvl1_serializer_full(running_app, full_record_to_dict_signposting):
ui_url = "https://127.0.0.1:5000/records/12345-abcde"
api_url = "https://127.0.0.1:5000/api/records/12345-abcde"
filename = "test.txt"
filename = "test%C3%A9.txt"

expected = [
f'<https://orcid.org/0000-0001-8135-3489> ; rel="author"',
Expand Down Expand Up @@ -154,7 +181,7 @@ def test_signposting_lvl1_serializer_full(running_app, full_record_to_dict):
]

serialized = FAIRSignpostingProfileLvl1Serializer().serialize_object(
full_record_to_dict
full_record_to_dict_signposting
)

assert expected == serialized.split(" , ")
Expand Down
Loading