Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_deposit_update.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from django.core.urlresolvers import reverse | from django.core.urlresolvers import reverse | ||||
from nose.tools import istest | |||||
from rest_framework import status | from rest_framework import status | ||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from swh.deposit.models import Deposit, DepositRequest | from swh.deposit.models import Deposit, DepositRequest | ||||
from swh.deposit.config import EDIT_SE_IRI, EM_IRI | from swh.deposit.config import EDIT_SE_IRI, EM_IRI | ||||
from ..common import BasicTestCase, WithAuthTestCase, CommonCreationRoutine | from ..common import BasicTestCase, WithAuthTestCase, CommonCreationRoutine | ||||
from ..common import FileSystemCreationRoutine, create_arborescence_archive | from ..common import FileSystemCreationRoutine, create_arborescence_archive | ||||
Show All 16 Lines | </entry>""" | ||||
self.atom_entry_data1 = b"""<?xml version="1.0"?> | self.atom_entry_data1 = b"""<?xml version="1.0"?> | ||||
<entry xmlns="http://www.w3.org/2005/Atom"> | <entry xmlns="http://www.w3.org/2005/Atom"> | ||||
<foobar>bar</foobar> | <foobar>bar</foobar> | ||||
</entry>""" | </entry>""" | ||||
self.archive2 = create_arborescence_archive( | self.archive2 = create_arborescence_archive( | ||||
self.root_path, 'archive2', 'file2', b'some other content in file') | self.root_path, 'archive2', 'file2', b'some other content in file') | ||||
@istest | def test_replace_archive_to_deposit_is_possible(self): | ||||
def replace_archive_to_deposit_is_possible(self): | |||||
"""Replace all archive with another one should return a 204 response | """Replace all archive with another one should return a 204 response | ||||
""" | """ | ||||
# given | # given | ||||
deposit_id = self.create_simple_binary_deposit(status_partial=True) | deposit_id = self.create_simple_binary_deposit(status_partial=True) | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
requests = DepositRequest.objects.filter( | requests = DepositRequest.objects.filter( | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | def test_replace_archive_to_deposit_is_possible(self): | ||||
self.assertEquals(len(list(requests)), 1) | self.assertEquals(len(list(requests)), 1) | ||||
self.assertRegex(requests[0].archive.name, self.archive2['name']) | self.assertRegex(requests[0].archive.name, self.archive2['name']) | ||||
# check we did not touch the other parts | # check we did not touch the other parts | ||||
requests = list(DepositRequest.objects.filter( | requests = list(DepositRequest.objects.filter( | ||||
deposit=deposit, type=self.deposit_request_types['metadata'])) | deposit=deposit, type=self.deposit_request_types['metadata'])) | ||||
self.assertEquals(len(requests), 1) | self.assertEquals(len(requests), 1) | ||||
@istest | def test_replace_metadata_to_deposit_is_possible(self): | ||||
def replace_metadata_to_deposit_is_possible(self): | |||||
"""Replace all metadata with another one should return a 204 response | """Replace all metadata with another one should return a 204 response | ||||
""" | """ | ||||
# given | # given | ||||
deposit_id = self.create_simple_binary_deposit(status_partial=True) | deposit_id = self.create_simple_binary_deposit(status_partial=True) | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
requests = DepositRequest.objects.filter( | requests = DepositRequest.objects.filter( | ||||
Show All 23 Lines | def test_replace_metadata_to_deposit_is_possible(self): | ||||
metadata = requests[0].metadata | metadata = requests[0].metadata | ||||
self.assertEquals(metadata['foobar'], 'bar') | self.assertEquals(metadata['foobar'], 'bar') | ||||
# check we did not touch the other parts | # check we did not touch the other parts | ||||
requests = list(DepositRequest.objects.filter( | requests = list(DepositRequest.objects.filter( | ||||
deposit=deposit, type=self.deposit_request_types['archive'])) | deposit=deposit, type=self.deposit_request_types['archive'])) | ||||
self.assertEquals(len(requests), 1) | self.assertEquals(len(requests), 1) | ||||
@istest | def test_add_archive_to_deposit_is_possible(self): | ||||
def add_archive_to_deposit_is_possible(self): | |||||
"""Add another archive to a deposit return a 201 response | """Add another archive to a deposit return a 201 response | ||||
""" | """ | ||||
# given | # given | ||||
deposit_id = self.create_simple_binary_deposit(status_partial=True) | deposit_id = self.create_simple_binary_deposit(status_partial=True) | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
requests = DepositRequest.objects.filter( | requests = DepositRequest.objects.filter( | ||||
Show All 36 Lines | def test_add_archive_to_deposit_is_possible(self): | ||||
# a new one was added | # a new one was added | ||||
self.assertRegex(requests[1].archive.name, self.archive2['name']) | self.assertRegex(requests[1].archive.name, self.archive2['name']) | ||||
# check we did not touch the other parts | # check we did not touch the other parts | ||||
requests = list(DepositRequest.objects.filter( | requests = list(DepositRequest.objects.filter( | ||||
deposit=deposit, type=self.deposit_request_types['metadata'])) | deposit=deposit, type=self.deposit_request_types['metadata'])) | ||||
self.assertEquals(len(requests), 0) | self.assertEquals(len(requests), 0) | ||||
@istest | def test_add_metadata_to_deposit_is_possible(self): | ||||
def add_metadata_to_deposit_is_possible(self): | |||||
"""Add metadata with another one should return a 204 response | """Add metadata with another one should return a 204 response | ||||
""" | """ | ||||
# given | # given | ||||
deposit_id = self.create_deposit_partial() | deposit_id = self.create_deposit_partial() | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
requests = DepositRequest.objects.filter( | requests = DepositRequest.objects.filter( | ||||
Show All 30 Lines | def test_add_metadata_to_deposit_is_possible(self): | ||||
self.assertEquals(len(requests), 0) | self.assertEquals(len(requests), 0) | ||||
class DepositUpdateFailuresTest(APITestCase, WithAuthTestCase, BasicTestCase, | class DepositUpdateFailuresTest(APITestCase, WithAuthTestCase, BasicTestCase, | ||||
CommonCreationRoutine): | CommonCreationRoutine): | ||||
"""Failure scenario about add/replace (post/put) query on deposit. | """Failure scenario about add/replace (post/put) query on deposit. | ||||
""" | """ | ||||
@istest | def test_add_metadata_to_unknown_collection(self): | ||||
def add_metadata_to_unknown_collection(self): | |||||
"""Replacing metadata to unknown deposit should return a 404 response | """Replacing metadata to unknown deposit should return a 404 response | ||||
""" | """ | ||||
url = reverse(EDIT_SE_IRI, args=['test', 1000]) | url = reverse(EDIT_SE_IRI, args=['test', 1000]) | ||||
response = self.client.post( | response = self.client.post( | ||||
url, | url, | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Unknown collection name test') | 'Unknown collection name test') | ||||
@istest | def test_add_metadata_to_unknown_deposit(self): | ||||
def add_metadata_to_unknown_deposit(self): | |||||
"""Replacing metadata to unknown deposit should return a 404 response | """Replacing metadata to unknown deposit should return a 404 response | ||||
""" | """ | ||||
url = reverse(EDIT_SE_IRI, args=[self.collection.name, 999]) | url = reverse(EDIT_SE_IRI, args=[self.collection.name, 999]) | ||||
response = self.client.post( | response = self.client.post( | ||||
url, | url, | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Deposit with id 999 does not exist') | 'Deposit with id 999 does not exist') | ||||
@istest | def test_replace_metadata_to_unknown_deposit(self): | ||||
def replace_metadata_to_unknown_deposit(self): | |||||
"""Adding metadata to unknown deposit should return a 404 response | """Adding metadata to unknown deposit should return a 404 response | ||||
""" | """ | ||||
url = reverse(EDIT_SE_IRI, args=[self.collection.name, 998]) | url = reverse(EDIT_SE_IRI, args=[self.collection.name, 998]) | ||||
response = self.client.put( | response = self.client.put( | ||||
url, | url, | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Deposit with id 998 does not exist') | 'Deposit with id 998 does not exist') | ||||
@istest | def test_add_archive_to_unknown_deposit(self): | ||||
def add_archive_to_unknown_deposit(self): | |||||
"""Adding metadata to unknown deposit should return a 404 response | """Adding metadata to unknown deposit should return a 404 response | ||||
""" | """ | ||||
url = reverse(EM_IRI, args=[self.collection.name, 997]) | url = reverse(EM_IRI, args=[self.collection.name, 997]) | ||||
response = self.client.post( | response = self.client.post( | ||||
url, | url, | ||||
content_type='application/zip', | content_type='application/zip', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Deposit with id 997 does not exist') | 'Deposit with id 997 does not exist') | ||||
@istest | def test_replace_archive_to_unknown_deposit(self): | ||||
def replace_archive_to_unknown_deposit(self): | |||||
"""Replacing archive to unknown deposit should return a 404 response | """Replacing archive to unknown deposit should return a 404 response | ||||
""" | """ | ||||
url = reverse(EM_IRI, args=[self.collection.name, 996]) | url = reverse(EM_IRI, args=[self.collection.name, 996]) | ||||
response = self.client.put( | response = self.client.put( | ||||
url, | url, | ||||
content_type='application/zip', | content_type='application/zip', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Deposit with id 996 does not exist') | 'Deposit with id 996 does not exist') | ||||
@istest | def test_post_metadata_to_em_iri_failure(self): | ||||
def post_metadata_to_em_iri_failure(self): | |||||
"""Update (POST) archive with wrong content type should return 400 | """Update (POST) archive with wrong content type should return 400 | ||||
""" | """ | ||||
deposit_id = self.create_deposit_partial() # only update on partial | deposit_id = self.create_deposit_partial() # only update on partial | ||||
update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) | update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) | ||||
response = self.client.post( | response = self.client.post( | ||||
update_uri, | update_uri, | ||||
content_type='application/x-gtar-compressed', | content_type='application/x-gtar-compressed', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Packaging format supported is restricted to ' | 'Packaging format supported is restricted to ' | ||||
'application/zip, application/x-tar') | 'application/zip, application/x-tar') | ||||
@istest | def test_put_metadata_to_em_iri_failure(self): | ||||
def put_metadata_to_em_iri_failure(self): | |||||
"""Update (PUT) archive with wrong content type should return 400 | """Update (PUT) archive with wrong content type should return 400 | ||||
""" | """ | ||||
# given | # given | ||||
deposit_id = self.create_deposit_partial() # only update on partial | deposit_id = self.create_deposit_partial() # only update on partial | ||||
# when | # when | ||||
update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) | update_uri = reverse(EM_IRI, args=[self.collection.name, deposit_id]) | ||||
response = self.client.put( | response = self.client.put( | ||||
update_uri, | update_uri, | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data0) | data=self.atom_entry_data0) | ||||
# then | # then | ||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||
self.assertRegex(response.content.decode('utf-8'), | self.assertRegex(response.content.decode('utf-8'), | ||||
'Packaging format supported is restricted to ' | 'Packaging format supported is restricted to ' | ||||
'application/zip, application/x-tar') | 'application/zip, application/x-tar') |