<s> import argparse <EOL> import mock <EOL> import pkg_resources <EOL> import requests <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> from cinderclient import auth_plugin <EOL> from cinderclient import exceptions <EOL> from cinderclient . tests . unit import utils <EOL> from cinderclient . v1 import client <EOL> def mock_http_request ( resp = None ) : <EOL> """<STR_LIT>""" <EOL> if not resp : <EOL> resp = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> } <EOL> } , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } , <EOL> } <EOL> auth_response = utils . TestResponse ( { <EOL> "<STR_LIT>" : <NUM_LIT:200> , <EOL> "<STR_LIT:text>" : json . dumps ( resp ) , <EOL> } ) <EOL> return mock . Mock ( return_value = ( auth_response ) ) <EOL> def requested_headers ( cs ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : cs . client . USER_AGENT , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> } <EOL> class DeprecatedAuthPluginTest ( utils . TestCase ) : <EOL> def test_auth_system_success ( self ) : <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return self . authenticate <EOL> def authenticate ( self , cls , auth_url ) : <EOL> cls . _authenticate ( auth_url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def mock_iter_entry_points ( _type , name ) : <EOL> if _type == '<STR_LIT>' : <EOL> return [ MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> else : <EOL> return [ ] <EOL> mock_request = mock_http_request ( ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" , <EOL> mock_iter_entry_points ) <EOL> @ mock . patch . object ( requests , "<STR_LIT>" , mock_request ) <EOL> def test_auth_call ( ) : <EOL> plugin = auth_plugin . DeprecatedAuthPlugin ( "<STR_LIT>" ) <EOL> cs = client . Client ( "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , auth_system = "<STR_LIT>" , <EOL> auth_plugin = plugin ) <EOL> cs . client . authenticate ( ) <EOL> headers = requested_headers ( cs ) <EOL> token_url = cs . client . auth_url + "<STR_LIT>" <EOL> mock_request . assert_called_with ( <EOL> "<STR_LIT:POST>" , <EOL> token_url , <EOL> headers = headers , <EOL> data = '<STR_LIT>' , <EOL> allow_redirects = True , <EOL> ** self . TEST_REQUEST_BASE ) <EOL> test_auth_call ( ) <EOL> def test_auth_system_not_exists ( self ) : <EOL> def mock_iter_entry_points ( _t , name = None ) : <EOL> return [ pkg_resources . EntryPoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> mock_request = mock_http_request ( ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" , <EOL> mock_iter_entry_points ) <EOL> @ mock . patch . object ( requests . Session , "<STR_LIT>" , mock_request ) <EOL> def test_auth_call ( ) : <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . DeprecatedAuthPlugin ( "<STR_LIT>" ) <EOL> cs = client . Client ( "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , auth_system = "<STR_LIT>" , <EOL> auth_plugin = plugin ) <EOL> self . assertRaises ( exceptions . AuthSystemNotFound , <EOL> cs . client . authenticate ) <EOL> test_auth_call ( ) <EOL> def test_auth_system_defining_auth_url ( self ) : <EOL> class MockAuthUrlEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return self . auth_url <EOL> def auth_url ( self ) : <EOL> return "<STR_LIT>" <EOL> class MockAuthenticateEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return self . authenticate <EOL> def authenticate ( self , cls , auth_url ) : <EOL> cls . _authenticate ( auth_url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def mock_iter_entry_points ( _type , name ) : <EOL> if _type == '<STR_LIT>' : <EOL> return [ MockAuthUrlEntrypoint ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT>" ] ) ] <EOL> elif _type == '<STR_LIT>' : <EOL> return [ MockAuthenticateEntrypoint ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT>" ] ) ] <EOL> else : <EOL> return [ ] <EOL> mock_request = mock_http_request ( ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" , <EOL> mock_iter_entry_points ) <EOL> @ mock . patch . object ( requests . Session , "<STR_LIT>" , mock_request ) <EOL> def test_auth_call ( ) : <EOL> plugin = auth_plugin . DeprecatedAuthPlugin ( "<STR_LIT>" ) <EOL> cs = client . Client ( "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> auth_system = "<STR_LIT>" , <EOL> auth_plugin = plugin ) <EOL> cs . client . authenticate ( ) <EOL> self . assertEqual ( cs . client . auth_url , "<STR_LIT>" ) <EOL> test_auth_call ( ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_client_raises_exc_without_auth_url ( self , mock_iter_entry_points ) : <EOL> class MockAuthUrlEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return self . auth_url <EOL> def auth_url ( self ) : <EOL> return None <EOL> mock_iter_entry_points . side_effect = lambda _t , name : [ <EOL> MockAuthUrlEntrypoint ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT>" ] ) ] <EOL> plugin = auth_plugin . DeprecatedAuthPlugin ( "<STR_LIT>" ) <EOL> self . assertRaises ( <EOL> exceptions . EndpointNotFound , <EOL> client . Client , "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> auth_system = "<STR_LIT>" , auth_plugin = plugin ) <EOL> class AuthPluginTest ( utils . TestCase ) : <EOL> @ mock . patch . object ( requests , "<STR_LIT>" ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def _test_auth_success ( self , mock_iter_entry_points , mock_request , <EOL> ** client_kwargs ) : <EOL> """<STR_LIT>""" <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> def authenticate ( self , cls , auth_url ) : <EOL> cls . _authenticate ( auth_url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> mock_request . side_effect = mock_http_request ( ) <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . load_plugin ( "<STR_LIT>" ) <EOL> cs = client . Client ( "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , auth_system = "<STR_LIT>" , <EOL> auth_plugin = plugin , ** client_kwargs ) <EOL> cs . client . authenticate ( ) <EOL> headers = requested_headers ( cs ) <EOL> token_url = cs . client . auth_url + "<STR_LIT>" <EOL> mock_request . assert_called_with ( <EOL> "<STR_LIT:POST>" , <EOL> token_url , <EOL> headers = headers , <EOL> data = '<STR_LIT>' , <EOL> allow_redirects = True , <EOL> ** self . TEST_REQUEST_BASE ) <EOL> return cs . client <EOL> def test_auth_system_success ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _test_auth_success ( ) <EOL> self . assertIsNone ( c . bypass_url ) <EOL> self . assertIsNone ( c . proxy_token ) <EOL> def test_auth_bypass_url ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _test_auth_success ( bypass_url = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , c . bypass_url ) <EOL> self . assertEqual ( '<STR_LIT>' , c . management_url ) <EOL> self . assertIsNone ( c . proxy_token ) <EOL> def test_auth_bypass_url_proxy_token ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _test_auth_success ( proxy_token = '<STR_LIT:abc>' , <EOL> bypass_url = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , c . bypass_url ) <EOL> self . assertEqual ( '<STR_LIT>' , c . management_url ) <EOL> self . assertEqual ( '<STR_LIT:abc>' , c . proxy_token ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_discover_auth_system_options ( self , mock_iter_entry_points ) : <EOL> """<STR_LIT>""" <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> @ staticmethod <EOL> def add_opts ( parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> parser = argparse . ArgumentParser ( ) <EOL> auth_plugin . discover_auth_systems ( ) <EOL> auth_plugin . load_auth_system_opts ( parser ) <EOL> opts , args = parser . parse_known_args ( [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( opts . auth_system_opt ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_parse_auth_system_options ( self , mock_iter_entry_points ) : <EOL> """<STR_LIT>""" <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> def __init__ ( self ) : <EOL> self . opts = { "<STR_LIT>" : True } <EOL> def parse_opts ( self , args ) : <EOL> return self . opts <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . load_plugin ( "<STR_LIT>" ) <EOL> plugin . parse_opts ( [ ] ) <EOL> self . assertIn ( "<STR_LIT>" , plugin . opts ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_auth_system_defining_url ( self , mock_iter_entry_points ) : <EOL> """<STR_LIT>""" <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> def get_auth_url ( self ) : <EOL> return "<STR_LIT>" <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . load_plugin ( "<STR_LIT>" ) <EOL> cs = client . Client ( "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> auth_system = "<STR_LIT>" , <EOL> auth_plugin = plugin ) <EOL> self . assertEqual ( cs . client . auth_url , "<STR_LIT>" ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_exception_if_no_authenticate ( self , mock_iter_entry_points ) : <EOL> """<STR_LIT>""" <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> pass <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . load_plugin ( "<STR_LIT>" ) <EOL> self . assertRaises ( <EOL> exceptions . EndpointNotFound , <EOL> client . Client , "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> auth_system = "<STR_LIT>" , auth_plugin = plugin ) <EOL> @ mock . patch . object ( pkg_resources , "<STR_LIT>" ) <EOL> def test_exception_if_no_url ( self , mock_iter_entry_points ) : <EOL> """<STR_LIT>""" <EOL> class MockEntrypoint ( pkg_resources . EntryPoint ) : <EOL> def load ( self ) : <EOL> return FakePlugin <EOL> class FakePlugin ( auth_plugin . BaseAuthPlugin ) : <EOL> pass <EOL> mock_iter_entry_points . side_effect = lambda _t : [ <EOL> MockEntrypoint ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] ) ] <EOL> auth_plugin . discover_auth_systems ( ) <EOL> plugin = auth_plugin . load_plugin ( "<STR_LIT>" ) <EOL> self . assertRaises ( <EOL> exceptions . EndpointNotFound , <EOL> client . Client , "<STR_LIT:username>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> auth_system = "<STR_LIT>" , auth_plugin = plugin ) </s>
