Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_deposit_atom.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 io import BytesIO | from io import BytesIO | ||||
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.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED | from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED | ||||
from swh.deposit.models import Deposit, DepositRequest | from swh.deposit.models import Deposit, DepositRequest | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from ..common import BasicTestCase, WithAuthTestCase | from ..common import BasicTestCase, WithAuthTestCase | ||||
▲ Show 20 Lines • Show All 229 Lines • ▼ Show 20 Lines | <entry xmlns="http://www.w3.org/2005/Atom" xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0"> | ||||
<contributor> | <contributor> | ||||
<name>Someone Nice</name> | <name>Someone Nice</name> | ||||
<email>someone@nice.fr</email> | <email>someone@nice.fr</email> | ||||
<codemeta:affiliation>FFJ</codemeta:affiliation> | <codemeta:affiliation>FFJ</codemeta:affiliation> | ||||
</contributor> | </contributor> | ||||
</entry> | </entry> | ||||
""" # noqa | """ # noqa | ||||
@istest | def test_post_deposit_atom_entry_serialization_error(self): | ||||
def post_deposit_atom_entry_serialization_error(self): | |||||
"""Posting an initial atom entry should return 201 with deposit receipt | """Posting an initial atom entry should return 201 with deposit receipt | ||||
""" | """ | ||||
# given | # given | ||||
# when | # when | ||||
response = self.client.post( | response = self.client.post( | ||||
reverse(COL_IRI, args=[self.collection.name]), | reverse(COL_IRI, args=[self.collection.name]), | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
Show All 9 Lines | def test_post_deposit_atom_entry_serialization_error(self): | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
dr = DepositRequest.objects.get(deposit=deposit) | dr = DepositRequest.objects.get(deposit=deposit) | ||||
self.assertIsNotNone(dr.metadata) | self.assertIsNotNone(dr.metadata) | ||||
sw_version = dr.metadata.get('codemeta:softwareVersion') | sw_version = dr.metadata.get('codemeta:softwareVersion') | ||||
self.assertEquals(sw_version, '10.4') | self.assertEquals(sw_version, '10.4') | ||||
@istest | def test_post_deposit_atom_empty_body_request(self): | ||||
def post_deposit_atom_empty_body_request(self): | |||||
"""Posting empty body request should return a 400 response | """Posting empty body request should return a 400 response | ||||
""" | """ | ||||
response = self.client.post( | response = self.client.post( | ||||
reverse(COL_IRI, args=[self.collection.name]), | reverse(COL_IRI, args=[self.collection.name]), | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data_empty_body) | data=self.atom_entry_data_empty_body) | ||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||
@istest | def test_post_deposit_atom_badly_formatted_is_a_bad_request(self): | ||||
def post_deposit_atom_badly_formatted_is_a_bad_request(self): | |||||
"""Posting a badly formatted atom should return a 400 response | """Posting a badly formatted atom should return a 400 response | ||||
""" | """ | ||||
response = self.client.post( | response = self.client.post( | ||||
reverse(COL_IRI, args=[self.collection.name]), | reverse(COL_IRI, args=[self.collection.name]), | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data_badly_formatted) | data=self.atom_entry_data_badly_formatted) | ||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||
@istest | def test_post_deposit_atom_without_slug_header_is_bad_request(self): | ||||
def post_deposit_atom_without_slug_header_is_bad_request(self): | |||||
"""Posting an atom entry without a slug header should return a 400 | """Posting an atom entry without a slug header should return a 400 | ||||
""" | """ | ||||
url = reverse(COL_IRI, args=[self.collection.name]) | url = reverse(COL_IRI, args=[self.collection.name]) | ||||
# when | # when | ||||
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, | ||||
# + headers | # + headers | ||||
HTTP_IN_PROGRESS='false') | HTTP_IN_PROGRESS='false') | ||||
self.assertIn(b'Missing SLUG header', response.content) | self.assertIn(b'Missing SLUG header', response.content) | ||||
self.assertEqual(response.status_code, | self.assertEqual(response.status_code, | ||||
status.HTTP_400_BAD_REQUEST) | status.HTTP_400_BAD_REQUEST) | ||||
@istest | def test_post_deposit_atom_unknown_collection(self): | ||||
def post_deposit_atom_unknown_collection(self): | |||||
"""Posting an atom entry to an unknown collection should return a 404 | """Posting an atom entry to an unknown collection should return a 404 | ||||
""" | """ | ||||
response = self.client.post( | response = self.client.post( | ||||
reverse(COL_IRI, args=['unknown-one']), | reverse(COL_IRI, args=['unknown-one']), | ||||
content_type='application/atom+xml;type=entry', | content_type='application/atom+xml;type=entry', | ||||
data=self.atom_entry_data3, | data=self.atom_entry_data3, | ||||
HTTP_SLUG='something') | HTTP_SLUG='something') | ||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||||
@istest | def test_post_deposit_atom_entry_initial(self): | ||||
def post_deposit_atom_entry_initial(self): | |||||
"""Posting an initial atom entry should return 201 with deposit receipt | """Posting an initial atom entry should return 201 with deposit receipt | ||||
""" | """ | ||||
# given | # given | ||||
external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | ||||
with self.assertRaises(Deposit.DoesNotExist): | with self.assertRaises(Deposit.DoesNotExist): | ||||
Deposit.objects.get(external_id=external_id) | Deposit.objects.get(external_id=external_id) | ||||
Show All 22 Lines | def test_post_deposit_atom_entry_initial(self): | ||||
# one associated request to a deposit | # one associated request to a deposit | ||||
deposit_request = DepositRequest.objects.get(deposit=deposit) | deposit_request = DepositRequest.objects.get(deposit=deposit) | ||||
self.assertIsNotNone(deposit_request.metadata) | self.assertIsNotNone(deposit_request.metadata) | ||||
self.assertEquals( | self.assertEquals( | ||||
deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | ||||
self.assertFalse(bool(deposit_request.archive)) | self.assertFalse(bool(deposit_request.archive)) | ||||
@istest | def test_post_deposit_atom_entry_with_codemeta(self): | ||||
def post_deposit_atom_entry_with_codemeta(self): | |||||
"""Posting an initial atom entry should return 201 with deposit receipt | """Posting an initial atom entry should return 201 with deposit receipt | ||||
""" | """ | ||||
# given | # given | ||||
external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | ||||
with self.assertRaises(Deposit.DoesNotExist): | with self.assertRaises(Deposit.DoesNotExist): | ||||
Deposit.objects.get(external_id=external_id) | Deposit.objects.get(external_id=external_id) | ||||
Show All 25 Lines | def test_post_deposit_atom_entry_with_codemeta(self): | ||||
# one associated request to a deposit | # one associated request to a deposit | ||||
deposit_request = DepositRequest.objects.get(deposit=deposit) | deposit_request = DepositRequest.objects.get(deposit=deposit) | ||||
self.assertIsNotNone(deposit_request.metadata) | self.assertIsNotNone(deposit_request.metadata) | ||||
self.assertEquals( | self.assertEquals( | ||||
deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | ||||
self.assertFalse(bool(deposit_request.archive)) | self.assertFalse(bool(deposit_request.archive)) | ||||
@istest | |||||
def test_post_deposit_atom_entry_tei(self): | def test_post_deposit_atom_entry_tei(self): | ||||
"""Posting initial atom entry as TEI should return 201 with receipt | """Posting initial atom entry as TEI should return 201 with receipt | ||||
""" | """ | ||||
# given | # given | ||||
external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | external_id = 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a' | ||||
with self.assertRaises(Deposit.DoesNotExist): | with self.assertRaises(Deposit.DoesNotExist): | ||||
Deposit.objects.get(external_id=external_id) | Deposit.objects.get(external_id=external_id) | ||||
Show All 22 Lines | def test_post_deposit_atom_entry_tei(self): | ||||
# one associated request to a deposit | # one associated request to a deposit | ||||
deposit_request = DepositRequest.objects.get(deposit=deposit) | deposit_request = DepositRequest.objects.get(deposit=deposit) | ||||
self.assertIsNotNone(deposit_request.metadata) | self.assertIsNotNone(deposit_request.metadata) | ||||
self.assertEquals( | self.assertEquals( | ||||
deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | deposit_request.raw_metadata, atom_entry_data.decode('utf-8')) | ||||
self.assertFalse(bool(deposit_request.archive)) | self.assertFalse(bool(deposit_request.archive)) | ||||
@istest | def test_post_deposit_atom_entry_multiple_steps(self): | ||||
def post_deposit_atom_entry_multiple_steps(self): | |||||
"""After initial deposit, updating a deposit should return a 201 | """After initial deposit, updating a deposit should return a 201 | ||||
""" | """ | ||||
# given | # given | ||||
external_id = 'urn:uuid:2225c695-cfb8-4ebb-aaaa-80da344efa6a' | external_id = 'urn:uuid:2225c695-cfb8-4ebb-aaaa-80da344efa6a' | ||||
with self.assertRaises(Deposit.DoesNotExist): | with self.assertRaises(Deposit.DoesNotExist): | ||||
deposit = Deposit.objects.get(external_id=external_id) | deposit = Deposit.objects.get(external_id=external_id) | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |