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 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.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 | ||||
Show All 35 Lines | def test_content_view_text(client, archive_data, content): | ||||
assert resp.status_code == 200 | assert resp.status_code == 200 | ||||
assert_template_used(resp, "browse/content.html") | assert_template_used(resp, "browse/content.html") | ||||
if mimetype.startswith("text/"): | if mimetype.startswith("text/"): | ||||
assert_contains(resp, '<code class="%s">' % content_display["language"]) | assert_contains(resp, '<code class="%s">' % content_display["language"]) | ||||
assert_contains(resp, escape(content_display["content_data"])) | assert_contains(resp, escape(content_display["content_data"])) | ||||
assert_contains(resp, url_raw) | assert_contains(resp, url_raw) | ||||
swh_cnt_id = get_swh_persistent_id("content", sha1_git) | swh_cnt_id = get_swh_persistent_id(CONTENT, sha1_git) | ||||
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( | assert_contains( | ||||
resp, | resp, | ||||
textwrap.indent( | textwrap.indent( | ||||
( | ( | ||||
f"Browse archived content\n" | f"Browse archived content\n" | ||||
Show All 20 Lines | def test_content_view_text_no_highlight(client, archive_data, content): | ||||
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="nohighlight">') | assert_contains(resp, '<code class="nohighlight">') | ||||
assert_contains(resp, escape(content_display["content_data"])) | assert_contains(resp, escape(content_display["content_data"])) | ||||
assert_contains(resp, url_raw) | assert_contains(resp, url_raw) | ||||
swh_cnt_id = get_swh_persistent_id("content", sha1_git) | swh_cnt_id = get_swh_persistent_id(CONTENT, sha1_git) | ||||
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) | ||||
@given(content_text_non_utf8()) | @given(content_text_non_utf8()) | ||||
def test_content_view_no_utf8_text(client, archive_data, content): | def test_content_view_no_utf8_text(client, archive_data, content): | ||||
sha1_git = content["sha1_git"] | sha1_git = content["sha1_git"] | ||||
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 resp.status_code == 200 | assert resp.status_code == 200 | ||||
assert_template_used(resp, "browse/content.html") | assert_template_used(resp, "browse/content.html") | ||||
swh_cnt_id = get_swh_persistent_id("content", sha1_git) | swh_cnt_id = get_swh_persistent_id(CONTENT, sha1_git) | ||||
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_url) | assert_contains(resp, swh_cnt_id_url) | ||||
assert_contains(resp, escape(content_display["content_data"])) | assert_contains(resp, escape(content_display["content_data"])) | ||||
@given(content_image_type()) | @given(content_image_type()) | ||||
def test_content_view_image(client, archive_data, content): | def test_content_view_image(client, archive_data, content): | ||||
url = reverse("browse-content", url_args={"query_string": content["sha1"]}) | url = reverse("browse-content", url_args={"query_string": content["sha1"]}) | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | if mimetype.startswith("text/"): | ||||
assert_contains(resp, escape(content_display["content_data"])) | assert_contains(resp, escape(content_display["content_data"])) | ||||
split_path = path.split("/") | split_path = path.split("/") | ||||
root_dir_sha1 = split_path[0] | root_dir_sha1 = split_path[0] | ||||
filename = split_path[-1] | filename = split_path[-1] | ||||
path = path.replace(root_dir_sha1 + "/", "").replace(filename, "") | path = path.replace(root_dir_sha1 + "/", "").replace(filename, "") | ||||
swhid_context = { | |||||
"anchor": get_swh_persistent_id(DIRECTORY, root_dir_sha1), | |||||
"path": f"/{path}{filename}", | |||||
} | |||||
swh_cnt_id = get_swh_persistent_id( | |||||
CONTENT, content["sha1_git"], metadata=swhid_context | |||||
) | |||||
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_url) | |||||
path_info = gen_path_info(path) | path_info = gen_path_info(path) | ||||
root_dir_url = reverse("browse-directory", url_args={"sha1_git": root_dir_sha1}) | root_dir_url = reverse("browse-directory", url_args={"sha1_git": root_dir_sha1}) | ||||
assert_contains(resp, '<li class="swh-path">', count=len(path_info) + 1) | assert_contains(resp, '<li class="swh-path">', count=len(path_info) + 1) | ||||
assert_contains( | assert_contains( | ||||
resp, '<a href="' + root_dir_url + '">' + root_dir_sha1[:7] + "</a>" | resp, '<a href="' + root_dir_url + '">' + root_dir_sha1[:7] + "</a>" | ||||
▲ Show 20 Lines • Show All 225 Lines • Show Last 20 Lines |