Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_origin.py
# Copyright (C) 2017-2021 The Software Heritage developers | # Copyright (C) 2017-2021 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 | ||||
from hypothesis import given | from hypothesis import given | ||||
import pytest | |||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.identifiers import ObjectType | from swh.model.identifiers import ObjectType | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
▲ Show 20 Lines • Show All 593 Lines • ▼ Show 20 Lines | def test_browse_origin_content_unknown_visit(client, mocker, 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 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 _add_empty_snapshot_origin(new_origin, archive_data): | ||||
def test_browse_origin_content_directory_empty_snapshot(client, mocker, origin): | snapshot = Snapshot(branches={}) | ||||
mock_snapshot_archive = mocker.patch("swh.web.browse.snapshot_context.archive") | archive_data.origin_add([new_origin]) | ||||
mock_get_origin_visit_snapshot = mocker.patch( | archive_data.snapshot_add([snapshot]) | ||||
"swh.web.browse.snapshot_context.get_origin_visit_snapshot" | visit = archive_data.origin_visit_add( | ||||
) | [OriginVisit(origin=new_origin.url, date=now(), type="git",)] | ||||
mock_get_origin_visit_snapshot.return_value = ([], [], {}) | )[0] | ||||
mock_snapshot_archive.lookup_origin.return_value = origin | visit_status = OriginVisitStatus( | ||||
mock_snapshot_archive.lookup_snapshot_sizes.return_value = { | origin=new_origin.url, | ||||
"alias": 0, | visit=visit.visit, | ||||
"revision": 0, | date=now(), | ||||
"release": 0, | status="full", | ||||
} | snapshot=snapshot.id, | ||||
) | |||||
archive_data.origin_visit_status_add([visit_status]) | |||||
@pytest.mark.django_db | |||||
@pytest.mark.parametrize("object_type", ["content", "directory"]) | |||||
@given(new_origin()) | |||||
def test_browse_origin_content_directory_empty_snapshot( | |||||
client, staff_user, archive_data, object_type, new_origin | |||||
): | |||||
_add_empty_snapshot_origin(new_origin, archive_data) | |||||
# to check proper generation of raw extrinsic metadata api links | |||||
client.force_login(staff_user) | |||||
for browse_context in ("content", "directory"): | |||||
url = reverse( | url = reverse( | ||||
f"browse-origin-{browse_context}", | f"browse-origin-{object_type}", | ||||
query_params={"origin_url": origin["url"], "path": "baz"}, | query_params={"origin_url": new_origin.url, "path": "baz"}, | ||||
) | ) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used=f"browse/{browse_context}.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")) | ||||
assert mock_get_origin_visit_snapshot.called | |||||
assert mock_snapshot_archive.lookup_origin.called | |||||
@given(origin()) | @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"}, | ||||
) | ) | ||||
Show All 14 Lines | def test_browse_directory_snapshot_not_found(client, mocker, 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(resp, "Snapshot not found", status_code=404) | assert_contains(resp, "Snapshot not found", status_code=404) | ||||
assert mock_get_snapshot_context.called | assert mock_get_snapshot_context.called | ||||
@given(origin()) | @given(new_origin()) | ||||
def test_origin_empty_snapshot(client, mocker, origin): | def test_origin_empty_snapshot(client, archive_data, new_origin): | ||||
mock_archive = mocker.patch("swh.web.browse.snapshot_context.archive") | |||||
mock_get_origin_visit_snapshot = mocker.patch( | _add_empty_snapshot_origin(new_origin, archive_data) | ||||
"swh.web.browse.snapshot_context.get_origin_visit_snapshot" | |||||
) | url = reverse( | ||||
mock_get_origin_visit_snapshot.return_value = ([], [], {}) | "browse-origin-directory", query_params={"origin_url": new_origin.url} | ||||
mock_archive.lookup_snapshot_sizes.return_value = { | ) | ||||
"alias": 0, | |||||
"revision": 0, | |||||
"release": 0, | |||||
} | |||||
mock_archive.lookup_origin.return_value = origin | |||||
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" | ||||
) | ) | ||||
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) | ||||
assert mock_get_origin_visit_snapshot.called | |||||
@given(new_origin()) | @given(new_origin()) | ||||
def test_origin_empty_snapshot_null_revision(client, archive_data, new_origin): | def test_origin_empty_snapshot_null_revision(client, archive_data, new_origin): | ||||
snapshot = Snapshot( | snapshot = Snapshot( | ||||
branches={ | branches={ | ||||
b"HEAD": SnapshotBranch( | b"HEAD": SnapshotBranch( | ||||
target="refs/head/master".encode(), target_type=TargetType.ALIAS, | target="refs/head/master".encode(), target_type=TargetType.ALIAS, | ||||
▲ Show 20 Lines • Show All 625 Lines • Show Last 20 Lines |