files: fixed breadcrumbs tests and escape of copy path

This commit is contained in:
Daniel Dourvaris 2019-06-10 13:02:08 +02:00
parent 6a2e2f0b94
commit 88f9c583e0
2 changed files with 38 additions and 12 deletions

View file

@ -219,6 +219,7 @@ class _ToolTip(object):
tooltip = _ToolTip()
files_icon = icon = '<i class="file-breadcrumb-copy tooltip icon-clipboard clipboard-action" data-clipboard-text="{}" title="Copy the full path"></i>'
def files_breadcrumbs(repo_name, commit_id, file_path, at_ref=None, limit_items=False):
if isinstance(file_path, str):
@ -265,7 +266,7 @@ def files_breadcrumbs(repo_name, commit_id, file_path, at_ref=None, limit_items=
url_segments = limited_url_segments
full_path = file_path
icon = '<i class="file-breadcrumb-copy tooltip icon-clipboard clipboard-action" data-clipboard-text="{}" title="Copy the full path"></i>'.format(full_path)
icon = files_icon.format(escape(full_path))
if file_path == '':
return root_name
else:

View file

@ -40,27 +40,52 @@ def test_urlify_text(url, expected_url):
@pytest.mark.parametrize('repo_name, commit_id, path, expected_result', [
# Simple case 1
('repo', 'commit', 'a/b',
'<a href="/repo/files/commit/"><i class="icon-home"></i></a>'
' / '
'<a href="/repo/files/commit/a">a</a>'
' / '
'b'),
# Simple case
('rX<X', 'cX<X', 'pX<X/aX<X/bX<X',
'<a href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/'
'<a href="/rX%3CX/files/cX%3CX/pX%3CX">pX&lt;X</a>/'
'<a href="/rX%3CX/files/cX%3CX/pX%3CX/aX%3CX">aX&lt;X'
'</a>/bX&lt;X'),
'<a href="/rX%3CX/files/cX%3CX/"><i class="icon-home"></i></a>'
' / '
'<a href="/rX%3CX/files/cX%3CX/pX%3CX">pX&lt;X</a>'
' / '
'<a href="/rX%3CX/files/cX%3CX/pX%3CX/aX%3CX">aX&lt;X</a>'
' / '
'bX&lt;X'),
# Path with only one segment
('rX<X', 'cX<X', 'pX<X',
'<a href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/pX&lt;X'),
'<a href="/rX%3CX/files/cX%3CX/"><i class="icon-home"></i></a>'
' / '
'pX&lt;X'),
# Empty path
('rX<X', 'cX<X', '', 'rX&lt;X'),
('rX<X', 'cX<X', '',
'<i class="icon-home"></i>'),
# simple quote
('rX"X', 'cX"X', 'pX"X/aX"X/bX"X',
'<a href="/rX%22X/files/cX%22X/">rX&#34;X</a>/'
'<a href="/rX%22X/files/cX%22X/pX%22X">pX&#34;X</a>/'
'<a href="/rX%22X/files/cX%22X/pX%22X/aX%22X">aX&#34;X'
'</a>/bX&#34;X'),
], ids=['simple', 'one_segment', 'empty_path', 'simple_quote'])
'<a href="/rX%22X/files/cX%22X/"><i class="icon-home"></i></a>'
' / '
'<a href="/rX%22X/files/cX%22X/pX%22X">pX&#34;X</a>'
' / '
'<a href="/rX%22X/files/cX%22X/pX%22X/aX%22X">aX&#34;X</a>'
' / '
'bX&#34;X'),
], ids=['simple1', 'simple2', 'one_segment', 'empty_path', 'simple_quote'])
def test_files_breadcrumbs_xss(
repo_name, commit_id, path, app, expected_result):
result = helpers.files_breadcrumbs(repo_name, commit_id, path)
# Expect it to encode all path fragments properly. This is important
# because it returns an instance of `literal`.
if path != '':
expected_result = expected_result + helpers.files_icon.format(helpers.escape(path))
assert result == expected_result