Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_identifiers.py
# Copyright (C) 2018-2020 The Software Heritage developers | # Copyright (C) 2018-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 | |||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.identifiers import CONTENT, REVISION, SNAPSHOT | |||||
from swh.web.common.identifiers import get_swh_persistent_id | |||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.strategies import content, directory, revision, release, snapshot | from swh.web.tests.django_asserts import assert_contains | ||||
from swh.web.tests.strategies import ( | |||||
content, | |||||
directory, | |||||
origin, | |||||
revision, | |||||
release, | |||||
snapshot, | |||||
) | |||||
swh_id_prefix = "swh:1:" | swh_id_prefix = "swh:1:" | ||||
@given(content()) | @given(content()) | ||||
def test_content_id_browse(client, content): | def test_content_id_browse(client, content): | ||||
cnt_sha1_git = content["sha1_git"] | cnt_sha1_git = content["sha1_git"] | ||||
swh_id = swh_id_prefix + "cnt:" + cnt_sha1_git | swh_id = swh_id_prefix + "cnt:" + cnt_sha1_git | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | |||||
@given(release()) | @given(release()) | ||||
def test_origin_id_not_resolvable(client, release): | def test_origin_id_not_resolvable(client, release): | ||||
swh_id = "swh:1:ori:8068d0075010b590762c6cb5682ed53cb3c13deb" | swh_id = "swh:1:ori:8068d0075010b590762c6cb5682ed53cb3c13deb" | ||||
url = reverse("browse-swh-id", url_args={"swh_id": swh_id}) | url = reverse("browse-swh-id", url_args={"swh_id": swh_id}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 400 | assert resp.status_code == 400 | ||||
@given(origin()) | |||||
def test_legacy_swhid_browse(archive_data, client, origin): | |||||
snapshot = archive_data.snapshot_get_latest(origin["url"]) | |||||
revision = archive_data.snapshot_get_head(snapshot) | |||||
directory = archive_data.revision_get(revision)["directory"] | |||||
directory_content = archive_data.directory_ls(directory) | |||||
directory_file = random.choice( | |||||
[e for e in directory_content if e["type"] == "file"] | |||||
) | |||||
legacy_swhid = get_swh_persistent_id( | |||||
CONTENT, | |||||
directory_file["checksums"]["sha1_git"], | |||||
metadata={"origin": origin["url"]}, | |||||
) | |||||
url = reverse("browse-swh-id", url_args={"swh_id": legacy_swhid}) | |||||
resp = client.get(url) | |||||
assert resp.status_code == 302 | |||||
resp = client.get(resp["location"]) | |||||
swhid = get_swh_persistent_id( | |||||
CONTENT, | |||||
directory_file["checksums"]["sha1_git"], | |||||
metadata={ | |||||
"origin": origin["url"], | |||||
"visit": get_swh_persistent_id(SNAPSHOT, snapshot), | |||||
"anchor": get_swh_persistent_id(REVISION, revision), | |||||
}, | |||||
) | |||||
assert_contains(resp, swhid) |