Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_origin.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
import re | import re | ||||
import swh.web.browse.utils | |||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.web.browse.utils import process_snapshot_branches | from swh.web.browse.utils import process_snapshot_branches | ||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, gen_path_info, format_utc_iso_date, | reverse, gen_path_info, format_utc_iso_date, | ||||
parse_timestamp, get_swh_persistent_id | parse_timestamp, get_swh_persistent_id | ||||
) | ) | ||||
from swh.web.tests.data import get_content | from swh.web.tests.data import get_content | ||||
from swh.web.tests.django_asserts import assert_contains, assert_template_used | from swh.web.tests.django_asserts import assert_contains, assert_template_used | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
origin, origin_with_multiple_visits, new_origin, | origin, origin_with_multiple_visits, new_origin, | ||||
new_snapshot, visit_dates, revisions | new_snapshot, visit_dates, revisions, origin_with_releases | ||||
) | ) | ||||
@given(origin_with_multiple_visits()) | @given(origin_with_multiple_visits()) | ||||
def test_origin_visits_browse(client, archive_data, origin): | def test_origin_visits_browse(client, archive_data, origin): | ||||
url = reverse('browse-origin-visits', | url = reverse('browse-origin-visits', | ||||
url_args={'origin_url': origin['url']}) | url_args={'origin_url': origin['url']}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
▲ Show 20 Lines • Show All 369 Lines • ▼ Show 20 Lines | def test_origin_request_errors(client, archive_data, mocker): | ||||
assert re.search('Visit.*not found', resp.content.decode('utf-8')) | assert re.search('Visit.*not found', resp.content.decode('utf-8')) | ||||
mock_get_origin_visits.return_value = [{ | mock_get_origin_visits.return_value = [{ | ||||
'date': '2015-09-26T09:30:52.373449+00:00', | 'date': '2015-09-26T09:30:52.373449+00:00', | ||||
'metadata': {}, | 'metadata': {}, | ||||
'origin': 457, | 'origin': 457, | ||||
'snapshot': 'bdaf9ac436488a8c6cda927a0f44e172934d3f65', | 'snapshot': 'bdaf9ac436488a8c6cda927a0f44e172934d3f65', | ||||
'status': 'full', | 'status': 'full', | ||||
'type': 'git', | |||||
'visit': 1 | 'visit': 1 | ||||
}] | }] | ||||
mock_get_origin_visit_snapshot.side_effect = None | mock_get_origin_visit_snapshot.side_effect = None | ||||
mock_get_origin_visit_snapshot.return_value = ([], []) | mock_get_origin_visit_snapshot.return_value = ([], []) | ||||
mock_utils_service.lookup_snapshot_size.return_value = { | |||||
'revision': 0, | |||||
'release': 0 | |||||
} | |||||
mock_utils_service.lookup_origin.return_value = {'type': 'foo', | |||||
'url': 'bar', | |||||
'id': 457} | |||||
url = reverse('browse-origin-content', | url = reverse('browse-origin-content', | ||||
url_args={'origin_url': 'bar', | url_args={'origin_url': 'bar', | ||||
'path': 'baz'}) | 'path': 'baz'}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 404 | assert resp.status_code == 200 | ||||
assert_template_used('error.html') | assert_template_used('error.html') | ||||
assert re.search('Origin.*has an empty list of branches', | assert re.search('snapshot.*is empty', resp.content.decode('utf-8')) | ||||
resp.content.decode('utf-8')) | |||||
mock_get_origin_visit_snapshot.return_value = ( | mock_get_origin_visit_snapshot.return_value = ( | ||||
[{'directory': 'ae59ceecf46367e8e4ad800e231fc76adc3afffb', | [{'directory': 'ae59ceecf46367e8e4ad800e231fc76adc3afffb', | ||||
'name': 'HEAD', | 'name': 'HEAD', | ||||
'revision': '7bc08e1aa0b08cb23e18715a32aa38517ad34672', | 'revision': '7bc08e1aa0b08cb23e18715a32aa38517ad34672', | ||||
'date': '04 May 2017, 13:27 UTC', | 'date': '04 May 2017, 13:27 UTC', | ||||
'message': ''}], | 'message': ''}], | ||||
[] | [] | ||||
) | ) | ||||
mock_utils_service.lookup_snapshot_size.return_value = { | |||||
'revision': 1, | |||||
'release': 0 | |||||
} | |||||
mock_snapshot_service.lookup_directory_with_path.return_value = { | mock_snapshot_service.lookup_directory_with_path.return_value = { | ||||
'target': '5ecd9f37b7a2d2e9980d201acd6286116f2ba1f1' | 'target': '5ecd9f37b7a2d2e9980d201acd6286116f2ba1f1' | ||||
} | } | ||||
mock_request_content.side_effect = NotFoundExc('Content not found') | mock_request_content.side_effect = NotFoundExc('Content not found') | ||||
url = reverse('browse-origin-content', | url = reverse('browse-origin-content', | ||||
url_args={'origin_url': 'bar', | url_args={'origin_url': 'bar', | ||||
'path': 'baz'}) | 'path': 'baz'}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
Show All 40 Lines | def test_origin_empty_snapshot(client, mocker): | ||||
url = reverse('browse-origin-directory', | url = reverse('browse-origin-directory', | ||||
url_args={'origin_url': 'bar'}) | url_args={'origin_url': 'bar'}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 200 | assert resp.status_code == 200 | ||||
assert_template_used('content.html') | assert_template_used('content.html') | ||||
assert re.search('snapshot.*is empty', resp.content.decode('utf-8')) | assert re.search('snapshot.*is empty', resp.content.decode('utf-8')) | ||||
@given(origin_with_releases()) | |||||
def test_origin_release_browse(client, archive_data, origin): | |||||
# for swh.web.browse.utils.get_snapshot_content to only return one branch | |||||
snapshot_max_size = swh.web.browse.utils.snapshot_content_max_size | |||||
swh.web.browse.utils.snapshot_content_max_size = 1 | |||||
try: | |||||
snapshot = archive_data.snapshot_get_latest(origin['url']) | |||||
release = [b for b in snapshot['branches'].values() | |||||
if b['target_type'] == 'release'][-1] | |||||
release_data = archive_data.release_get(release['target']) | |||||
url = reverse('browse-origin-directory', | |||||
url_args={'origin_url': origin['url']}, | |||||
query_params={'release': release_data['name']}) | |||||
vlorentz: this line should be in a `finally:` clause, to not break other tests if this one crashes. | |||||
Done Inline Actionsack anlambert: ack | |||||
resp = client.get(url) | |||||
assert resp.status_code == 200 | |||||
assert_contains(resp, release_data['name']) | |||||
assert_contains(resp, release['target']) | |||||
finally: | |||||
swh.web.browse.utils.snapshot_content_max_size = snapshot_max_size | |||||
@given(origin_with_releases()) | |||||
def test_origin_release_browse_not_found(client, archive_data, origin): | |||||
invalid_release_name = 'swh-foo-bar' | |||||
url = reverse('browse-origin-directory', | |||||
url_args={'origin_url': origin['url']}, | |||||
query_params={'release': invalid_release_name}) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 404 | |||||
assert re.search(f'Release {invalid_release_name}.*not found', | |||||
resp.content.decode('utf-8')) | |||||
def _origin_content_view_test_helper(client, origin_info, origin_visits, | def _origin_content_view_test_helper(client, origin_info, origin_visits, | ||||
origin_branches, origin_releases, | origin_branches, origin_releases, | ||||
root_dir_sha1, content, | root_dir_sha1, content, | ||||
visit_id=None, timestamp=None): | visit_id=None, timestamp=None): | ||||
content_path = '/'.join(content['path'].split('/')[1:]) | content_path = '/'.join(content['path'].split('/')[1:]) | ||||
url_args = {'origin_url': origin_info['url'], | url_args = {'origin_url': origin_info['url'], | ||||
'path': content_path} | 'path': content_path} | ||||
▲ Show 20 Lines • Show All 357 Lines • Show Last 20 Lines |
this line should be in a finally: clause, to not break other tests if this one crashes.