コード例 #1
0
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, )
コード例 #2
0
ファイル: test_helpers.py プロジェクト: bqbn/addons-server
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 &lt;script&gt;alert("omg")&lt;/script&gt;'
    actual = from_string('{{ s|truncate(5) }}').render({'s': t})
    assert actual == 'safe ...'
コード例 #3
0
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 &lt;script&gt;alert("omg")&lt;/script&gt;'
    actual = from_string('{{ s|truncate(5) }}').render({'s': t})
    assert actual == 'safe ...'
コード例 #4
0
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: &#34; and a &lt;'
    with no_jinja_autoescape():
        template = from_string(tpl)
        assert template.render(ctx) == 'some double quote: " and a <'
コード例 #5
0
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: &#34; and a &lt;'
    with no_jinja_autoescape():
        template = from_string(tpl)
        assert template.render(ctx) == 'some double quote: " and a <'
コード例 #6
0
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)]
コード例 #7
0
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)
    ]
コード例 #8
0
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>')
コード例 #9
0
ファイル: test_ext.py プロジェクト: Osmose/olympia
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>')
コード例 #10
0
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
コード例 #11
0
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
コード例 #12
0
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, ))
コード例 #13
0
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
コード例 #14
0
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
コード例 #15
0
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
コード例 #16
0
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'
コード例 #17
0
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
コード例 #18
0
ファイル: test_helpers.py プロジェクト: bqbn/addons-server
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'
コード例 #19
0
def test_clean_strip_all_html():
    s = '<a href="#woo">yeah</a>'

    expected = 'yeah'
    assert from_string('{{ s|clean(true) }}').render({'s': s}) == expected
コード例 #20
0
def test_clean_in_template():
    s = '<a href="#woo">yeah</a>'
    assert from_string('{{ s|clean }}').render({'s': s}) == s
コード例 #21
0
ファイル: test_ext.py プロジェクト: Osmose/olympia
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
コード例 #22
0
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
コード例 #23
0
ファイル: test_helpers.py プロジェクト: bqbn/addons-server
def test_clean_strip_all_html():
    s = '<a href="#woo">yeah</a>'

    expected = 'yeah'
    assert from_string('{{ s|clean(true) }}').render({'s': s}) == expected
コード例 #24
0
def render(s, context=None):
    if context is None:
        context = {}
    t = utils.from_string(s)
    return t.render(context)
コード例 #25
0
def render(s, context=None):
    if context is None:
        context = {}
    t = utils.from_string(s)
    return t.render(context)
コード例 #26
0
ファイル: test_helpers.py プロジェクト: bqbn/addons-server
def test_clean_in_template():
    s = '<a href="#woo">yeah</a>'
    assert from_string('{{ s|clean }}').render({'s': s}) == s
コード例 #27
0
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))
コード例 #28
0
ファイル: test_helpers.py プロジェクト: bqbn/addons-server
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
コード例 #29
0
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))
コード例 #30
0
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))
コード例 #31
0
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))
コード例 #32
0
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