def test_js_helper(): """ Given the js() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<script src="%s"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common'] ]) assert rendered == expected template = from_string('{{ js("common", debug=False) }}') rendered = template.render() expected = f'<script src="{settings.STATIC_URL}js/common-min.js"></script>' assert rendered == expected template = from_string('{{ js("common_bundle", debug=True) }}') rendered = template.render() assert rendered == ( f'<script src="{settings.STATIC_URL}js/test.js"></script>\n' f'<script src="{settings.STATIC_URL}js/test2.js"></script>') template = from_string('{{ js("common_bundle", debug=False) }}') rendered = template.render() assert rendered == '<script src="{}js/common_bundle-min.js"></script>'.format( settings.STATIC_URL, )
def test_truncate_purified_field_xss(): """Truncating should not introduce xss issues.""" s = 'safe <script>alert("omg")</script>' t = PurifiedTranslation(localized_string=s) actual = from_string('{{ s|truncate(100) }}').render({'s': t}) assert actual == 'safe <script>alert("omg")</script>' actual = from_string('{{ s|truncate(5) }}').render({'s': t}) assert actual == 'safe ...'
def test_no_jinja_autoescape(): val = 'some double quote: " and a <' tpl = '{{ val }}' ctx = {'val': val} template = from_string(tpl) assert template.render(ctx) == 'some double quote: " and a <' with no_jinja_autoescape(): template = from_string(tpl) assert template.render(ctx) == 'some double quote: " and a <'
def test_compiled_css(open_mock, subprocess_mock, getmtime_mock): getmtime_mock.side_effect = [ # The first call is for the source 1531144805.1225898, # The second call is for the destination 1530885814.6340182] from_string('{{ css("compiled", debug=True) }}")').render() source = os.path.realpath(os.path.join( settings.ROOT, 'static/css/impala/buttons.less')) assert subprocess_mock.Popen.mock_calls == [ mock.call([settings.LESS_BIN, source], stdout=mock.ANY)]
def test_compiled_css(open_mock, subprocess_mock, getmtime_mock): getmtime_mock.side_effect = [ # The first call is for the source 1531144805.1225898, # The second call is for the destination 1530885814.6340182 ] from_string('{{ css("compiled", debug=True) }}")').render() source = os.path.realpath( os.path.join(settings.ROOT, 'static/css/impala/buttons.less')) assert subprocess_mock.Popen.mock_calls == [ mock.call([settings.LESS_BIN, source], stdout=mock.ANY) ]
def test_app_in_fragment_cache_key(cache_mock): cache_mock.return_value = '' request = mock.Mock() request.APP.id = '<app>' request.user.is_authenticated.return_value = False template = from_string('{% cache 1 %}{% endcache %}') template.render(request=request) assert cache_mock.call_args[0][0].endswith('<app>')
def test_js(): template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<script src="%s"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common'] ]) assert rendered == expected
def test_css(): template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<link rel="stylesheet" media="all" ' 'href="%s" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common'] ]) assert rendered == expected
def test_css_helper(): """ Given the css() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<link rel="stylesheet" media="all" ' 'href="%s" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common'] ]) assert rendered == expected template = from_string('{{ css("common", debug=False) }}') rendered = template.render() expected = ('<link rel="stylesheet" media="all" ' 'href="%scss/common-min.css" />' % (settings.STATIC_URL, )) assert rendered == expected assert rendered == expected template = from_string('{{ css("common_bundle", debug=True) }}') rendered = template.render() assert rendered == ('<link rel="stylesheet" media="all" ' f'href="{settings.STATIC_URL}css/test.css" />\n' '<link rel="stylesheet" media="all" ' f'href="{settings.STATIC_URL}css/test2.css" />') template = from_string('{{ css("common_bundle", debug=False) }}') rendered = template.render() assert (rendered == '<link rel="stylesheet" media="all" ' 'href="%scss/common_bundle-min.css" />' % (settings.STATIC_URL, ))
def test_js(getmtime, time): getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = "\n".join( ['<script src="%s?build=1"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common']]) assert rendered == expected
def test_css(getmtime, time): getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = "\n".join( ['<link rel="stylesheet" media="all" ' 'href="%s?build=1" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common']]) assert rendered == expected
def test_js(getmtime, time): getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = "\n".join([ '<script src="%s?build=1"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common'] ]) assert rendered == expected
def test_no_links(): template = from_string('{{ s|no_links }}') s = 'a <a href="http://url.link">http://example.com</a>, http://text.link' expected = 'a http://example.com, http://text.link' assert template.render({'s': s}) == expected # Bad markup. s = '<http://bad.markup.com' assert template.render({'s': s}) == '' # Bad markup. s = 'some text <http://bad.markup.com' assert template.render({'s': s}) == 'some text'
def test_css(getmtime, time): getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = "\n".join([ '<link rel="stylesheet" media="all" ' 'href="%s?build=1" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common'] ]) assert rendered == expected
def test_clean_strip_all_html(): s = '<a href="#woo">yeah</a>' expected = 'yeah' assert from_string('{{ s|clean(true) }}').render({'s': s}) == expected
def test_clean_in_template(): s = '<a href="#woo">yeah</a>' assert from_string('{{ s|clean }}').render({'s': s}) == s
def test_fragment_cache_key_no_app(cache_mock): cache_mock.return_value = 'xx' template = from_string('{% cache 1 %}{% endcache %}') assert template.render() == 'xx' assert cache_mock.called
def test_truncate_purified_field(): s = '<i>one</i><i>two</i>' t = PurifiedTranslation(localized_string=s) actual = from_string('{{ s|truncate(6) }}').render({'s': t}) assert actual == s
def render(s, context=None): if context is None: context = {} t = utils.from_string(s) return t.render(context)
def test_css_helper(getmtime, time): """ Given the css() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = "\n".join([ '<link rel="stylesheet" media="all" ' 'href="%s?build=1" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common'] ]) assert rendered == expected template = from_string('{{ css("common", debug=False) }}') rendered = template.render() expected = ('<link rel="stylesheet" media="all" ' 'href="%scss/common-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_url", debug=True) }}') rendered = template.render() expected = ('<link rel="stylesheet" media="all" ' 'href="http://example.com/test.css?build=1" />') assert rendered == expected template = from_string('{{ css("common_url", debug=False) }}') rendered = template.render() expected = ('<link rel="stylesheet" media="all" ' 'href="%scss/common_url-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_protocol_less_url", debug=True) }}') rendered = template.render() assert (rendered == ('<link rel="stylesheet" media="all" ' 'href="//example.com/test.css?build=1" />')) template = from_string( '{{ css("common_protocol_less_url", debug=False) }}') rendered = template.render() expected = ('<link rel="stylesheet" media="all" ' 'href="%scss/common_protocol_less_url-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_bundle", debug=True) }}') rendered = template.render() assert ( rendered == '<link rel="stylesheet" media="all" href="http://testserver/static/css/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="http://example.com/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="//example.com/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="https://example.com/test.css?build=1" />' ) # noqa template = from_string('{{ css("common_bundle", debug=False) }}') rendered = template.render() assert (rendered == '<link rel="stylesheet" media="all" ' 'href="%scss/common_bundle-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS))
def test_js_helper(getmtime, time): """ Given the js() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<script src="%s?build=1"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common']]) assert rendered == expected template = from_string('{{ js("common", debug=False) }}') rendered = template.render() expected = ( '<script src="%sjs/common-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_url", debug=True) }}') rendered = template.render() expected = '<script src="http://example.com/test.js?build=1"></script>' assert rendered == expected template = from_string('{{ js("common_url", debug=False) }}') rendered = template.render() expected = ( '<script src="%sjs/common_url-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_protocol_less_url", debug=True) }}') rendered = template.render() assert rendered == '<script src="//example.com/test.js?build=1"></script>' template = from_string('{{ js("common_protocol_less_url", debug=False) }}') rendered = template.render() expected = ( '<script src="%sjs/common_protocol_less_url-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_bundle", debug=True) }}') rendered = template.render() assert ( rendered == ( '<script src="%sjs/test.js?build=1"></script>\n' '<script src="http://example.com/test.js?build=1"></script>\n' '<script src="//example.com/test.js?build=1"></script>\n' '<script src="https://example.com/test.js?build=1"></script>' % settings.STATIC_URL)) template = from_string('{{ js("common_bundle", debug=False) }}') rendered = template.render() assert ( rendered == '<script src="%sjs/common_bundle-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS))
def test_css_helper(getmtime, time): """ Given the css() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ css("common", debug=True) }}') rendered = template.render() expected = "\n".join([ '<link rel="stylesheet" media="all" ' 'href="%s?build=1" />' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['css']['common'] ]) assert rendered == expected template = from_string('{{ css("common", debug=False) }}') rendered = template.render() expected = ( '<link rel="stylesheet" media="all" ' 'href="%scss/common-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_url", debug=True) }}') rendered = template.render() expected = ( '<link rel="stylesheet" media="all" ' 'href="http://example.com/test.css?build=1" />') assert rendered == expected template = from_string('{{ css("common_url", debug=False) }}') rendered = template.render() expected = ( '<link rel="stylesheet" media="all" ' 'href="%scss/common_url-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_protocol_less_url", debug=True) }}') rendered = template.render() assert ( rendered == ( '<link rel="stylesheet" media="all" ' 'href="//example.com/test.css?build=1" />')) template = from_string( '{{ css("common_protocol_less_url", debug=False) }}') rendered = template.render() expected = ( '<link rel="stylesheet" media="all" ' 'href="%scss/common_protocol_less_url-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS)) assert rendered == expected template = from_string('{{ css("common_bundle", debug=True) }}') rendered = template.render() assert ( rendered == '<link rel="stylesheet" media="all" href="http://testserver/static/css/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="http://example.com/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="//example.com/test.css?build=1" />\n' # noqa '<link rel="stylesheet" media="all" href="https://example.com/test.css?build=1" />') # noqa template = from_string('{{ css("common_bundle", debug=False) }}') rendered = template.render() assert ( rendered == '<link rel="stylesheet" media="all" ' 'href="%scss/common_bundle-min.css?build=%s" />' % (settings.STATIC_URL, BUILD_ID_CSS))
def test_js_helper(getmtime, time): """ Given the js() tag if we return the assets that make up that bundle as defined in settings.MINIFY_BUNDLES. If we're not in debug mode, we just return a minified url """ getmtime.return_value = 1 time.return_value = 1 template = from_string('{{ js("common", debug=True) }}') rendered = template.render() expected = '\n'.join([ '<script src="%s?build=1"></script>' % (settings.STATIC_URL + j) for j in settings.MINIFY_BUNDLES['js']['common'] ]) assert rendered == expected template = from_string('{{ js("common", debug=False) }}') rendered = template.render() expected = ('<script src="%sjs/common-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_url", debug=True) }}') rendered = template.render() expected = '<script src="http://example.com/test.js?build=1"></script>' assert rendered == expected template = from_string('{{ js("common_url", debug=False) }}') rendered = template.render() expected = ('<script src="%sjs/common_url-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_protocol_less_url", debug=True) }}') rendered = template.render() assert rendered == '<script src="//example.com/test.js?build=1"></script>' template = from_string('{{ js("common_protocol_less_url", debug=False) }}') rendered = template.render() expected = ( '<script src="%sjs/common_protocol_less_url-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS)) assert rendered == expected template = from_string('{{ js("common_bundle", debug=True) }}') rendered = template.render() assert (rendered == ( '<script src="%sjs/test.js?build=1"></script>\n' '<script src="http://example.com/test.js?build=1"></script>\n' '<script src="//example.com/test.js?build=1"></script>\n' '<script src="https://example.com/test.js?build=1"></script>' % settings.STATIC_URL)) template = from_string('{{ js("common_bundle", debug=False) }}') rendered = template.render() assert (rendered == '<script src="%sjs/common_bundle-min.js?build=%s"></script>' % (settings.STATIC_URL, BUILD_ID_JS))