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 | import random | ||||
from urllib.parse import quote | from urllib.parse import quote | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.model.model import Origin | |||||
from swh.web.common.identifiers import gen_swhid | from swh.web.common.identifiers import gen_swhid | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.django_asserts import assert_contains | from swh.web.tests.django_asserts import assert_contains | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
content, | content, | ||||
directory, | directory, | ||||
origin, | origin, | ||||
release, | release, | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | |||||
def test_bad_id_browse(client, release): | def test_bad_id_browse(client, release): | ||||
swhid = f"swh:1:foo:{release}" | swhid = f"swh:1:foo:{release}" | ||||
url = reverse("browse-swhid", url_args={"swhid": swhid}) | url = reverse("browse-swhid", url_args={"swhid": swhid}) | ||||
check_html_get_response(client, url, status_code=400) | check_html_get_response(client, url, status_code=400) | ||||
@given(content()) | @given(content()) | ||||
def test_content_id_optional_parts_browse(client, content): | def test_content_id_optional_parts_browse(client, archive_data, content): | ||||
cnt_sha1_git = content["sha1_git"] | cnt_sha1_git = content["sha1_git"] | ||||
origin_url = "https://github.com/user/repo" | origin_url = "https://github.com/user/repo" | ||||
archive_data.origin_add([Origin(url=origin_url)]) | |||||
swhid = gen_swhid( | swhid = gen_swhid( | ||||
CONTENT, cnt_sha1_git, metadata={"lines": "4-20", "origin": origin_url}, | CONTENT, cnt_sha1_git, metadata={"lines": "4-20", "origin": origin_url}, | ||||
) | ) | ||||
url = reverse("browse-swhid", url_args={"swhid": swhid}) | url = reverse("browse-swhid", url_args={"swhid": swhid}) | ||||
query_string = "sha1_git:" + cnt_sha1_git | query_string = "sha1_git:" + cnt_sha1_git | ||||
content_browse_url = reverse( | content_browse_url = reverse( | ||||
"browse-content", | "browse-content", | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | swhid = gen_swhid( | ||||
"anchor": gen_swhid(REVISION, revision), | "anchor": gen_swhid(REVISION, revision), | ||||
}, | }, | ||||
) | ) | ||||
assert_contains(resp, swhid) | assert_contains(resp, swhid) | ||||
@given(directory()) | @given(directory()) | ||||
def test_browse_swhid_special_characters_escaping(client, directory): | def test_browse_swhid_special_characters_escaping(client, archive_data, directory): | ||||
origin = "http://example.org/?project=abc;" | origin = "http://example.org/?project=abc;" | ||||
archive_data.origin_add([Origin(url=origin)]) | |||||
origin_swhid_escaped = quote(origin, safe="/?:@&") | origin_swhid_escaped = quote(origin, safe="/?:@&") | ||||
origin_swhid_url_escaped = quote(origin, safe="/:@;") | origin_swhid_url_escaped = quote(origin, safe="/:@;") | ||||
swhid = gen_swhid(DIRECTORY, directory, metadata={"origin": origin_swhid_escaped}) | swhid = gen_swhid(DIRECTORY, directory, metadata={"origin": origin_swhid_escaped}) | ||||
url = reverse("browse-swhid", url_args={"swhid": swhid}) | url = reverse("browse-swhid", url_args={"swhid": swhid}) | ||||
resp = check_html_get_response(client, url, status_code=302) | resp = check_html_get_response(client, url, status_code=302) | ||||
assert origin_swhid_url_escaped in resp["location"] | assert origin_swhid_url_escaped in resp["location"] |