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: fix: fallback to level 2 linkset if collections size too big to control link header size #1966

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def fair_signposting_tostring(cls, record):
"""Stringify a FAIR Signposting record."""
links = []
for rel, values in record.items():
# if rel not in excluded_keys:
for value in values:
link = f'<{value["href"]}> ; rel="{rel}"'
if "type" in value:
Expand Down
14 changes: 13 additions & 1 deletion invenio_rdm_records/resources/serializers/signposting/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""Signposting schemas."""

import idutils
from marshmallow import Schema, fields, missing
from marshmallow import Schema, fields, missing, post_dump

from ...urls import download_url_for
from ..utils import get_vocabulary_props
Expand Down Expand Up @@ -157,6 +157,18 @@ def serialize_linkset(self, obj, **kwargs):
}
]

@post_dump
def fallback_to_lvl2_linkset_only_if_collections_too_big(self, data, **kwargs):
"""Fallback to level 2 linkset only if the size of some collections is too big for the level 1 landing page.

Generating an HTTP Link header which is too big could make nginx fail.
"""
for key in ["author", "item", "license"]:
if key in data and len(data[key]) > 5:
data = {"linkset": data["linkset"]}
break
return data


class LandingPageLvl2Schema(LandingPageSchema):
"""Schema for serialization of link context object for the level 2 landing page.
Expand Down
224 changes: 196 additions & 28 deletions tests/resources/serializers/test_signposting_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,105 @@
)


@pytest.fixture
def full_record_to_dict_signposting(full_record_to_dict):
# raise ValueError("boom boom")
def full_record_to_dict_signposting(full_record_to_dict, collections_size):
file_entries = {
f"testé_{n+1}.txt": {
"checksum": "md5:e795abeef2c38de2b064be9f6364ceae",
"ext": "txt",
"id": "d22bde05-5a36-48a3-86a7-acf2c4bb6f64",
"key": f"testé_{n+1}.txt",
"metadata": None,
"mimetype": "text/plain",
"size": 9,
}
for n in range(collections_size)
}
full_record_to_dict["files"] = {
"count": 1,
"count": len(file_entries),
"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,
},
},
"entries": file_entries,
"order": [],
"total_bytes": 9,
}

creators = [
{
"person_or_org": {
"family_name": "Family Name",
"given_name": "Given Name",
"identifiers": [
{
"identifier": f"0000-0001-8135-348{n+1}",
"scheme": "orcid",
},
],
"name": "Family Name, Given Name",
"type": "personal",
},
}
for n in range(collections_size)
]
full_record_to_dict["metadata"]["creators"] = creators

rights_custom = [
{
"description": {
"en": "A description",
},
"link": f"https://customlicense.org/licenses/by/{n+1}.0/",
"title": {
"en": "A custom license",
},
}
for n in range(int(collections_size / 2))
]
rights_standard = [
{
"description": {
"en": "The Creative Commons Attribution license allows re-distribution and re-use of a licensed work on the condition that the creator is appropriately credited.", # noqa
},
"id": f"cc-by-{n+1}.0",
"props": {
"scheme": "spdx",
"url": f"https://creativecommons.org/licenses/by/{n+1}.0/legalcode",
},
"title": {
"en": f"Creative Commons Attribution {n+1}.0 International",
},
}
for n in range(int(collections_size / 2), collections_size)
]
full_record_to_dict["metadata"]["rights"] = rights_custom + rights_standard

return full_record_to_dict


def test_signposting_serializer_full(running_app, full_record_to_dict_signposting):
@pytest.fixture
def full_record_to_dict_signposting_collections_five(full_record_to_dict):
return full_record_to_dict_signposting(full_record_to_dict, 5)


@pytest.fixture
def full_record_to_dict_signposting_collections_six(full_record_to_dict):
return full_record_to_dict_signposting(full_record_to_dict, 6)


def test_signposting_serializer_full(
running_app, full_record_to_dict_signposting_collections_six
):
expected = {
"linkset": [
# Landing page Link Context Object
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde",
"author": [{"href": "https://orcid.org/0000-0001-8135-3489"}],
"author": [
{"href": "https://orcid.org/0000-0001-8135-3481"},
{"href": "https://orcid.org/0000-0001-8135-3482"},
{"href": "https://orcid.org/0000-0001-8135-3483"},
{"href": "https://orcid.org/0000-0001-8135-3484"},
{"href": "https://orcid.org/0000-0001-8135-3485"},
{"href": "https://orcid.org/0000-0001-8135-3486"},
],
"cite-as": [{"href": "https://doi.org/10.1234/12345-abcde"}],
"describedby": [
{
Expand Down Expand Up @@ -107,13 +176,37 @@ def test_signposting_serializer_full(running_app, full_record_to_dict_signpostin
],
"item": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9.txt", # noqa
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_1.txt", # noqa
"type": "text/plain",
}
},
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_2.txt", # noqa
"type": "text/plain",
},
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_3.txt", # noqa
"type": "text/plain",
},
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_4.txt", # noqa
"type": "text/plain",
},
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_5.txt", # noqa
"type": "text/plain",
},
{
"href": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_6.txt", # noqa
"type": "text/plain",
},
],
"license": [
{"href": "https://customlicense.org/licenses/by/4.0/"},
{"href": "https://customlicense.org/licenses/by/1.0/"},
{"href": "https://customlicense.org/licenses/by/2.0/"},
{"href": "https://customlicense.org/licenses/by/3.0/"},
{"href": "https://creativecommons.org/licenses/by/4.0/legalcode"},
{"href": "https://creativecommons.org/licenses/by/5.0/legalcode"},
{"href": "https://creativecommons.org/licenses/by/6.0/legalcode"},
],
"type": [
{"href": "https://schema.org/Photograph"},
Expand All @@ -122,7 +215,52 @@ def test_signposting_serializer_full(running_app, full_record_to_dict_signpostin
},
# Content Resource (file) Link Context Object
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9.txt",
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_1.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
"type": "text/html",
}
],
},
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_2.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
"type": "text/html",
}
],
},
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_3.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
"type": "text/html",
}
],
},
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_4.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
"type": "text/html",
}
],
},
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_5.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
"type": "text/html",
}
],
},
{
"anchor": "https://127.0.0.1:5000/records/12345-abcde/files/test%C3%A9_6.txt",
"collection": [
{
"href": "https://127.0.0.1:5000/records/12345-abcde",
Expand All @@ -144,19 +282,42 @@ def test_signposting_serializer_full(running_app, full_record_to_dict_signpostin
}

serialized = FAIRSignpostingProfileLvl2Serializer().dump_obj(
full_record_to_dict_signposting
full_record_to_dict_signposting_collections_six
)

assert expected == serialized


def test_signposting_lvl1_serializer_full(running_app, full_record_to_dict_signposting):
def test_signposting_lvl1_serializer_full_collections_too_big(
running_app, full_record_to_dict_signposting_collections_six
):
api_url = "https://127.0.0.1:5000/api/records/12345-abcde"

# Collections are too big, so only the linkset link is included.
expected = [
f'<{api_url}> ; rel="linkset" ; type="application/linkset+json"',
]

serialized = FAIRSignpostingProfileLvl1Serializer().serialize_object(
full_record_to_dict_signposting_collections_six
)

assert expected == serialized.split(" , ")


def test_signposting_lvl1_serializer_full_collections_size_ok(
running_app, full_record_to_dict_signposting_collections_five
):
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%C3%A9.txt"

# Collections size are OK, so the whole level 1 headers are included.
expected = [
f'<https://orcid.org/0000-0001-8135-3489> ; rel="author"',
f'<https://orcid.org/0000-0001-8135-3481> ; rel="author"',
f'<https://orcid.org/0000-0001-8135-3482> ; rel="author"',
f'<https://orcid.org/0000-0001-8135-3483> ; rel="author"',
f'<https://orcid.org/0000-0001-8135-3484> ; rel="author"',
f'<https://orcid.org/0000-0001-8135-3485> ; rel="author"',
f'<https://doi.org/10.1234/12345-abcde> ; rel="cite-as"',
f'<{api_url}> ; rel="describedby" ; type="application/dcat+xml"',
f'<{api_url}> ; rel="describedby" ; type="application/json"',
Expand All @@ -172,16 +333,23 @@ def test_signposting_lvl1_serializer_full(running_app, full_record_to_dict_signp
f'<{api_url}> ; rel="describedby" ; type="application/x-bibtex"',
f'<{api_url}> ; rel="describedby" ; type="application/x-dc+xml"',
f'<{api_url}> ; rel="describedby" ; type="text/x-bibliography"',
f'<{ui_url}/files/{filename}> ; rel="item" ; type="text/plain"',
'<https://customlicense.org/licenses/by/4.0/> ; rel="license"',
f'<{ui_url}/files/test%C3%A9_1.txt> ; rel="item" ; type="text/plain"',
f'<{ui_url}/files/test%C3%A9_2.txt> ; rel="item" ; type="text/plain"',
f'<{ui_url}/files/test%C3%A9_3.txt> ; rel="item" ; type="text/plain"',
f'<{ui_url}/files/test%C3%A9_4.txt> ; rel="item" ; type="text/plain"',
f'<{ui_url}/files/test%C3%A9_5.txt> ; rel="item" ; type="text/plain"',
'<https://customlicense.org/licenses/by/1.0/> ; rel="license"',
'<https://customlicense.org/licenses/by/2.0/> ; rel="license"',
'<https://creativecommons.org/licenses/by/3.0/legalcode> ; rel="license"',
'<https://creativecommons.org/licenses/by/4.0/legalcode> ; rel="license"',
'<https://creativecommons.org/licenses/by/5.0/legalcode> ; rel="license"',
'<https://schema.org/Photograph> ; rel="type"',
'<https://schema.org/AboutPage> ; rel="type"',
f'<{api_url}> ; rel="linkset" ; type="application/linkset+json"',
]

serialized = FAIRSignpostingProfileLvl1Serializer().serialize_object(
full_record_to_dict_signposting
full_record_to_dict_signposting_collections_five
)

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