Show More
@@ -1,294 +1,294 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.tests.test_libs |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | Package for testing various lib/helper functions in rhodecode |
|
8 | 8 | |
|
9 | 9 | :created_on: Jun 9, 2011 |
|
10 | 10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | from __future__ import with_statement |
|
26 | 26 | import datetime |
|
27 | 27 | import hashlib |
|
28 | 28 | import mock |
|
29 | 29 | from rhodecode.tests import * |
|
30 | 30 | |
|
31 | 31 | proto = 'http' |
|
32 | 32 | TEST_URLS = [ |
|
33 | 33 | ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
34 | 34 | '%s://127.0.0.1' % proto), |
|
35 | 35 | ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
36 | 36 | '%s://127.0.0.1' % proto), |
|
37 | 37 | ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
38 | 38 | '%s://127.0.0.1' % proto), |
|
39 | 39 | ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], |
|
40 | 40 | '%s://127.0.0.1:8080' % proto), |
|
41 | 41 | ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], |
|
42 | 42 | '%s://domain.org' % proto), |
|
43 | 43 | ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', |
|
44 | 44 | '8080'], |
|
45 | 45 | '%s://domain.org:8080' % proto), |
|
46 | 46 | ] |
|
47 | 47 | |
|
48 | 48 | proto = 'https' |
|
49 | 49 | TEST_URLS += [ |
|
50 | 50 | ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
51 | 51 | '%s://127.0.0.1' % proto), |
|
52 | 52 | ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
53 | 53 | '%s://127.0.0.1' % proto), |
|
54 | 54 | ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
55 | 55 | '%s://127.0.0.1' % proto), |
|
56 | 56 | ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], |
|
57 | 57 | '%s://127.0.0.1:8080' % proto), |
|
58 | 58 | ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], |
|
59 | 59 | '%s://domain.org' % proto), |
|
60 | 60 | ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', |
|
61 | 61 | '8080'], |
|
62 | 62 | '%s://domain.org:8080' % proto), |
|
63 | 63 | ] |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | class TestLibs(BaseTestCase): |
|
67 | 67 | |
|
68 | 68 | @parameterized.expand(TEST_URLS) |
|
69 | 69 | def test_uri_filter(self, test_url, expected, expected_creds): |
|
70 | 70 | from rhodecode.lib.utils2 import uri_filter |
|
71 | 71 | self.assertEqual(uri_filter(test_url), expected) |
|
72 | 72 | |
|
73 | 73 | @parameterized.expand(TEST_URLS) |
|
74 | 74 | def test_credentials_filter(self, test_url, expected, expected_creds): |
|
75 | 75 | from rhodecode.lib.utils2 import credentials_filter |
|
76 | 76 | self.assertEqual(credentials_filter(test_url), expected_creds) |
|
77 | 77 | |
|
78 | 78 | @parameterized.expand([('t', True), |
|
79 | 79 | ('true', True), |
|
80 | 80 | ('y', True), |
|
81 | 81 | ('yes', True), |
|
82 | 82 | ('on', True), |
|
83 | 83 | ('1', True), |
|
84 | 84 | ('Y', True), |
|
85 | 85 | ('yeS', True), |
|
86 | 86 | ('Y', True), |
|
87 | 87 | ('TRUE', True), |
|
88 | 88 | ('T', True), |
|
89 | 89 | ('False', False), |
|
90 | 90 | ('F', False), |
|
91 | 91 | ('FALSE', False), |
|
92 | 92 | ('0', False), |
|
93 | 93 | ('-1', False), |
|
94 | 94 | ('', False) |
|
95 | 95 | ]) |
|
96 | 96 | def test_str2bool(self, str_bool, expected): |
|
97 | 97 | from rhodecode.lib.utils2 import str2bool |
|
98 | 98 | self.assertEqual(str2bool(str_bool), expected) |
|
99 | 99 | |
|
100 | 100 | def test_mention_extractor(self): |
|
101 | 101 | from rhodecode.lib.utils2 import extract_mentioned_users |
|
102 | 102 | sample = ( |
|
103 | 103 | "@first hi there @marcink here's my email marcin@email.com " |
|
104 | 104 | "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three " |
|
105 | 105 | "@MARCIN @maRCiN @2one_more22 @john please see this http://org.pl " |
|
106 | 106 | "@marian.user just do it @marco-polo and next extract @marco_polo " |
|
107 | 107 | "user.dot hej ! not-needed maril@domain.org" |
|
108 | 108 | ) |
|
109 | 109 | |
|
110 | 110 | s = sorted([ |
|
111 | 111 | 'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john', |
|
112 | 112 | 'marian.user', 'marco-polo', 'marco_polo' |
|
113 | 113 | ], key=lambda k: k.lower()) |
|
114 | 114 | self.assertEqual(s, extract_mentioned_users(sample)) |
|
115 | 115 | |
|
116 | 116 | @parameterized.expand([ |
|
117 | 117 | (dict(), u'just now'), |
|
118 | 118 | (dict(seconds= -1), u'1 second ago'), |
|
119 | 119 | (dict(seconds= -60 * 2), u'2 minutes ago'), |
|
120 | 120 | (dict(hours= -1), u'1 hour ago'), |
|
121 | 121 | (dict(hours= -24), u'1 day ago'), |
|
122 | 122 | (dict(hours= -24 * 5), u'5 days ago'), |
|
123 | 123 | (dict(months= -1), u'1 month ago'), |
|
124 | 124 | (dict(months= -1, days= -2), u'1 month and 2 days ago'), |
|
125 | 125 | (dict(years= -1, months= -1), u'1 year and 1 month ago'), |
|
126 | 126 | ]) |
|
127 | 127 | def test_age(self, age_args, expected): |
|
128 | 128 | from rhodecode.lib.utils2 import age |
|
129 | 129 | from dateutil import relativedelta |
|
130 | 130 | n = datetime.datetime(year=2012, month=5, day=17) |
|
131 | 131 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
132 | 132 | self.assertEqual(age(n + delt(**age_args), now=n), expected) |
|
133 | 133 | |
|
134 | 134 | @parameterized.expand([ |
|
135 | 135 | |
|
136 | 136 | (dict(), u'just now'), |
|
137 | 137 | (dict(seconds=1), u'in 1 second'), |
|
138 | 138 | (dict(seconds=60 * 2), u'in 2 minutes'), |
|
139 | 139 | (dict(hours=1), u'in 1 hour'), |
|
140 | 140 | (dict(hours=24), u'in 1 day'), |
|
141 | 141 | (dict(hours=24 * 5), u'in 5 days'), |
|
142 | 142 | (dict(months=1), u'in 1 month'), |
|
143 | 143 | (dict(months=1, days=1), u'in 1 month and 1 day'), |
|
144 | 144 | (dict(years=1, months=1), u'in 1 year and 1 month') |
|
145 | 145 | ]) |
|
146 | 146 | def test_age_in_future(self, age_args, expected): |
|
147 | 147 | from rhodecode.lib.utils2 import age |
|
148 | 148 | from dateutil import relativedelta |
|
149 | 149 | n = datetime.datetime(year=2012, month=5, day=17) |
|
150 | 150 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
151 | 151 | self.assertEqual(age(n + delt(**age_args), now=n), expected) |
|
152 | 152 | |
|
153 | 153 | def test_tag_exctrator(self): |
|
154 | 154 | sample = ( |
|
155 | 155 | "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]" |
|
156 | 156 | "[requires] [stale] [see<>=>] [see => http://url.com]" |
|
157 | 157 | "[requires => url] [lang => python] [just a tag]" |
|
158 | 158 | "[,d] [ => ULR ] [obsolete] [desc]]" |
|
159 | 159 | ) |
|
160 | 160 | from rhodecode.lib.helpers import desc_stylize |
|
161 | 161 | res = desc_stylize(sample) |
|
162 | 162 | self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res) |
|
163 | 163 | self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res) |
|
164 | 164 | self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res) |
|
165 | 165 | self.assertTrue('<div class="metatag" tag="lang">python</div>' in res) |
|
166 | 166 | self.assertTrue('<div class="metatag" tag="requires">requires => <a href="/url">url</a></div>' in res) |
|
167 | 167 | self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res) |
|
168 | 168 | |
|
169 | 169 | def test_alternative_gravatar(self): |
|
170 | 170 | from rhodecode.lib.helpers import gravatar_url |
|
171 | 171 | _md5 = lambda s: hashlib.md5(s).hexdigest() |
|
172 | 172 | |
|
173 | 173 | def fake_conf(**kwargs): |
|
174 | 174 | from pylons import config |
|
175 |
config |
|
|
176 |
config |
|
|
177 |
config |
|
|
175 | config = {} | |
|
176 | config['use_gravatar'] = True | |
|
177 | config.update(kwargs) | |
|
178 | 178 | return config |
|
179 | 179 | |
|
180 | 180 | class fake_url(): |
|
181 | 181 | @classmethod |
|
182 | 182 | def current(cls, *args, **kwargs): |
|
183 | 183 | return 'https://server.com' |
|
184 | 184 | |
|
185 | 185 | with mock.patch('pylons.url', fake_url): |
|
186 | 186 | fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') |
|
187 |
with mock.patch(' |
|
|
187 | with mock.patch('rhodecode.CONFIG', fake): | |
|
188 | 188 | from pylons import url |
|
189 | 189 | assert url.current() == 'https://server.com' |
|
190 | 190 | grav = gravatar_url(email_address='test@foo.com', size=24) |
|
191 | 191 | assert grav == 'http://test.com/test@foo.com' |
|
192 | 192 | |
|
193 | 193 | fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') |
|
194 |
with mock.patch(' |
|
|
194 | with mock.patch('rhodecode.CONFIG', fake): | |
|
195 | 195 | grav = gravatar_url(email_address='test@foo.com', size=24) |
|
196 | 196 | assert grav == 'http://test.com/test@foo.com' |
|
197 | 197 | |
|
198 | 198 | fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}') |
|
199 |
with mock.patch(' |
|
|
199 | with mock.patch('rhodecode.CONFIG', fake): | |
|
200 | 200 | em = 'test@foo.com' |
|
201 | 201 | grav = gravatar_url(email_address=em, size=24) |
|
202 | 202 | assert grav == 'http://test.com/%s' % (_md5(em)) |
|
203 | 203 | |
|
204 | 204 | fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}') |
|
205 |
with mock.patch(' |
|
|
205 | with mock.patch('rhodecode.CONFIG', fake): | |
|
206 | 206 | em = 'test@foo.com' |
|
207 | 207 | grav = gravatar_url(email_address=em, size=24) |
|
208 | 208 | assert grav == 'http://test.com/%s/%s' % (_md5(em), 24) |
|
209 | 209 | |
|
210 | 210 | fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}') |
|
211 |
with mock.patch(' |
|
|
211 | with mock.patch('rhodecode.CONFIG', fake): | |
|
212 | 212 | em = 'test@foo.com' |
|
213 | 213 | grav = gravatar_url(email_address=em, size=24) |
|
214 | 214 | assert grav == 'https://server.com/%s/%s' % (_md5(em), 24) |
|
215 | 215 | |
|
216 | 216 | def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None): |
|
217 | 217 | """ |
|
218 | 218 | Changes `some text url[foo]` => `some text <a href="/">foo</a> |
|
219 | 219 | |
|
220 | 220 | :param text: |
|
221 | 221 | """ |
|
222 | 222 | import re |
|
223 | 223 | # quickly change expected url[] into a link |
|
224 | 224 | URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])') |
|
225 | 225 | |
|
226 | 226 | def url_func(match_obj): |
|
227 | 227 | _url = match_obj.groups()[0] |
|
228 | 228 | return tmpl % (url_ or '/some-url', _url) |
|
229 | 229 | return URL_PAT.sub(url_func, text) |
|
230 | 230 | |
|
231 | 231 | @parameterized.expand([ |
|
232 | 232 | ("", |
|
233 | 233 | ""), |
|
234 | 234 | ("git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68", |
|
235 | 235 | "git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68"), |
|
236 | 236 | ("from rev 000000000000", |
|
237 | 237 | "from rev url[000000000000]"), |
|
238 | 238 | ("from rev 000000000000123123 also rev 000000000000", |
|
239 | 239 | "from rev url[000000000000123123] also rev url[000000000000]"), |
|
240 | 240 | ("this should-000 00", |
|
241 | 241 | "this should-000 00"), |
|
242 | 242 | ("longtextffffffffff rev 123123123123", |
|
243 | 243 | "longtextffffffffff rev url[123123123123]"), |
|
244 | 244 | ("rev ffffffffffffffffffffffffffffffffffffffffffffffffff", |
|
245 | 245 | "rev ffffffffffffffffffffffffffffffffffffffffffffffffff"), |
|
246 | 246 | ("ffffffffffff some text traalaa", |
|
247 | 247 | "url[ffffffffffff] some text traalaa"), |
|
248 | 248 | ("""Multi line |
|
249 | 249 | 123123123123 |
|
250 | 250 | some text 123123123123 |
|
251 | 251 | sometimes ! |
|
252 | 252 | """, |
|
253 | 253 | """Multi line |
|
254 | 254 | url[123123123123] |
|
255 | 255 | some text url[123123123123] |
|
256 | 256 | sometimes ! |
|
257 | 257 | """) |
|
258 | 258 | ]) |
|
259 | 259 | def test_urlify_changesets(self, sample, expected): |
|
260 | 260 | def fake_url(self, *args, **kwargs): |
|
261 | 261 | return '/some-url' |
|
262 | 262 | |
|
263 | 263 | expected = self._quick_url(expected) |
|
264 | 264 | |
|
265 | 265 | with mock.patch('pylons.url', fake_url): |
|
266 | 266 | from rhodecode.lib.helpers import urlify_changesets |
|
267 | 267 | self.assertEqual(urlify_changesets(sample, 'repo_name'), expected) |
|
268 | 268 | |
|
269 | 269 | @parameterized.expand([ |
|
270 | 270 | ("", |
|
271 | 271 | "", |
|
272 | 272 | ""), |
|
273 | 273 | ("https://svn.apache.org/repos", |
|
274 | 274 | "url[https://svn.apache.org/repos]", |
|
275 | 275 | "https://svn.apache.org/repos"), |
|
276 | 276 | ("http://svn.apache.org/repos", |
|
277 | 277 | "url[http://svn.apache.org/repos]", |
|
278 | 278 | "http://svn.apache.org/repos"), |
|
279 | 279 | ("from rev a also rev http://google.com", |
|
280 | 280 | "from rev a also rev url[http://google.com]", |
|
281 | 281 | "http://google.com"), |
|
282 | 282 | ("""Multi line |
|
283 | 283 | https://foo.bar.com |
|
284 | 284 | some text lalala""", |
|
285 | 285 | """Multi line |
|
286 | 286 | url[https://foo.bar.com] |
|
287 | 287 | some text lalala""", |
|
288 | 288 | "https://foo.bar.com") |
|
289 | 289 | ]) |
|
290 | 290 | def test_urlify_test(self, sample, expected, url_): |
|
291 | 291 | from rhodecode.lib.helpers import urlify_text |
|
292 | 292 | expected = self._quick_url(expected, |
|
293 | 293 | tmpl="""<a href="%s">%s</a>""", url_=url_) |
|
294 | 294 | self.assertEqual(urlify_text(sample), expected) |
General Comments 0
You need to be logged in to leave comments.
Login now