diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py
--- a/kallithea/lib/helpers.py
+++ b/kallithea/lib/helpers.py
@@ -985,18 +985,15 @@ def gravatar_url(email_address, size=30,
if email_address == _def:
return default
- # re-import url so tests can mock it
- from kallithea.config.routing import url
from kallithea.model.db import User
parsed_url = urllib.parse.urlparse(url.current(qualified=True))
- url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
+ return (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
.replace('{email}', email_address) \
.replace('{md5email}', hashlib.md5(safe_bytes(email_address).lower()).hexdigest()) \
.replace('{netloc}', parsed_url.netloc) \
.replace('{scheme}', parsed_url.scheme) \
.replace('{size}', str(size))
- return url
def changed_tooltip(nodes):
@@ -1125,15 +1122,14 @@ def urlify_text(s, repo_name=None, link_
"""
def _replace(match_obj):
- url = match_obj.group('url')
- if url is not None:
- return '%(url)s' % {'url': url}
+ match_url = match_obj.group('url')
+ if match_url is not None:
+ return '%(url)s' % {'url': match_url}
mention = match_obj.group('mention')
if mention is not None:
return '%s' % mention
hash_ = match_obj.group('hash')
if hash_ is not None and repo_name is not None:
- from kallithea.config.routing import url # doh, we need to re-import url to mock it later
return '%(hash)s' % {
'url': url('changeset_home', repo_name=repo_name, revision=hash_),
'hash': hash_,
diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py
--- a/kallithea/tests/other/test_libs.py
+++ b/kallithea/tests/other/test_libs.py
@@ -71,26 +71,6 @@ TEST_URLS += [
]
-class FakeUrlGenerator(object):
-
- def __init__(self, current_url=None, default_route=None, **routes):
- """Initialize using specified 'current' URL template,
- default route template, and all other aguments describing known
- routes (format: route=template)"""
- self.current_url = current_url
- self.default_route = default_route
- self.routes = routes
-
- def __call__(self, route_name, *args, **kwargs):
- if route_name in self.routes:
- return self.routes[route_name] % kwargs
-
- return self.default_route % kwargs
-
- def current(self, *args, **kwargs):
- return self.current_url % kwargs
-
-
class TestLibs(base.TestController):
@base.parametrize('test_url,expected,expected_creds', TEST_URLS)
@@ -239,8 +219,7 @@ class TestLibs(base.TestController):
return _c
- fake_url = FakeUrlGenerator(current_url='https://example.com')
- with mock.patch('kallithea.config.routing.url', fake_url):
+ with mock.patch('kallithea.config.routing.url.current', lambda *a, **b: 'https://example.com'):
fake = fake_tmpl_context(_url='http://example.com/{email}')
with mock.patch('tg.tmpl_context', fake):
from kallithea.config.routing import url
@@ -338,8 +317,10 @@ class TestLibs(base.TestController):
])
def test_urlify_text(self, sample, expected):
expected = self._quick_url(expected)
- fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
- with mock.patch('kallithea.config.routing.url', fake_url):
+
+ with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+ lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+ ):
from kallithea.lib.helpers import urlify_text
assert urlify_text(sample, 'repo_name') == expected
@@ -393,8 +374,9 @@ class TestLibs(base.TestController):
def test_urlify_test(self, sample, expected, url_):
expected = self._quick_url(expected,
tmpl="""%s""", url_=url_)
- fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
- with mock.patch('kallithea.config.routing.url', fake_url):
+ with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+ lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+ ):
from kallithea.lib.helpers import urlify_text
assert urlify_text(sample, 'repo_name', stylize=True) == expected
@@ -406,8 +388,9 @@ class TestLibs(base.TestController):
""" yo"""),
])
def test_urlify_link(self, sample, expected):
- fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
- with mock.patch('kallithea.config.routing.url', fake_url):
+ with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+ lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+ ):
from kallithea.lib.helpers import urlify_text
assert urlify_text(sample, 'repo_name', link_='#the-link') == expected