<s> import mock <EOL> import webob <EOL> from nailgun . middleware . keystone import NailgunFakeKeystoneAuthMiddleware <EOL> from nailgun . middleware . keystone import NailgunKeystoneAuthMiddleware <EOL> from nailgun . test . base import BaseUnitTest <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class AuthMiddlewareTestBase ( object ) : <EOL> MiddlewareClass = None <EOL> def setUp ( self ) : <EOL> self . app = mock . Mock ( ) <EOL> self . fake_start_response = mock . Mock ( ) <EOL> super ( AuthMiddlewareTestBase , self ) . setUp ( ) <EOL> def get_middleware ( self ) : <EOL> return self . MiddlewareClass ( self . app ) <EOL> def test_do_not_verify_public_urls_and_methods ( self , mock_public_urls ) : <EOL> mock_public_urls . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> } <EOL> fake_req = webob . Request . blank ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) <EOL> middleware = self . get_middleware ( ) <EOL> middleware ( fake_req . environ , self . fake_start_response ) <EOL> self . assertTrue ( self . app . called ) <EOL> self . app . assert_called_with ( fake_req . environ , self . fake_start_response ) <EOL> self . assertTrue ( fake_req . environ [ '<STR_LIT>' ] ) <EOL> def test_verify_non_public_urls ( self , mock_public_urls ) : <EOL> mock_public_urls . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT:GET>' ] <EOL> } <EOL> fake_req = webob . Request . blank ( '<STR_LIT>' ) <EOL> middleware = self . get_middleware ( ) <EOL> middleware ( fake_req . environ , self . fake_start_response ) <EOL> self . assertFalse ( self . app . called ) <EOL> self . assertFalse ( fake_req . environ [ '<STR_LIT>' ] ) <EOL> def test_verify_non_public_method_on_public_url ( self , mock_public_urls ) : <EOL> mock_public_urls . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT:GET>' ] <EOL> } <EOL> fake_req = webob . Request . blank ( '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> middleware = self . get_middleware ( ) <EOL> middleware ( fake_req . environ , self . fake_start_response ) <EOL> self . assertFalse ( self . app . called ) <EOL> self . assertFalse ( fake_req . environ [ '<STR_LIT>' ] ) <EOL> def test_raise_error_on_invalid_route_regexp ( self , mock_public_urls ) : <EOL> mock_public_urls . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT:GET>' ] <EOL> } <EOL> with self . assertRaisesRegexp ( Exception , <EOL> '<STR_LIT>' ) : <EOL> self . get_middleware ( ) <EOL> class TestFakeKeystoneAuthMiddleware ( AuthMiddlewareTestBase , BaseUnitTest ) : <EOL> MiddlewareClass = NailgunFakeKeystoneAuthMiddleware <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_401_on_invalid_token ( self , mock_invalid_token ) : <EOL> mock_invalid_token . return_value = False <EOL> fake_req = webob . Request . blank ( '<STR_LIT>' ) <EOL> middleware = self . get_middleware ( ) <EOL> middleware ( fake_req . environ , self . fake_start_response ) <EOL> self . fake_start_response . assert_called_with ( '<STR_LIT>' , [ ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_token_is_valid ( self , mock_invalid_token ) : <EOL> mock_invalid_token . return_value = True <EOL> fake_req = webob . Request . blank ( '<STR_LIT>' ) <EOL> middleware = self . get_middleware ( ) <EOL> middleware ( fake_req . environ , self . fake_start_response ) <EOL> self . assertTrue ( self . app . called ) <EOL> self . app . assert_called_with ( fake_req . environ , self . fake_start_response ) <EOL> class TestKeystoneAuthMiddleware ( AuthMiddlewareTestBase , BaseUnitTest ) : <EOL> MiddlewareClass = NailgunKeystoneAuthMiddleware </s>
