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