def test_out_of_context_access(self): template = Template("""${loop.index}""") assert_raises_message( exceptions.RuntimeException, "No loop context is established", template.render, )
def test_dont_accept_relative_outside_of_root(self): """test the mechanics of an include where the include goes outside of the path""" tl = lookup.TemplateLookup( directories=[os.path.join(config.template_base, "subdir")]) index = tl.get_template("index.html") ctx = runtime.Context(FastEncodingBuffer()) ctx._with_template = index assert_raises_message( exceptions.TemplateLookupException, 'Template uri "../index.html" is invalid - it ' "cannot be relative outside of the root path", runtime._lookup_template, ctx, "../index.html", index.uri, ) assert_raises_message( exceptions.TemplateLookupException, 'Template uri "../othersubdir/foo.html" is invalid - it ' "cannot be relative outside of the root path", runtime._lookup_template, ctx, "../othersubdir/foo.html", index.uri, ) # this is OK since the .. cancels out runtime._lookup_template(ctx, "foo/../index.html", index.uri)
def test_names_in_template(self): for name in ("context", "loop", "UNDEFINED", "STOP_RENDERING"): assert_raises_message( exceptions.NameConflictError, r"Reserved words declared in template: %s" % name, Template, "<%% %s = 5 %%>" % name, )
def test_names_on_context(self): for name in ("context", "loop", "UNDEFINED", "STOP_RENDERING"): assert_raises_message( exceptions.NameConflictError, r"Reserved words passed to render\(\): %s" % name, Template("x").render, **{name: "foo"}, )
def test_load_plugin_failure(self): loader = util.PluginLoader("fakegroup") assert_raises_message( exceptions.RuntimeException, "Can't load plugin fakegroup fake", loader.load, "fake", )
def test_no_lookup(self): t = Template("hi <%include file='foo.html'/>") assert_raises_message( exceptions.TemplateLookupException, "Template 'memory:%s' has no TemplateLookup associated" % hex(id(t)), t.render, )
def test_no_named_in_def(self): assert_raises_message( exceptions.CompileException, "Named block 'y' not allowed inside of def 'q'", Template, """ <%def name="q()"> <%block name="y"> </%block> </%def> """, )
def test_unmatched_control(self): template = """ % if foo: % for x in range(1,5): % endif """ assert_raises_message( exceptions.SyntaxException, "Keyword 'endif' doesn't match keyword 'for' at line: 5 char: 1", Lexer(template).parse, )
def test_anonymous_block_namespace_raises(self): assert_raises_message( exceptions.CompileException, "Can't put anonymous blocks inside <%namespace>", Template, """ <%namespace name="foo"> <%block> block </%block> </%namespace> """, )
def test_unmatched_control_2(self): template = """ % if foo: % for x in range(1,5): % endfor """ assert_raises_message( exceptions.SyntaxException, "Unterminated control keyword: 'if' at line: 3 char: 1", Lexer(template).parse, )
def test_nested_dupe_names_raise(self): assert_raises_message( exceptions.CompileException, "%def or %block named 'header' already exists in this template.", Template, """ <%inherit file="base"/> <%block name="header"> <%block name="header"> inner header </%block> </%block> """, )
def test_name_collision_blocks_toplevel(self): assert_raises_message( exceptions.CompileException, "%def or %block named 'x' already exists in this template", Template, """ <%block name="x"> block </%block> foob <%block name="x"> block </%block> """, )
def test_name_collision_blocks_nested_def(self): assert_raises_message( exceptions.CompileException, "Named block 'x' not allowed inside of def 'foo'", Template, """ <%def name="foo()"> <%block name="x"> block </%block> foob <%block name="x"> block </%block> </%def> """, )
def test_named_block_in_call(self): assert_raises_message( exceptions.CompileException, "Named block 'y' not allowed inside of <%call> tag", Template, """ <%self:foo x="5"> <%block name="y"> this is the block </%block> </%self:foo> <%def name="foo(x)"> foo: ${caller.body()} ${caller.y()} </%def> """, )
def test_dont_accept_relative_outside_of_root(self): assert_raises_message( exceptions.TemplateLookupException, 'Template uri "../../foo.html" is invalid - it ' "cannot be relative outside of the root path", Template, "test", uri="../../foo.html", ) assert_raises_message( exceptions.TemplateLookupException, 'Template uri "/../../foo.html" is invalid - it ' "cannot be relative outside of the root path", Template, "test", uri="/../../foo.html", ) # normalizes in the root is OK t = Template("test", uri="foo/bar/../../foo.html") eq_(t.uri, "foo/bar/../../foo.html")