diff --git a/swh/deposit/api/private/deposit_list.py b/swh/deposit/api/private/deposit_list.py index be54007e..237c5c14 100644 --- a/swh/deposit/api/private/deposit_list.py +++ b/swh/deposit/api/private/deposit_list.py @@ -1,38 +1,53 @@ # Copyright (C) 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 rest_framework.fields import _UnvalidatedField from rest_framework.generics import ListAPIView from rest_framework.pagination import PageNumberPagination from rest_framework import serializers from ..common import SWHPrivateAPIView +from ..deposit_status import convert_status_detail from ...models import Deposit class DefaultPagination(PageNumberPagination): page_size = 100 page_size_query_param = 'page_size' +class StatusDetailField(_UnvalidatedField): + """status_detail field is a dict, we want a simple message instead. + So, we reuse the convert_status_detail from deposit_status + endpoint to that effect. + + """ + def to_representation(self, value): + return convert_status_detail(value) + + class DepositSerializer(serializers.ModelSerializer): + status_detail = StatusDetailField() + class Meta: model = Deposit + # fields = '__all__' fields = ('id', 'reception_date', 'complete_date', 'status', 'collection', 'external_id', 'client', 'swh_id', 'swh_id_context', 'swh_anchor_id', 'swh_anchor_id_context', 'status', 'status_detail', 'parent') class DepositList(ListAPIView, SWHPrivateAPIView): """Deposit request class to list the deposit's status per page. HTTP verbs supported: GET """ queryset = Deposit.objects.all().order_by('id') serializer_class = DepositSerializer pagination_class = DefaultPagination diff --git a/swh/deposit/tests/api/test_deposit_list.py b/swh/deposit/tests/api/test_deposit_list.py index 401f04a5..b0e22431 100644 --- a/swh/deposit/tests/api/test_deposit_list.py +++ b/swh/deposit/tests/api/test_deposit_list.py @@ -1,64 +1,96 @@ # 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.core.urlresolvers import reverse from nose.tools import istest from nose.plugins.attrib import attr from rest_framework import status from rest_framework.test import APITestCase +from swh.deposit.api.deposit_status import convert_status_detail + from ...config import DEPOSIT_STATUS_PARTIAL, PRIVATE_LIST_DEPOSITS from ..common import BasicTestCase, WithAuthTestCase, CommonCreationRoutine +from ...models import Deposit @attr('fs') -class CheckDepositStatusesTest(APITestCase, WithAuthTestCase, - BasicTestCase, CommonCreationRoutine): - """Check deposit statuses endpoints. +class CheckDepositListTest(APITestCase, WithAuthTestCase, + BasicTestCase, CommonCreationRoutine): + """Check deposit list endpoints. """ def setUp(self): super().setUp() @istest def deposit_list(self): """Deposit list api should return the deposits """ deposit_id = self.create_deposit_partial() + # amend the deposit with a status_detail + deposit = Deposit.objects.get(pk=deposit_id) + status_detail = { + 'url': { + 'summary': 'At least one compatible url field. Failed', + 'fields': ['testurl'], + }, + 'metadata': [ + { + 'summary': 'Mandatory fields missing', + 'fields': ['9', 10, 1.212], + }, + ], + 'archive': [ + { + 'summary': 'Invalid archive', + 'fields': ['3'], + }, + { + 'summary': 'Unsupported archive', + 'fields': [2], + } + ], + } + deposit.status_detail = status_detail + deposit.save() + deposit_id2 = self.create_deposit_partial() # NOTE: does not work as documented # https://docs.djangoproject.com/en/1.11/ref/urlresolvers/#django.core.urlresolvers.reverse # noqa # url = reverse(PRIVATE_LIST_DEPOSITS, kwargs={'page_size': 1}) main_url = reverse(PRIVATE_LIST_DEPOSITS) url = '%s?page_size=1' % main_url response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(data['count'], 2) # 2 deposits expected_next = '%s?page=2&page_size=1' % main_url self.assertTrue(data['next'].endswith(expected_next)) self.assertIsNone(data['previous']) self.assertEqual(len(data['results']), 1) # page of size 1 deposit = data['results'][0] self.assertEquals(deposit['id'], deposit_id) self.assertEquals(deposit['status'], DEPOSIT_STATUS_PARTIAL) + expected_status_detail = convert_status_detail(status_detail) + self.assertEquals(deposit['status_detail'], expected_status_detail) # then 2nd page response2 = self.client.get(expected_next) self.assertEqual(response2.status_code, status.HTTP_200_OK) data2 = response2.json() self.assertEqual(data2['count'], 2) # still 2 deposits self.assertIsNone(data2['next']) expected_previous = '%s?page_size=1' % main_url self.assertTrue(data2['previous'].endswith(expected_previous)) self.assertEqual(len(data2['results']), 1) # page of size 1 deposit2 = data2['results'][0] self.assertEquals(deposit2['id'], deposit_id2) self.assertEquals(deposit2['status'], DEPOSIT_STATUS_PARTIAL)