##// END OF EJS Templates
chore(deps): bumped gevent to 24.2.1 to address latest python release problems
chore(deps): bumped gevent to 24.2.1 to address latest python release problems

File last commit:

r5187:51a848c7 default
r5301:49f472a9 default
Show More
test_helpers.py
240 lines | 8.6 KiB | text/x-python | PythonLexer
project: added all source files and assets
r1
copyrights: updated for 2023
r5088 # Copyright (C) 2010-2023 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import copy
import mock
import pytest
from rhodecode.lib import helpers
from rhodecode.lib.utils2 import AttributeDict
from rhodecode.model.settings import IssueTrackerSettingsModel
home: moved home and repo group views into pyramid....
r1774 from rhodecode.tests import no_newline_id_generator
project: added all source files and assets
r1
@pytest.mark.parametrize('url, expected_url', [
helpers: fix tests and tag replacers
r5152 (r'https://rc.com', '<a href="https://rc.com">https://rc.com</a>'),
code: fixes to escape characters improperly used
r5149 (r'https://rc.com/test', '<a href="https://rc.com/test">https://rc.com/test</a>'),
(r'https://rc.com/!foo', '<a href="https://rc.com/!foo">https://rc.com/!foo</a>'),
(r'https://rc.com/&foo', '<a href="https://rc.com/&amp;foo">https://rc.com/&amp;foo</a>'),
(r'https://rc.com/?foo-1&bar=1', '<a href="https://rc.com/?foo-1&amp;bar=1">https://rc.com/?foo-1&amp;bar=1</a>'),
(r'https://rc.com?foo-1&bar=1', '<a href="https://rc.com?foo-1&amp;bar=1">https://rc.com?foo-1&amp;bar=1</a>'),
(r'https://rc.com/#foo', '<a href="https://rc.com/#foo">https://rc.com/#foo</a>'),
(r'https://rc.com/@foo', '<a href="https://rc.com/@foo">https://rc.com/@foo</a>'),
project: added all source files and assets
r1 ])
def test_urlify_text(url, expected_url):
assert helpers.urlify_text(url) == expected_url
@pytest.mark.parametrize('repo_name, commit_id, path, expected_result', [
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 # 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
project: added all source files and assets
r1 ('rX<X', 'cX<X', 'pX<X/aX<X/bX<X',
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 '<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'),
project: added all source files and assets
r1 # Path with only one segment
('rX<X', 'cX<X', 'pX<X',
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 '<a href="/rX%3CX/files/cX%3CX/"><i class="icon-home"></i></a>'
' / '
'pX&lt;X'),
project: added all source files and assets
r1 # Empty path
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 ('rX<X', 'cX<X', '',
'<i class="icon-home"></i>'),
# simple quote
project: added all source files and assets
r1 ('rX"X', 'cX"X', 'pX"X/aX"X/bX"X',
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 '<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'])
files: use ref names in the url, and make usage of default landing refs....
r4372 def test_files_breadcrumbs_xss(repo_name, commit_id, path, app, expected_result):
result = helpers.files_breadcrumbs(repo_name, 'hg', commit_id, path)
project: added all source files and assets
r1 # Expect it to encode all path fragments properly. This is important
# because it returns an instance of `literal`.
dan
files: fixed breadcrumbs tests and escape of copy path
r3741 if path != '':
expected_result = expected_result + helpers.files_icon.format(helpers.escape(path))
project: added all source files and assets
r1 assert result == expected_result
def test_format_binary():
assert helpers.format_byte_size_binary(298489462784) == '278.0 GiB'
dan
integrations: add integration support...
r411 @pytest.mark.parametrize('text_string, pattern, expected', [
code: fixes to escape characters improperly used
r5149 ('No issue here', r'(?:#)(?P<issue_id>\d+)', []),
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)(?P<issue_id>\d+)',
code: fixes to escape characters improperly used
r5149 [{'url': 'https://r.io/{repo}/i/42', 'id': '42'}]),
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42, #53', r'(?:#)(?P<issue_id>\d+)', [
code: fixes to escape characters improperly used
r5149 {'url': 'https://r.io/{repo}/i/42', 'id': '42'},
{'url': 'https://r.io/{repo}/i/53', 'id': '53'}]),
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)?<issue_id>\d+)', []), # Broken regex
dan
integrations: add integration support...
r411 ])
def test_extract_issues(backend, text_string, pattern, expected):
repo = backend.create_repo()
config = {
'123': {
'uid': '123',
'pat': pattern,
code: fixes to escape characters improperly used
r5149 'url': r'https://r.io/${repo}/i/${issue_id}',
dan
integrations: add integration support...
r411 'pref': '#',
tooltips: small fixes/tests fixes.
r4033 'desc': 'Test Pattern'
dan
integrations: add integration support...
r411 }
}
def get_settings_mock(self, cache=True):
return config
with mock.patch.object(IssueTrackerSettingsModel,
'get_settings', get_settings_mock):
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 text, issues, errors = helpers.process_patterns(text_string, repo.repo_name)
dan
integrations: add integration support...
r411
expected = copy.deepcopy(expected)
for item in expected:
item['url'] = item['url'].format(repo=repo.repo_name)
assert issues == expected
comments: allow parsing the issue tracker patterns inside the markup...
r1672 @pytest.mark.parametrize('text_string, pattern, link_format, expected_text', [
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)(?P<issue_id>\d+)', 'html',
helpers: fix tests and tag replacers
r5152 'Fix <a class="tooltip issue-tracker-link" href="https://r.io/{repo}/i/42" title="Test Pattern">#42</a>'),
comments: allow parsing the issue tracker patterns inside the markup...
r1672
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)(?P<issue_id>\d+)', 'markdown',
helpers: fix tests and tag replacers
r5152 'Fix [#42](https://r.io/{repo}/i/42)'),
comments: allow parsing the issue tracker patterns inside the markup...
r1672
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)(?P<issue_id>\d+)', 'rst',
helpers: fix tests and tag replacers
r5152 'Fix `#42 <https://r.io/{repo}/i/42>`_'),
comments: allow parsing the issue tracker patterns inside the markup...
r1672
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)?<issue_id>\d+)', 'html',
comments: allow parsing the issue tracker patterns inside the markup...
r1672 'Fix #42'), # Broken regex
project: added all source files and assets
r1 ])
comments: allow parsing the issue tracker patterns inside the markup...
r1672 def test_process_patterns_repo(backend, text_string, pattern, expected_text, link_format):
project: added all source files and assets
r1 repo = backend.create_repo()
tests: fixed tests for helpers.
r273
def get_settings_mock(self, cache=True):
comments: allow parsing the issue tracker patterns inside the markup...
r1672 return {
'123': {
'uid': '123',
'pat': pattern,
helpers: fix tests and tag replacers
r5152 'url': 'https://r.io/${repo}/i/${issue_id}',
comments: allow parsing the issue tracker patterns inside the markup...
r1672 'pref': '#',
tooltips: small fixes/tests fixes.
r4033 'desc': 'Test Pattern'
comments: allow parsing the issue tracker patterns inside the markup...
r1672 }
}
tests: fixed tests for helpers.
r273
project: added all source files and assets
r1 with mock.patch.object(IssueTrackerSettingsModel,
tests: fixed tests for helpers.
r273 'get_settings', get_settings_mock):
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 processed_text, issues, error = helpers.process_patterns(
comments: allow parsing the issue tracker patterns inside the markup...
r1672 text_string, repo.repo_name, link_format)
project: added all source files and assets
r1
assert processed_text == expected_text.format(repo=repo.repo_name)
@pytest.mark.parametrize('text_string, pattern, expected_text', [
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)(?P<issue_id>\d+)',
helpers: fix tests and tag replacers
r5152 'Fix <a class="tooltip issue-tracker-link" href="https://r.io/i/42" title="Test Pattern">#42</a>'),
chore(regex): properly escape \d used in regexes
r5187 ('Fix #42', r'(?:#)?<issue_id>\d+)',
comments: allow parsing the issue tracker patterns inside the markup...
r1672 'Fix #42'), # Broken regex
project: added all source files and assets
r1 ])
def test_process_patterns_no_repo(text_string, pattern, expected_text):
tests: fixed tests for helpers.
r273
def get_settings_mock(self, cache=True):
comments: allow parsing the issue tracker patterns inside the markup...
r1672 return {
'123': {
'uid': '123',
'pat': pattern,
helpers: fix tests and tag replacers
r5152 'url': 'https://r.io/i/${issue_id}',
comments: allow parsing the issue tracker patterns inside the markup...
r1672 'pref': '#',
tooltips: small fixes/tests fixes.
r4033 'desc': 'Test Pattern'
comments: allow parsing the issue tracker patterns inside the markup...
r1672 }
}
tests: fixed tests for helpers.
r273
project: added all source files and assets
r1 with mock.patch.object(IssueTrackerSettingsModel,
tests: fixed tests for helpers.
r273 'get_global_settings', get_settings_mock):
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 processed_text, issues, errors = helpers.process_patterns(
comments: allow parsing the issue tracker patterns inside the markup...
r1672 text_string, '')
project: added all source files and assets
r1
assert processed_text == expected_text
def test_process_patterns_non_existent_repo_name(backend):
text_string = 'Fix #42'
helpers: fix tests and tag replacers
r5152 pattern = r'(?:#)(?P<issue_id>\d+)'
tooltips: small fixes/tests fixes.
r4033 expected_text = ('Fix <a class="tooltip issue-tracker-link" '
helpers: fix tests and tag replacers
r5152 'href="https://r.io/do-not-exist/i/42" title="Test Pattern">#42</a>')
tests: fixed tests for helpers.
r273
def get_settings_mock(self, cache=True):
comments: allow parsing the issue tracker patterns inside the markup...
r1672 return {
'123': {
'uid': '123',
'pat': pattern,
helpers: fix tests and tag replacers
r5152 'url': 'https://r.io/${repo}/i/${issue_id}',
comments: allow parsing the issue tracker patterns inside the markup...
r1672 'pref': '#',
tooltips: small fixes/tests fixes.
r4033 'desc': 'Test Pattern'
comments: allow parsing the issue tracker patterns inside the markup...
r1672 }
}
tests: fixed tests for helpers.
r273
project: added all source files and assets
r1 with mock.patch.object(IssueTrackerSettingsModel,
tests: fixed tests for helpers.
r273 'get_global_settings', get_settings_mock):
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 processed_text, issues, errors = helpers.process_patterns(
comments: allow parsing the issue tracker patterns inside the markup...
r1672 text_string, 'do-not-exist')
project: added all source files and assets
r1
assert processed_text == expected_text
pylons: remove pylons as dependency...
r2351 def test_get_visual_attr(baseapp):
from rhodecode.apps._base import TemplateArgs
c = TemplateArgs()
project: added all source files and assets
r1 assert None is helpers.get_visual_attr(c, 'fakse')
# emulate the c.visual behaviour
c.visual = AttributeDict({})
assert None is helpers.get_visual_attr(c, 'some_var')
c.visual.some_var = 'foobar'
assert 'foobar' == helpers.get_visual_attr(c, 'some_var')
@pytest.mark.parametrize('test_text, inclusive, expected_text', [
('just a string', False, 'just a string'),
('just a string\n', False, 'just a string'),
('just a string\n next line', False, 'just a string...'),
('just a string\n next line', True, 'just a string\n...'),
home: moved home and repo group views into pyramid....
r1774 ], ids=no_newline_id_generator)
project: added all source files and assets
r1 def test_chop_at(test_text, inclusive, expected_text):
assert helpers.chop_at_smart(
test_text, '\n', inclusive, '...') == expected_text