Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_templatetags.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-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 | ||||
import pytest | import pytest | ||||
from swh.web.common.swh_templatetags import ( | from swh.web.common.swh_templatetags import ( | ||||
urlize_links_and_mails, urlize_header_links, safe_docstring_display | urlize_links_and_mails, urlize_header_links, safe_docstring_display | ||||
) | ) | ||||
def test_urlize_api_links_api(): | def test_urlize_api_links_api(request_factory): | ||||
request = request_factory.get('/some/endpoint') | |||||
# update api link with html links content with links | |||||
content = '/api/1/abc/' | |||||
expected_content = '<a href="/api/1/abc/">/api/1/abc/</a>' | |||||
assert urlize_links_and_mails(content, request) == expected_content | |||||
def test_urlize_api_links_api_remove_prefix(request_factory): | |||||
request = request_factory.get('/some/endpoint') | |||||
# update api link with html links content with links | # update api link with html links content with links | ||||
content = '{"url": "/api/1/abc/"}' | content = request.build_absolute_uri('/api/1/abc/') | ||||
expected_content = ('{"url": "<a href="/api/1/abc/">/api/1/abc/</a>"}') | expected_content = ('<a href="/api/1/abc/">/api/1/abc/</a>') | ||||
assert urlize_links_and_mails(content, request) == expected_content | |||||
def test_urlize_api_links_browse(request_factory): | |||||
request = request_factory.get('/some/endpoint') | |||||
# update /browse link with html links content with links | |||||
content = '/browse/def/' | |||||
expected_content = '<a href="/browse/def/">/browse/def/</a>' | |||||
assert urlize_links_and_mails(content, request) == expected_content | |||||
assert urlize_links_and_mails(content) == expected_content | |||||
def test_urlize_api_links_browse_remove_prefix(request_factory): | |||||
request = request_factory.get('/some/endpoint') | |||||
def test_urlize_api_links_browse(): | |||||
# update /browse link with html links content with links | # update /browse link with html links content with links | ||||
content = '{"url": "/browse/def/"}' | content = request.build_absolute_uri('/browse/def/') | ||||
expected_content = ('{"url": "<a href="/browse/def/">' | expected_content = '<a href="/browse/def/">/browse/def/</a>' | ||||
'/browse/def/</a>"}') | assert urlize_links_and_mails(content, request) == expected_content | ||||
assert urlize_links_and_mails(content) == expected_content | |||||
def test_urlize_header_links(request_factory): | |||||
request = request_factory.get('/some/endpoint') | |||||
def test_urlize_header_links(): | |||||
# update api link with html links content with links | # update api link with html links content with links | ||||
content = '</api/1/abc/>; rel="next"\n</api/1/def/>; rel="prev"' | content = '</api/1/abc/>; rel="next"\n</api/1/def/>; rel="prev"' | ||||
expected_content = ('<<a href="/api/1/abc/">/api/1/abc/</a>>; rel="next"\n' | expected_content = ('<<a href="/api/1/abc/">/api/1/abc/</a>>; rel="next"\n' | ||||
'<<a href="/api/1/def/">/api/1/def/</a>>; rel="prev"') | '<<a href="/api/1/def/">/api/1/def/</a>>; rel="prev"') | ||||
assert urlize_header_links(content) == expected_content | assert urlize_header_links(content, request) == expected_content | ||||
def test_urlize_header_links_remove_prefix(request_factory): | |||||
request = request_factory.get('/some/endpoint') | |||||
# update api link with html links content with links | |||||
content = (f'<{request.build_absolute_uri("/api/1/abc/")}>; rel="next"\n' | |||||
f'<{request.build_absolute_uri("/api/1/def/")}>; rel="prev"') | |||||
expected_content = ('<<a href="/api/1/abc/">/api/1/abc/</a>>; rel="next"\n' | |||||
'<<a href="/api/1/def/">/api/1/def/</a>>; rel="prev"') | |||||
assert urlize_header_links(content, request) == expected_content | |||||
# remove deprecation warnings related to docutils | # remove deprecation warnings related to docutils | ||||
@pytest.mark.filterwarnings( | @pytest.mark.filterwarnings( | ||||
'ignore:.*U.*mode is deprecated:DeprecationWarning') | 'ignore:.*U.*mode is deprecated:DeprecationWarning') | ||||
def test_safe_docstring_display(): | def test_safe_docstring_display(): | ||||
# update api link with html links content with links | # update api link with html links content with links | ||||
docstring = ( | docstring = ( | ||||
Show All 18 Lines |