def pytest_runtest_call(item): # All tests are run with a theme override `shoop.themes.classic_gray.ClassicGrayTheme`. # To un-override, use `with override_current_theme_class()` (no arguments to re-enable database lookup) from shoop.themes.classic_gray import ClassicGrayTheme item.session._theme_overrider = override_current_theme_class(ClassicGrayTheme) item.session._theme_overrider.__enter__()
def test_layout_edit_render(): request = get_request(edit=True) with override_current_theme_class(None): with plugin_override(): (template, layout, gibberish, ctx) = get_test_template_bits(request) result = six.text_type(render_placeholder(ctx, "test", layout, "test")) # Look for evidence of editing: assert "xt-ph-edit" in result assert "data-xt-placeholder-name" in result assert "data-xt-row" in result assert "data-xt-cell" in result
def test_xtheme_extra_views(rf): with override_current_theme_class(H2G2Theme): request = rf.get("/", {"name": "Arthur Dent"}) # Simulate /xtheme/greeting response = extra_view_dispatch(request, "greeting") assert force_text(response.content) == "So long, and thanks for all the fish, Arthur Dent" # Try that again (to exercise the _VIEW_CACHE code path): response = extra_view_dispatch(request, "greeting") assert force_text(response.content) == "So long, and thanks for all the fish, Arthur Dent" # Now test that CBVs work assert not extra_view_dispatch(request, "faux").content
def test_view_config_caches_into_context(rf): # This is a silly test... request = get_request(edit=False) with override_current_theme_class(None): (template, layout, gibberish, ctx) = get_test_template_bits(request) cfg1 = get_view_config(ctx) cfg2 = get_view_config(ctx) assert cfg1 is cfg2 (template, layout, gibberish, ctx) = get_test_template_bits(request, False) cfg1 = get_view_config(ctx) cfg2 = get_view_config(ctx) assert cfg1 is cfg2
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", ["shoop_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", [ "shoop_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_layout_edit_render(): request = get_request(edit=True) with override_current_theme_class(None): with plugin_override(): (template, layout, gibberish, ctx) = get_test_template_bits(request) result = six.text_type( render_placeholder(ctx, "test", layout, "test")) # Look for evidence of editing: assert "xt-ph-edit" in result assert "data-xt-placeholder-name" in result assert "data-xt-row" in result assert "data-xt-cell" in result
def test_theme_activation(): with override_current_theme_class(): with override_provides("xtheme", [ "shoop_tests.xtheme.utils:FauxTheme", "shoop_tests.xtheme.utils:FauxTheme2" ]): ThemeSettings.objects.all().delete() assert not get_current_theme() set_current_theme(FauxTheme.identifier) assert isinstance(get_current_theme(), FauxTheme) set_current_theme(FauxTheme2.identifier) assert isinstance(get_current_theme(), FauxTheme2) with pytest.raises(ValueError): set_current_theme(printable_gibberish())
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_xtheme_extra_views(rf): with override_current_theme_class(H2G2Theme): request = rf.get("/", {"name": "Arthur Dent"}) # Simulate /xtheme/greeting response = extra_view_dispatch(request, "greeting") assert force_text( response.content ) == "So long, and thanks for all the fish, Arthur Dent" # Try that again (to exercise the _VIEW_CACHE code path): response = extra_view_dispatch(request, "greeting") assert force_text( response.content ) == "So long, and thanks for all the fish, Arthur Dent" # Now test that CBVs work assert not extra_view_dispatch(request, "faux").content
def test_layout_rendering(rf): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): (template, layout, gibberish, ctx) = get_test_template_bits(request) result = six.text_type(render_placeholder(ctx, "test", layout, "test")) expect = """ <div class="xt-ph" id="xt-ph-test"> <div class="row xt-ph-row"> <div class="col-md-12 hidden-xs xt-ph-cell"><p>%s</p></div> </div> </div> """ % gibberish assert close_enough(result, expect)
def initialize_editor_view(view_name, placeholder_name, request=None): if request is None: request = RequestFactory().get("/") request.user = SuperUser() if hasattr(request.GET, "_mutable"): request.GET._mutable = True # Ahem request.GET.update({ "theme": FauxTheme.identifier, "view": view_name, "ph": placeholder_name }) with plugin_override(): with override_provides("xtheme", ["shoop_tests.xtheme.utils:FauxTheme"]): with override_current_theme_class(FauxTheme): yield EditorView(request=request, args=(), kwargs={})
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(rf): request = get_request(edit=False) with override_current_theme_class(None): with plugin_override(): (template, layout, gibberish, ctx) = get_test_template_bits(request) result = six.text_type( render_placeholder(ctx, "test", layout, "test")) expect = """ <div class="xt-ph" id="xt-ph-test"> <div class="row xt-ph-row"> <div class="col-md-12 hidden-xs xt-ph-cell"><p>%s</p></div> </div> </div> """ % gibberish assert close_enough(result, expect)
def test_rendering(edit, injectable, theme_class): request = get_request(edit) with override_current_theme_class(theme_class): with plugin_override(): jeng = get_jinja2_engine() 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_theme_selection(): """ Test that a theme with a `template_dir` actually affects template directory selection. """ with override_current_theme_class(), override_provides("xtheme", [ "shoop_tests.xtheme.utils:FauxTheme", "shoop_tests.xtheme.utils:FauxTheme2", "shoop_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_rendering(edit, injectable, theme_class): request = get_request(edit) with override_current_theme_class(theme_class): with plugin_override(): jeng = get_jinja2_engine() 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_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_theme_selection(): """ Test that a theme with a `template_dir` actually affects template directory selection. """ with override_current_theme_class(), override_provides( "xtheme", [ "shoop_tests.xtheme.utils:FauxTheme", "shoop_tests.xtheme.utils:FauxTheme2", "shoop_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 pytest_runtest_call(item): # All tests are run with a default theme override `shoop.themes.default_theme.DefaultTheme`. # To un-override, use `with override_current_theme_class()` (no arguments to re-enable database lookup) from shoop.themes.default_theme import DefaultTheme item.session._theme_overrider = override_current_theme_class(DefaultTheme) item.session._theme_overrider.__enter__()
def test_xtheme_extra_view_exceptions(rf): with override_current_theme_class(H2G2Theme): request = rf.get("/") assert extra_view_dispatch(request, "vogons").status_code == 404 with pytest.raises(ImproperlyConfigured): assert extra_view_dispatch(request, "true")
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!