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 | 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, origin_with_releases | new_snapshot, visit_dates, revisions, origin_with_releases, | ||||
origin_with_directory_named_directory | |||||
) | ) | ||||
@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 495 Lines • ▼ Show 20 Lines | url = reverse('browse-origin-directory', | ||||
query_params={'release': invalid_release_name}) | query_params={'release': invalid_release_name}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 404 | assert resp.status_code == 404 | ||||
assert re.search(f'Release {invalid_release_name}.*not found', | assert re.search(f'Release {invalid_release_name}.*not found', | ||||
resp.content.decode('utf-8')) | resp.content.decode('utf-8')) | ||||
@given(origin_with_directory_named_directory()) | |||||
def test_origin_browse_directory_named_directory(client, origin_and_path): | |||||
url = reverse('browse-origin-directory', | |||||
url_args={'origin_url': origin_and_path['origin'], | |||||
'path': origin_and_path['path']}) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 200 | |||||
assert_template_used('directory.html') | |||||
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} | ||||
if not visit_id: | if not visit_id: | ||||
visit_id = origin_visits[-1]['visit'] | visit_id = origin_visits[-1]['visit'] | ||||
query_params = {} | query_params = {} | ||||
if timestamp: | if timestamp: | ||||
url_args['timestamp'] = timestamp | url_args['timestamp'] = timestamp | ||||
if visit_id: | if visit_id: | ||||
query_params['visit_id'] = visit_id | query_params['visit_id'] = visit_id | ||||
url = reverse('browse-origin-content', | url = reverse('browse-origin-content', | ||||
url_args=url_args, | url_args=url_args, | ||||
query_params=query_params) | query_params=query_params) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 200 | assert resp.status_code == 200 | ||||
assert_template_used(resp, 'browse/content.html') | assert_template_used(resp, 'browse/content.html') | ||||
assert_contains(resp, '<code class="%s">' % | assert_contains(resp, '<code class="%s">' % | ||||
content['hljs_language']) | content['hljs_language']) | ||||
assert_contains(resp, escape(content['data'])) | assert_contains(resp, escape(content['data'])) | ||||
split_path = content_path.split('/') | split_path = content_path.split('/') | ||||
filename = split_path[-1] | filename = split_path[-1] | ||||
path = content_path.replace(filename, '')[:-1] | path = content_path.replace(filename, '')[:-1] | ||||
path_info = gen_path_info(path) | path_info = gen_path_info(path) | ||||
del url_args['path'] | del url_args['path'] | ||||
if timestamp: | if timestamp: | ||||
url_args['timestamp'] = format_utc_iso_date( | url_args['timestamp'] = format_utc_iso_date( | ||||
parse_timestamp(timestamp).isoformat(), '%Y-%m-%dT%H:%M:%S') | parse_timestamp(timestamp).isoformat(), '%Y-%m-%dT%H:%M:%S') | ||||
root_dir_url = reverse('browse-origin-directory', | root_dir_url = reverse('browse-origin-directory', | ||||
url_args=url_args, | url_args=url_args, | ||||
query_params=query_params) | query_params=query_params) | ||||
assert_contains(resp, '<li class="swh-path">', | assert_contains(resp, '<li class="swh-path">', | ||||
vlorentz: Shouldn't test data be out of the tests now? | |||||
Done Inline ActionsIndeed, I could move the data creation inside an hypothesis strategy. I should also use the class from swh.model.model instead of raw dicts. anlambert: Indeed, I could move the data creation inside an hypothesis strategy. I should also use the… | |||||
count=len(path_info)+1) | count=len(path_info)+1) | ||||
assert_contains(resp, '<a href="%s">%s</a>' % | assert_contains(resp, '<a href="%s">%s</a>' % | ||||
(root_dir_url, root_dir_sha1[:7])) | (root_dir_url, root_dir_sha1[:7])) | ||||
for p in path_info: | for p in path_info: | ||||
url_args['path'] = p['path'] | url_args['path'] = p['path'] | ||||
dir_url = reverse('browse-origin-directory', | dir_url = reverse('browse-origin-directory', | ||||
▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Lines | for release in origin_releases: | ||||
url_args=url_args, | url_args=url_args, | ||||
query_params=query_params) | query_params=query_params) | ||||
assert_contains(resp, '<a href="%s">' % root_dir_release_url) | assert_contains(resp, '<a href="%s">' % root_dir_release_url) | ||||
assert_contains(resp, 'vault-cook-directory') | assert_contains(resp, 'vault-cook-directory') | ||||
assert_contains(resp, 'vault-cook-revision') | assert_contains(resp, 'vault-cook-revision') | ||||
swh_dir_id = get_swh_persistent_id('directory', directory_entries[0]['dir_id']) # noqa | if directory_entries: | ||||
swh_dir_id = get_swh_persistent_id('directory', | |||||
directory_entries[0]['dir_id']) | |||||
swh_dir_id_url = reverse('browse-swh-id', | swh_dir_id_url = reverse('browse-swh-id', | ||||
url_args={'swh_id': swh_dir_id}) | url_args={'swh_id': swh_dir_id}) | ||||
assert_contains(resp, swh_dir_id) | assert_contains(resp, swh_dir_id) | ||||
assert_contains(resp, swh_dir_id_url) | assert_contains(resp, swh_dir_id_url) | ||||
assert_contains(resp, 'swh-take-new-snapshot') | assert_contains(resp, 'swh-take-new-snapshot') | ||||
def _origin_branches_test_helper(client, origin_info, origin_snapshot): | def _origin_branches_test_helper(client, origin_info, origin_snapshot): | ||||
url_args = {'origin_url': origin_info['url']} | url_args = {'origin_url': origin_info['url']} | ||||
url = reverse('browse-origin-branches', | url = reverse('browse-origin-branches', | ||||
▲ Show 20 Lines • Show All 87 Lines • Show Last 20 Lines |
Shouldn't test data be out of the tests now?