Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_origin.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 string | |||||
import swh.web.browse.utils | 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.model.model import Snapshot | from swh.model.model import Snapshot | ||||
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, random_sha1 | ||||
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, origin_with_releases | new_snapshot, visit_dates, revisions, origin_with_releases, | ||||
release as existing_release | |||||
) | ) | ||||
@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 862 Lines • ▼ Show 20 Lines | for release in origin_releases: | ||||
'browse-revision', | 'browse-revision', | ||||
url_args={'sha1_git': release['target']}, | url_args={'sha1_git': release['target']}, | ||||
query_params={'origin': origin_info['url']}) | query_params={'origin': origin_info['url']}) | ||||
assert_contains(resp, '<a href="%s">' % | assert_contains(resp, '<a href="%s">' % | ||||
escape(browse_release_url)) | escape(browse_release_url)) | ||||
assert_contains(resp, '<a href="%s">' % | assert_contains(resp, '<a href="%s">' % | ||||
escape(browse_revision_url)) | escape(browse_revision_url)) | ||||
@given(new_origin(), visit_dates(), revisions(min_size=10, max_size=10), | |||||
existing_release()) | |||||
def test_origin_branches_pagination_with_alias(client, archive_data, mocker, | |||||
new_origin, visit_dates, | |||||
revisions, existing_release): | |||||
""" | |||||
When a snapshot contains a branch or a release alias, pagination links | |||||
in the branches / releases view should be displayed. | |||||
""" | |||||
mocker.patch('swh.web.browse.views.utils.snapshot_context.PER_PAGE', | |||||
len(revisions) / 2) | |||||
snp_dict = {'branches': {}, 'id': hash_to_bytes(random_sha1())} | |||||
for i in range(len(revisions)): | |||||
branch = ''.join(random.choices(string.ascii_lowercase, k=8)) | |||||
snp_dict['branches'][branch.encode()] = { | |||||
'target_type': 'revision', | |||||
'target': hash_to_bytes(revisions[i]), | |||||
} | |||||
release = ''.join(random.choices(string.ascii_lowercase, k=8)) | |||||
snp_dict['branches'][b'RELEASE_ALIAS'] = { | |||||
'target_type': 'alias', | |||||
'target': release.encode() | |||||
} | |||||
snp_dict['branches'][release.encode()] = { | |||||
'target_type': 'release', | |||||
'target': hash_to_bytes(existing_release) | |||||
} | |||||
new_origin = archive_data.origin_add([new_origin])[0] | |||||
archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) | |||||
visit = archive_data.origin_visit_add( | |||||
new_origin['url'], visit_dates[0], type='git') | |||||
archive_data.origin_visit_update(new_origin['url'], visit.visit, | |||||
status='full', | |||||
snapshot=snp_dict['id']) | |||||
url = reverse('browse-origin-branches', | |||||
url_args={'origin_url': new_origin['url']}) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 200 | |||||
assert_template_used(resp, 'browse/branches.html') | |||||
assert_contains(resp, '<ul class="pagination') |