Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_content.py
Show All 11 Lines | |||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.web.browse.snapshot_context import process_snapshot_branches | 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 gen_swhid | ||||
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, | ||||
Show All 29 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 = gen_swhid(CONTENT, sha1_git) | ||||
swh_cnt_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_cnt_id}) | swh_cnt_id_url = reverse("browse-swhid", url_args={"swhid": 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_no_highlight()) | @given(content_text_no_highlight()) | ||||
def test_content_view_text_no_highlight(client, archive_data, content): | def test_content_view_text_no_highlight(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"]}) | ||||
url_raw = reverse("browse-content-raw", url_args={"query_string": content["sha1"]}) | url_raw = reverse("browse-content-raw", 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") | ||||
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 = gen_swhid(CONTENT, sha1_git) | ||||
swh_cnt_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_cnt_id}) | swh_cnt_id_url = reverse("browse-swhid", url_args={"swhid": 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 = gen_swhid(CONTENT, sha1_git) | ||||
swh_cnt_id_url = reverse("browse-swh-id", url_args={"swh_id": swh_cnt_id}) | swh_cnt_id_url = reverse("browse-swhid", url_args={"swhid": 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 | def test_content_view_text_with_path(client, archive_data, content): | ||||
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 = { | swhid_context = { | ||||
"anchor": get_swh_persistent_id(DIRECTORY, root_dir_sha1), | "anchor": gen_swhid(DIRECTORY, root_dir_sha1), | ||||
"path": f"/{path}{filename}", | "path": f"/{path}{filename}", | ||||
} | } | ||||
swh_cnt_id = get_swh_persistent_id( | swh_cnt_id = gen_swhid(CONTENT, content["sha1_git"], metadata=swhid_context) | ||||
CONTENT, content["sha1_git"], metadata=swhid_context | swh_cnt_id_url = reverse("browse-swhid", url_args={"swhid": 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) | ||||
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) | ||||
▲ Show 20 Lines • Show All 236 Lines • ▼ Show 20 Lines | def test_content_origin_snapshot_branch_browse(client, archive_data, origin): | ||||
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") | ||||
_check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | _check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | ||||
assert_contains(resp, directory_file["name"]) | assert_contains(resp, directory_file["name"]) | ||||
assert_contains(resp, f"Branch: <strong>{branch_info['name']}</strong>") | assert_contains(resp, f"Branch: <strong>{branch_info['name']}</strong>") | ||||
cnt_swhid = get_swh_persistent_id( | cnt_swhid = gen_swhid( | ||||
CONTENT, | CONTENT, | ||||
directory_file["checksums"]["sha1_git"], | directory_file["checksums"]["sha1_git"], | ||||
metadata={ | metadata={ | ||||
"origin": origin["url"], | "origin": origin["url"], | ||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | "visit": gen_swhid(SNAPSHOT, snapshot), | ||||
"anchor": get_swh_persistent_id(REVISION, branch_info["revision"]), | "anchor": gen_swhid(REVISION, branch_info["revision"]), | ||||
"path": f"/{directory_file['name']}", | "path": f"/{directory_file['name']}", | ||||
}, | }, | ||||
) | ) | ||||
assert_contains(resp, cnt_swhid) | assert_contains(resp, cnt_swhid) | ||||
dir_swhid = get_swh_persistent_id( | dir_swhid = gen_swhid( | ||||
DIRECTORY, | DIRECTORY, | ||||
directory, | directory, | ||||
metadata={ | metadata={ | ||||
"origin": origin["url"], | "origin": origin["url"], | ||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | "visit": gen_swhid(SNAPSHOT, snapshot), | ||||
"anchor": get_swh_persistent_id(REVISION, branch_info["revision"]), | "anchor": gen_swhid(REVISION, branch_info["revision"]), | ||||
"path": "/", | "path": "/", | ||||
}, | }, | ||||
) | ) | ||||
assert_contains(resp, dir_swhid) | assert_contains(resp, dir_swhid) | ||||
rev_swhid = get_swh_persistent_id( | rev_swhid = gen_swhid( | ||||
REVISION, | REVISION, | ||||
branch_info["revision"], | branch_info["revision"], | ||||
metadata={ | metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, | ||||
"origin": origin["url"], | |||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | |||||
}, | |||||
) | ) | ||||
assert_contains(resp, rev_swhid) | assert_contains(resp, rev_swhid) | ||||
snp_swhid = get_swh_persistent_id( | snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) | ||||
SNAPSHOT, snapshot, metadata={"origin": origin["url"],}, | |||||
) | |||||
assert_contains(resp, snp_swhid) | assert_contains(resp, snp_swhid) | ||||
@given(origin_with_multiple_visits()) | @given(origin_with_multiple_visits()) | ||||
def test_content_origin_snapshot_release_browse(client, archive_data, origin): | def test_content_origin_snapshot_release_browse(client, archive_data, origin): | ||||
visits = archive_data.origin_visit_get(origin["url"]) | visits = archive_data.origin_visit_get(origin["url"]) | ||||
visit = random.choice(visits) | visit = random.choice(visits) | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
Show All 18 Lines | def test_content_origin_snapshot_release_browse(client, archive_data, origin): | ||||
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") | ||||
_check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | _check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases) | ||||
assert_contains(resp, directory_file["name"]) | assert_contains(resp, directory_file["name"]) | ||||
assert_contains(resp, f"Release: <strong>{release_info['name']}</strong>") | assert_contains(resp, f"Release: <strong>{release_info['name']}</strong>") | ||||
cnt_swhid = get_swh_persistent_id( | cnt_swhid = gen_swhid( | ||||
CONTENT, | CONTENT, | ||||
directory_file["checksums"]["sha1_git"], | directory_file["checksums"]["sha1_git"], | ||||
metadata={ | metadata={ | ||||
"origin": origin["url"], | "origin": origin["url"], | ||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | "visit": gen_swhid(SNAPSHOT, snapshot), | ||||
"anchor": get_swh_persistent_id(RELEASE, release_info["id"]), | "anchor": gen_swhid(RELEASE, release_info["id"]), | ||||
"path": f"/{directory_file['name']}", | "path": f"/{directory_file['name']}", | ||||
}, | }, | ||||
) | ) | ||||
assert_contains(resp, cnt_swhid) | assert_contains(resp, cnt_swhid) | ||||
dir_swhid = get_swh_persistent_id( | dir_swhid = gen_swhid( | ||||
DIRECTORY, | DIRECTORY, | ||||
release_info["directory"], | release_info["directory"], | ||||
metadata={ | metadata={ | ||||
"origin": origin["url"], | "origin": origin["url"], | ||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | "visit": gen_swhid(SNAPSHOT, snapshot), | ||||
"anchor": get_swh_persistent_id(RELEASE, release_info["id"]), | "anchor": gen_swhid(RELEASE, release_info["id"]), | ||||
"path": "/", | "path": "/", | ||||
}, | }, | ||||
) | ) | ||||
assert_contains(resp, dir_swhid) | assert_contains(resp, dir_swhid) | ||||
rev_swhid = get_swh_persistent_id( | rev_swhid = gen_swhid( | ||||
REVISION, | REVISION, | ||||
release_info["target"], | release_info["target"], | ||||
metadata={ | metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, | ||||
"origin": origin["url"], | |||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | |||||
}, | |||||
) | ) | ||||
assert_contains(resp, rev_swhid) | assert_contains(resp, rev_swhid) | ||||
rel_swhid = get_swh_persistent_id( | rel_swhid = gen_swhid( | ||||
RELEASE, | RELEASE, | ||||
release_info["id"], | release_info["id"], | ||||
metadata={ | metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, | ||||
"origin": origin["url"], | |||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | |||||
}, | |||||
) | ) | ||||
assert_contains(resp, rel_swhid) | assert_contains(resp, rel_swhid) | ||||
snp_swhid = get_swh_persistent_id( | snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) | ||||
SNAPSHOT, snapshot, metadata={"origin": origin["url"],}, | |||||
) | |||||
assert_contains(resp, snp_swhid) | assert_contains(resp, snp_swhid) | ||||
def _check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases): | def _check_origin_snapshot_related_html(resp, origin, snapshot, branches, releases): | ||||
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, f'href="{browse_origin_url}"') | ||||
Show All 37 Lines |