def test_should_not_hoist_if_do(self): code = """ #def f #end def #def foo($bar) #if True #do $f($bar) $reg_f($bar) #else $reg_f($bar) #end if #end def """ final_tree = self._get_final_tree(code) def pred_if(node): return type(node) == ast.IfNode if_node = walker.find_node(final_tree, pred_if) def pred(node): return type(node) == ast.AssignNode alias = walker.find_node(if_node, pred) if not alias: self.fail('ast.AssignNode should be present in the If block.')
def test_private_filter_function_macro(self): code = """ #def foo $my_macro() #end def """ def macro_function(macro_node, arg_map, compiler): return '#set $bar = self._filter_function("test")\n' self.compiler.register_macro('macro_function_my_macro', macro_function, parse_rule='fragment_goal') ast_root = self._compile(code) semantic_analyzer = analyzer.SemanticAnalyzer( 'TestTemplate', ast_root, self.compiler.analyzer_options, self.compiler) analyzed_tree = semantic_analyzer.get_ast() optimization_analyzer = self._get_analyzer(analyzed_tree) optimized_tree = optimization_analyzer.optimize_ast() def pred(node): return bool(type(node) == ast.IdentifierNode and node.name == '_self_private_filter_function') filter_node = walker.find_node(optimized_tree, pred) if not filter_node: self.fail('Expected _self_private_filter_function in ast')
def test_template_method_direct(self): code = """ #def foo Hello #end def #def bar $foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return ( type(node) == ast.CallFunctionNode and type(node.expression) == ast.PlaceholderNode and node.expression.name == "foo" ) foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail("Expected foo() in ast") self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
def test_external_function_maybe(self): code = """ #from module import my_lib #def bar $my_lib.foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return (type(node) == ast.CallFunctionNode and type(node.expression) == ast.GetUDNNode and type(node.expression.expression) == ast.PlaceholderNode and node.expression.expression.name == 'my_lib' and node.expression.name == 'foo') foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail('Expected my_libfoo() in ast') self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.UNKNOWN)
def test_template_method_direct(self): code = """ #def foo Hello #end def #def bar $foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return (type(node) == ast.CallFunctionNode and type(node.expression) == ast.PlaceholderNode and node.expression.name == 'foo') foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail('Expected foo() in ast') self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
def test_external_function_maybe(self): code = """ #from module import my_lib #def bar $my_lib.foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return ( type(node) == ast.CallFunctionNode and type(node.expression) == ast.GetUDNNode and type(node.expression.expression) == ast.PlaceholderNode and node.expression.expression.name == "my_lib" and node.expression.name == "foo" ) foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail("Expected my_libfoo() in ast") self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.UNKNOWN)
def test_do_syntax(self): code = """ #def foo #do $bar() #end def """ template = self._compile(code) do_node = walker.find_node(template, TestDo._do_pred) if not do_node: self.fail('Do node should be present in AST')
def test_allow_raw(self): code = """ #allow_raw #def foo #end def """ template = self._compile(code) allow_raw_node = walker.find_node(template, TestAllowRaw._allow_raw_pred) if not allow_raw_node: self.fail('ast.AllowRawNode should be present in AST')
def test_escape_needed(self): code = """ #def foo \#if #end def """ template = self._compile(code) def_node = walker.find_node(template, TestEscapeHash._def_foo_pred) text = ''.join([node.value for node in def_node.child_nodes if type(node) == ast.TextNode]) self.assertEqual(text, '#if')
def test_should_hoist_for(self): code = """ #def foo($bar) #for $i in [] $reg_f($bar) #end for #end def """ final_tree = self._get_final_tree(code) def pred(node): return type(node) == ast.AssignNode and type(node.parent) == ast.FunctionNode alias = walker.find_node(final_tree, pred) if not alias: self.fail('Expected to find ast.AssignNode hoisted to function scope.')
def test_escape_needed(self): code = """ #def foo \#if #end def """ template = self._compile(code) def_node = walker.find_node(template, TestEscapeHash._def_foo_pred) text = ''.join([ node.value for node in def_node.child_nodes if type(node) == ast.TextNode ]) self.assertEqual(text, '#if')
def test_should_not_need_sanitization_filter(self): code = """ #def foo($bar) $bar() #end def """ optimized_tree = self._get_optimized_tree(code) def pred(node): return type(node) == ast.CallFunctionNode and type(node.parent) == ast.FilterNode call_node = walker.find_node(optimized_tree, pred) if not call_node: self.fail("Expected to find a ast.CallFunctionNode.") if call_node.sanitization_state != ast.SanitizedState.OUTPUTTED_IMMEDIATELY: self.fail("Expected node in ast.FilterNode to not need sanitization.")
def test_should_not_need_sanitization_do(self): code = """ #def foo($bar) #do $bar.baz() #end def """ optimized_tree = self._get_optimized_tree(code) def pred(node): return type(node) == ast.CallFunctionNode call_node = walker.find_node(optimized_tree, pred) if not call_node: self.fail('Expected to find a ast.CallFunctionNode.') if call_node.sanitization_state != ast.SanitizedState.NOT_OUTPUTTED: self.fail('Expected node in #do to not need sanitization.')
def test_should_not_hoist_for(self): code = """ #def foo($bar) #for $i in [] #set $bar["1"] = 1 $reg_f($bar) #end for #end def """ final_tree = self._get_final_tree(code) def pred(node): return type(node) == ast.AssignNode and type( node.parent) == ast.FunctionNode alias = walker.find_node(final_tree, pred) if alias: self.fail('ast.AssignNode should not be hoisted to function scope.')
def test_should_not_hoist_if_set_output(self): code = """ #def foo($bar) #if True #set $bar[1] = 1 $bar #else $bar #end if #end def """ final_tree = self._get_final_tree(code) def pred(node): return type(node) == ast.AssignNode and type(node.parent) == ast.FunctionNode alias = walker.find_node(final_tree, pred) if alias: self.fail('ast.AssignNode should not be hoisted to the ast.FunctionNode.')
def test_should_not_need_sanitization_filter(self): code = """ #def foo($bar) $bar() #end def """ optimized_tree = self._get_optimized_tree(code) def pred(node): return (type(node) == ast.CallFunctionNode and type(node.parent) == ast.FilterNode) call_node = walker.find_node(optimized_tree, pred) if not call_node: self.fail('Expected to find a ast.CallFunctionNode.') if (call_node.sanitization_state != ast.SanitizedState.OUTPUTTED_IMMEDIATELY): self.fail( 'Expected node in ast.FilterNode to not need sanitization.')
def test_library_function_registry_yes(self): code = """ #def bar $reg_f() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return ( type(node) == ast.CallFunctionNode and type(node.expression) == ast.PlaceholderNode and node.expression.name == "reg_f" ) foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail("Expected reg_f() in ast") self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED)
def test_library_function_registry_yes(self): code = """ #def bar $reg_f() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return (type(node) == ast.CallFunctionNode and type(node.expression) == ast.PlaceholderNode and node.expression.name == 'reg_f') foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail('Expected reg_f() in ast') self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED)
def test_should_hoist_if(self): code = """ #def foo($bar) #if True $reg_f($bar) #else $reg_f($bar) #end if #end def """ final_tree = self._get_final_tree(code) def pred(node): return type(node) == ast.AssignNode and type( node.parent) == ast.FunctionNode alias = walker.find_node(final_tree, pred) if not alias: self.fail( 'Expected to find ast.AssignNode hoisted to function scope.')
def test_should_not_hoist_if_set_output(self): code = """ #def foo($bar) #if True #set $bar[1] = 1 $bar #else $bar #end if #end def """ final_tree = self._get_final_tree(code) def pred(node): return type(node) == ast.AssignNode and type( node.parent) == ast.FunctionNode alias = walker.find_node(final_tree, pred) if alias: self.fail( 'ast.AssignNode should not be hoisted to the ast.FunctionNode.')
def test_library_function_direct(self): code = """ #from module import library my_lib #def bar $my_lib.foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return ( type(node) == ast.CallFunctionNode and type(node.expression) == ast.IdentifierNode and node.expression.name == "my_lib.foo" ) foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail("Expected my_lib.foo() in ast") self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
def test_library_function_direct(self): code = """ #from module import library my_lib #def bar $my_lib.foo() #end def """ template = self._compile(code) semantic_analyzer = self._get_analyzer(template) analyzed_ast = semantic_analyzer.get_ast() def pred(node): return (type(node) == ast.CallFunctionNode and type(node.expression) == ast.IdentifierNode and node.expression.name == 'my_lib.foo') foo_call = walker.find_node(analyzed_ast, pred) if not foo_call: self.fail('Expected my_lib.foo() in ast') self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
def test_cache_filter_args_udn(self): code = """ #from foo import bar #def func $bar.baz('arg') #end def """ ast_root = self._compile(code) semantic_analyzer = analyzer.SemanticAnalyzer( 'TestTemplate', ast_root, self.compiler.analyzer_options, self.compiler) analyzed_tree = semantic_analyzer.get_ast() optimization_analyzer = self._get_analyzer(analyzed_tree) optimized_tree = optimization_analyzer.optimize_ast() def pred(node): return type(node) == ast.AssignNode alias_assign = walker.find_node(optimized_tree, pred) if not alias_assign: self.fail('There should be an ast.AssignNode due to caching')
def pred(node): return type(node) == ast.AssignNode alias_assign = walker.find_node(optimized_tree, pred) if not alias_assign: self.fail('There should be an ast.AssignNode due to caching')