Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_person.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 | ||||
import random | |||||
from hypothesis import given | from hypothesis import given | ||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.strategies import person, unknown_person | from swh.web.tests.strategies import person | ||||
from swh.web.tests.testcase import WebTestCase | from swh.web.tests.testcase import WebTestCase | ||||
class PersonApiTestCase(WebTestCase, APITestCase): | class PersonApiTestCase(WebTestCase, APITestCase): | ||||
@given(person()) | @given(person()) | ||||
def test_api_person(self, person): | def test_api_person(self, person): | ||||
url = reverse('api-person', url_args={'person_id': person}) | url = reverse('api-person', url_args={'person_id': person}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
expected_person = self.person_get(person) | expected_person = self.person_get(person) | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | self.assertEqual(rv['Content-Type'], 'application/json') | ||||
self.assertEqual(rv.data, expected_person) | self.assertEqual(rv.data, expected_person) | ||||
@given(unknown_person()) | def test_api_person_not_found(self): | ||||
def test_api_person_not_found(self, unknown_person): | unknown_person_ = random.randint(1000, 10000000) | ||||
url = reverse('api-person', url_args={'person_id': unknown_person}) | url = reverse('api-person', url_args={'person_id': unknown_person_}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 404) | self.assertEqual(rv.status_code, 404) | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | self.assertEqual(rv['Content-Type'], 'application/json') | ||||
self.assertEqual(rv.data, { | self.assertEqual(rv.data, { | ||||
'exception': 'NotFoundExc', | 'exception': 'NotFoundExc', | ||||
'reason': 'Person with id %s not found' % unknown_person}) | 'reason': 'Person with id %s not found' % unknown_person_}) |