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 string | ||||
import textwrap | |||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.storage.utils import now | from swh.storage.utils import now | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | def test_origin_visits_browse(client, archive_data, origin): | ||||
for v in visits: | for v in visits: | ||||
vdate = format_utc_iso_date(v["date"], "%Y-%m-%dT%H:%M:%SZ") | vdate = format_utc_iso_date(v["date"], "%Y-%m-%dT%H:%M:%SZ") | ||||
browse_dir_url = reverse( | browse_dir_url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin["url"], "timestamp": vdate}, | query_params={"origin_url": origin["url"], "timestamp": vdate}, | ||||
) | ) | ||||
assert_contains(resp, browse_dir_url) | assert_contains(resp, browse_dir_url) | ||||
_check_origin_view_title(resp, origin["url"], "visits") | _check_origin_link(resp, origin["url"]) | ||||
@given(origin_with_multiple_visits()) | @given(origin_with_multiple_visits()) | ||||
def test_origin_content_view(client, archive_data, origin): | def test_origin_content_view(client, archive_data, origin): | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | origin_visits = archive_data.origin_visit_get(origin["url"]) | ||||
def _get_archive_data(visit_idx): | def _get_archive_data(visit_idx): | ||||
snapshot = archive_data.snapshot_get(origin_visits[visit_idx]["snapshot"]) | snapshot = archive_data.snapshot_get(origin_visits[visit_idx]["snapshot"]) | ||||
▲ Show 20 Lines • Show All 867 Lines • ▼ Show 20 Lines | ): | ||||
) | ) | ||||
assert_contains(resp, url_raw) | assert_contains(resp, url_raw) | ||||
if "path" in query_params: | if "path" in query_params: | ||||
del query_params["path"] | del query_params["path"] | ||||
origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_branches_url)}"') | ||||
resp, | assert_contains(resp, f"Branches ({len(origin_branches)})") | ||||
'<a href="%s">Branches (%s)</a>' | |||||
% (escape(origin_branches_url), len(origin_branches)), | |||||
) | |||||
origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_releases_url)}">') | ||||
resp, | assert_contains(resp, f"Releases ({len(origin_releases)})") | ||||
'<a href="%s">Releases (%s)</a>' | |||||
% (escape(origin_releases_url), len(origin_releases)), | |||||
) | |||||
assert_contains(resp, '<li class="swh-branch">', count=len(origin_branches)) | assert_contains(resp, '<li class="swh-branch">', count=len(origin_branches)) | ||||
query_params["path"] = content_path | query_params["path"] = content_path | ||||
for branch in origin_branches: | for branch in origin_branches: | ||||
root_dir_branch_url = reverse( | root_dir_branch_url = reverse( | ||||
"browse-origin-content", | "browse-origin-content", | ||||
Show All 33 Lines | swh_cnt_id = get_swh_persistent_id( | ||||
CONTENT, content["sha1_git"], metadata=swhid_context | CONTENT, content["sha1_git"], metadata=swhid_context | ||||
) | ) | ||||
swh_cnt_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_cnt_id}) | swh_cnt_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_cnt_id}) | ||||
assert_contains(resp, swh_cnt_id) | assert_contains(resp, swh_cnt_id) | ||||
assert_contains(resp, swh_cnt_id_url) | assert_contains(resp, swh_cnt_id_url) | ||||
assert_contains(resp, "swh-take-new-snapshot") | assert_contains(resp, "swh-take-new-snapshot") | ||||
_check_origin_view_title(resp, origin_info["url"], "content") | _check_origin_link(resp, origin_info["url"]) | ||||
def _origin_directory_view_test_helper( | def _origin_directory_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin_info, | origin_info, | ||||
origin_visit, | origin_visit, | ||||
origin_branches, | origin_branches, | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | ): | ||||
assert_contains(resp, '<li class="swh-path">', count=nb_bc_paths) | assert_contains(resp, '<li class="swh-path">', count=nb_bc_paths) | ||||
assert_contains( | assert_contains( | ||||
resp, '<a href="%s">%s</a>' % (root_dir_branch_url, root_directory_sha1[:7]) | resp, '<a href="%s">%s</a>' % (root_dir_branch_url, root_directory_sha1[:7]) | ||||
) | ) | ||||
origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_branches_url)}"') | ||||
resp, | assert_contains(resp, f"Branches ({len(origin_branches)})") | ||||
'<a href="%s">Branches (%s)</a>' | |||||
% (escape(origin_branches_url), len(origin_branches)), | |||||
) | |||||
origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | ||||
nb_releases = len(origin_releases) | nb_releases = len(origin_releases) | ||||
if nb_releases > 0: | if nb_releases > 0: | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_releases_url)}"') | ||||
resp, | assert_contains(resp, f"Releases ({nb_releases})") | ||||
'<a href="%s">Releases (%s)</a>' | |||||
% (escape(origin_releases_url), nb_releases), | |||||
) | |||||
if path: | if path: | ||||
query_params["path"] = path | query_params["path"] = path | ||||
assert_contains(resp, '<li class="swh-branch">', count=len(origin_branches)) | assert_contains(resp, '<li class="swh-branch">', count=len(origin_branches)) | ||||
for branch in origin_branches: | for branch in origin_branches: | ||||
query_params["branch"] = branch["name"] | query_params["branch"] = branch["name"] | ||||
root_dir_branch_url = reverse( | root_dir_branch_url = reverse( | ||||
"browse-origin-directory", query_params=query_params | "browse-origin-directory", query_params=query_params | ||||
) | ) | ||||
assert_contains(resp, '<a href="%s">' % root_dir_branch_url) | assert_contains(resp, '<a href="%s">' % root_dir_branch_url) | ||||
assert_contains(resp, '<li class="swh-release">', count=len(origin_releases)) | assert_contains(resp, '<li class="swh-release">', count=len(origin_releases)) | ||||
query_params["branch"] = None | query_params["branch"] = None | ||||
for release in origin_releases: | for release in origin_releases: | ||||
query_params["release"] = release["name"] | query_params["release"] = release["name"] | ||||
root_dir_release_url = reverse( | root_dir_release_url = reverse( | ||||
"browse-origin-directory", query_params=query_params | "browse-origin-directory", query_params=query_params | ||||
) | ) | ||||
assert_contains(resp, '<a href="%s">' % root_dir_release_url) | assert_contains(resp, '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") | ||||
snapshot = archive_data.snapshot_get(origin_visit["snapshot"]) | snapshot = archive_data.snapshot_get(origin_visit["snapshot"]) | ||||
head_rev_id = archive_data.snapshot_get_head(snapshot) | head_rev_id = archive_data.snapshot_get_head(snapshot) | ||||
swhid_context = { | swhid_context = { | ||||
"origin": origin_info["url"], | "origin": origin_info["url"], | ||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot["id"]), | "visit": get_swh_persistent_id(SNAPSHOT, snapshot["id"]), | ||||
"anchor": get_swh_persistent_id(REVISION, head_rev_id), | "anchor": get_swh_persistent_id(REVISION, head_rev_id), | ||||
"path": f"/{path}" if path else "/", | "path": f"/{path}" if path else "/", | ||||
} | } | ||||
swh_dir_id = get_swh_persistent_id( | swh_dir_id = get_swh_persistent_id( | ||||
DIRECTORY, directory_entries[0]["dir_id"], metadata=swhid_context | DIRECTORY, directory_entries[0]["dir_id"], metadata=swhid_context | ||||
) | ) | ||||
swh_dir_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_dir_id}) | swh_dir_id_url = reverse("browse-swh-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") | ||||
_check_origin_view_title(resp, origin_info["url"], "directory") | _check_origin_link(resp, origin_info["url"]) | ||||
def _origin_branches_test_helper( | def _origin_branches_test_helper( | ||||
client, origin_info, origin_snapshot, snapshot_id=None | client, origin_info, origin_snapshot, snapshot_id=None | ||||
): | ): | ||||
query_params = {"origin_url": origin_info["url"], "snapshot": snapshot_id} | query_params = {"origin_url": origin_info["url"], "snapshot": snapshot_id} | ||||
url = reverse("browse-origin-branches", query_params=query_params) | url = reverse("browse-origin-branches", 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/branches.html") | assert_template_used(resp, "browse/branches.html") | ||||
origin_branches = origin_snapshot[0] | origin_branches = origin_snapshot[0] | ||||
origin_releases = origin_snapshot[1] | origin_releases = origin_snapshot[1] | ||||
origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_branches_url)}"') | ||||
resp, | assert_contains(resp, f"Branches ({len(origin_branches)})") | ||||
'<a href="%s">Branches (%s)</a>' | |||||
% (escape(origin_branches_url), len(origin_branches)), | |||||
) | |||||
origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | ||||
nb_releases = len(origin_releases) | nb_releases = len(origin_releases) | ||||
if nb_releases > 0: | if nb_releases > 0: | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_releases_url)}">') | ||||
resp, | assert_contains(resp, f"Releases ({nb_releases})") | ||||
'<a href="%s">Releases (%s)</a>' | |||||
% (escape(origin_releases_url), nb_releases), | |||||
) | |||||
assert_contains(resp, '<tr class="swh-branch-entry', count=len(origin_branches)) | assert_contains(resp, '<tr class="swh-branch-entry', count=len(origin_branches)) | ||||
for branch in origin_branches: | for branch in origin_branches: | ||||
browse_branch_url = reverse( | browse_branch_url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"branch": branch["name"], **query_params}, | query_params={"branch": branch["name"], **query_params}, | ||||
) | ) | ||||
assert_contains(resp, '<a href="%s">' % escape(browse_branch_url)) | assert_contains(resp, '<a href="%s">' % escape(browse_branch_url)) | ||||
browse_revision_url = reverse( | browse_revision_url = reverse( | ||||
"browse-revision", | "browse-revision", | ||||
url_args={"sha1_git": branch["revision"]}, | url_args={"sha1_git": branch["revision"]}, | ||||
query_params=query_params, | query_params=query_params, | ||||
) | ) | ||||
assert_contains(resp, '<a href="%s">' % escape(browse_revision_url)) | assert_contains(resp, '<a href="%s">' % escape(browse_revision_url)) | ||||
_check_origin_view_title(resp, origin_info["url"], "branches") | _check_origin_link(resp, origin_info["url"]) | ||||
def _origin_releases_test_helper( | def _origin_releases_test_helper( | ||||
client, origin_info, origin_snapshot, snapshot_id=None | client, origin_info, origin_snapshot, snapshot_id=None | ||||
): | ): | ||||
query_params = {"origin_url": origin_info["url"], "snapshot": snapshot_id} | query_params = {"origin_url": origin_info["url"], "snapshot": snapshot_id} | ||||
url = reverse("browse-origin-releases", query_params=query_params) | url = reverse("browse-origin-releases", 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/releases.html") | assert_template_used(resp, "browse/releases.html") | ||||
origin_branches = origin_snapshot[0] | origin_branches = origin_snapshot[0] | ||||
origin_releases = origin_snapshot[1] | origin_releases = origin_snapshot[1] | ||||
origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | origin_branches_url = reverse("browse-origin-branches", query_params=query_params) | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_branches_url)}"') | ||||
resp, | assert_contains(resp, f"Branches ({len(origin_branches)})") | ||||
'<a href="%s">Branches (%s)</a>' | |||||
% (escape(origin_branches_url), len(origin_branches)), | |||||
) | |||||
origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | origin_releases_url = reverse("browse-origin-releases", query_params=query_params) | ||||
nb_releases = len(origin_releases) | nb_releases = len(origin_releases) | ||||
if nb_releases > 0: | if nb_releases > 0: | ||||
assert_contains( | assert_contains(resp, f'href="{escape(origin_releases_url)}"') | ||||
resp, | assert_contains(resp, f"Releases ({nb_releases})") | ||||
'<a href="%s">Releases (%s)</a>' | |||||
% (escape(origin_releases_url), nb_releases), | |||||
) | |||||
assert_contains(resp, '<tr class="swh-release-entry', count=nb_releases) | assert_contains(resp, '<tr class="swh-release-entry', count=nb_releases) | ||||
for release in origin_releases: | for release in origin_releases: | ||||
browse_release_url = reverse( | browse_release_url = reverse( | ||||
"browse-release", | "browse-release", | ||||
url_args={"sha1_git": release["id"]}, | url_args={"sha1_git": release["id"]}, | ||||
query_params=query_params, | query_params=query_params, | ||||
) | ) | ||||
browse_revision_url = reverse( | browse_revision_url = reverse( | ||||
"browse-revision", | "browse-revision", | ||||
url_args={"sha1_git": release["target"]}, | url_args={"sha1_git": release["target"]}, | ||||
query_params=query_params, | query_params=query_params, | ||||
) | ) | ||||
assert_contains(resp, '<a href="%s">' % escape(browse_release_url)) | assert_contains(resp, '<a href="%s">' % escape(browse_release_url)) | ||||
assert_contains(resp, '<a href="%s">' % escape(browse_revision_url)) | assert_contains(resp, '<a href="%s">' % escape(browse_revision_url)) | ||||
_check_origin_view_title(resp, origin_info["url"], "releases") | _check_origin_link(resp, origin_info["url"]) | ||||
@given( | @given( | ||||
new_origin(), visit_dates(), revisions(min_size=10, max_size=10), existing_release() | new_origin(), visit_dates(), revisions(min_size=10, max_size=10), existing_release() | ||||
) | ) | ||||
def test_origin_branches_pagination_with_alias( | def test_origin_branches_pagination_with_alias( | ||||
client, archive_data, mocker, new_origin, visit_dates, revisions, existing_release | client, archive_data, mocker, new_origin, visit_dates, revisions, existing_release | ||||
): | ): | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | url = reverse( | ||||
"browse-origin-branches", query_params={"origin_url": new_origin["url"]} | "browse-origin-branches", query_params={"origin_url": new_origin["url"]} | ||||
) | ) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 200 | assert resp.status_code == 200 | ||||
assert_template_used(resp, "browse/branches.html") | assert_template_used(resp, "browse/branches.html") | ||||
assert_contains(resp, '<ul class="pagination') | assert_contains(resp, '<ul class="pagination') | ||||
def _check_origin_view_title(resp, origin_url, object_type): | def _check_origin_link(resp, origin_url): | ||||
browse_origin_url = reverse( | browse_origin_url = reverse( | ||||
"browse-origin", query_params={"origin_url": origin_url} | "browse-origin", query_params={"origin_url": origin_url} | ||||
) | ) | ||||
assert_contains(resp, f'href="{browse_origin_url}"') | |||||
assert_contains( | |||||
resp, | |||||
textwrap.indent( | |||||
( | |||||
f"Browse archived {object_type} for origin\n" | |||||
f'<a href="{browse_origin_url}">\n' | |||||
f" {origin_url}\n" | |||||
f"</a>" | |||||
), | |||||
" " * 6, | |||||
), | |||||
) |