<s> import unittest <EOL> class AuthenticatedPredicateTests ( unittest . TestCase ) : <EOL> def _getFUT ( self ) : <EOL> from repoze . who . restrict import authenticated_predicate <EOL> return authenticated_predicate ( ) <EOL> def test___call___no_identity_returns_False ( self ) : <EOL> predicate = self . _getFUT ( ) <EOL> environ = { } <EOL> self . assertFalse ( predicate ( environ ) ) <EOL> def test___call___w_REMOTE_AUTH_returns_True ( self ) : <EOL> predicate = self . _getFUT ( ) <EOL> environ = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertTrue ( predicate ( environ ) ) <EOL> def test___call___w_repoze_who_identity_returns_True ( self ) : <EOL> predicate = self . _getFUT ( ) <EOL> environ = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertTrue ( predicate ( environ ) ) <EOL> class MakeAuthenticatedRestrictionTests ( unittest . TestCase ) : <EOL> def _getFUT ( self ) : <EOL> from repoze . who . restrict import make_authenticated_restriction <EOL> return make_authenticated_restriction <EOL> def test_enabled ( self ) : <EOL> fut = self . _getFUT ( ) <EOL> app = DummyApp ( ) <EOL> filter = fut ( app , { } , enabled = True ) <EOL> self . assertTrue ( filter . app is app ) <EOL> self . assertTrue ( filter . enabled ) <EOL> predicate = filter . predicate <EOL> self . assertTrue ( predicate ( { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertTrue ( predicate ( { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ) ) <EOL> class PredicateRestrictionTests ( unittest . TestCase ) : <EOL> def _getTargetClass ( self ) : <EOL> from repoze . who . restrict import PredicateRestriction <EOL> return PredicateRestriction <EOL> def _makeOne ( self , app = None , ** kw ) : <EOL> if app is None : <EOL> app = DummyApp ( ) <EOL> return self . _getTargetClass ( ) ( app , ** kw ) <EOL> def test___call___disabled_predicate_false_calls_app_not_predicate ( self ) : <EOL> _tested = [ ] <EOL> def _factory ( ) : <EOL> def _predicate ( env ) : <EOL> assert False <EOL> return _predicate <EOL> def _start_response ( status , headers ) : <EOL> assert False <EOL> environ = { '<STR_LIT>' : True } <EOL> restrict = self . _makeOne ( predicate = _factory , enabled = False ) <EOL> restrict ( environ , _start_response ) <EOL> self . assertEqual ( len ( _tested ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( restrict . app . environ , environ ) <EOL> def test___call___enabled_predicate_false_returns_401 ( self ) : <EOL> _tested = [ ] <EOL> def _factory ( ) : <EOL> def _predicate ( env ) : <EOL> _tested . append ( env ) <EOL> return False <EOL> return _predicate <EOL> _started = [ ] <EOL> def _start_response ( status , headers ) : <EOL> _started . append ( ( status , headers ) ) <EOL> environ = { '<STR_LIT>' : True } <EOL> restrict = self . _makeOne ( predicate = _factory ) <EOL> restrict ( environ , _start_response ) <EOL> self . assertEqual ( len ( _tested ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( _started ) , <NUM_LIT:1> , _started ) <EOL> self . assertEqual ( _started [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( restrict . app . environ , None ) <EOL> def test___call___enabled_predicate_true_calls_app ( self ) : <EOL> _tested = [ ] <EOL> def _factory ( ) : <EOL> def _predicate ( env ) : <EOL> _tested . append ( env ) <EOL> return True <EOL> return _predicate <EOL> def _start_response ( status , headers ) : <EOL> assert False <EOL> environ = { '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' } <EOL> restrict = self . _makeOne ( predicate = _factory ) <EOL> restrict ( environ , _start_response ) <EOL> self . assertEqual ( len ( _tested ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( restrict . app . environ , environ ) <EOL> class MakePredicateRestrictionTests ( unittest . TestCase ) : <EOL> def _getFUT ( self ) : <EOL> from repoze . who . restrict import make_predicate_restriction <EOL> return make_predicate_restriction <EOL> def test_non_string_predicate_no_args ( self ) : <EOL> fut = self . _getFUT ( ) <EOL> app = DummyApp ( ) <EOL> def _predicate ( env ) : <EOL> return True <EOL> def _factory ( ) : <EOL> return _predicate <EOL> filter = fut ( app , { } , predicate = _factory ) <EOL> self . assertTrue ( filter . app is app ) <EOL> self . assertTrue ( filter . predicate is _predicate ) <EOL> self . assertTrue ( filter . enabled ) <EOL> def test_disabled_non_string_predicate_w_args ( self ) : <EOL> fut = self . _getFUT ( ) <EOL> app = DummyApp ( ) <EOL> filter = fut ( app , { } , predicate = DummyPredicate , enabled = False , <EOL> foo = '<STR_LIT>' ) <EOL> self . assertTrue ( filter . app is app ) <EOL> self . assertTrue ( isinstance ( filter . predicate , DummyPredicate ) ) <EOL> self . assertEqual ( filter . predicate . foo , '<STR_LIT>' ) <EOL> self . assertFalse ( filter . enabled ) <EOL> def test_enabled_string_predicate_w_args ( self ) : <EOL> fut = self . _getFUT ( ) <EOL> app = DummyApp ( ) <EOL> filter = fut ( app , { } , <EOL> predicate = '<STR_LIT>' , <EOL> enabled = True , foo = '<STR_LIT>' ) <EOL> self . assertTrue ( filter . app is app ) <EOL> self . assertTrue ( isinstance ( filter . predicate , DummyPredicate ) ) <EOL> self . assertEqual ( filter . predicate . foo , '<STR_LIT>' ) <EOL> self . assertTrue ( filter . enabled ) <EOL> class DummyApp ( object ) : <EOL> environ = None <EOL> def __call__ ( self , environ , start_response ) : <EOL> self . environ = environ <EOL> return [ ] <EOL> class DummyPredicate ( object ) : <EOL> def __init__ ( self , ** kw ) : <EOL> self . __dict__ . update ( kw ) </s>
