Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/test_utils.py
- This file was copied to swh/web/tests/browse/test_snapshot_context.py.
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 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 | ||||
from hypothesis import given | from swh.web.browse.utils import ( | ||||
get_mimetype_and_encoding_for_content, | |||||
from swh.web.browse import utils | gen_link, | ||||
from swh.web.common.utils import reverse, format_utc_iso_date | gen_revision_link, | ||||
from swh.web.tests.strategies import origin_with_multiple_visits | gen_person_mail_link, | ||||
) | |||||
from swh.web.common.utils import reverse | |||||
def test_get_mimetype_and_encoding_for_content(): | def test_get_mimetype_and_encoding_for_content(): | ||||
text = b"Hello world!" | text = b"Hello world!" | ||||
assert utils.get_mimetype_and_encoding_for_content(text) == ( | assert get_mimetype_and_encoding_for_content(text) == ("text/plain", "us-ascii",) | ||||
"text/plain", | |||||
"us-ascii", | |||||
) | |||||
@given(origin_with_multiple_visits()) | |||||
def test_get_origin_visit_snapshot_simple(archive_data, origin): | |||||
visits = archive_data.origin_visit_get(origin["url"]) | |||||
for visit in visits: | |||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | |||||
branches = [] | |||||
releases = [] | |||||
def _process_branch_data(branch, branch_data): | |||||
if branch_data["target_type"] == "revision": | |||||
rev_data = archive_data.revision_get(branch_data["target"]) | |||||
branches.append( | |||||
{ | |||||
"name": branch, | |||||
"revision": branch_data["target"], | |||||
"directory": rev_data["directory"], | |||||
"date": format_utc_iso_date(rev_data["date"]), | |||||
"message": rev_data["message"], | |||||
} | |||||
) | |||||
elif branch_data["target_type"] == "release": | |||||
rel_data = archive_data.release_get(branch_data["target"]) | |||||
rev_data = archive_data.revision_get(rel_data["target"]) | |||||
releases.append( | |||||
{ | |||||
"name": rel_data["name"], | |||||
"branch_name": branch, | |||||
"date": format_utc_iso_date(rel_data["date"]), | |||||
"id": rel_data["id"], | |||||
"message": rel_data["message"], | |||||
"target_type": rel_data["target_type"], | |||||
"target": rel_data["target"], | |||||
"directory": rev_data["directory"], | |||||
} | |||||
) | |||||
for branch in sorted(snapshot["branches"].keys()): | |||||
branch_data = snapshot["branches"][branch] | |||||
if branch_data["target_type"] == "alias": | |||||
target_data = snapshot["branches"][branch_data["target"]] | |||||
_process_branch_data(branch, target_data) | |||||
else: | |||||
_process_branch_data(branch, branch_data) | |||||
assert branches and releases, "Incomplete test data." | |||||
origin_visit_branches = utils.get_origin_visit_snapshot( | |||||
origin, visit_id=visit["visit"] | |||||
) | |||||
assert origin_visit_branches == (branches, releases) | |||||
def test_gen_link(): | def test_gen_link(): | ||||
assert ( | assert ( | ||||
utils.gen_link("https://www.softwareheritage.org/", "swh") | gen_link("https://www.softwareheritage.org/", "swh") | ||||
== '<a href="https://www.softwareheritage.org/">swh</a>' | == '<a href="https://www.softwareheritage.org/">swh</a>' | ||||
) | ) | ||||
def test_gen_revision_link(): | def test_gen_revision_link(): | ||||
revision_id = "28a0bc4120d38a394499382ba21d6965a67a3703" | revision_id = "28a0bc4120d38a394499382ba21d6965a67a3703" | ||||
revision_url = reverse("browse-revision", url_args={"sha1_git": revision_id}) | revision_url = reverse("browse-revision", url_args={"sha1_git": revision_id}) | ||||
assert utils.gen_revision_link( | assert gen_revision_link( | ||||
revision_id, link_text=None, link_attrs=None | revision_id, link_text=None, link_attrs=None | ||||
) == '<a href="%s">%s</a>' % (revision_url, revision_id) | ) == '<a href="%s">%s</a>' % (revision_url, revision_id) | ||||
assert utils.gen_revision_link( | assert gen_revision_link( | ||||
revision_id, shorten_id=True, link_attrs=None | revision_id, shorten_id=True, link_attrs=None | ||||
) == '<a href="%s">%s</a>' % (revision_url, revision_id[:7]) | ) == '<a href="%s">%s</a>' % (revision_url, revision_id[:7]) | ||||
def test_gen_person_mail_link(): | def test_gen_person_mail_link(): | ||||
person_full = { | person_full = { | ||||
"name": "John Doe", | "name": "John Doe", | ||||
"email": "john.doe@swh.org", | "email": "john.doe@swh.org", | ||||
"fullname": "John Doe <john.doe@swh.org>", | "fullname": "John Doe <john.doe@swh.org>", | ||||
} | } | ||||
assert utils.gen_person_mail_link(person_full) == '<a href="mailto:%s">%s</a>' % ( | assert gen_person_mail_link(person_full) == '<a href="mailto:%s">%s</a>' % ( | ||||
person_full["email"], | person_full["email"], | ||||
person_full["name"], | person_full["name"], | ||||
) | ) | ||||
link_text = "Mail" | link_text = "Mail" | ||||
assert utils.gen_person_mail_link( | assert gen_person_mail_link( | ||||
person_full, link_text=link_text | person_full, link_text=link_text | ||||
) == '<a href="mailto:%s">%s</a>' % (person_full["email"], link_text) | ) == '<a href="mailto:%s">%s</a>' % (person_full["email"], link_text) | ||||
person_partial_email = {"name": None, "email": None, "fullname": "john.doe@swh.org"} | person_partial_email = {"name": None, "email": None, "fullname": "john.doe@swh.org"} | ||||
assert utils.gen_person_mail_link( | assert gen_person_mail_link( | ||||
person_partial_email | person_partial_email | ||||
) == '<a href="mailto:%s">%s</a>' % ( | ) == '<a href="mailto:%s">%s</a>' % ( | ||||
person_partial_email["fullname"], | person_partial_email["fullname"], | ||||
person_partial_email["fullname"], | person_partial_email["fullname"], | ||||
) | ) | ||||
person_partial = { | person_partial = { | ||||
"name": None, | "name": None, | ||||
"email": None, | "email": None, | ||||
"fullname": "John Doe <john.doe@swh.org>", | "fullname": "John Doe <john.doe@swh.org>", | ||||
} | } | ||||
assert utils.gen_person_mail_link(person_partial) == person_partial["fullname"] | assert gen_person_mail_link(person_partial) == person_partial["fullname"] | ||||
person_none = {"name": None, "email": None, "fullname": None} | person_none = {"name": None, "email": None, "fullname": None} | ||||
assert utils.gen_person_mail_link(person_none) == "None" | assert gen_person_mail_link(person_none) == "None" |