Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_directory.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 | 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 directory, unknown_directory | from swh.web.tests.data import random_sha1 | ||||
from swh.web.tests.strategies import directory | |||||
from swh.web.tests.testcase import WebTestCase | from swh.web.tests.testcase import WebTestCase | ||||
class DirectoryApiTestCase(WebTestCase, APITestCase): | class DirectoryApiTestCase(WebTestCase, APITestCase): | ||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory(self, directory): | def test_api_directory(self, directory): | ||||
url = reverse('api-directory', url_args={'sha1_git': directory}) | url = reverse('api-directory', url_args={'sha1_git': directory}) | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
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') | ||||
expected_data = list(map(self._enrich_dir_data, | expected_data = list(map(self._enrich_dir_data, | ||||
self.directory_ls(directory))) | self.directory_ls(directory))) | ||||
self.assertEqual(rv.data, expected_data) | self.assertEqual(rv.data, expected_data) | ||||
@given(unknown_directory()) | def test_api_directory_not_found(self): | ||||
def test_api_directory_not_found(self, unknown_directory): | unknown_directory_ = random_sha1() | ||||
url = reverse('api-directory', | url = reverse('api-directory', | ||||
url_args={'sha1_git': unknown_directory}) | url_args={'sha1_git': unknown_directory_}) | ||||
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': 'Directory with sha1_git %s not found' | 'reason': 'Directory with sha1_git %s not found' | ||||
% unknown_directory}) | % unknown_directory_}) | ||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory_with_path_found(self, directory): | def test_api_directory_with_path_found(self, directory): | ||||
directory_content = self.directory_ls(directory) | directory_content = self.directory_ls(directory) | ||||
path = random.choice(directory_content) | path = random.choice(directory_content) | ||||
url = reverse('api-directory', | url = reverse('api-directory', | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |