comparison rhodecode/tests/other/test_libs.py @ 4116:ffd45b185016 rhodecode-2.2.5-gpl

Imported some of the GPLv3'd changes from RhodeCode v2.2.5. This imports changes between changesets 21af6c4eab3d and 6177597791c2 in RhodeCode's original repository, including only changes to Python files and HTML. RhodeCode clearly licensed its changes to these files under GPLv3 in their /LICENSE file, which states the following: The Python code and integrated HTML are licensed under the GPLv3 license. (See: https://code.rhodecode.com/rhodecode/files/v2.2.5/LICENSE or http://web.archive.org/web/20140512193334/https://code.rhodecode.com/rhodecode/files/f3b123159901f15426d18e3dc395e8369f70ebe0/LICENSE for an online copy of that LICENSE file) Conservancy reviewed these changes and confirmed that they can be licensed as a whole to the Kallithea project under GPLv3-only. While some of the contents committed herein are clearly licensed GPLv3-or-later, on the whole we must assume the are GPLv3-only, since the statement above from RhodeCode indicates that they intend GPLv3-only as their license, per GPLv3ยง14 and other relevant sections of GPLv3.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:03:13 -0400
parents 5f192af1ba21
children 7e5f8c12a3fc
comparison
equal deleted inserted replaced
4115:8b7294a804a0 4116:ffd45b185016
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.tests.test_libs
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
6
7 Package for testing various lib/helper functions in rhodecode
8
9 :created_on: Jun 9, 2011
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
12 """
13 # This program is free software: you can redistribute it and/or modify 2 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by 3 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or 4 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version. 5 # (at your option) any later version.
17 # 6 #
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details. 10 # GNU General Public License for more details.
22 # 11 #
23 # You should have received a copy of the GNU General Public License 12 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
15 rhodecode.tests.test_libs
16 ~~~~~~~~~~~~~~~~~~~~~~~~~
17
18 Package for testing various lib/helper functions in rhodecode
19
20 :created_on: Jun 9, 2011
21 :author: marcink
22 :copyright: (c) 2013 RhodeCode GmbH.
23 :license: GPLv3, see LICENSE for more details.
24 """
25
25 from __future__ import with_statement 26 from __future__ import with_statement
26 import datetime 27 import datetime
27 import hashlib 28 import hashlib
28 import mock 29 import mock
29 from rhodecode.tests import * 30 from rhodecode.tests import *
31 from rhodecode.lib.utils2 import AttributeDict
32 from rhodecode.model.db import Repository
30 33
31 proto = 'http' 34 proto = 'http'
32 TEST_URLS = [ 35 TEST_URLS = [
33 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], 36 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
34 '%s://127.0.0.1' % proto), 37 '%s://127.0.0.1' % proto),
168 171
169 def test_alternative_gravatar(self): 172 def test_alternative_gravatar(self):
170 from rhodecode.lib.helpers import gravatar_url 173 from rhodecode.lib.helpers import gravatar_url
171 _md5 = lambda s: hashlib.md5(s).hexdigest() 174 _md5 = lambda s: hashlib.md5(s).hexdigest()
172 175
173 def fake_conf(**kwargs): 176 #mock pylons.url
174 from pylons import config 177 class fake_url(object):
175 config = {}
176 config['use_gravatar'] = True
177 config.update(kwargs)
178 return config
179
180 class fake_url():
181 @classmethod 178 @classmethod
182 def current(cls, *args, **kwargs): 179 def current(cls, *args, **kwargs):
183 return 'https://server.com' 180 return 'https://server.com'
184 181
182 #mock pylons.tmpl_context
183 def fake_tmpl_context(_url):
184 _c = AttributeDict()
185 _c.visual = AttributeDict()
186 _c.visual.use_gravatar = True
187 _c.visual.gravatar_url = _url
188
189 return _c
190
191
185 with mock.patch('pylons.url', fake_url): 192 with mock.patch('pylons.url', fake_url):
186 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') 193 fake = fake_tmpl_context(_url='http://test.com/{email}')
187 with mock.patch('rhodecode.CONFIG', fake): 194 with mock.patch('pylons.tmpl_context', fake):
188 from pylons import url 195 from pylons import url
189 assert url.current() == 'https://server.com' 196 assert url.current() == 'https://server.com'
190 grav = gravatar_url(email_address='test@foo.com', size=24) 197 grav = gravatar_url(email_address='test@foo.com', size=24)
191 assert grav == 'http://test.com/test@foo.com' 198 assert grav == 'http://test.com/test@foo.com'
192 199
193 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') 200 fake = fake_tmpl_context(_url='http://test.com/{email}')
194 with mock.patch('rhodecode.CONFIG', fake): 201 with mock.patch('pylons.tmpl_context', fake):
195 grav = gravatar_url(email_address='test@foo.com', size=24) 202 grav = gravatar_url(email_address='test@foo.com', size=24)
196 assert grav == 'http://test.com/test@foo.com' 203 assert grav == 'http://test.com/test@foo.com'
197 204
198 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}') 205 fake = fake_tmpl_context(_url='http://test.com/{md5email}')
199 with mock.patch('rhodecode.CONFIG', fake): 206 with mock.patch('pylons.tmpl_context', fake):
200 em = 'test@foo.com' 207 em = 'test@foo.com'
201 grav = gravatar_url(email_address=em, size=24) 208 grav = gravatar_url(email_address=em, size=24)
202 assert grav == 'http://test.com/%s' % (_md5(em)) 209 assert grav == 'http://test.com/%s' % (_md5(em))
203 210
204 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}') 211 fake = fake_tmpl_context(_url='http://test.com/{md5email}/{size}')
205 with mock.patch('rhodecode.CONFIG', fake): 212 with mock.patch('pylons.tmpl_context', fake):
206 em = 'test@foo.com' 213 em = 'test@foo.com'
207 grav = gravatar_url(email_address=em, size=24) 214 grav = gravatar_url(email_address=em, size=24)
208 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24) 215 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
209 216
210 fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}') 217 fake = fake_tmpl_context(_url='{scheme}://{netloc}/{md5email}/{size}')
211 with mock.patch('rhodecode.CONFIG', fake): 218 with mock.patch('pylons.tmpl_context', fake):
212 em = 'test@foo.com' 219 em = 'test@foo.com'
213 grav = gravatar_url(email_address=em, size=24) 220 grav = gravatar_url(email_address=em, size=24)
214 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24) 221 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
222
223 @parameterized.expand([
224 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {}, '', 'http://vps1:8000/group/repo1'),
225 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'marcink'}, '', 'http://marcink@vps1:8000/group/repo1'),
226 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {}, '/rc', 'http://vps1:8000/rc/group/repo1'),
227 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'user'}, '/rc', 'http://user@vps1:8000/rc/group/repo1'),
228 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'marcink'}, '/rc', 'http://marcink@vps1:8000/rc/group/repo1'),
229 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'user'}, '/rc/', 'http://user@vps1:8000/rc/group/repo1'),
230 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'marcink'}, '/rc/', 'http://marcink@vps1:8000/rc/group/repo1'),
231 ('{scheme}://{user}@{netloc}/_{repoid}', 'group/repo1', {}, '', 'http://vps1:8000/_23'),
232 ('{scheme}://{user}@{netloc}/_{repoid}', 'group/repo1', {'user': 'marcink'}, '', 'http://marcink@vps1:8000/_23'),
233 ('http://{user}@{netloc}/_{repoid}', 'group/repo1', {'user': 'marcink'}, '', 'http://marcink@vps1:8000/_23'),
234 ('http://{netloc}/_{repoid}', 'group/repo1', {'user': 'marcink'}, '', 'http://vps1:8000/_23'),
235 ('https://{user}@proxy1.server.com/{repo}', 'group/repo1', {'user': 'marcink'}, '', 'https://marcink@proxy1.server.com/group/repo1'),
236 ('https://{user}@proxy1.server.com/{repo}', 'group/repo1', {}, '', 'https://proxy1.server.com/group/repo1'),
237 ('https://proxy1.server.com/{user}/{repo}', 'group/repo1', {'user': 'marcink'}, '', 'https://proxy1.server.com/marcink/group/repo1'),
238 ])
239 def test_clone_url_generator(self, tmpl, repo_name, overrides, prefix, expected):
240 from rhodecode.lib.utils2 import get_clone_url
241 clone_url = get_clone_url(uri_tmpl=tmpl, qualifed_home_url='http://vps1:8000'+prefix,
242 repo_name=repo_name, repo_id=23, **overrides)
243 self.assertEqual(clone_url, expected)
215 244
216 def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None): 245 def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None):
217 """ 246 """
218 Changes `some text url[foo]` => `some text <a href="/">foo</a> 247 Changes `some text url[foo]` => `some text <a href="/">foo</a>
219 248
290 def test_urlify_test(self, sample, expected, url_): 319 def test_urlify_test(self, sample, expected, url_):
291 from rhodecode.lib.helpers import urlify_text 320 from rhodecode.lib.helpers import urlify_text
292 expected = self._quick_url(expected, 321 expected = self._quick_url(expected,
293 tmpl="""<a href="%s">%s</a>""", url_=url_) 322 tmpl="""<a href="%s">%s</a>""", url_=url_)
294 self.assertEqual(urlify_text(sample), expected) 323 self.assertEqual(urlify_text(sample), expected)
324
325 @parameterized.expand([
326 ("", None),
327 ("/_2", '2'),
328 ("_2", '2'),
329 ("/_2/", '2'),
330 ("_2/", '2'),
331
332 ("/_21", '21'),
333 ("_21", '21'),
334 ("/_21/", '21'),
335 ("_21/", '21'),
336
337 ("/_21/foobar", '21'),
338 ("_21/121", '21'),
339 ("/_21/_12", '21'),
340 ("_21/rc/foo", '21'),
341
342 ])
343 def test_get_repo_by_id(self, test, expected):
344 from rhodecode.lib.utils import _extract_id_from_repo_name
345 _test = _extract_id_from_repo_name(test)
346 self.assertEqual(_test, expected, msg='url:%s, got:`%s` expected: `%s`'
347 % (test, _test, expected))