Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_revision.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 | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.web.api.utils import enrich_revision | |||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
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 revision | from swh.web.tests.strategies import revision | ||||
@given(revision()) | @given(revision()) | ||||
def test_api_revision(api_client, archive_data, revision): | def test_api_revision(api_client, archive_data, revision): | ||||
url = reverse('api-1-revision', url_args={'sha1_git': revision}) | url = reverse('api-1-revision', url_args={'sha1_git': revision}) | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
expected_revision = archive_data.revision_get(revision) | expected_revision = archive_data.revision_get(revision) | ||||
_enrich_revision(expected_revision) | enrich_revision(expected_revision, rv.wsgi_request) | ||||
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 == expected_revision | assert rv.data == expected_revision | ||||
def test_api_revision_not_found(api_client): | def test_api_revision_not_found(api_client): | ||||
unknown_revision_ = random_sha1() | unknown_revision_ = random_sha1() | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | def test_api_revision_log(api_client, archive_data, revision): | ||||
per_page = 10 | per_page = 10 | ||||
url = reverse('api-1-revision-log', url_args={'sha1_git': revision}, | url = reverse('api-1-revision-log', url_args={'sha1_git': revision}, | ||||
query_params={'per_page': per_page}) | query_params={'per_page': per_page}) | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
expected_log = archive_data.revision_log(revision, limit=per_page+1) | expected_log = archive_data.revision_log(revision, limit=per_page+1) | ||||
expected_log = list(map(_enrich_revision, expected_log)) | expected_log = list(map(enrich_revision, expected_log, | ||||
[rv.wsgi_request] * len(expected_log))) | |||||
has_next = len(expected_log) > per_page | has_next = len(expected_log) > per_page | ||||
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 == (expected_log[:-1] if has_next else expected_log) | assert rv.data == (expected_log[:-1] if has_next else expected_log) | ||||
if has_next: | if has_next: | ||||
Show All 36 Lines | url = reverse('api-1-revision-log', | ||||
'prev_sha1s': prev_rev}, | 'prev_sha1s': prev_rev}, | ||||
query_params={'per_page': per_page}) | query_params={'per_page': per_page}) | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
expected_log = archive_data.revision_log(rev, limit=per_page) | expected_log = archive_data.revision_log(rev, limit=per_page) | ||||
prev_revision = archive_data.revision_get(prev_rev) | prev_revision = archive_data.revision_get(prev_rev) | ||||
expected_log.insert(0, prev_revision) | expected_log.insert(0, prev_revision) | ||||
expected_log = list(map(_enrich_revision, expected_log)) | expected_log = list(map(enrich_revision, expected_log, | ||||
[rv.wsgi_request] * len(expected_log))) | |||||
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 == expected_log | assert rv.data == expected_log | ||||
def test_api_revision_directory_ko_not_found(api_client, mocker): | def test_api_revision_directory_ko_not_found(api_client, mocker): | ||||
mock_rev_dir = mocker.patch( | mock_rev_dir = mocker.patch( | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | stub_dir = { | ||||
} | } | ||||
] | ] | ||||
} | } | ||||
mock_rev_dir.return_value = stub_dir | mock_rev_dir.return_value = stub_dir | ||||
rv = api_client.get('/api/1/revision/999/directory/some/path/') | rv = api_client.get('/api/1/revision/999/directory/some/path/') | ||||
stub_dir['content'][0]['target_url'] = rv.wsgi_request.build_absolute_uri( | |||||
stub_dir['content'][0]['target_url']) | |||||
stub_dir['content'][0]['file_url'] = rv.wsgi_request.build_absolute_uri( | |||||
stub_dir['content'][0]['file_url']) | |||||
stub_dir['content'][1]['target_url'] = rv.wsgi_request.build_absolute_uri( | |||||
stub_dir['content'][1]['target_url']) | |||||
stub_dir['content'][1]['dir_url'] = rv.wsgi_request.build_absolute_uri( | |||||
stub_dir['content'][1]['dir_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 == stub_dir | assert rv.data == stub_dir | ||||
mock_rev_dir.assert_called_once_with( | mock_rev_dir.assert_called_once_with( | ||||
{'sha1_git': '999'}, | {'sha1_git': '999'}, | ||||
'some/path', | 'some/path', | ||||
'/api/1/revision/999/directory/some/path/', | '/api/1/revision/999/directory/some/path/', | ||||
Show All 14 Lines | stub_content = { | ||||
} | } | ||||
} | } | ||||
mock_rev_dir.return_value = stub_content | mock_rev_dir.return_value = stub_content | ||||
url = '/api/1/revision/666/directory/some/other/path/' | url = '/api/1/revision/666/directory/some/other/path/' | ||||
rv = api_client.get(url) | rv = api_client.get(url) | ||||
stub_content['content']['data_url'] = rv.wsgi_request.build_absolute_uri( | |||||
stub_content['content']['data_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 == stub_content | assert rv.data == stub_content | ||||
mock_rev_dir.assert_called_once_with( | mock_rev_dir.assert_called_once_with( | ||||
{'sha1_git': '666'}, 'some/other/path', url, with_data=False) | {'sha1_git': '666'}, 'some/other/path', url, with_data=False) | ||||
@given(revision()) | @given(revision()) | ||||
def test_api_revision_uppercase(api_client, revision): | def test_api_revision_uppercase(api_client, revision): | ||||
url = reverse('api-1-revision-uppercase-checksum', | url = reverse('api-1-revision-uppercase-checksum', | ||||
url_args={'sha1_git': revision.upper()}) | url_args={'sha1_git': revision.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-revision', | redirect_url = reverse('api-1-revision', | ||||
url_args={'sha1_git': revision}) | url_args={'sha1_git': revision}) | ||||
assert resp['location'] == redirect_url | assert resp['location'] == redirect_url | ||||
def _enrich_revision(revision): | |||||
directory_url = reverse( | |||||
'api-1-directory', | |||||
url_args={'sha1_git': revision['directory']}) | |||||
history_url = reverse('api-1-revision-log', | |||||
url_args={'sha1_git': revision['id']}) | |||||
parents_id_url = [] | |||||
for p in revision['parents']: | |||||
parents_id_url.append({ | |||||
'id': p, | |||||
'url': reverse('api-1-revision', url_args={'sha1_git': p}) | |||||
}) | |||||
revision_url = reverse('api-1-revision', | |||||
url_args={'sha1_git': revision['id']}) | |||||
revision['directory_url'] = directory_url | |||||
revision['history_url'] = history_url | |||||
revision['url'] = revision_url | |||||
revision['parents'] = parents_id_url | |||||
return revision |