<s> import copy <EOL> import mock <EOL> from keystoneauth1 import exceptions as ks_exc <EOL> from openstackclient . common import exceptions <EOL> from openstackclient . identity . v2_0 import role <EOL> from openstackclient . tests import fakes <EOL> from openstackclient . tests . identity . v2_0 import fakes as identity_fakes <EOL> class TestRole ( identity_fakes . TestIdentityv2 ) : <EOL> def setUp ( self ) : <EOL> super ( TestRole , self ) . setUp ( ) <EOL> self . projects_mock = self . app . client_manager . identity . tenants <EOL> self . projects_mock . reset_mock ( ) <EOL> self . users_mock = self . app . client_manager . identity . users <EOL> self . users_mock . reset_mock ( ) <EOL> self . roles_mock = self . app . client_manager . identity . roles <EOL> self . roles_mock . reset_mock ( ) <EOL> class TestRoleAdd ( TestRole ) : <EOL> def setUp ( self ) : <EOL> super ( TestRoleAdd , self ) . setUp ( ) <EOL> self . projects_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . PROJECT ) , <EOL> loaded = True , <EOL> ) <EOL> self . users_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . USER ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . add_user_role . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . cmd = role . AddRole ( self . app , None ) <EOL> def test_role_add ( self ) : <EOL> arglist = [ <EOL> '<STR_LIT>' , identity_fakes . project_name , <EOL> '<STR_LIT>' , identity_fakes . user_name , <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . project_name ) , <EOL> ( '<STR_LIT:user>' , identity_fakes . user_name ) , <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . add_user_role . assert_called_with ( <EOL> identity_fakes . user_id , <EOL> identity_fakes . role_id , <EOL> identity_fakes . project_id , <EOL> ) <EOL> collist = ( '<STR_LIT:id>' , '<STR_LIT:name>' ) <EOL> self . assertEqual ( collist , columns ) <EOL> datalist = ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> ) <EOL> self . assertEqual ( datalist , data ) <EOL> class TestRoleCreate ( TestRole ) : <EOL> columns = ( <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT:name>' <EOL> ) <EOL> datalist = ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> ) <EOL> def setUp ( self ) : <EOL> super ( TestRoleCreate , self ) . setUp ( ) <EOL> self . roles_mock . create . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . cmd = role . CreateRole ( self . app , None ) <EOL> def test_role_create_no_options ( self ) : <EOL> arglist = [ <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . create . assert_called_with ( <EOL> identity_fakes . role_name , <EOL> ) <EOL> self . assertEqual ( self . columns , columns ) <EOL> self . assertEqual ( self . datalist , data ) <EOL> def test_role_create_or_show_exists ( self ) : <EOL> def _raise_conflict ( * args , ** kwargs ) : <EOL> raise ks_exc . Conflict ( None ) <EOL> self . roles_mock . create . side_effect = _raise_conflict <EOL> self . roles_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> arglist = [ <EOL> '<STR_LIT>' , <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ( '<STR_LIT>' , True ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . get . assert_called_with ( identity_fakes . role_name ) <EOL> self . roles_mock . create . assert_called_with ( <EOL> identity_fakes . role_name , <EOL> ) <EOL> self . assertEqual ( self . columns , columns ) <EOL> self . assertEqual ( self . datalist , data ) <EOL> def test_role_create_or_show_not_exists ( self ) : <EOL> arglist = [ <EOL> '<STR_LIT>' , <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ( '<STR_LIT>' , True ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . create . assert_called_with ( <EOL> identity_fakes . role_name , <EOL> ) <EOL> self . assertEqual ( self . columns , columns ) <EOL> self . assertEqual ( self . datalist , data ) <EOL> class TestRoleDelete ( TestRole ) : <EOL> def setUp ( self ) : <EOL> super ( TestRoleDelete , self ) . setUp ( ) <EOL> self . roles_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . delete . return_value = None <EOL> self . cmd = role . DeleteRole ( self . app , None ) <EOL> def test_role_delete_no_options ( self ) : <EOL> arglist = [ <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , [ identity_fakes . role_name ] ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> result = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . delete . assert_called_with ( <EOL> identity_fakes . role_id , <EOL> ) <EOL> self . assertIsNone ( result ) <EOL> class TestRoleList ( TestRole ) : <EOL> def setUp ( self ) : <EOL> super ( TestRoleList , self ) . setUp ( ) <EOL> self . roles_mock . list . return_value = [ <EOL> fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) , <EOL> ] <EOL> self . cmd = role . ListRole ( self . app , None ) <EOL> def test_role_list_no_options ( self ) : <EOL> arglist = [ ] <EOL> verifylist = [ ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . list . assert_called_with ( ) <EOL> collist = ( '<STR_LIT>' , '<STR_LIT:Name>' ) <EOL> self . assertEqual ( collist , columns ) <EOL> datalist = ( ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> ) , ) <EOL> self . assertEqual ( datalist , tuple ( data ) ) <EOL> class TestUserRoleList ( TestRole ) : <EOL> columns = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:Name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> def setUp ( self ) : <EOL> super ( TestUserRoleList , self ) . setUp ( ) <EOL> self . projects_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . PROJECT ) , <EOL> loaded = True , <EOL> ) <EOL> self . users_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . USER ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . roles_for_user . return_value = [ <EOL> fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) , <EOL> ] <EOL> self . cmd = role . ListUserRole ( self . app , None ) <EOL> def test_user_role_list_no_options ( self ) : <EOL> arglist = [ ] <EOL> verifylist = [ ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> self . assertRaises ( <EOL> exceptions . CommandError , <EOL> self . cmd . take_action , <EOL> parsed_args , <EOL> ) <EOL> def test_user_role_list_no_options_def_creds ( self ) : <EOL> auth_ref = self . app . client_manager . auth_ref = mock . MagicMock ( ) <EOL> auth_ref . project_id . return_value = identity_fakes . project_id <EOL> auth_ref . user_id . return_value = identity_fakes . user_id <EOL> arglist = [ ] <EOL> verifylist = [ ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . roles_for_user . assert_called_with ( <EOL> identity_fakes . user_id , <EOL> identity_fakes . project_id , <EOL> ) <EOL> collist = ( '<STR_LIT>' , '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( collist , columns ) <EOL> datalist = ( ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> identity_fakes . project_name , <EOL> identity_fakes . user_name , <EOL> ) , ) <EOL> self . assertEqual ( datalist , tuple ( data ) ) <EOL> def test_user_role_list_project ( self ) : <EOL> self . projects_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . PROJECT_2 ) , <EOL> loaded = True , <EOL> ) <EOL> arglist = [ <EOL> '<STR_LIT>' , identity_fakes . PROJECT_2 [ '<STR_LIT:name>' ] , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . PROJECT_2 [ '<STR_LIT:name>' ] ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> self . assertRaises ( <EOL> exceptions . CommandError , <EOL> self . cmd . take_action , <EOL> parsed_args , <EOL> ) <EOL> def test_user_role_list_project_def_creds ( self ) : <EOL> auth_ref = self . app . client_manager . auth_ref = mock . MagicMock ( ) <EOL> auth_ref . project_id . return_value = identity_fakes . project_id <EOL> auth_ref . user_id . return_value = identity_fakes . user_id <EOL> self . projects_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . PROJECT_2 ) , <EOL> loaded = True , <EOL> ) <EOL> arglist = [ <EOL> '<STR_LIT>' , identity_fakes . PROJECT_2 [ '<STR_LIT:name>' ] , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . PROJECT_2 [ '<STR_LIT:name>' ] ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . roles_for_user . assert_called_with ( <EOL> identity_fakes . user_id , <EOL> identity_fakes . PROJECT_2 [ '<STR_LIT:id>' ] , <EOL> ) <EOL> self . assertEqual ( columns , columns ) <EOL> datalist = ( ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> identity_fakes . PROJECT_2 [ '<STR_LIT:name>' ] , <EOL> identity_fakes . user_name , <EOL> ) , ) <EOL> self . assertEqual ( datalist , tuple ( data ) ) <EOL> class TestRoleRemove ( TestRole ) : <EOL> def setUp ( self ) : <EOL> super ( TestRoleRemove , self ) . setUp ( ) <EOL> self . projects_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . PROJECT ) , <EOL> loaded = True , <EOL> ) <EOL> self . users_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . USER ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . roles_mock . remove_user_role . return_value = None <EOL> self . cmd = role . RemoveRole ( self . app , None ) <EOL> def test_role_remove ( self ) : <EOL> arglist = [ <EOL> '<STR_LIT>' , identity_fakes . project_name , <EOL> '<STR_LIT>' , identity_fakes . user_name , <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ( '<STR_LIT>' , identity_fakes . project_name ) , <EOL> ( '<STR_LIT:user>' , identity_fakes . user_name ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> result = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . remove_user_role . assert_called_with ( <EOL> identity_fakes . user_id , <EOL> identity_fakes . role_id , <EOL> identity_fakes . project_id , <EOL> ) <EOL> self . assertIsNone ( result ) <EOL> class TestRoleShow ( TestRole ) : <EOL> def setUp ( self ) : <EOL> super ( TestRoleShow , self ) . setUp ( ) <EOL> self . roles_mock . get . return_value = fakes . FakeResource ( <EOL> None , <EOL> copy . deepcopy ( identity_fakes . ROLE ) , <EOL> loaded = True , <EOL> ) <EOL> self . cmd = role . ShowRole ( self . app , None ) <EOL> def test_service_show ( self ) : <EOL> arglist = [ <EOL> identity_fakes . role_name , <EOL> ] <EOL> verifylist = [ <EOL> ( '<STR_LIT>' , identity_fakes . role_name ) , <EOL> ] <EOL> parsed_args = self . check_parser ( self . cmd , arglist , verifylist ) <EOL> columns , data = self . cmd . take_action ( parsed_args ) <EOL> self . roles_mock . get . assert_called_with ( <EOL> identity_fakes . role_name , <EOL> ) <EOL> collist = ( '<STR_LIT:id>' , '<STR_LIT:name>' ) <EOL> self . assertEqual ( collist , columns ) <EOL> datalist = ( <EOL> identity_fakes . role_id , <EOL> identity_fakes . role_name , <EOL> ) <EOL> self . assertEqual ( datalist , data ) </s>
