Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_content.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 textwrap | import textwrap | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.identifiers import CONTENT, DIRECTORY | from swh.model.identifiers import CONTENT, DIRECTORY | ||||
from swh.web.browse.snapshot_context import process_snapshot_branches | |||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
get_mimetype_and_encoding_for_content, | get_mimetype_and_encoding_for_content, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
_re_encode_content, | _re_encode_content, | ||||
) | ) | ||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.identifiers import get_swh_persistent_id | from swh.web.common.identifiers import get_swh_persistent_id | ||||
from swh.web.common.utils import gen_path_info, reverse | from swh.web.common.utils import gen_path_info, reverse | ||||
from swh.web.tests.django_asserts import ( | from swh.web.tests.django_asserts import ( | ||||
assert_contains, | assert_contains, | ||||
assert_not_contains, | assert_not_contains, | ||||
assert_template_used, | assert_template_used, | ||||
) | ) | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
content, | content, | ||||
content_text_non_utf8, | content_text_non_utf8, | ||||
content_text_no_highlight, | content_text_no_highlight, | ||||
content_image_type, | content_image_type, | ||||
content_unsupported_image_type_rendering, | content_unsupported_image_type_rendering, | ||||
content_text, | content_text, | ||||
invalid_sha1, | invalid_sha1, | ||||
unknown_content, | unknown_content, | ||||
content_utf8_detected_as_binary, | content_utf8_detected_as_binary, | ||||
origin_with_multiple_visits, | |||||
) | ) | ||||
@given(content_text()) | @given(content_text()) | ||||
def test_content_view_text(client, archive_data, content): | def test_content_view_text(client, archive_data, content): | ||||
sha1_git = content["sha1_git"] | sha1_git = content["sha1_git"] | ||||
url = reverse( | url = reverse( | ||||
▲ Show 20 Lines • Show All 371 Lines • ▼ Show 20 Lines | def test_content_utf8_detected_as_binary_display(client, archive_data, content): | ||||
url = reverse("browse-content", url_args={"query_string": content["sha1"]}) | url = reverse("browse-content", url_args={"query_string": content["sha1"]}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
content_display = _process_content_for_display(archive_data, content) | content_display = _process_content_for_display(archive_data, content) | ||||
assert_contains(resp, escape(content_display["content_data"])) | assert_contains(resp, escape(content_display["content_data"])) | ||||
@given(origin_with_multiple_visits()) | |||||
def test_content_origin_snapshot_branch_browse(client, archive_data, origin): | |||||
visits = archive_data.origin_visit_get(origin["url"]) | |||||
visit = random.choice(visits) | |||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | |||||
branches, releases = process_snapshot_branches(snapshot) | |||||
branch_info = random.choice(branches) | |||||
directory = archive_data.revision_get(branch_info["revision"])["directory"] | |||||
directory_content = archive_data.directory_ls(directory) | |||||
directory_file = random.choice( | |||||
[e for e in directory_content if e["type"] == "file"] | |||||
) | |||||
url = reverse( | |||||
"browse-content", | |||||
url_args={"query_string": directory_file["checksums"]["sha1"]}, | |||||
query_params={ | |||||
"origin_url": origin["url"], | |||||
"snapshot": snapshot["id"], | |||||
"branch": branch_info["name"], | |||||
"path": directory_file["name"], | |||||
}, | |||||
) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 200 | |||||
assert_template_used(resp, "browse/content.html") | |||||
_check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | |||||
assert_contains(resp, directory_file["name"]) | |||||
assert_contains(resp, f"Branch: <strong>{branch_info['name']}</strong>") | |||||
@given(origin_with_multiple_visits()) | |||||
def test_content_origin_snapshot_release_browse(client, archive_data, origin): | |||||
visits = archive_data.origin_visit_get(origin["url"]) | |||||
visit = random.choice(visits) | |||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | |||||
branches, releases = process_snapshot_branches(snapshot) | |||||
release_info = random.choice(releases) | |||||
directory_content = archive_data.directory_ls(release_info["directory"]) | |||||
directory_file = random.choice( | |||||
[e for e in directory_content if e["type"] == "file"] | |||||
) | |||||
url = reverse( | |||||
"browse-content", | |||||
url_args={"query_string": directory_file["checksums"]["sha1"]}, | |||||
query_params={ | |||||
"origin_url": origin["url"], | |||||
"snapshot": snapshot["id"], | |||||
"release": release_info["name"], | |||||
"path": directory_file["name"], | |||||
}, | |||||
) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 200 | |||||
assert_template_used(resp, "browse/content.html") | |||||
_check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | |||||
assert_contains(resp, directory_file["name"]) | |||||
assert_contains(resp, f"Release: <strong>{release_info['name']}</strong>") | |||||
def _check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases): | |||||
browse_origin_url = reverse( | |||||
"browse-origin", query_params={"origin_url": origin["url"]} | |||||
) | |||||
assert_contains( | |||||
resp, | |||||
textwrap.indent( | |||||
( | |||||
"Browse archived content for origin\n" | |||||
f'<a href="{browse_origin_url}">\n' | |||||
f" {origin['url']}\n" | |||||
f"</a>" | |||||
), | |||||
" " * 6, | |||||
), | |||||
) | |||||
origin_branches_url = reverse( | |||||
"browse-origin-branches", | |||||
query_params={"origin_url": origin["url"], "snapshot": snapshot["id"]}, | |||||
) | |||||
assert_contains( | |||||
resp, | |||||
'<a href="%s">Branches (%s)</a>' % (escape(origin_branches_url), len(branches)), | |||||
) | |||||
origin_releases_url = reverse( | |||||
"browse-origin-releases", | |||||
query_params={"origin_url": origin["url"], "snapshot": snapshot["id"]}, | |||||
) | |||||
assert_contains( | |||||
resp, | |||||
'<a href="%s">Releases (%s)</a>' % (escape(origin_releases_url), len(releases)), | |||||
) | |||||
assert_contains(resp, '<li class="swh-branch">', count=len(branches)) | |||||
assert_contains(resp, '<li class="swh-release">', count=len(releases)) | |||||
def _process_content_for_display(archive_data, content): | def _process_content_for_display(archive_data, content): | ||||
content_data = archive_data.content_get(content["sha1"]) | content_data = archive_data.content_get(content["sha1"]) | ||||
mime_type, encoding = get_mimetype_and_encoding_for_content(content_data["data"]) | mime_type, encoding = get_mimetype_and_encoding_for_content(content_data["data"]) | ||||
mime_type, encoding, content_data = _re_encode_content( | mime_type, encoding, content_data = _re_encode_content( | ||||
mime_type, encoding, content_data["data"] | mime_type, encoding, content_data["data"] | ||||
) | ) | ||||
content_display = prepare_content_for_display( | content_display = prepare_content_for_display( | ||||
content_data, mime_type, content["path"] | content_data, mime_type, content["path"] | ||||
) | ) | ||||
assert type(content_display["content_data"]) == str | assert type(content_display["content_data"]) == str | ||||
return content_display | return content_display |