Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/views/test_identifiers.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-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 | ||||
from urllib.parse import quote | from urllib.parse import quote | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.model import Origin | from swh.model.model import Origin | ||||
from swh.model.swhids import ObjectType | from swh.model.swhids import ObjectType | ||||
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 directory, origin, release, revision, snapshot | from swh.web.tests.strategies import origin, release, revision, snapshot | ||||
from swh.web.tests.utils import check_html_get_response | from swh.web.tests.utils import check_html_get_response | ||||
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"] | ||||
swhid = gen_swhid(ObjectType.CONTENT, cnt_sha1_git) | swhid = gen_swhid(ObjectType.CONTENT, cnt_sha1_git) | ||||
for swhid_ in (swhid, swhid.upper()): | for swhid_ in (swhid, swhid.upper()): | ||||
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", url_args={"query_string": query_string} | "browse-content", url_args={"query_string": query_string} | ||||
) | ) | ||||
resp = check_html_get_response(client, url, status_code=302) | resp = check_html_get_response(client, url, status_code=302) | ||||
assert resp["location"] == content_browse_url | assert resp["location"] == content_browse_url | ||||
@given(directory()) | |||||
def test_directory_id_browse(client, directory): | def test_directory_id_browse(client, directory): | ||||
swhid = gen_swhid(ObjectType.DIRECTORY, directory) | swhid = gen_swhid(ObjectType.DIRECTORY, directory) | ||||
for swhid_ in (swhid, swhid.upper()): | for swhid_ in (swhid, swhid.upper()): | ||||
url = reverse("browse-swhid", url_args={"swhid": swhid_}) | url = reverse("browse-swhid", url_args={"swhid": swhid_}) | ||||
directory_browse_url = reverse( | directory_browse_url = reverse( | ||||
"browse-directory", url_args={"sha1_git": directory} | "browse-directory", url_args={"sha1_git": directory} | ||||
▲ Show 20 Lines • Show All 169 Lines • ▼ Show 20 Lines | def test_legacy_swhid_browse(archive_data, client, origin): | ||||
resp = check_html_get_response(client, url, status_code=302) | resp = check_html_get_response(client, url, status_code=302) | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, resp["location"], status_code=200, template_used="browse/content.html" | client, resp["location"], status_code=200, template_used="browse/content.html" | ||||
) | ) | ||||
assert_contains(resp, swhid) | assert_contains(resp, swhid) | ||||
@given(directory()) | |||||
def test_browse_swhid_special_characters_escaping(client, archive_data, 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)]) | 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( | swhid = gen_swhid( | ||||
ObjectType.DIRECTORY, directory, metadata={"origin": origin_swhid_escaped} | ObjectType.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"] |