Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_origin.py
Show All 27 Lines | |||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
format_utc_iso_date, | format_utc_iso_date, | ||||
gen_path_info, | gen_path_info, | ||||
parse_iso8601_date_to_utc, | parse_iso8601_date_to_utc, | ||||
reverse, | reverse, | ||||
) | ) | ||||
from swh.web.tests.data import get_content, random_sha1 | from swh.web.tests.data import get_content, random_sha1 | ||||
from swh.web.tests.django_asserts import assert_contains, assert_not_contains | from swh.web.tests.django_asserts import assert_contains, assert_not_contains | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import new_origin, new_snapshot, visit_dates | ||||
new_origin, | |||||
new_snapshot, | |||||
origin, | |||||
origin_with_multiple_visits, | |||||
origin_with_pull_request_branches, | |||||
origin_with_releases, | |||||
) | |||||
from swh.web.tests.strategies import release as existing_release | |||||
from swh.web.tests.strategies import revisions, unknown_revision, visit_dates | |||||
from swh.web.tests.utils import check_html_get_response | from swh.web.tests.utils import check_html_get_response | ||||
@given(origin_with_multiple_visits()) | def test_origin_visits_browse(client, archive_data, origin_with_multiple_visits): | ||||
def test_origin_visits_browse(client, archive_data, origin): | origin_url = origin_with_multiple_visits["url"] | ||||
url = reverse("browse-origin-visits", query_params={"origin_url": origin["url"]}) | url = reverse("browse-origin-visits", query_params={"origin_url": origin_url}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/origin-visits.html" | client, url, status_code=200, template_used="browse/origin-visits.html" | ||||
) | ) | ||||
visits = archive_data.origin_visit_get(origin["url"]) | visits = archive_data.origin_visit_get(origin_url) | ||||
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_link(resp, origin["url"]) | _check_origin_link(resp, origin_url) | ||||
@given(origin_with_multiple_visits()) | def test_origin_content_view( | ||||
def test_origin_content_view(client, archive_data, swh_scheduler, origin): | client, archive_data, swh_scheduler, origin_with_multiple_visits | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | ): | ||||
origin_visits = archive_data.origin_visit_get(origin_with_multiple_visits["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"]) | ||||
head_rev_id = archive_data.snapshot_get_head(snapshot) | head_rev_id = archive_data.snapshot_get_head(snapshot) | ||||
head_rev = archive_data.revision_get(head_rev_id) | head_rev = archive_data.revision_get(head_rev_id) | ||||
dir_content = archive_data.directory_ls(head_rev["directory"]) | dir_content = archive_data.directory_ls(head_rev["directory"]) | ||||
dir_files = [e for e in dir_content if e["type"] == "file"] | dir_files = [e for e in dir_content if e["type"] == "file"] | ||||
dir_file = random.choice(dir_files) | dir_file = random.choice(dir_files) | ||||
branches, releases, _ = process_snapshot_branches(snapshot) | branches, releases, _ = process_snapshot_branches(snapshot) | ||||
return { | return { | ||||
"branches": branches, | "branches": branches, | ||||
"releases": releases, | "releases": releases, | ||||
"root_dir_sha1": head_rev["directory"], | "root_dir_sha1": head_rev["directory"], | ||||
"content": get_content(dir_file["checksums"]["sha1"]), | "content": get_content(dir_file["checksums"]["sha1"]), | ||||
"visit": origin_visits[visit_idx], | "visit": origin_visits[visit_idx], | ||||
"snapshot_sizes": archive_data.snapshot_count_branches(snapshot["id"]), | "snapshot_sizes": archive_data.snapshot_count_branches(snapshot["id"]), | ||||
} | } | ||||
tdata = _get_archive_data(-1) | tdata = _get_archive_data(-1) | ||||
_origin_content_view_test_helper( | _origin_content_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin, | origin_with_multiple_visits, | ||||
origin_visits[-1], | origin_visits[-1], | ||||
tdata["snapshot_sizes"], | tdata["snapshot_sizes"], | ||||
tdata["branches"], | tdata["branches"], | ||||
tdata["releases"], | tdata["releases"], | ||||
tdata["root_dir_sha1"], | tdata["root_dir_sha1"], | ||||
tdata["content"], | tdata["content"], | ||||
) | ) | ||||
_origin_content_view_test_helper( | _origin_content_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin, | origin_with_multiple_visits, | ||||
origin_visits[-1], | origin_visits[-1], | ||||
tdata["snapshot_sizes"], | tdata["snapshot_sizes"], | ||||
tdata["branches"], | tdata["branches"], | ||||
tdata["releases"], | tdata["releases"], | ||||
tdata["root_dir_sha1"], | tdata["root_dir_sha1"], | ||||
tdata["content"], | tdata["content"], | ||||
timestamp=tdata["visit"]["date"], | timestamp=tdata["visit"]["date"], | ||||
) | ) | ||||
_origin_content_view_test_helper( | _origin_content_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin, | origin_with_multiple_visits, | ||||
origin_visits[-1], | origin_visits[-1], | ||||
tdata["snapshot_sizes"], | tdata["snapshot_sizes"], | ||||
tdata["branches"], | tdata["branches"], | ||||
tdata["releases"], | tdata["releases"], | ||||
tdata["root_dir_sha1"], | tdata["root_dir_sha1"], | ||||
tdata["content"], | tdata["content"], | ||||
snapshot_id=tdata["visit"]["snapshot"], | snapshot_id=tdata["visit"]["snapshot"], | ||||
) | ) | ||||
tdata = _get_archive_data(0) | tdata = _get_archive_data(0) | ||||
_origin_content_view_test_helper( | _origin_content_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin, | origin_with_multiple_visits, | ||||
origin_visits[0], | origin_visits[0], | ||||
tdata["snapshot_sizes"], | tdata["snapshot_sizes"], | ||||
tdata["branches"], | tdata["branches"], | ||||
tdata["releases"], | tdata["releases"], | ||||
tdata["root_dir_sha1"], | tdata["root_dir_sha1"], | ||||
tdata["content"], | tdata["content"], | ||||
visit_id=tdata["visit"]["visit"], | visit_id=tdata["visit"]["visit"], | ||||
) | ) | ||||
_origin_content_view_test_helper( | _origin_content_view_test_helper( | ||||
client, | client, | ||||
archive_data, | archive_data, | ||||
origin, | origin_with_multiple_visits, | ||||
origin_visits[0], | origin_visits[0], | ||||
tdata["snapshot_sizes"], | tdata["snapshot_sizes"], | ||||
tdata["branches"], | tdata["branches"], | ||||
tdata["releases"], | tdata["releases"], | ||||
tdata["root_dir_sha1"], | tdata["root_dir_sha1"], | ||||
tdata["content"], | tdata["content"], | ||||
snapshot_id=tdata["visit"]["snapshot"], | snapshot_id=tdata["visit"]["snapshot"], | ||||
) | ) | ||||
@given(origin()) | |||||
def test_origin_root_directory_view(client, archive_data, swh_scheduler, origin): | def test_origin_root_directory_view(client, archive_data, swh_scheduler, origin): | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | origin_visits = archive_data.origin_visit_get(origin["url"]) | ||||
visit = origin_visits[-1] | visit = origin_visits[-1] | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | ||||
head_rev_id = archive_data.snapshot_get_head(snapshot) | head_rev_id = archive_data.snapshot_get_head(snapshot) | ||||
head_rev = archive_data.revision_get(head_rev_id) | head_rev = archive_data.revision_get(head_rev_id) | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | _origin_directory_view_test_helper( | ||||
branches, | branches, | ||||
releases, | releases, | ||||
root_dir_sha1, | root_dir_sha1, | ||||
dir_content, | dir_content, | ||||
snapshot_id=visit["snapshot"], | snapshot_id=visit["snapshot"], | ||||
) | ) | ||||
@given(origin()) | |||||
def test_origin_sub_directory_view(client, archive_data, swh_scheduler, origin): | def test_origin_sub_directory_view(client, archive_data, swh_scheduler, origin): | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | origin_visits = archive_data.origin_visit_get(origin["url"]) | ||||
visit = origin_visits[-1] | visit = origin_visits[-1] | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | ||||
head_rev_id = archive_data.snapshot_get_head(snapshot) | head_rev_id = archive_data.snapshot_get_head(snapshot) | ||||
head_rev = archive_data.revision_get(head_rev_id) | head_rev = archive_data.revision_get(head_rev_id) | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | _origin_directory_view_test_helper( | ||||
releases, | releases, | ||||
root_dir_sha1, | root_dir_sha1, | ||||
subdir_content, | subdir_content, | ||||
path=subdir_path, | path=subdir_path, | ||||
snapshot_id=visit["snapshot"], | snapshot_id=visit["snapshot"], | ||||
) | ) | ||||
@given(origin()) | |||||
def test_origin_branches(client, archive_data, origin): | def test_origin_branches(client, archive_data, origin): | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | origin_visits = archive_data.origin_visit_get(origin["url"]) | ||||
visit = origin_visits[-1] | visit = origin_visits[-1] | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | ||||
snapshot_content = process_snapshot_branches(snapshot) | snapshot_content = process_snapshot_branches(snapshot) | ||||
_origin_branches_test_helper(client, origin, snapshot_content, snapshot_sizes) | _origin_branches_test_helper(client, origin, snapshot_content, snapshot_sizes) | ||||
_origin_branches_test_helper( | _origin_branches_test_helper( | ||||
client, origin, snapshot_content, snapshot_sizes, snapshot_id=visit["snapshot"] | client, origin, snapshot_content, snapshot_sizes, snapshot_id=visit["snapshot"] | ||||
) | ) | ||||
@given(origin()) | |||||
def test_origin_releases(client, archive_data, origin): | def test_origin_releases(client, archive_data, origin): | ||||
origin_visits = archive_data.origin_visit_get(origin["url"]) | origin_visits = archive_data.origin_visit_get(origin["url"]) | ||||
visit = origin_visits[-1] | visit = origin_visits[-1] | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) | ||||
snapshot_content = process_snapshot_branches(snapshot) | snapshot_content = process_snapshot_branches(snapshot) | ||||
_origin_releases_test_helper(client, origin, snapshot_content, snapshot_sizes) | _origin_releases_test_helper(client, origin, snapshot_content, snapshot_sizes) | ||||
_origin_releases_test_helper( | _origin_releases_test_helper( | ||||
client, origin, snapshot_content, snapshot_sizes, snapshot_id=visit["snapshot"] | client, origin, snapshot_content, snapshot_sizes, snapshot_id=visit["snapshot"] | ||||
) | ) | ||||
@given( | @given( | ||||
new_origin(), | new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), | ||||
new_snapshot(min_size=4, max_size=4), | |||||
visit_dates(), | |||||
revisions(min_size=3, max_size=3), | |||||
) | ) | ||||
def test_origin_snapshot_null_branch( | def test_origin_snapshot_null_branch( | ||||
client, archive_data, new_origin, new_snapshot, visit_dates, revisions | client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, | ||||
): | ): | ||||
revisions = revisions_list(size=4) | |||||
snp_dict = new_snapshot.to_dict() | snp_dict = new_snapshot.to_dict() | ||||
archive_data.origin_add([new_origin]) | archive_data.origin_add([new_origin]) | ||||
for i, branch in enumerate(snp_dict["branches"].keys()): | for i, branch in enumerate(snp_dict["branches"].keys()): | ||||
if i == 0: | if i == 0: | ||||
snp_dict["branches"][branch] = None | snp_dict["branches"][branch] = None | ||||
else: | else: | ||||
snp_dict["branches"][branch] = { | snp_dict["branches"][branch] = { | ||||
"target_type": "revision", | "target_type": "revision", | ||||
Show All 18 Lines | ): | ||||
) | ) | ||||
check_html_get_response( | check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/directory.html" | client, url, status_code=200, template_used="browse/directory.html" | ||||
) | ) | ||||
@given( | @given( | ||||
new_origin(), | new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), | ||||
new_snapshot(min_size=4, max_size=4), | |||||
visit_dates(), | |||||
revisions(min_size=4, max_size=4), | |||||
) | ) | ||||
def test_origin_snapshot_invalid_branch( | def test_origin_snapshot_invalid_branch( | ||||
client, archive_data, new_origin, new_snapshot, visit_dates, revisions | client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, | ||||
): | ): | ||||
revisions = revisions_list(size=4) | |||||
snp_dict = new_snapshot.to_dict() | snp_dict = new_snapshot.to_dict() | ||||
archive_data.origin_add([new_origin]) | archive_data.origin_add([new_origin]) | ||||
for i, branch in enumerate(snp_dict["branches"].keys()): | for i, branch in enumerate(snp_dict["branches"].keys()): | ||||
snp_dict["branches"][branch] = { | snp_dict["branches"][branch] = { | ||||
"target_type": "revision", | "target_type": "revision", | ||||
"target": hash_to_bytes(revisions[i]), | "target": hash_to_bytes(revisions[i]), | ||||
} | } | ||||
Show All 25 Lines | def test_browse_visits_origin_not_found(client, new_origin): | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="error.html" | client, url, status_code=404, template_used="error.html" | ||||
) | ) | ||||
assert_contains( | assert_contains( | ||||
resp, f"Origin with url {new_origin.url} not found", status_code=404 | resp, f"Origin with url {new_origin.url} not found", status_code=404 | ||||
) | ) | ||||
@given(origin()) | |||||
def test_browse_origin_directory_no_visit(client, mocker, origin): | def test_browse_origin_directory_no_visit(client, mocker, origin): | ||||
mock_get_origin_visits = mocker.patch( | mock_get_origin_visits = mocker.patch( | ||||
"swh.web.common.origin_visits.get_origin_visits" | "swh.web.common.origin_visits.get_origin_visits" | ||||
) | ) | ||||
mock_get_origin_visits.return_value = [] | mock_get_origin_visits.return_value = [] | ||||
mock_archive = mocker.patch("swh.web.common.origin_visits.archive") | mock_archive = mocker.patch("swh.web.common.origin_visits.archive") | ||||
mock_archive.lookup_origin_visit_latest.return_value = None | mock_archive.lookup_origin_visit_latest.return_value = None | ||||
url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) | url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="error.html" | client, url, status_code=404, template_used="error.html" | ||||
) | ) | ||||
assert_contains(resp, "No valid visit", status_code=404) | assert_contains(resp, "No valid visit", status_code=404) | ||||
assert not mock_get_origin_visits.called | assert not mock_get_origin_visits.called | ||||
@given(origin()) | |||||
def test_browse_origin_directory_unknown_visit(client, mocker, origin): | def test_browse_origin_directory_unknown_visit(client, mocker, origin): | ||||
mock_get_origin_visits = mocker.patch( | mock_get_origin_visits = mocker.patch( | ||||
"swh.web.common.origin_visits.get_origin_visits" | "swh.web.common.origin_visits.get_origin_visits" | ||||
) | ) | ||||
mock_get_origin_visits.return_value = [{"visit": 1}] | mock_get_origin_visits.return_value = [{"visit": 1}] | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin["url"], "visit_id": 2}, | query_params={"origin_url": origin["url"], "visit_id": 2}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="error.html" | client, url, status_code=404, template_used="error.html" | ||||
) | ) | ||||
assert re.search("Visit.*not found", resp.content.decode("utf-8")) | assert re.search("Visit.*not found", resp.content.decode("utf-8")) | ||||
assert mock_get_origin_visits.called | assert mock_get_origin_visits.called | ||||
@given(origin()) | |||||
def test_browse_origin_directory_not_found(client, origin): | def test_browse_origin_directory_not_found(client, origin): | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin["url"], "path": "/invalid/dir/path/"}, | query_params={"origin_url": origin["url"], "path": "/invalid/dir/path/"}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="browse/directory.html" | client, url, status_code=404, template_used="browse/directory.html" | ||||
) | ) | ||||
assert re.search("Directory.*not found", resp.content.decode("utf-8")) | assert re.search("Directory.*not found", resp.content.decode("utf-8")) | ||||
@given(origin()) | |||||
def test_browse_origin_content_no_visit(client, mocker, origin): | def test_browse_origin_content_no_visit(client, mocker, origin): | ||||
mock_get_origin_visits = mocker.patch( | mock_get_origin_visits = mocker.patch( | ||||
"swh.web.common.origin_visits.get_origin_visits" | "swh.web.common.origin_visits.get_origin_visits" | ||||
) | ) | ||||
mock_get_origin_visits.return_value = [] | mock_get_origin_visits.return_value = [] | ||||
mock_archive = mocker.patch("swh.web.common.origin_visits.archive") | mock_archive = mocker.patch("swh.web.common.origin_visits.archive") | ||||
mock_archive.lookup_origin_visit_latest.return_value = None | mock_archive.lookup_origin_visit_latest.return_value = None | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-content", | "browse-origin-content", | ||||
query_params={"origin_url": origin["url"], "path": "foo"}, | query_params={"origin_url": origin["url"], "path": "foo"}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="error.html" | client, url, status_code=404, template_used="error.html" | ||||
) | ) | ||||
assert_contains(resp, "No valid visit", status_code=404) | assert_contains(resp, "No valid visit", status_code=404) | ||||
assert not mock_get_origin_visits.called | assert not mock_get_origin_visits.called | ||||
@given(origin()) | |||||
def test_browse_origin_content_unknown_visit(client, mocker, origin): | def test_browse_origin_content_unknown_visit(client, mocker, origin): | ||||
mock_get_origin_visits = mocker.patch( | mock_get_origin_visits = mocker.patch( | ||||
"swh.web.common.origin_visits.get_origin_visits" | "swh.web.common.origin_visits.get_origin_visits" | ||||
) | ) | ||||
mock_get_origin_visits.return_value = [{"visit": 1}] | mock_get_origin_visits.return_value = [{"visit": 1}] | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-content", | "browse-origin-content", | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | ): | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used=f"browse/{object_type}.html" | client, url, status_code=200, template_used=f"browse/{object_type}.html" | ||||
) | ) | ||||
assert re.search("snapshot.*is empty", resp.content.decode("utf-8")) | assert re.search("snapshot.*is empty", resp.content.decode("utf-8")) | ||||
@given(origin()) | |||||
def test_browse_origin_content_not_found(client, origin): | def test_browse_origin_content_not_found(client, origin): | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-content", | "browse-origin-content", | ||||
query_params={"origin_url": origin["url"], "path": "/invalid/file/path"}, | query_params={"origin_url": origin["url"], "path": "/invalid/file/path"}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="browse/content.html" | client, url, status_code=404, template_used="browse/content.html" | ||||
) | ) | ||||
assert re.search("Directory entry.*not found", resp.content.decode("utf-8")) | assert re.search("Directory entry.*not found", resp.content.decode("utf-8")) | ||||
@given(origin()) | |||||
def test_browse_directory_snapshot_not_found(client, mocker, origin): | def test_browse_directory_snapshot_not_found(client, mocker, origin): | ||||
mock_get_snapshot_context = mocker.patch( | mock_get_snapshot_context = mocker.patch( | ||||
"swh.web.browse.snapshot_context.get_snapshot_context" | "swh.web.browse.snapshot_context.get_snapshot_context" | ||||
) | ) | ||||
mock_get_snapshot_context.side_effect = NotFoundExc("Snapshot not found") | mock_get_snapshot_context.side_effect = NotFoundExc("Snapshot not found") | ||||
url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) | url = reverse("browse-origin-directory", query_params={"origin_url": origin["url"]}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | def test_origin_empty_snapshot_null_revision(client, archive_data, new_origin): | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/directory.html" | client, url, status_code=200, template_used="browse/directory.html" | ||||
) | ) | ||||
resp_content = resp.content.decode("utf-8") | resp_content = resp.content.decode("utf-8") | ||||
assert re.search("snapshot.*is empty", resp_content) | assert re.search("snapshot.*is empty", resp_content) | ||||
assert not re.search("swh-tr-link", resp_content) | assert not re.search("swh-tr-link", resp_content) | ||||
@given(origin_with_releases()) | def test_origin_release_browse(client, archive_data, origin_with_releases): | ||||
def test_origin_release_browse(client, archive_data, origin): | origin_url = origin_with_releases["url"] | ||||
snapshot = archive_data.snapshot_get_latest(origin["url"]) | snapshot = archive_data.snapshot_get_latest(origin_url) | ||||
release = [ | release = [ | ||||
b for b in snapshot["branches"].values() if b["target_type"] == "release" | b for b in snapshot["branches"].values() if b["target_type"] == "release" | ||||
][-1] | ][-1] | ||||
release_data = archive_data.release_get(release["target"]) | release_data = archive_data.release_get(release["target"]) | ||||
revision_data = archive_data.revision_get(release_data["target"]) | revision_data = archive_data.revision_get(release_data["target"]) | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin["url"], "release": release_data["name"]}, | query_params={"origin_url": origin_url, "release": release_data["name"]}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/directory.html" | client, url, status_code=200, template_used="browse/directory.html" | ||||
) | ) | ||||
assert_contains(resp, release_data["name"]) | assert_contains(resp, release_data["name"]) | ||||
assert_contains(resp, release["target"]) | assert_contains(resp, release["target"]) | ||||
swhid_context = { | swhid_context = { | ||||
"origin": origin["url"], | "origin": origin_url, | ||||
"visit": gen_swhid(ObjectType.SNAPSHOT, snapshot["id"]), | "visit": gen_swhid(ObjectType.SNAPSHOT, snapshot["id"]), | ||||
"anchor": gen_swhid(ObjectType.RELEASE, release_data["id"]), | "anchor": gen_swhid(ObjectType.RELEASE, release_data["id"]), | ||||
} | } | ||||
swh_dir_id = gen_swhid( | swh_dir_id = gen_swhid( | ||||
ObjectType.DIRECTORY, revision_data["directory"], metadata=swhid_context | ObjectType.DIRECTORY, revision_data["directory"], metadata=swhid_context | ||||
) | ) | ||||
swh_dir_id_url = reverse("browse-swhid", url_args={"swhid": swh_dir_id}) | swh_dir_id_url = reverse("browse-swhid", url_args={"swhid": 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) | ||||
@given(origin_with_releases()) | def test_origin_release_browse_not_found(client, origin_with_releases): | ||||
def test_origin_release_browse_not_found(client, origin): | |||||
invalid_release_name = "swh-foo-bar" | invalid_release_name = "swh-foo-bar" | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin["url"], "release": invalid_release_name}, | query_params={ | ||||
"origin_url": origin_with_releases["url"], | |||||
"release": invalid_release_name, | |||||
}, | |||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=404, template_used="error.html" | client, url, status_code=404, template_used="error.html" | ||||
) | ) | ||||
assert re.search( | assert re.search( | ||||
f"Release {invalid_release_name}.*not found", resp.content.decode("utf-8") | f"Release {invalid_release_name}.*not found", resp.content.decode("utf-8") | ||||
) | ) | ||||
@given(new_origin(), unknown_revision()) | @given(new_origin()) | ||||
def test_origin_browse_directory_branch_with_non_resolvable_revision( | def test_origin_browse_directory_branch_with_non_resolvable_revision( | ||||
client, archive_data, new_origin, unknown_revision | client, archive_data, unknown_revision, new_origin, | ||||
): | ): | ||||
branch_name = "master" | branch_name = "master" | ||||
snapshot = Snapshot( | snapshot = Snapshot( | ||||
branches={ | branches={ | ||||
branch_name.encode(): SnapshotBranch( | branch_name.encode(): SnapshotBranch( | ||||
target=hash_to_bytes(unknown_revision), target_type=TargetType.REVISION, | target=hash_to_bytes(unknown_revision), target_type=TargetType.REVISION, | ||||
) | ) | ||||
} | } | ||||
Show All 30 Lines | ): | ||||
assert_not_contains(resp, "swh-vault-download") | assert_not_contains(resp, "swh-vault-download") | ||||
# no History link | # no History link | ||||
assert_not_contains(resp, "swh-tr-link") | assert_not_contains(resp, "swh-tr-link") | ||||
# no SWHIDs for directory and revision | # no SWHIDs for directory and revision | ||||
assert_not_contains(resp, "swh:1:dir:") | assert_not_contains(resp, "swh:1:dir:") | ||||
assert_not_contains(resp, "swh:1:rev:") | assert_not_contains(resp, "swh:1:rev:") | ||||
@given(origin()) | |||||
def test_origin_content_no_path(client, origin): | def test_origin_content_no_path(client, origin): | ||||
url = reverse("browse-origin-content", query_params={"origin_url": origin["url"]}) | url = reverse("browse-origin-content", query_params={"origin_url": origin["url"]}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=400, template_used="error.html" | client, url, status_code=400, template_used="error.html" | ||||
) | ) | ||||
assert_contains( | assert_contains( | ||||
resp, "The path of a content must be given as query parameter.", status_code=400 | resp, "The path of a content must be given as query parameter.", status_code=400 | ||||
▲ Show 20 Lines • Show All 390 Lines • ▼ Show 20 Lines | for release in origin_releases: | ||||
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_link(resp, origin_info["url"]) | _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(), | ||||
) | ) | ||||
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, release, revisions_list, new_origin, visit_dates, | ||||
): | ): | ||||
""" | """ | ||||
When a snapshot contains a branch or a release alias, pagination links | When a snapshot contains a branch or a release alias, pagination links | ||||
in the branches / releases view should be displayed. | in the branches / releases view should be displayed. | ||||
""" | """ | ||||
revisions = revisions_list(size=10) | |||||
mocker.patch("swh.web.browse.snapshot_context.PER_PAGE", len(revisions) / 2) | mocker.patch("swh.web.browse.snapshot_context.PER_PAGE", len(revisions) / 2) | ||||
snp_dict = {"branches": {}, "id": hash_to_bytes(random_sha1())} | snp_dict = {"branches": {}, "id": hash_to_bytes(random_sha1())} | ||||
for i in range(len(revisions)): | for i in range(len(revisions)): | ||||
branch = "".join(random.choices(string.ascii_lowercase, k=8)) | branch = "".join(random.choices(string.ascii_lowercase, k=8)) | ||||
snp_dict["branches"][branch.encode()] = { | snp_dict["branches"][branch.encode()] = { | ||||
"target_type": "revision", | "target_type": "revision", | ||||
"target": hash_to_bytes(revisions[i]), | "target": hash_to_bytes(revisions[i]), | ||||
} | } | ||||
release = "".join(random.choices(string.ascii_lowercase, k=8)) | release_name = "".join(random.choices(string.ascii_lowercase, k=8)) | ||||
snp_dict["branches"][b"RELEASE_ALIAS"] = { | snp_dict["branches"][b"RELEASE_ALIAS"] = { | ||||
"target_type": "alias", | "target_type": "alias", | ||||
"target": release.encode(), | "target": release_name.encode(), | ||||
} | } | ||||
snp_dict["branches"][release.encode()] = { | snp_dict["branches"][release_name.encode()] = { | ||||
"target_type": "release", | "target_type": "release", | ||||
"target": hash_to_bytes(existing_release), | "target": hash_to_bytes(release), | ||||
} | } | ||||
archive_data.origin_add([new_origin]) | archive_data.origin_add([new_origin]) | ||||
archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) | archive_data.snapshot_add([Snapshot.from_dict(snp_dict)]) | ||||
visit = archive_data.origin_visit_add( | visit = archive_data.origin_visit_add( | ||||
[OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] | [OriginVisit(origin=new_origin.url, date=visit_dates[0], type="git",)] | ||||
)[0] | )[0] | ||||
visit_status = OriginVisitStatus( | visit_status = OriginVisitStatus( | ||||
origin=new_origin.url, | origin=new_origin.url, | ||||
Show All 14 Lines | |||||
def _check_origin_link(resp, origin_url): | 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, f'href="{browse_origin_url}"') | ||||
@given(origin_with_pull_request_branches()) | def test_pull_request_branches_filtering(client, origin_with_pull_request_branches): | ||||
def test_pull_request_branches_filtering(client, origin): | origin_url = origin_with_pull_request_branches.url | ||||
# check no pull request branches are displayed in the Branches / Releases dropdown | # check no pull request branches are displayed in the Branches / Releases dropdown | ||||
url = reverse("browse-origin-directory", query_params={"origin_url": origin.url}) | url = reverse("browse-origin-directory", query_params={"origin_url": origin_url}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/directory.html" | client, url, status_code=200, template_used="browse/directory.html" | ||||
) | ) | ||||
assert_not_contains(resp, "refs/pull/") | assert_not_contains(resp, "refs/pull/") | ||||
# check no pull request branches are displayed in the branches view | # check no pull request branches are displayed in the branches view | ||||
url = reverse("browse-origin-branches", query_params={"origin_url": origin.url}) | url = reverse("browse-origin-branches", query_params={"origin_url": origin_url}) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/branches.html" | client, url, status_code=200, template_used="browse/branches.html" | ||||
) | ) | ||||
assert_not_contains(resp, "refs/pull/") | assert_not_contains(resp, "refs/pull/") | ||||
@given(origin_with_pull_request_branches()) | def test_browse_pull_request_branch( | ||||
def test_browse_pull_request_branch(client, archive_data, origin): | client, archive_data, origin_with_pull_request_branches | ||||
snapshot = archive_data.snapshot_get_latest(origin.url) | ): | ||||
origin_url = origin_with_pull_request_branches.url | |||||
snapshot = archive_data.snapshot_get_latest(origin_url) | |||||
pr_branch = random.choice( | pr_branch = random.choice( | ||||
[ | [ | ||||
branch | branch | ||||
for branch in snapshot["branches"].keys() | for branch in snapshot["branches"].keys() | ||||
if branch.startswith("refs/pull/") | if branch.startswith("refs/pull/") | ||||
] | ] | ||||
) | ) | ||||
url = reverse( | url = reverse( | ||||
"browse-origin-directory", | "browse-origin-directory", | ||||
query_params={"origin_url": origin.url, "branch": pr_branch}, | query_params={"origin_url": origin_url, "branch": pr_branch}, | ||||
) | ) | ||||
check_html_get_response( | check_html_get_response( | ||||
client, url, status_code=200, template_used="browse/directory.html" | client, url, status_code=200, template_used="browse/directory.html" | ||||
) | ) |