<s> import mock <EOL> from oslo_serialization import jsonutils <EOL> import webob <EOL> from oslo_policy import policy as oslo_policy <EOL> from nova . api . openstack . compute import keypairs as keypairs_v21 <EOL> from nova . api . openstack . compute . legacy_v2 . contrib import keypairs as keypairs_v2 <EOL> from nova . api . openstack import wsgi as os_wsgi <EOL> from nova . compute import api as compute_api <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova import policy <EOL> from nova import quota <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> from nova . tests . unit . objects import test_keypair <EOL> from nova . tests import uuidsentinel as uuids <EOL> QUOTAS = quota . QUOTAS <EOL> keypair_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> FAKE_UUID = '<STR_LIT>' <EOL> def fake_keypair ( name ) : <EOL> return dict ( test_keypair . fake_keypair , <EOL> name = name , ** keypair_data ) <EOL> def db_key_pair_get_all_by_user ( self , user_id ) : <EOL> return [ fake_keypair ( '<STR_LIT>' ) ] <EOL> def db_key_pair_create ( self , keypair ) : <EOL> return fake_keypair ( name = keypair [ '<STR_LIT:name>' ] ) <EOL> def db_key_pair_destroy ( context , user_id , name ) : <EOL> if not ( user_id and name ) : <EOL> raise Exception ( ) <EOL> def db_key_pair_create_duplicate ( context , keypair ) : <EOL> raise exception . KeyPairExists ( key_name = keypair . get ( '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> class KeypairsTestV21 ( test . TestCase ) : <EOL> base_url = '<STR_LIT>' <EOL> validation_error = exception . ValidationError <EOL> wsgi_api_version = os_wsgi . DEFAULT_API_VERSION <EOL> def _setup_app_and_controller ( self ) : <EOL> self . app_server = fakes . wsgi_app_v21 ( init_only = ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> self . controller = keypairs_v21 . KeypairController ( ) <EOL> def setUp ( self ) : <EOL> super ( KeypairsTestV21 , self ) . setUp ( ) <EOL> fakes . stub_out_networking ( self ) <EOL> fakes . stub_out_rate_limiting ( self . stubs ) <EOL> self . stub_out ( "<STR_LIT>" , <EOL> db_key_pair_get_all_by_user ) <EOL> self . stub_out ( "<STR_LIT>" , <EOL> db_key_pair_create ) <EOL> self . stub_out ( "<STR_LIT>" , <EOL> db_key_pair_destroy ) <EOL> self . flags ( <EOL> osapi_compute_extension = [ <EOL> '<STR_LIT>' ] , <EOL> osapi_compute_ext_list = [ '<STR_LIT>' ] ) <EOL> self . _setup_app_and_controller ( ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' , version = self . wsgi_api_version ) <EOL> def test_keypair_list ( self ) : <EOL> res_dict = self . controller . index ( self . req ) <EOL> response = { '<STR_LIT>' : [ { '<STR_LIT>' : dict ( keypair_data , name = '<STR_LIT>' ) } ] } <EOL> self . assertEqual ( res_dict , response ) <EOL> def test_keypair_create ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> res_dict = self . controller . create ( self . req , body = body ) <EOL> self . assertTrue ( len ( res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) > <NUM_LIT:0> ) <EOL> self . assertTrue ( len ( res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) > <NUM_LIT:0> ) <EOL> self . _assert_keypair_type ( res_dict ) <EOL> def _test_keypair_create_bad_request_case ( self , <EOL> body , <EOL> exception ) : <EOL> self . assertRaises ( exception , <EOL> self . controller . create , self . req , body = body ) <EOL> def test_keypair_create_with_empty_name ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> self . validation_error ) <EOL> def test_keypair_create_with_name_too_long ( self ) : <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT:a>' * <NUM_LIT> <EOL> } <EOL> } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> self . validation_error ) <EOL> def test_keypair_create_with_name_leading_trailing_spaces ( self ) : <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> self . validation_error ) <EOL> def test_keypair_create_with_name_leading_trailing_spaces_compat_mode ( <EOL> self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> self . req . set_legacy_v2 ( ) <EOL> res_dict = self . controller . create ( self . req , body = body ) <EOL> self . assertEqual ( '<STR_LIT:test>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> def test_keypair_create_with_non_alphanumeric_name ( self ) : <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> webob . exc . HTTPBadRequest ) <EOL> def test_keypair_import_bad_key ( self ) : <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> webob . exc . HTTPBadRequest ) <EOL> def test_keypair_create_with_invalid_keypair_body ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> self . _test_keypair_create_bad_request_case ( body , <EOL> self . validation_error ) <EOL> def test_keypair_import ( self ) : <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } <EOL> res_dict = self . controller . create ( self . req , body = body ) <EOL> self . assertTrue ( len ( res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) > <NUM_LIT:0> ) <EOL> self . assertNotIn ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] ) <EOL> self . _assert_keypair_type ( res_dict ) <EOL> def test_keypair_import_quota_limit ( self ) : <EOL> def fake_quotas_count ( self , context , resource , * args , ** kwargs ) : <EOL> return <NUM_LIT:100> <EOL> self . stubs . Set ( QUOTAS , "<STR_LIT:count>" , fake_quotas_count ) <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } <EOL> ex = self . assertRaises ( webob . exc . HTTPForbidden , <EOL> self . controller . create , self . req , body = body ) <EOL> self . assertIn ( '<STR_LIT>' , ex . explanation ) <EOL> def test_keypair_create_quota_limit ( self ) : <EOL> def fake_quotas_count ( self , context , resource , * args , ** kwargs ) : <EOL> return <NUM_LIT:100> <EOL> self . stubs . Set ( QUOTAS , "<STR_LIT:count>" , fake_quotas_count ) <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> ex = self . assertRaises ( webob . exc . HTTPForbidden , <EOL> self . controller . create , self . req , body = body ) <EOL> self . assertIn ( '<STR_LIT>' , ex . explanation ) <EOL> def test_keypair_create_duplicate ( self ) : <EOL> self . stub_out ( "<STR_LIT>" , db_key_pair_create_duplicate ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> ex = self . assertRaises ( webob . exc . HTTPConflict , <EOL> self . controller . create , self . req , body = body ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> ex . explanation ) <EOL> def test_keypair_delete ( self ) : <EOL> self . controller . delete ( self . req , '<STR_LIT>' ) <EOL> def test_keypair_get_keypair_not_found ( self ) : <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . show , self . req , '<STR_LIT>' ) <EOL> def test_keypair_delete_not_found ( self ) : <EOL> def db_key_pair_get_not_found ( context , user_id , name ) : <EOL> raise exception . KeypairNotFound ( user_id = user_id , name = name ) <EOL> self . stub_out ( "<STR_LIT>" , <EOL> db_key_pair_get_not_found ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . delete , self . req , '<STR_LIT>' ) <EOL> def test_keypair_show ( self ) : <EOL> def _db_key_pair_get ( context , user_id , name ) : <EOL> return dict ( test_keypair . fake_keypair , <EOL> name = '<STR_LIT:foo>' , public_key = '<STR_LIT>' , fingerprint = '<STR_LIT>' , <EOL> type = '<STR_LIT>' ) <EOL> self . stub_out ( "<STR_LIT>" , _db_key_pair_get ) <EOL> res_dict = self . controller . show ( self . req , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . _assert_keypair_type ( res_dict ) <EOL> def test_keypair_show_not_found ( self ) : <EOL> def _db_key_pair_get ( context , user_id , name ) : <EOL> raise exception . KeypairNotFound ( user_id = user_id , name = name ) <EOL> self . stub_out ( "<STR_LIT>" , _db_key_pair_get ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . show , self . req , '<STR_LIT>' ) <EOL> def test_show_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fakes . fake_instance_get ( ) ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fakes . fake_instance_get ( ) ) <EOL> req = webob . Request . blank ( self . base_url + '<STR_LIT>' + uuids . server ) <EOL> req . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> response = req . get_response ( self . app_server ) <EOL> self . assertEqual ( response . status_int , <NUM_LIT:200> ) <EOL> res_dict = jsonutils . loads ( response . body ) <EOL> self . assertIn ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( res_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_detail_servers ( self , mock_get_all ) : <EOL> mock_get_all . return_value = objects . InstanceList ( <EOL> objects = [ fakes . stub_instance_obj ( ctxt = None , id = <NUM_LIT:1> ) , <EOL> fakes . stub_instance_obj ( ctxt = None , id = <NUM_LIT:2> ) ] ) <EOL> req = fakes . HTTPRequest . blank ( self . base_url + '<STR_LIT>' ) <EOL> res = req . get_response ( self . app_server ) <EOL> server_dicts = jsonutils . loads ( res . body ) [ '<STR_LIT>' ] <EOL> self . assertEqual ( len ( server_dicts ) , <NUM_LIT:2> ) <EOL> for server_dict in server_dicts : <EOL> self . assertIn ( '<STR_LIT>' , server_dict ) <EOL> self . assertEqual ( server_dict [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def _assert_keypair_type ( self , res_dict ) : <EOL> self . assertNotIn ( '<STR_LIT:type>' , res_dict [ '<STR_LIT>' ] ) <EOL> def test_create_server_keypair_name_with_leading_trailing ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> req . body = jsonutils . dump_as_bytes ( { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : FAKE_UUID } } ) <EOL> res = req . get_response ( self . app_server ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_code ) <EOL> self . assertIn ( '<STR_LIT>' , res . body ) <EOL> @ mock . patch . object ( compute_api . API , '<STR_LIT>' ) <EOL> def test_create_server_keypair_name_with_leading_trailing_compat_mode ( <EOL> self , mock_create ) : <EOL> mock_create . return_value = ( <EOL> objects . InstanceList ( objects = [ <EOL> fakes . stub_instance_obj ( ctxt = None , id = <NUM_LIT:1> ) ] ) , <EOL> None ) <EOL> req = fakes . HTTPRequest . blank ( self . base_url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> req . body = jsonutils . dump_as_bytes ( { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : FAKE_UUID } } ) <EOL> req . set_legacy_v2 ( ) <EOL> res = req . get_response ( self . app_server ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_code ) <EOL> class KeypairPolicyTestV21 ( test . NoDBTestCase ) : <EOL> KeyPairController = keypairs_v21 . KeypairController ( ) <EOL> policy_path = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( KeypairPolicyTestV21 , self ) . setUp ( ) <EOL> def _db_key_pair_get ( context , user_id , name ) : <EOL> return dict ( test_keypair . fake_keypair , <EOL> name = '<STR_LIT:foo>' , public_key = '<STR_LIT>' , fingerprint = '<STR_LIT>' , <EOL> type = '<STR_LIT>' ) <EOL> self . stub_out ( "<STR_LIT>" , _db_key_pair_get ) <EOL> self . stub_out ( "<STR_LIT>" , <EOL> db_key_pair_get_all_by_user ) <EOL> self . stub_out ( "<STR_LIT>" , db_key_pair_destroy ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_keypair_list_fail_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> self . assertRaises ( exception . Forbidden , <EOL> self . KeyPairController . index , <EOL> self . req ) <EOL> def test_keypair_list_pass_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> res = self . KeyPairController . index ( self . req ) <EOL> self . assertIn ( '<STR_LIT>' , res ) <EOL> def test_keypair_show_fail_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> self . assertRaises ( exception . Forbidden , <EOL> self . KeyPairController . show , <EOL> self . req , '<STR_LIT>' ) <EOL> def test_keypair_show_pass_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> res = self . KeyPairController . show ( self . req , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , res ) <EOL> def test_keypair_create_fail_policy ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> self . assertRaises ( exception . Forbidden , <EOL> self . KeyPairController . create , <EOL> self . req , body = body ) <EOL> def _assert_keypair_create ( self , mock_create , req ) : <EOL> mock_create . assert_called_with ( req , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ mock . patch . object ( compute_api . KeypairAPI , '<STR_LIT>' ) <EOL> def test_keypair_create_pass_policy ( self , mock_create ) : <EOL> keypair_obj = objects . KeyPair ( name = '<STR_LIT>' , public_key = '<STR_LIT>' , <EOL> fingerprint = '<STR_LIT>' , user_id = '<STR_LIT>' ) <EOL> mock_create . return_value = ( keypair_obj , '<STR_LIT>' ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> res = self . KeyPairController . create ( self . req , body = body ) <EOL> self . assertIn ( '<STR_LIT>' , res ) <EOL> req = self . req . environ [ '<STR_LIT>' ] <EOL> self . _assert_keypair_create ( mock_create , req ) <EOL> def test_keypair_delete_fail_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> self . assertRaises ( exception . Forbidden , <EOL> self . KeyPairController . delete , <EOL> self . req , '<STR_LIT>' ) <EOL> def test_keypair_delete_pass_policy ( self ) : <EOL> rules = { self . policy_path + '<STR_LIT>' : '<STR_LIT>' } <EOL> policy . set_rules ( oslo_policy . Rules . from_dict ( rules ) ) <EOL> self . KeyPairController . delete ( self . req , '<STR_LIT>' ) <EOL> class KeypairsTestV2 ( KeypairsTestV21 ) : <EOL> validation_error = webob . exc . HTTPBadRequest <EOL> def _setup_app_and_controller ( self ) : <EOL> self . app_server = fakes . wsgi_app ( init_only = ( '<STR_LIT>' , ) ) <EOL> self . controller = keypairs_v2 . KeypairController ( ) <EOL> def test_keypair_create_with_name_leading_trailing_spaces ( <EOL> self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> self . req . set_legacy_v2 ( ) <EOL> res_dict = self . controller . create ( self . req , body = body ) <EOL> self . assertEqual ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> def test_keypair_create_with_name_leading_trailing_spaces_compat_mode ( <EOL> self ) : <EOL> pass <EOL> def test_create_server_keypair_name_with_leading_trailing ( self ) : <EOL> pass <EOL> @ mock . patch . object ( compute_api . API , '<STR_LIT>' ) <EOL> def test_create_server_keypair_name_with_leading_trailing_compat_mode ( <EOL> self , mock_create ) : <EOL> mock_create . return_value = ( <EOL> objects . InstanceList ( objects = [ <EOL> fakes . stub_instance_obj ( ctxt = None , id = <NUM_LIT:1> ) ] ) , <EOL> None ) <EOL> req = fakes . HTTPRequest . blank ( self . base_url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> req . body = jsonutils . dump_as_bytes ( { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : FAKE_UUID } } ) <EOL> res = req . get_response ( self . app_server ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_code ) <EOL> class KeypairsTestV22 ( KeypairsTestV21 ) : <EOL> wsgi_api_version = '<STR_LIT>' <EOL> def test_keypair_list ( self ) : <EOL> res_dict = self . controller . index ( self . req ) <EOL> expected = { '<STR_LIT>' : [ { '<STR_LIT>' : dict ( keypair_data , name = '<STR_LIT>' , <EOL> type = '<STR_LIT>' ) } ] } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def _assert_keypair_type ( self , res_dict ) : <EOL> self . assertEqual ( '<STR_LIT>' , res_dict [ '<STR_LIT>' ] [ '<STR_LIT:type>' ] ) <EOL> def test_keypair_create_with_name_leading_trailing_spaces_compat_mode ( <EOL> self ) : <EOL> pass <EOL> def test_create_server_keypair_name_with_leading_trailing_compat_mode ( <EOL> self ) : <EOL> pass <EOL> class KeypairsTestV210 ( KeypairsTestV22 ) : <EOL> wsgi_api_version = '<STR_LIT>' <EOL> def test_keypair_create_with_name_leading_trailing_spaces_compat_mode ( <EOL> self ) : <EOL> pass <EOL> def test_create_server_keypair_name_with_leading_trailing_compat_mode ( <EOL> self ) : <EOL> pass <EOL> def test_keypair_list_other_user ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version , <EOL> use_admin_context = True ) <EOL> with mock . patch . object ( self . controller . api , '<STR_LIT>' ) as mock_g : <EOL> self . controller . index ( req ) <EOL> userid = mock_g . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( '<STR_LIT:foo>' , userid ) <EOL> def test_keypair_list_other_user_not_admin ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version ) <EOL> with mock . patch . object ( self . controller . api , '<STR_LIT>' ) : <EOL> self . assertRaises ( exception . PolicyNotAuthorized , <EOL> self . controller . index , req ) <EOL> def test_keypair_show_other_user ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version , <EOL> use_admin_context = True ) <EOL> with mock . patch . object ( self . controller . api , '<STR_LIT>' ) as mock_g : <EOL> self . controller . show ( req , '<STR_LIT>' ) <EOL> userid = mock_g . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( '<STR_LIT:foo>' , userid ) <EOL> def test_keypair_show_other_user_not_admin ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version ) <EOL> with mock . patch . object ( self . controller . api , '<STR_LIT>' ) : <EOL> self . assertRaises ( exception . PolicyNotAuthorized , <EOL> self . controller . show , req , '<STR_LIT>' ) <EOL> def test_keypair_delete_other_user ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version , <EOL> use_admin_context = True ) <EOL> with mock . patch . object ( self . controller . api , <EOL> '<STR_LIT>' ) as mock_g : <EOL> self . controller . delete ( req , '<STR_LIT>' ) <EOL> userid = mock_g . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( '<STR_LIT:foo>' , userid ) <EOL> def test_keypair_delete_other_user_not_admin ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version ) <EOL> with mock . patch . object ( self . controller . api , '<STR_LIT>' ) : <EOL> self . assertRaises ( exception . PolicyNotAuthorized , <EOL> self . controller . delete , req , '<STR_LIT>' ) <EOL> def test_keypair_create_other_user ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version , <EOL> use_admin_context = True ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> with mock . patch . object ( self . controller . api , <EOL> '<STR_LIT>' , <EOL> return_value = ( mock . MagicMock ( ) , <NUM_LIT:1> ) ) as mock_g : <EOL> res = self . controller . create ( req , body = body ) <EOL> userid = mock_g . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( '<STR_LIT>' , userid ) <EOL> self . assertIn ( '<STR_LIT>' , res ) <EOL> def test_keypair_import_other_user ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version , <EOL> use_admin_context = True ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> with mock . patch . object ( self . controller . api , <EOL> '<STR_LIT>' ) as mock_g : <EOL> res = self . controller . create ( req , body = body ) <EOL> userid = mock_g . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( '<STR_LIT>' , userid ) <EOL> self . assertIn ( '<STR_LIT>' , res ) <EOL> def test_keypair_create_other_user_not_admin ( self ) : <EOL> req = fakes . HTTPRequest . blank ( self . base_url + <EOL> '<STR_LIT>' , <EOL> version = self . wsgi_api_version ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertRaises ( exception . PolicyNotAuthorized , <EOL> self . controller . create , <EOL> req , body = body ) <EOL> class KeypairPolicyTestV2 ( KeypairPolicyTestV21 ) : <EOL> KeyPairController = keypairs_v2 . KeypairController ( ) <EOL> policy_path = '<STR_LIT>' <EOL> def _assert_keypair_create ( self , mock_create , req ) : <EOL> mock_create . assert_called_with ( req , '<STR_LIT>' , '<STR_LIT>' ) </s>
