Skip to content

Commit

Permalink
PB-1157 Add hreflang to collection links
Browse files Browse the repository at this point in the history
  • Loading branch information
schtibe committed Dec 5, 2024
1 parent 02c7876 commit 41753d8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/stac_api/serializers/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CollectionLinkSerializer(NonNullModelSerializer):

class Meta:
model = CollectionLink
fields = ['href', 'rel', 'title', 'type']
fields = ['href', 'rel', 'title', 'type', 'hreflang']

# NOTE: when explicitely declaring fields, we need to add the validation as for the field
# in model !
Expand Down
21 changes: 21 additions & 0 deletions app/tests/tests_10/sample_data/collection_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@
}
}

links_hreflanged = {
'link-1': {
'title': 'Link with hreflang',
'rel': 'describedBy',
'href': 'http://perdu.com/',
'hreflang': 'de'
},
'link-2': {
'title': 'Link with hreflang',
'rel': 'copiedFrom',
'href': 'http://perdu.com/',
'hreflang': 'fr-CH'
}
}

collections = {
'collection-1': {
'name': 'collection-1',
Expand Down Expand Up @@ -104,6 +119,12 @@
'license': 'proprietary',
'links': [multiple_links['link-1'], multiple_links['link-2']]
},
'collection-hreflang-links': {
'name': 'collection-1',
'description': 'This a collection description',
'license': 'proprietary',
'links': links_hreflanged.values()
},
'collection-invalid-providers': {
'name': 'collection-invalid-provider',
'description': 'This is a collection with invalid provider',
Expand Down
70 changes: 69 additions & 1 deletion app/tests/tests_10/test_collections_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from datetime import datetime
from typing import cast

from django.contrib.auth import get_user_model
from django.test import Client
Expand All @@ -13,7 +14,7 @@
from tests.tests_10.base_test import STAC_BASE_V
from tests.tests_10.base_test import StacBaseTestCase
from tests.tests_10.base_test import StacBaseTransactionTestCase
from tests.tests_10.data_factory import CollectionAssetFactory
from tests.tests_10.data_factory import CollectionAssetFactory, SampleData
from tests.tests_10.data_factory import CollectionFactory
from tests.tests_10.data_factory import Factory
from tests.tests_10.utils import reverse_version
Expand Down Expand Up @@ -596,3 +597,70 @@ def test_unauthorized_collection_delete(self):
self.assertStatusCode(
401, response, msg="unauthorized and unimplemented collection delete was permitted."
)


class CollectionLinksEndpointTestCase(StacBaseTestCase):

def setUp(self):
self.client = Client()
client_login(self.client)

@classmethod
def setUpTestData(cls) -> None:
cls.factory = Factory()
cls.collection_data = cls.factory.create_collection_sample(db_create=True)
cls.collection = cast(Collection, cls.collection_data.model)
return super().setUpTestData()

def test_create_collection_link_with_simple_link(self):
data = self.collection_data.get_json('put')

path = f'/{STAC_BASE_V}/collections/{self.collection.name}'
response = self.client.put(path, data=data, content_type="application/json")

self.assertEqual(response.status_code, 200)

link = CollectionLink.objects.last()
assert link is not None
self.assertEqual(link.rel, data['links'][0]['rel'])
self.assertEqual(link.hreflang, None)

def test_create_collection_link_with_hreflang(self):
data = self.collection_data.get_json('put')
data['links'] = [{
'rel': 'more-info',
'href': 'http://www.meteoschweiz.ch/',
'title': 'A link to a german page',
'type': 'text/html',
'hreflang': "de"
}]

path = f'/{STAC_BASE_V}/collections/{self.collection.name}'
response = self.client.put(path, data=data, content_type="application/json")

self.assertEqual(response.status_code, 200)

link = CollectionLink.objects.last()
# for the type checker here. But it's of course also valuable for the test
# since pytest should be the future, maybe I can afford this?
assert link is not None
self.assertEqual(link.hreflang, 'de')

def test_read_collection_with_hreflang(self):
collection_data: SampleData = self.factory.create_collection_sample(
sample='collection-hreflang-links', db_create=False
)
collection = cast(Collection, collection_data.model)

path = f'/{STAC_BASE_V}/collections/{collection.name}'
response = self.client.get(path, content_type="application/json")

self.assertEqual(response.status_code, 200)

json_data = response.json()
self.assertIn('links', json_data)
link_data = json_data['links']
de_link = link_data[-2]
fr_link = link_data[-1]
self.assertEqual(de_link['hreflang'], 'de')
self.assertEqual(fr_link['hreflang'], 'fr-CH')

0 comments on commit 41753d8

Please sign in to comment.