##// END OF EJS Templates
tests: fixed tests for helpers.
marcink -
r273:c45a42ca default
parent child Browse files
Show More
@@ -1,196 +1,210 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import copy
21 import copy
22 import mock
22 import mock
23 import pytest
23 import pytest
24
24
25 from pylons.util import ContextObj
25 from pylons.util import ContextObj
26
26
27 from rhodecode.lib import helpers
27 from rhodecode.lib import helpers
28 from rhodecode.lib.utils2 import AttributeDict
28 from rhodecode.lib.utils2 import AttributeDict
29 from rhodecode.model.settings import IssueTrackerSettingsModel
29 from rhodecode.model.settings import IssueTrackerSettingsModel
30
30
31
31
32 @pytest.mark.parametrize('url, expected_url', [
32 @pytest.mark.parametrize('url, expected_url', [
33 ('http://rc.rc/test', '<a href="http://rc.rc/test">http://rc.rc/test</a>'),
33 ('http://rc.rc/test', '<a href="http://rc.rc/test">http://rc.rc/test</a>'),
34 ('http://rc.rc/@foo', '<a href="http://rc.rc/@foo">http://rc.rc/@foo</a>'),
34 ('http://rc.rc/@foo', '<a href="http://rc.rc/@foo">http://rc.rc/@foo</a>'),
35 ('http://rc.rc/!foo', '<a href="http://rc.rc/!foo">http://rc.rc/!foo</a>'),
35 ('http://rc.rc/!foo', '<a href="http://rc.rc/!foo">http://rc.rc/!foo</a>'),
36 ('http://rc.rc/&foo', '<a href="http://rc.rc/&foo">http://rc.rc/&foo</a>'),
36 ('http://rc.rc/&foo', '<a href="http://rc.rc/&foo">http://rc.rc/&foo</a>'),
37 ('http://rc.rc/#foo', '<a href="http://rc.rc/#foo">http://rc.rc/#foo</a>'),
37 ('http://rc.rc/#foo', '<a href="http://rc.rc/#foo">http://rc.rc/#foo</a>'),
38 ])
38 ])
39 def test_urlify_text(url, expected_url):
39 def test_urlify_text(url, expected_url):
40 assert helpers.urlify_text(url) == expected_url
40 assert helpers.urlify_text(url) == expected_url
41
41
42
42
43 @pytest.mark.parametrize('repo_name, commit_id, path, expected_result', [
43 @pytest.mark.parametrize('repo_name, commit_id, path, expected_result', [
44 ('rX<X', 'cX<X', 'pX<X/aX<X/bX<X',
44 ('rX<X', 'cX<X', 'pX<X/aX<X/bX<X',
45 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/'
45 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/'
46 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/pX%3CX">pX&lt;X</a>/'
46 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/pX%3CX">pX&lt;X</a>/'
47 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/pX%3CX/aX%3CX">aX&lt;X'
47 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/pX%3CX/aX%3CX">aX&lt;X'
48 '</a>/bX&lt;X'),
48 '</a>/bX&lt;X'),
49 # Path with only one segment
49 # Path with only one segment
50 ('rX<X', 'cX<X', 'pX<X',
50 ('rX<X', 'cX<X', 'pX<X',
51 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/pX&lt;X'),
51 '<a class="pjax-link" href="/rX%3CX/files/cX%3CX/">rX&lt;X</a>/pX&lt;X'),
52 # Empty path
52 # Empty path
53 ('rX<X', 'cX<X', '', 'rX&lt;X'),
53 ('rX<X', 'cX<X', '', 'rX&lt;X'),
54 ('rX"X', 'cX"X', 'pX"X/aX"X/bX"X',
54 ('rX"X', 'cX"X', 'pX"X/aX"X/bX"X',
55 '<a class="pjax-link" href="/rX%22X/files/cX%22X/">rX&#34;X</a>/'
55 '<a class="pjax-link" href="/rX%22X/files/cX%22X/">rX&#34;X</a>/'
56 '<a class="pjax-link" href="/rX%22X/files/cX%22X/pX%22X">pX&#34;X</a>/'
56 '<a class="pjax-link" href="/rX%22X/files/cX%22X/pX%22X">pX&#34;X</a>/'
57 '<a class="pjax-link" href="/rX%22X/files/cX%22X/pX%22X/aX%22X">aX&#34;X'
57 '<a class="pjax-link" href="/rX%22X/files/cX%22X/pX%22X/aX%22X">aX&#34;X'
58 '</a>/bX&#34;X'),
58 '</a>/bX&#34;X'),
59 ], ids=['simple', 'one_segment', 'empty_path', 'simple_quote'])
59 ], ids=['simple', 'one_segment', 'empty_path', 'simple_quote'])
60 def test_files_breadcrumbs_xss(
60 def test_files_breadcrumbs_xss(
61 repo_name, commit_id, path, pylonsapp, expected_result):
61 repo_name, commit_id, path, pylonsapp, expected_result):
62 result = helpers.files_breadcrumbs(repo_name, commit_id, path)
62 result = helpers.files_breadcrumbs(repo_name, commit_id, path)
63 # Expect it to encode all path fragments properly. This is important
63 # Expect it to encode all path fragments properly. This is important
64 # because it returns an instance of `literal`.
64 # because it returns an instance of `literal`.
65 assert result == expected_result
65 assert result == expected_result
66
66
67
67
68 def test_format_binary():
68 def test_format_binary():
69 assert helpers.format_byte_size_binary(298489462784) == '278.0 GiB'
69 assert helpers.format_byte_size_binary(298489462784) == '278.0 GiB'
70
70
71
71
72 @pytest.mark.parametrize('text_string, pattern, expected_text', [
72 @pytest.mark.parametrize('text_string, pattern, expected_text', [
73 ('Fix #42', '(?:#)(?P<issue_id>\d+)',
73 ('Fix #42', '(?:#)(?P<issue_id>\d+)',
74 'Fix <a class="issue-tracker-link" href="http://r.io/{repo}/i/42">#42</a>'
74 'Fix <a class="issue-tracker-link" href="http://r.io/{repo}/i/42">#42</a>'
75 ),
75 ),
76 ('Fix #42', '(?:#)?<issue_id>\d+)', 'Fix #42'), # Broken regex
76 ('Fix #42', '(?:#)?<issue_id>\d+)', 'Fix #42'), # Broken regex
77 ])
77 ])
78 def test_process_patterns_repo(backend, text_string, pattern, expected_text):
78 def test_process_patterns_repo(backend, text_string, pattern, expected_text):
79 repo = backend.create_repo()
79 repo = backend.create_repo()
80 config = {'123': {
80 config = {'123': {
81 'uid': '123',
81 'uid': '123',
82 'pat': pattern,
82 'pat': pattern,
83 'url': 'http://r.io/${repo}/i/${issue_id}',
83 'url': 'http://r.io/${repo}/i/${issue_id}',
84 'pref': '#',
84 'pref': '#',
85 }
85 }
86 }
86 }
87
88 def get_settings_mock(self, cache=True):
89 return config
90
87 with mock.patch.object(IssueTrackerSettingsModel,
91 with mock.patch.object(IssueTrackerSettingsModel,
88 'get_settings', lambda s: config):
92 'get_settings', get_settings_mock):
89 processed_text = helpers.process_patterns(
93 processed_text = helpers.process_patterns(
90 text_string, repo.repo_name, config)
94 text_string, repo.repo_name, config)
91
95
92 assert processed_text == expected_text.format(repo=repo.repo_name)
96 assert processed_text == expected_text.format(repo=repo.repo_name)
93
97
94
98
95 @pytest.mark.parametrize('text_string, pattern, expected_text', [
99 @pytest.mark.parametrize('text_string, pattern, expected_text', [
96 ('Fix #42', '(?:#)(?P<issue_id>\d+)',
100 ('Fix #42', '(?:#)(?P<issue_id>\d+)',
97 'Fix <a class="issue-tracker-link" href="http://r.io/i/42">#42</a>'
101 'Fix <a class="issue-tracker-link" href="http://r.io/i/42">#42</a>'
98 ),
102 ),
99 ('Fix #42', '(?:#)?<issue_id>\d+)', 'Fix #42'), # Broken regex
103 ('Fix #42', '(?:#)?<issue_id>\d+)', 'Fix #42'), # Broken regex
100 ])
104 ])
101 def test_process_patterns_no_repo(text_string, pattern, expected_text):
105 def test_process_patterns_no_repo(text_string, pattern, expected_text):
102 config = {'123': {
106 config = {'123': {
103 'uid': '123',
107 'uid': '123',
104 'pat': pattern,
108 'pat': pattern,
105 'url': 'http://r.io/i/${issue_id}',
109 'url': 'http://r.io/i/${issue_id}',
106 'pref': '#',
110 'pref': '#',
107 }
111 }
108 }
112 }
113
114 def get_settings_mock(self, cache=True):
115 return config
116
109 with mock.patch.object(IssueTrackerSettingsModel,
117 with mock.patch.object(IssueTrackerSettingsModel,
110 'get_global_settings', lambda s, cache: config):
118 'get_global_settings', get_settings_mock):
111 processed_text = helpers.process_patterns(
119 processed_text = helpers.process_patterns(
112 text_string, '', config)
120 text_string, '', config)
113
121
114 assert processed_text == expected_text
122 assert processed_text == expected_text
115
123
116
124
117 def test_process_patterns_non_existent_repo_name(backend):
125 def test_process_patterns_non_existent_repo_name(backend):
118 text_string = 'Fix #42'
126 text_string = 'Fix #42'
119 pattern = '(?:#)(?P<issue_id>\d+)'
127 pattern = '(?:#)(?P<issue_id>\d+)'
120 expected_text = ('Fix <a class="issue-tracker-link" '
128 expected_text = ('Fix <a class="issue-tracker-link" '
121 'href="http://r.io/do-not-exist/i/42">#42</a>')
129 'href="http://r.io/do-not-exist/i/42">#42</a>')
122 config = {'123': {
130 config = {'123': {
123 'uid': '123',
131 'uid': '123',
124 'pat': pattern,
132 'pat': pattern,
125 'url': 'http://r.io/${repo}/i/${issue_id}',
133 'url': 'http://r.io/${repo}/i/${issue_id}',
126 'pref': '#',
134 'pref': '#',
127 }
135 }
128 }
136 }
137
138 def get_settings_mock(self, cache=True):
139 return config
140
129 with mock.patch.object(IssueTrackerSettingsModel,
141 with mock.patch.object(IssueTrackerSettingsModel,
130 'get_global_settings', lambda s, cache: config):
142 'get_global_settings', get_settings_mock):
131 processed_text = helpers.process_patterns(
143 processed_text = helpers.process_patterns(
132 text_string, 'do-not-exist', config)
144 text_string, 'do-not-exist', config)
133
145
134 assert processed_text == expected_text
146 assert processed_text == expected_text
135
147
136
148
137 def test_get_visual_attr(pylonsapp):
149 def test_get_visual_attr(pylonsapp):
138 c = ContextObj()
150 c = ContextObj()
139 assert None is helpers.get_visual_attr(c, 'fakse')
151 assert None is helpers.get_visual_attr(c, 'fakse')
140
152
141 # emulate the c.visual behaviour
153 # emulate the c.visual behaviour
142 c.visual = AttributeDict({})
154 c.visual = AttributeDict({})
143 assert None is helpers.get_visual_attr(c, 'some_var')
155 assert None is helpers.get_visual_attr(c, 'some_var')
144
156
145 c.visual.some_var = 'foobar'
157 c.visual.some_var = 'foobar'
146 assert 'foobar' == helpers.get_visual_attr(c, 'some_var')
158 assert 'foobar' == helpers.get_visual_attr(c, 'some_var')
147
159
148
160
149 @pytest.mark.parametrize('test_text, inclusive, expected_text', [
161 @pytest.mark.parametrize('test_text, inclusive, expected_text', [
150 ('just a string', False, 'just a string'),
162 ('just a string', False, 'just a string'),
151 ('just a string\n', False, 'just a string'),
163 ('just a string\n', False, 'just a string'),
152 ('just a string\n next line', False, 'just a string...'),
164 ('just a string\n next line', False, 'just a string...'),
153 ('just a string\n next line', True, 'just a string\n...'),
165 ('just a string\n next line', True, 'just a string\n...'),
154 ])
166 ])
155 def test_chop_at(test_text, inclusive, expected_text):
167 def test_chop_at(test_text, inclusive, expected_text):
156 assert helpers.chop_at_smart(
168 assert helpers.chop_at_smart(
157 test_text, '\n', inclusive, '...') == expected_text
169 test_text, '\n', inclusive, '...') == expected_text
158
170
159
171
160 @pytest.mark.parametrize('test_text, expected_output', [
172 @pytest.mark.parametrize('test_text, expected_output', [
161 ('some text', ['some', 'text']),
173 ('some text', ['some', 'text']),
162 ('some text', ['some', 'text']),
174 ('some text', ['some', 'text']),
163 ('some text "with a phrase"', ['some', 'text', 'with a phrase']),
175 ('some text "with a phrase"', ['some', 'text', 'with a phrase']),
164 ('"a phrase" "another phrase"', ['a phrase', 'another phrase']),
176 ('"a phrase" "another phrase"', ['a phrase', 'another phrase']),
165 ('"justphrase"', ['justphrase']),
177 ('"justphrase"', ['justphrase']),
166 ('""', []),
178 ('""', []),
167 ('', []),
179 ('', []),
168 (' ', []),
180 (' ', []),
169 ('" "', []),
181 ('" "', []),
170 ])
182 ])
171 def test_extract_phrases(test_text, expected_output):
183 def test_extract_phrases(test_text, expected_output):
172 assert helpers.extract_phrases(test_text) == expected_output
184 assert helpers.extract_phrases(test_text) == expected_output
173
185
174
186
175 @pytest.mark.parametrize('test_text, text_phrases, expected_output', [
187 @pytest.mark.parametrize('test_text, text_phrases, expected_output', [
176 ('some text here', ['some', 'here'], [(0, 4), (10, 14)]),
188 ('some text here', ['some', 'here'], [(0, 4), (10, 14)]),
177 ('here here there', ['here'], [(0, 4), (5, 9), (11, 15)]),
189 ('here here there', ['here'], [(0, 4), (5, 9), (11, 15)]),
178 ('irrelevant', ['not found'], []),
190 ('irrelevant', ['not found'], []),
179 ('irrelevant', ['not found'], []),
191 ('irrelevant', ['not found'], []),
180 ])
192 ])
181 def test_get_matching_offsets(test_text, text_phrases, expected_output):
193 def test_get_matching_offsets(test_text, text_phrases, expected_output):
182 assert helpers.get_matching_offsets(
194 assert helpers.get_matching_offsets(
183 test_text, text_phrases) == expected_output
195 test_text, text_phrases) == expected_output
184
196
197
185 def test_normalize_text_for_matching():
198 def test_normalize_text_for_matching():
186 assert helpers.normalize_text_for_matching(
199 assert helpers.normalize_text_for_matching(
187 'OJjfe)*#$*@)$JF*)3r2f80h') == 'ojjfe jf 3r2f80h'
200 'OJjfe)*#$*@)$JF*)3r2f80h') == 'ojjfe jf 3r2f80h'
188
201
202
189 def test_get_matching_line_offsets():
203 def test_get_matching_line_offsets():
190 assert helpers.get_matching_line_offsets([
204 assert helpers.get_matching_line_offsets([
191 'words words words',
205 'words words words',
192 'words words words',
206 'words words words',
193 'some text some',
207 'some text some',
194 'words words words',
208 'words words words',
195 'words words words',
209 'words words words',
196 'text here what'], 'text') == {3: [(5, 9)], 6: [(0, 4)]} No newline at end of file
210 'text here what'], 'text') == {3: [(5, 9)], 6: [(0, 4)]}
General Comments 0
You need to be logged in to leave comments. Login now