def test_argumented_row_fails(): with pytest.raises(ValueError): get_jinja2_engine().from_string(""" {% placeholder foo %} {% row {"var": e} %}{% endrow %} {% endplaceholder %} """)
def test_nested_placeholders_fail(): with pytest.raises(NestingError): get_jinja2_engine().from_string(""" {% placeholder foo %} {% placeholder bar %}{% endplaceholder %} {% endplaceholder %} """)
def test_nonconstant_column_arg_fails(): with pytest.raises(ValueError): get_jinja2_engine().from_string(""" {% placeholder foo %} {% column {"var": e} %}{% endcolumn %} {% endplaceholder %} """)
def test_placeholder_accepts_valid_global_parameter(): get_jinja2_engine().from_string(""" {% placeholder "stuff" global %} {% plugin "text" %} text = "More stuff" {% endplugin %} {% endplaceholder %} """)
def test_nonstring_but_constant_plugin_content_fails(): with pytest.raises(NonConstant): get_jinja2_engine().from_string(""" {% placeholder foo %} {% plugin "text" %} text = {{ 8 }} {% endplugin %} {% endplaceholder %} """)
def test_placeholder_invalid_parameter(): with pytest.raises(TemplateSyntaxError): get_jinja2_engine().from_string(""" {% placeholder stuff some_invalid_parameter %} {% plugin "text" %} text = "Some stuff" {% endplugin %} {% endplaceholder %} """)
def test_placeholder_parameter_quotes_or_no_quotes_okay(): parameter_markup = """ {%% placeholder stuff %s %%} {%% plugin "text" %%} text = "More stuff" {%% endplugin %%} {%% endplaceholder %%} """ get_jinja2_engine().from_string(parameter_markup % "global") get_jinja2_engine().from_string(parameter_markup % "\"global\"")
def test_global_snippet_resource_injection(): request = get_request(edit=False) request.shop = get_default_shop() jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") request.resolver_match = MagicMock(app_name="shuup") context = dict(view=FauxView, request=request) with override_provides("xtheme_resource_injection", ["shuup.xtheme.resources:inject_global_snippet"]): with override_current_theme_class(): output = template.render(context, request=request) assert "<div>1</div>" not in output Snippet.objects.create( shop=request.shop, location="body_end", snippet_type=SnippetType.InlineJinjaHTMLMarkup, snippet=""" {% set x = 1 %} <div>{{- x -}}</div> """) cache.clear() with override_current_theme_class(): output = template.render(context, request=request) assert "<div>1</div>" in output
def test_rendering(edit, injectable, theme_class, global_type): request = get_request(edit) request.resolver_match = mock.Mock() request.resolver_match.app_name = "shuup" with override_current_theme_class(theme_class): with plugin_override(): jeng = get_jinja2_engine() if global_type: template = jeng.get_template("global_complex.jinja") else: template = jeng.get_template("complex.jinja") view = FauxView() view.xtheme_injection = bool(injectable) output = template.render(context={ "view": view, "request": request }, request=request) # From now on we render placholders in views that # actually can be edited. if injectable and theme_class: assert "wider column" in output assert "less wide column" in output if edit and injectable and theme_class: assert "xt-ph-edit" in output assert "data-xt-placeholder-name" in output assert "data-xt-row" in output assert "data-xt-cell" in output assert "XthemeEditorConfig" in output
def test_layout_rendering_with_global_type(rf): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): jeng = get_jinja2_engine() template = jeng.from_string("") (template, layout, gibberish, ctx) = get_test_template_bits(request) global_class = "xt-global-ph" result = six.text_type( render_placeholder(ctx, "test", layout, template.template.name, global_type=True)) assert global_class in result result = six.text_type( render_placeholder(ctx, "test", layout, template.template.name, global_type=False)) assert global_class not in result
def test_render_custom_size_cell(rf): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): with layout_override(): layout = Layout(FauxTheme, "test") gibberish = printable_gibberish() layout.begin_column({"md": None, "xs": None, "sm": None}) layout.add_plugin("text", {"text": "<p>%s</p>" % gibberish}) jeng = get_jinja2_engine() template = jeng.from_string("") template.template.name = "test" vars = { "view": FauxView(), "request": request } ctx = template.template.new_context(vars) result = six.text_type(render_placeholder(ctx, "test", layout, "test")) expect = """ <div class="placeholder-edit-wrap"> <div class="xt-ph" id="xt-ph-test"> <div class="row xt-ph-row"> <div class="xt-ph-cell"><p>%s</p></div> </div> </div> </div> """ % gibberish assert close_enough(result, expect)
def test_simple_addon_injection(): request = get_request(edit=False) request.shop = get_default_shop() jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") with override_current_theme_class(): with override_provides( "xtheme_resource_injection", [ "shuup_tests.xtheme.test_addon_injections:add_test_injection", ], ): # TestInjector should add alert to end of the body for every request output = template.render(request=request) head, body = output.split("</head>", 1) assert "window.injectedFromAddon=true;" in body with override_settings( SHUUP_XTHEME_EXCLUDE_TEMPLATES_FROM_RESOUCE_INJECTION=[ "resinject.jinja" ]): output = template.render(request=request) head, body = output.split("</head>", 1) assert "window.injectedFromAddon=true;" not in body
def test_simple_addon_injection(): request = get_request(edit=False) jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") with override_current_theme_class(): with override_provides( "xtheme_resource_injection", ["shuup_tests.xtheme.test_addon_injections:add_test_injection",]): # TestInjector should add alert to end of the body for every request output = template.render(request=request) head, body = output.split("</head>", 1) assert "window.injectedFromAddon=true;" in body
def test_simple_addon_injection(): request = get_request(edit=False) jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") with override_current_theme_class(): with override_provides("xtheme_resource_injection", [ "shuup_tests.xtheme.test_addon_injections:add_test_injection", ]): # TestInjector should add alert to end of the body for every request output = template.render(request=request) head, body = output.split("</head>", 1) assert "window.injectedFromAddon=true;" in body
def test_resources(): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") output = template.render(request=request) head, body = output.split("</head>", 1) assert "alert('xss')" in body # the inline script assert '"bars": [1, 2, 3]' in head # the script vars assert '(unknown resource type:' in body # the png assert 'href="://example.com/css.css"' in head # the css assert 'src="://example.com/js.js"' in body # the js assert head.count(ResourceInjectorPlugin.meta_markup) == 1 # the duplicate meta assert ResourceInjectorPlugin.message in output # the actual message
def test_layout_rendering_with_global_type(rf): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): jeng = get_jinja2_engine() template = jeng.from_string("") (template, layout, gibberish, ctx) = get_test_template_bits(request) global_class = "xt-global-ph" result = six.text_type(render_placeholder(ctx, "test", layout, template.template.name, global_type=True)) assert global_class in result result = six.text_type(render_placeholder(ctx, "test", layout, template.template.name, global_type=False)) assert global_class not in result
def test_resources(): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): jeng = get_jinja2_engine() template = jeng.get_template("resinject.jinja") output = template.render(request=request) head, body = output.split("</head>", 1) assert "alert('xss')" in body # the inline script assert '"bars": [1, 2, 3]' in head # the script vars assert '(unknown resource type:' in body # the png assert 'href="://example.com/css.css"' in head # the css assert 'src="://example.com/js.js"' in body # the js assert head.count( ResourceInjectorPlugin.meta_markup) == 1 # the duplicate meta assert ResourceInjectorPlugin.message in output # the actual message
def test_theme_selection(): """ Test that a theme with a `template_dir` actually affects template directory selection. """ with override_current_theme_class(), override_provides("xtheme", [ "shuup_tests.xtheme.utils:FauxTheme", "shuup_tests.xtheme.utils:FauxTheme2", "shuup_tests.xtheme.utils:H2G2Theme", ]): ThemeSettings.objects.all().delete() for theme in get_provide_objects("xtheme"): set_current_theme(theme.identifier) je = get_jinja2_engine() wrapper = (noop() if theme.identifier == "h2g2" else pytest.raises(TemplateDoesNotExist)) with wrapper: t = je.get_template("42.jinja") content = t.render().strip() assert "a slice of lemon wrapped around a large gold brick" in content.replace("\n", " ")
def test_templated_plugin(): jeng = get_jinja2_engine() my_plugin_class = templated_plugin_factory( "MyPlugin", "templated_plugin.jinja", inherited_variables=("name", ), config_copied_variables=("greeting", ), engine=jeng) top_context = { "name": printable_gibberish(), } config = {"greeting": "Good day"} plugin = my_plugin_class(config=config) assert isinstance(plugin, TemplatedPlugin) with override_current_theme_class(None): rendered_content = plugin.render(top_context) expected_content = "Good day %s" % top_context["name"] assert rendered_content == expected_content
def test_templated_plugin(): jeng = get_jinja2_engine() my_plugin_class = templated_plugin_factory("MyPlugin", "templated_plugin.jinja", inherited_variables=("name",), config_copied_variables=("greeting",), engine=jeng ) top_context = { "name": printable_gibberish(), } config = { "greeting": "Good day" } plugin = my_plugin_class(config=config) assert isinstance(plugin, TemplatedPlugin) with override_current_theme_class(None): rendered_content = plugin.render(top_context) expected_content = "Good day %s" % top_context["name"] assert rendered_content == expected_content
def test_rendering(edit, injectable, theme_class, global_type): request = get_request(edit) with override_current_theme_class(theme_class): with plugin_override(): jeng = get_jinja2_engine() if global_type: template = jeng.get_template("global_complex.jinja") else: template = jeng.get_template("complex.jinja") view = FauxView() view.xtheme_injection = bool(injectable) output = template.render(context={ "view": view, }, request=request) assert "wider column" in output assert "less wide column" in output if edit and injectable and theme_class: assert "xt-ph-edit" in output assert "data-xt-placeholder-name" in output assert "data-xt-row" in output assert "data-xt-cell" in output assert "XthemeEditorConfig" in output
def test_parsing(): with override_current_theme_class(None): jeng = get_jinja2_engine() template = jeng.get_template("complex.jinja") assert template # it'sa me! template!
def test_unplaceheld_cola_fails(): with pytest.raises(NestingError): get_jinja2_engine().from_string("""{% column %}{% endcolumn %}""")
def test_bare_placeholder_name_succeeds(): get_jinja2_engine().from_string("""{% placeholder foo %}{% endplaceholder %}""")
def test_nonconstant_placeholder_name_fails(): with pytest.raises(NonConstant): get_jinja2_engine().from_string("""{% placeholder "foo" ~ foo %}{% endplaceholder %}""")