You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2019/11/15 23:32:03 UTC
[allura] 01/07: [#8340] misc other coverage
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch db/8340
in repository https://gitbox.apache.org/repos/asf/allura.git
commit 6205d4e7ca4df13841b1ce3dabf6d6968ae238e0
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Wed Nov 13 18:13:32 2019 -0500
[#8340] misc other coverage
---
Allura/allura/config/app_cfg.py | 1 -
Allura/allura/lib/app_globals.py | 5 ---
Allura/allura/lib/helpers.py | 60 ++--------------------------
Allura/allura/lib/macro.py | 7 ++++
Allura/allura/tests/decorators.py | 2 +-
Allura/allura/tests/functional/test_admin.py | 4 ++
Allura/allura/tests/functional/test_root.py | 24 +++++++++--
Allura/allura/tests/test_globals.py | 5 +++
Allura/allura/tests/test_helpers.py | 16 ++++----
9 files changed, 50 insertions(+), 74 deletions(-)
diff --git a/Allura/allura/config/app_cfg.py b/Allura/allura/config/app_cfg.py
index 222fa54..f5b1464 100644
--- a/Allura/allura/config/app_cfg.py
+++ b/Allura/allura/config/app_cfg.py
@@ -103,7 +103,6 @@ class AlluraJinjaRenderer(JinjaRenderer):
cache_size=config.get('jinja_cache_size', -1),
extensions=['jinja2.ext.do', 'jinja2.ext.i18n'])
jinja2_env.install_gettext_translations(tg.i18n)
- jinja2_env.filters['filesizeformat'] = helpers.do_filesizeformat
jinja2_env.filters['datetimeformat'] = helpers.datetimeformat
jinja2_env.filters['filter'] = lambda s, t=None: filter(t and jinja2_env.tests[t], s)
jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter
diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index f558b74..c748579 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -562,11 +562,6 @@ class Globals(object):
'h.set_context() is preferred over this method'
c.app = c.project.app_instance(name)
- def postload_contents(self):
- text = '''
-'''
- return json.dumps(dict(text=text))
-
def year(self):
return datetime.datetime.utcnow().year
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 82285f9..69342c5 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -58,7 +58,7 @@ from tg.decorators import before_validate
from formencode.variabledecode import variable_decode
import formencode
from jinja2 import Markup
-from jinja2.filters import contextfilter, escape
+from jinja2.filters import contextfilter, escape, do_filesizeformat
from paste.deploy.converters import asbool, aslist, asint
from webhelpers import date, feedgenerator, html, number, misc, text
@@ -233,14 +233,6 @@ def make_neighborhoods(ids):
return _make_xs('Neighborhood', ids)
-def make_projects(ids):
- return _make_xs('Project', ids)
-
-
-def make_users(ids):
- return _make_xs('User', ids)
-
-
def make_roles(ids):
return _make_xs('ProjectRole', ids)
@@ -265,6 +257,8 @@ def make_app_admin_only(app):
@contextmanager
def push_config(obj, **kw):
+ # if you need similar for a dict, use mock.patch.dict
+
saved_attrs = {}
new_attrs = []
for k, v in kw.iteritems():
@@ -745,40 +739,6 @@ def render_any_markup(name, txt, code_mode=False, linenumbers_style=TABLE):
txt = '<pre>%s</pre>' % txt
return Markup(txt)
-# copied from jinja2 dev
-# latest release, 2.6, implements this incorrectly
-# can remove and use jinja2 implementation after upgrading to 2.7
-
-
-def do_filesizeformat(value, binary=False):
- """Format the value like a 'human-readable' file size (i.e. 13 kB,
-4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega,
-Giga, etc.), if the second parameter is set to `True` the binary
-prefixes are used (Mebi, Gibi).
-"""
- bytes = float(value)
- base = binary and 1024 or 1000
- prefixes = [
- (binary and 'KiB' or 'kB'),
- (binary and 'MiB' or 'MB'),
- (binary and 'GiB' or 'GB'),
- (binary and 'TiB' or 'TB'),
- (binary and 'PiB' or 'PB'),
- (binary and 'EiB' or 'EB'),
- (binary and 'ZiB' or 'ZB'),
- (binary and 'YiB' or 'YB')
- ]
- if bytes == 1:
- return '1 Byte'
- elif bytes < base:
- return '%d Bytes' % bytes
- else:
- for i, prefix in enumerate(prefixes):
- unit = base ** (i + 2)
- if bytes < unit:
- return '%.1f %s' % ((base * bytes / unit), prefix)
- return '%.1f %s' % ((base * bytes / unit), prefix)
-
def nl2br_jinja_filter(value):
result = '<br>\n'.join(escape(line) for line in value.split('\n'))
@@ -1165,20 +1125,6 @@ def login_overlay(exceptions=None):
c.show_login_overlay = True
-def get_filter(ctx, filter_name):
- """
- Gets a named Jinja2 filter, passing through
- any context requested by the filter.
- """
- filter_ = ctx.environment.filters[filter_name]
- if getattr(filter_, 'contextfilter', False):
- return partial(filter_, ctx)
- elif getattr(filter_, 'evalcontextfilter', False):
- return partial(filter_, ctx.eval_ctx)
- elif getattr(filter_, 'environmentfilter', False):
- return partial(filter_, ctx.environment)
-
-
def unidiff(old, new):
"""Returns unified diff between `one` and `two`."""
return '\n'.join(difflib.unified_diff(
diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index abb4776..b01eaad 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -20,6 +20,8 @@ import random
import shlex
import logging
import traceback
+import urllib2
+
import oembed
import jinja2
from operator import attrgetter
@@ -461,6 +463,11 @@ def embed(url=None):
html = consumer.embed(url)['html']
except oembed.OEmbedNoEndpoint:
html = None
+ except urllib2.HTTPError as e:
+ if e.code == 404:
+ return 'Video not available'
+ else:
+ raise
if html:
# youtube has a trailing ")" at the moment
diff --git a/Allura/allura/tests/decorators.py b/Allura/allura/tests/decorators.py
index 726a402..f044c0e 100644
--- a/Allura/allura/tests/decorators.py
+++ b/Allura/allura/tests/decorators.py
@@ -200,7 +200,7 @@ def out_audits(*messages, **kwargs):
message=re.compile(preamble + message))).count(), 'Found unexpected: "%s"' % message
-# not a decorator but use it with LogCapture() decorator
+# not a decorator but use it with LogCapture() context manager
def assert_logmsg_and_no_warnings_or_errors(logs, msg):
"""
:param testfixtures.logcapture.LogCapture logs: LogCapture() instance
diff --git a/Allura/allura/tests/functional/test_admin.py b/Allura/allura/tests/functional/test_admin.py
index 3b2c4df..534863e 100644
--- a/Allura/allura/tests/functional/test_admin.py
+++ b/Allura/allura/tests/functional/test_admin.py
@@ -951,6 +951,10 @@ class TestProjectAdmin(TestController):
assert url.endswith('/admin/ext/foo'), url
assert_equals('here the foo settings go', foo_page.body)
+ def test_nbhd_invitations(self):
+ r = self.app.get('/admin/invitations')
+ r.mustcontain('Neighborhood Invitation(s) for test')
+
class TestExport(TestController):
diff --git a/Allura/allura/tests/functional/test_root.py b/Allura/allura/tests/functional/test_root.py
index 34da302..ca078fb 100644
--- a/Allura/allura/tests/functional/test_root.py
+++ b/Allura/allura/tests/functional/test_root.py
@@ -28,8 +28,11 @@ functional tests exercise the whole application and its WSGI stack.
Please read http://pythonpaste.org/webtest/ for more information.
"""
+import os
+from urllib import quote
+
from tg import tmpl_context as c
-from nose.tools import assert_equal
+from nose.tools import assert_equal, assert_in
from ming.orm.ormsession import ThreadLocalORMSession
import mock
from IPython.testing.decorators import module_not_available, skipif
@@ -37,7 +40,7 @@ from IPython.testing.decorators import module_not_available, skipif
from allura.tests import decorators as td
from allura.tests import TestController
from allura import model as M
-from allura.lib.helpers import push_config
+from allura.lib import helpers as h
from alluratest.controller import setup_trove_categories
@@ -69,6 +72,14 @@ class TestRootController(TestController):
assert cat_links[0].find('a').get('href') == '/browse/clustering'
assert cat_links[0].find('a').find('span').string == 'Clustering'
+ def test_validation(self):
+ # this is not configured ON currently, so adding an individual test to get coverage of the validator itself
+ with mock.patch.dict(os.environ, ALLURA_VALIDATION='all'):
+ self.app.get('/neighborhood')
+ self.app.get('/nf/markdown_to_html?markdown=aaa&project=test&app=bugs&neighborhood=%s'
+ % M.Neighborhood.query.get(name='Projects')._id,
+ validate_chunk=True)
+
def test_sidebar_escaping(self):
# use this as a convenient way to get something in the sidebar
M.ProjectCategory(name='test-xss', label='<script>alert(1)</script>')
@@ -123,7 +134,7 @@ class TestRootController(TestController):
# Install home app
nb = M.Neighborhood.query.get(name='Adobe')
p = nb.neighborhood_project
- with push_config(c, user=M.User.query.get(username='test-admin')):
+ with h.push_config(c, user=M.User.query.get(username='test-admin')):
p.install_app('home', 'home', 'Home', ordinal=0)
response = self.app.get('/adobe/')
@@ -165,6 +176,13 @@ class TestRootController(TestController):
'/nf/markdown_to_html?markdown=*aaa*bb[wiki:Home]&project=test&app=bugs&neighborhood=%s' % n._id, validate_chunk=True)
assert '<p><em>aaa</em>bb<a class="alink" href="/p/test/wiki/Home/">[wiki:Home]</a></p>' in r, r
+ # this happens to trigger an error
+ bad_markdown = '<foo {bar}>'
+ r = self.app.get('/nf/markdown_to_html?markdown=%s&project=test&app=bugs&neighborhood=%s' %
+ (quote(bad_markdown), n._id))
+ r.mustcontain('The markdown supplied could not be parsed correctly.')
+ r.mustcontain('<pre><foo {bar}></pre>')
+
def test_slash_redirect(self):
self.app.get('/p', status=301)
self.app.get('/p/', status=302)
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index e8787fb..33453cb 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -345,6 +345,11 @@ def test_macro_embed(oembed_fetch):
r.replace('\n', ''))
+def test_macro_embed_video_gone():
+ r = g.markdown_wiki.convert('[[embed url=https://www.youtube.com/watch?v=OWsFqPZ3v-0]]')
+ assert_equal(r, '<div class="markdown_content"><p>Video not available</p></div>')
+
+
def test_macro_embed_notsupported():
r = g.markdown_wiki.convert('[[embed url=http://vimeo.com/46163090]]')
assert_equal(
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 3abde3b..8fccf90 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -107,11 +107,6 @@ def test_find_project():
assert proj is None
-def test_make_users():
- r = h.make_users([None]).next()
- assert r.username == '*anonymous', r
-
-
def test_make_roles():
h.set_context('test', 'wiki', neighborhood='Projects')
pr = M.ProjectRole.anonymous()
@@ -594,8 +589,11 @@ def test_base64uri_img():
def test_base64uri_text():
- b64txt = h.base64uri('blah blah blah 123 456 foo bar baz', mimetype='text/plain')
- assert b64txt.startswith('data:text/plain;base64,'), b64txt
+ b64txt = h.base64uri('blah blah blah\n123 456\nfoo bar baz', mimetype='text/plain')
+ assert_equals(b64txt, 'data:text/plain;base64,YmxhaCBibGFoIGJsYWgKMTIzIDQ1Ngpmb28gYmFyIGJheg==')
+
+ b64txt = h.base64uri('blah blah blah\n123 456\nfoo bar baz', mimetype='text/plain', windows_line_endings=True)
+ assert_equals(b64txt, 'data:text/plain;base64,YmxhaCBibGFoIGJsYWgNCjEyMyA0NTYNCmZvbyBiYXIgYmF6')
def test_slugify():
@@ -649,3 +647,7 @@ def test_hide_private_info():
with h.push_config(h.tg.config, hide_private_info=False):
assert_equals(h.hide_private_info('foo bar baz@bing.com'), 'foo bar baz@bing.com')
+
+
+def test_emojize():
+ assert_equals(h.emojize(':smile:'), u'😄')