<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> import tempfile <EOL> import unittest <EOL> from jinja2 . testsuite import JinjaTestCase <EOL> from jinja2 import Environment <EOL> from jinja2 . sandbox import SandboxedEnvironment , ImmutableSandboxedEnvironment , unsafe <EOL> from jinja2 import Markup , escape <EOL> from jinja2 . exceptions import SecurityError , TemplateSyntaxError <EOL> class PrivateStuff ( object ) : <EOL> def bar ( self ) : <EOL> return <NUM_LIT> <EOL> @ unsafe <EOL> def foo ( self ) : <EOL> return <NUM_LIT> <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class PublicStuff ( object ) : <EOL> bar = lambda self : <NUM_LIT> <EOL> _foo = lambda self : <NUM_LIT> <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class SandboxTestCase ( JinjaTestCase ) : <EOL> def test_unsafe ( self ) : <EOL> env = SandboxedEnvironment ( ) <EOL> self . assert_raises ( SecurityError , env . from_string ( "<STR_LIT>" ) . render , <EOL> foo = PrivateStuff ( ) ) <EOL> self . assert_equal ( env . from_string ( "<STR_LIT>" ) . render ( foo = PrivateStuff ( ) ) , '<STR_LIT>' ) <EOL> self . assert_raises ( SecurityError , env . from_string ( "<STR_LIT>" ) . render , <EOL> foo = PublicStuff ( ) ) <EOL> self . assert_equal ( env . from_string ( "<STR_LIT>" ) . render ( foo = PublicStuff ( ) ) , '<STR_LIT>' ) <EOL> self . assert_equal ( env . from_string ( "<STR_LIT>" ) . render ( foo = <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assert_equal ( env . from_string ( "<STR_LIT>" ) . render ( foo = lambda : None ) , '<STR_LIT>' ) <EOL> self . assert_raises ( SecurityError , env . from_string ( <EOL> "<STR_LIT>" ) . render , foo = <NUM_LIT> ) <EOL> def test_immutable_environment ( self ) : <EOL> env = ImmutableSandboxedEnvironment ( ) <EOL> self . assert_raises ( SecurityError , env . from_string ( <EOL> '<STR_LIT>' ) . render ) <EOL> self . assert_raises ( SecurityError , env . from_string ( <EOL> '<STR_LIT>' ) . render ) <EOL> def test_restricted ( self ) : <EOL> env = SandboxedEnvironment ( ) <EOL> self . assert_raises ( TemplateSyntaxError , env . from_string , <EOL> "<STR_LIT>" ) <EOL> self . assert_raises ( TemplateSyntaxError , env . from_string , <EOL> "<STR_LIT>" ) <EOL> def test_markup_operations ( self ) : <EOL> unsafe = '<STR_LIT>' <EOL> safe = Markup ( '<STR_LIT>' ) <EOL> assert unsafe + safe == unicode ( escape ( unsafe ) ) + unicode ( safe ) <EOL> assert Markup ( '<STR_LIT>' ) % '<STR_LIT>' == '<STR_LIT>' <EOL> assert Markup ( '<STR_LIT>' ) % { <EOL> '<STR_LIT:username>' : '<STR_LIT>' <EOL> } == '<STR_LIT>' <EOL> assert type ( Markup ( '<STR_LIT:foo>' ) + '<STR_LIT:bar>' ) is Markup <EOL> x = Markup ( "<STR_LIT:foo>" ) <EOL> assert x . __html__ ( ) is x <EOL> class Foo ( object ) : <EOL> def __html__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' <EOL> assert Markup ( Foo ( ) ) == '<STR_LIT>' <EOL> assert Markup ( '<STR_LIT>' ) % Foo ( ) == '<STR_LIT>' <EOL> assert escape ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert Markup ( "<STR_LIT>" ) . striptags ( ) == "<STR_LIT>" <EOL> assert Markup ( "<STR_LIT>" ) . unescape ( ) == "<STR_LIT>" <EOL> def test_template_data ( self ) : <EOL> env = Environment ( autoescape = True ) <EOL> t = env . from_string ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> escaped_out = '<STR_LIT>' <EOL> assert t . render ( ) == escaped_out <EOL> assert unicode ( t . module ) == escaped_out <EOL> assert escape ( t . module ) == escaped_out <EOL> assert t . module . say_hello ( '<STR_LIT>' ) == escaped_out <EOL> assert escape ( t . module . say_hello ( '<STR_LIT>' ) ) == escaped_out <EOL> def test_attr_filter ( self ) : <EOL> env = SandboxedEnvironment ( ) <EOL> tmpl = env . from_string ( '<STR_LIT>' ) <EOL> self . assert_raises ( SecurityError , tmpl . render ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( SandboxTestCase ) ) <EOL> return suite </s>
