diff --git a/swh/deposit/templates/rest_framework/api.html b/swh/deposit/templates/rest_framework/api.html new file mode 100644 index 00000000..a89f8c82 --- /dev/null +++ b/swh/deposit/templates/rest_framework/api.html @@ -0,0 +1,3 @@ + diff --git a/swh/deposit/tests/api/test_deposit_status.py b/swh/deposit/tests/api/test_deposit_status.py index 3a6223fe..06f89161 100644 --- a/swh/deposit/tests/api/test_deposit_status.py +++ b/swh/deposit/tests/api/test_deposit_status.py @@ -1,76 +1,92 @@ # 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 io import BytesIO from nose.tools import istest from rest_framework import status from rest_framework.test import APITestCase from swh.deposit.models import Deposit from swh.deposit.parsers import parse_xml from ..common import BasicTestCase, WithAuthTestCase, FileSystemCreationRoutine +from ..common import CommonCreationRoutine from ...config import COL_IRI, STATE_IRI, DEPOSIT_STATUS_READY class DepositStatusTestCase(APITestCase, WithAuthTestCase, BasicTestCase, - FileSystemCreationRoutine): + FileSystemCreationRoutine, CommonCreationRoutine): """Status on deposit """ @istest def post_deposit_with_status_check(self): """Binary upload should be accepted """ # given url = reverse(COL_IRI, args=[self.collection.name]) external_id = 'some-external-id-1' # when response = self.client.post( url, content_type='application/zip', # as zip data=self.archive['data'], # + headers CONTENT_LENGTH=self.archive['length'], HTTP_SLUG=external_id, HTTP_CONTENT_MD5=self.archive['md5sum'], HTTP_PACKAGING='http://purl.org/net/sword/package/SimpleZip', HTTP_IN_PROGRESS='false', HTTP_CONTENT_DISPOSITION='attachment; filename=filename0') # then self.assertEqual(response.status_code, status.HTTP_201_CREATED) deposit = Deposit.objects.get(external_id=external_id) status_url = reverse(STATE_IRI, args=[self.collection.name, deposit.id]) # check status status_response = self.client.get(status_url) self.assertEqual(status_response.status_code, status.HTTP_200_OK) r = parse_xml(BytesIO(status_response.content)) self.assertEqual(r['{http://www.w3.org/2005/Atom}deposit_id'], deposit.id) self.assertEqual(r['{http://www.w3.org/2005/Atom}status'], DEPOSIT_STATUS_READY) self.assertEqual(r['{http://www.w3.org/2005/Atom}detail'], 'Deposit is fully received, checked, and ready for ' 'injection') @istest def status_on_unknown_deposit(self): """Asking for the status of unknown deposit returns 404 response""" status_url = reverse(STATE_IRI, args=[self.collection.name, 999]) status_response = self.client.get(status_url) self.assertEqual(status_response.status_code, status.HTTP_404_NOT_FOUND) + + @istest + def status_with_http_accept_header_should_not_break(self): + """Asking deposit status with Accept header should return 200 + + """ + deposit_id = self.create_deposit_partial() + + status_url = reverse(STATE_IRI, args=[ + self.collection.name, deposit_id]) + response = self.client.get( + status_url, + HTTP_ACCEPT='text/html,application/xml;q=9,*/*,q=8') + + self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/swh/deposit/tests/api/test_service_document.py b/swh/deposit/tests/api/test_service_document.py index 489db130..6db695b4 100644 --- a/swh/deposit/tests/api/test_service_document.py +++ b/swh/deposit/tests/api/test_service_document.py @@ -1,68 +1,101 @@ # 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 is unauthorized""" + """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) - -class ServiceDocumentCase(APITestCase, WithAuthTestCase, BasicTestCase): @istest - def service_document(self): - """With authentication, service document list user's collection + 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) + response = self.client.get( + url, + HTTP_ACCEPT='text/html,application/xml;q=9,*/*,q=8') - # then + 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 Collection Policy Software Heritage Archive Collect, Preserve, Share false false http://purl.org/net/sword/package/SimpleZip https://deposit.softwareheritage.org/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)