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 swh.web.api.utils import enrich_directory | |||||
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 | ||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory(api_client, archive_data, directory): | def test_api_directory(api_client, archive_data, directory): | ||||
url = reverse('api-1-directory', url_args={'sha1_git': directory}) | url = reverse('api-1-directory', url_args={'sha1_git': directory}) | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
assert rv.status_code == 200, rv.data | assert rv.status_code == 200, rv.data | ||||
assert rv['Content-Type'] == 'application/json' | assert rv['Content-Type'] == 'application/json' | ||||
expected_data = list(map(_enrich_dir_data, | dir_content = list(archive_data.directory_ls(directory)) | ||||
archive_data.directory_ls(directory))) | expected_data = list(map(enrich_directory, | ||||
dir_content, | |||||
[rv.wsgi_request] * len(dir_content))) | |||||
assert rv.data == expected_data | assert rv.data == expected_data | ||||
def test_api_directory_not_found(api_client): | 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', | ||||
Show All 15 Lines | def test_api_directory_with_path_found(api_client, archive_data, 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, 'path': path['name']}) | url_args={'sha1_git': directory, 'path': path['name']}) | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
assert rv.status_code == 200, rv.data | assert rv.status_code == 200, rv.data | ||||
assert rv['Content-Type'] == 'application/json' | assert rv['Content-Type'] == 'application/json' | ||||
assert rv.data == _enrich_dir_data(path) | assert rv.data == enrich_directory(path, rv.wsgi_request) | ||||
@given(directory()) | @given(directory()) | ||||
def test_api_directory_with_path_not_found(api_client, 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, 'path': path}) | url_args={'sha1_git': directory, 'path': path}) | ||||
Show All 14 Lines | url = reverse('api-1-directory-uppercase-checksum', | ||||
url_args={'sha1_git': directory.upper()}) | url_args={'sha1_git': directory.upper()}) | ||||
resp = api_client.get(url) | resp = api_client.get(url) | ||||
assert resp.status_code == 302 | assert resp.status_code == 302 | ||||
redirect_url = reverse('api-1-directory', url_args={'sha1_git': directory}) | redirect_url = reverse('api-1-directory', url_args={'sha1_git': directory}) | ||||
assert resp['location'] == redirect_url | assert resp['location'] == redirect_url | ||||
def _enrich_dir_data(dir_data): | |||||
if dir_data['type'] == 'file': | |||||
dir_data['target_url'] = reverse( | |||||
'api-1-content', | |||||
url_args={'q': 'sha1_git:%s' % dir_data['target']}) | |||||
elif dir_data['type'] == 'dir': | |||||
dir_data['target_url'] = reverse( | |||||
'api-1-directory', | |||||
url_args={'sha1_git': dir_data['target']}) | |||||
elif dir_data['type'] == 'rev': | |||||
dir_data['target_url'] = reverse( | |||||
'api-1-revision', | |||||
url_args={'sha1_git': dir_data['target']}) | |||||
return dir_data |