Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_directory.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 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 swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.data import random_sha1 | from swh.web.tests.data import random_sha1 | ||||
from swh.web.tests.strategies import directory | from swh.web.tests.strategies import directory | ||||
from swh.web.tests.testcase import WebTestCase | |||||
class DirectoryApiTestCase(WebTestCase, APITestCase): | |||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory(self, directory): | def test_api_directory(api_client, test_archive, directory): | ||||
url = reverse('api-1-directory', url_args={'sha1_git': directory}) | url = reverse('api-1-directory', url_args={'sha1_git': directory}) | ||||
rv = self.client.get(url) | rv = api_client.get(url) | ||||
assert rv.status_code == 200, rv.data | |||||
assert rv['Content-Type'] == 'application/json' | |||||
self.assertEqual(rv.status_code, 200, rv.data) | expected_data = list(map(_enrich_dir_data, | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | test_archive.directory_ls(directory))) | ||||
expected_data = list(map(self._enrich_dir_data, | assert rv.data == expected_data | ||||
self.directory_ls(directory))) | |||||
self.assertEqual(rv.data, expected_data) | |||||
def test_api_directory_not_found(self): | def test_api_directory_not_found(api_client): | ||||
unknown_directory_ = random_sha1() | unknown_directory_ = random_sha1() | ||||
url = reverse('api-1-directory', | url = reverse('api-1-directory', | ||||
url_args={'sha1_git': unknown_directory_}) | url_args={'sha1_git': unknown_directory_}) | ||||
rv = self.client.get(url) | rv = api_client.get(url) | ||||
self.assertEqual(rv.status_code, 404, rv.data) | assert rv.status_code == 404, rv.data | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | assert rv['Content-Type'] == 'application/json' | ||||
self.assertEqual(rv.data, { | assert 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(api_client, test_archive, directory): | ||||
directory_content = self.directory_ls(directory) | directory_content = test_archive.directory_ls(directory) | ||||
path = random.choice(directory_content) | path = random.choice(directory_content) | ||||
url = reverse('api-1-directory', | url = reverse('api-1-directory', | ||||
url_args={'sha1_git': directory, | url_args={'sha1_git': directory, 'path': path['name']}) | ||||
'path': path['name']}) | rv = api_client.get(url) | ||||
rv = self.client.get(url) | |||||
assert rv.status_code == 200, rv.data | |||||
self.assertEqual(rv.status_code, 200, rv.data) | assert rv['Content-Type'] == 'application/json' | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | assert rv.data == _enrich_dir_data(path) | ||||
self.assertEqual(rv.data, self._enrich_dir_data(path)) | |||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory_with_path_not_found(self, directory): | def test_api_directory_with_path_not_found(api_client, directory): | ||||
path = 'some/path/to/nonexistent/dir/' | path = 'some/path/to/nonexistent/dir/' | ||||
url = reverse('api-1-directory', | url = reverse('api-1-directory', | ||||
url_args={'sha1_git': directory, | url_args={'sha1_git': directory, 'path': path}) | ||||
'path': path}) | rv = api_client.get(url) | ||||
rv = self.client.get(url) | |||||
assert rv.status_code == 404, rv.data | |||||
self.assertEqual(rv.status_code, 404, rv.data) | assert rv['Content-Type'] == 'application/json' | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | assert rv.data == { | ||||
self.assertEqual(rv.data, { | |||||
'exception': 'NotFoundExc', | 'exception': 'NotFoundExc', | ||||
'reason': ('Directory entry with path %s from %s not found' | 'reason': ('Directory entry with path %s from %s not found' % | ||||
% (path, directory))}) | (path, directory)) | ||||
} | |||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory_uppercase(self, directory): | def test_api_directory_uppercase(api_client, directory): | ||||
url = reverse('api-1-directory-uppercase-checksum', | url = reverse('api-1-directory-uppercase-checksum', | ||||
url_args={'sha1_git': directory.upper()}) | url_args={'sha1_git': directory.upper()}) | ||||
resp = self.client.get(url) | resp = api_client.get(url) | ||||
self.assertEqual(resp.status_code, 302) | assert resp.status_code == 302 | ||||
redirect_url = reverse('api-1-directory', | redirect_url = reverse('api-1-directory', url_args={'sha1_git': directory}) | ||||
url_args={'sha1_git': directory}) | |||||
self.assertEqual(resp['location'], redirect_url) | assert resp['location'] == redirect_url | ||||
@classmethod | |||||
def _enrich_dir_data(cls, dir_data): | def _enrich_dir_data(dir_data): | ||||
if dir_data['type'] == 'file': | if dir_data['type'] == 'file': | ||||
dir_data['target_url'] = \ | dir_data['target_url'] = reverse( | ||||
reverse('api-1-content', | 'api-1-content', | ||||
url_args={'q': 'sha1_git:%s' % dir_data['target']}) | url_args={'q': 'sha1_git:%s' % dir_data['target']}) | ||||
elif dir_data['type'] == 'dir': | elif dir_data['type'] == 'dir': | ||||
dir_data['target_url'] = \ | dir_data['target_url'] = reverse( | ||||
reverse('api-1-directory', | 'api-1-directory', | ||||
url_args={'sha1_git': dir_data['target']}) | url_args={'sha1_git': dir_data['target']}) | ||||
elif dir_data['type'] == 'rev': | elif dir_data['type'] == 'rev': | ||||
dir_data['target_url'] = \ | dir_data['target_url'] = reverse( | ||||
reverse('api-1-revision', | 'api-1-revision', | ||||
url_args={'sha1_git': dir_data['target']}) | url_args={'sha1_git': dir_data['target']}) | ||||
return dir_data | return dir_data |