diff --git a/swh/deposit/api/service_document.py b/swh/deposit/api/service_document.py index 9ecac083..0b04103a 100644 --- a/swh/deposit/api/service_document.py +++ b/swh/deposit/api/service_document.py @@ -1,29 +1,33 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from django.shortcuts import render +from django.core.urlresolvers import reverse from .common import SWHBaseDeposit, ACCEPT_PACKAGINGS from .common import ACCEPT_ARCHIVE_CONTENT_TYPES +from ..config import COL_IRI from ..models import DepositClient, DepositCollection class SWHServiceDocument(SWHBaseDeposit): def get(self, req, *args, **kwargs): client = DepositClient.objects.get(username=req.user) - collections = [] + collections = {} + for col_id in client.collections: col = DepositCollection.objects.get(pk=col_id) - collections.append(col) + col_uri = req.build_absolute_uri(reverse(COL_IRI, args=[col.name])) + collections[col.name] = col_uri context = { 'max_upload_size': self.config['max_upload_size'], 'accept_packagings': ACCEPT_PACKAGINGS, 'accept_content_types': ACCEPT_ARCHIVE_CONTENT_TYPES, 'collections': collections, } return render(req, 'deposit/service_document.xml', context, content_type='application/xml') diff --git a/swh/deposit/templates/deposit/service_document.xml b/swh/deposit/templates/deposit/service_document.xml index aec0d822..7d60eded 100644 --- a/swh/deposit/templates/deposit/service_document.xml +++ b/swh/deposit/templates/deposit/service_document.xml @@ -1,24 +1,24 @@ 2.0 {{ max_upload_size }} The Software Heritage (SWH) Archive - {% for collection in collections %} - {{ collection.name }} Software Collection + {% for col_name, col_uri in collections.items %} + {{ col_name }} Software Collection {% for accept_content_type in accept_content_types %}{{ accept_content_type }} {% endfor %}Collection Policy Software Heritage Archive Collect, Preserve, Share false false {% for accept_packaging in accept_packagings %}{{ accept_packaging }} - {% endfor %}https://deposit.softwareheritage.org/1/{{ collection.name }}/ + {% endfor %}{{ col_uri }} {% endfor %} diff --git a/swh/deposit/tests/api/test_service_document.py b/swh/deposit/tests/api/test_service_document.py index 453d16a1..6d57ed39 100644 --- a/swh/deposit/tests/api/test_service_document.py +++ b/swh/deposit/tests/api/test_service_document.py @@ -1,102 +1,102 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from django.core.urlresolvers import reverse from nose.tools import istest from rest_framework import status from rest_framework.test import APITestCase from swh.deposit.tests import TEST_CONFIG from swh.deposit.config import SD_IRI from ..common import BasicTestCase, WithAuthTestCase class ServiceDocumentNoAuthCase(APITestCase, BasicTestCase): """Service document endpoints are protected with basic authentication. """ @istest def service_document_no_authentication_fails(self): """Without authentication, service document endpoint should return 401 """ url = reverse(SD_IRI) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) @istest def service_document_with_http_accept_should_not_break(self): """Without auth, sd endpoint through browser should return 401 """ url = reverse(SD_IRI) # when response = self.client.get( url, HTTP_ACCEPT='text/html,application/xml;q=9,*/*,q=8') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) class ServiceDocumentCase(APITestCase, WithAuthTestCase, BasicTestCase): def assertResponseOk(self, response): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEquals(response.content.decode('utf-8'), ''' 2.0 %s The Software Heritage (SWH) Archive - + %s Software Collection application/zip application/x-tar Collection Policy Software Heritage Archive Collect, Preserve, Share false false http://purl.org/net/sword/package/SimpleZip - https://deposit.softwareheritage.org/1/%s/ + http://testserver/1/%s/ ''' % (TEST_CONFIG['max_upload_size'], self.username, self.username, self.username)) # noqa @istest def service_document(self): """With authentication, service document list user's collection """ url = reverse(SD_IRI) # when response = self.client.get(url) # then self.assertResponseOk(response) @istest def service_document_with_http_accept_header(self): """With authentication, with browser, sd list user's collection """ url = reverse(SD_IRI) # when response = self.client.get( url, HTTP_ACCEPT='text/html,application/xml;q=9,*/*,q=8') self.assertResponseOk(response)