<s> """<STR_LIT>""" <EOL> import random <EOL> import string <EOL> from shade import operator_cloud <EOL> from shade import OpenStackCloudException <EOL> from shade . tests . functional import base <EOL> class TestIdentity ( base . BaseFunctionalTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestIdentity , self ) . setUp ( ) <EOL> self . cloud = operator_cloud ( cloud = '<STR_LIT>' ) <EOL> self . role_prefix = '<STR_LIT>' + '<STR_LIT>' . join ( <EOL> random . choice ( string . ascii_lowercase ) for _ in range ( <NUM_LIT:5> ) ) <EOL> self . user_prefix = self . getUniqueString ( '<STR_LIT:user>' ) <EOL> self . group_prefix = self . getUniqueString ( '<STR_LIT>' ) <EOL> self . addCleanup ( self . _cleanup_users ) <EOL> self . identity_version = self . cloud . cloud_config . get_api_version ( '<STR_LIT>' ) <EOL> if self . identity_version not in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> self . addCleanup ( self . _cleanup_groups ) <EOL> self . addCleanup ( self . _cleanup_roles ) <EOL> def _cleanup_groups ( self ) : <EOL> exception_list = list ( ) <EOL> for group in self . cloud . list_groups ( ) : <EOL> if group [ '<STR_LIT:name>' ] . startswith ( self . group_prefix ) : <EOL> try : <EOL> self . cloud . delete_group ( group [ '<STR_LIT:id>' ] ) <EOL> except Exception as e : <EOL> exception_list . append ( str ( e ) ) <EOL> continue <EOL> if exception_list : <EOL> raise OpenStackCloudException ( '<STR_LIT:\n>' . join ( exception_list ) ) <EOL> def _cleanup_users ( self ) : <EOL> exception_list = list ( ) <EOL> for user in self . cloud . list_users ( ) : <EOL> if user [ '<STR_LIT:name>' ] . startswith ( self . user_prefix ) : <EOL> try : <EOL> self . cloud . delete_user ( user [ '<STR_LIT:id>' ] ) <EOL> except Exception as e : <EOL> exception_list . append ( str ( e ) ) <EOL> continue <EOL> if exception_list : <EOL> raise OpenStackCloudException ( '<STR_LIT:\n>' . join ( exception_list ) ) <EOL> def _cleanup_roles ( self ) : <EOL> exception_list = list ( ) <EOL> for role in self . cloud . list_roles ( ) : <EOL> if role [ '<STR_LIT:name>' ] . startswith ( self . role_prefix ) : <EOL> try : <EOL> self . cloud . delete_role ( role [ '<STR_LIT:name>' ] ) <EOL> except Exception as e : <EOL> exception_list . append ( str ( e ) ) <EOL> continue <EOL> if exception_list : <EOL> raise OpenStackCloudException ( '<STR_LIT:\n>' . join ( exception_list ) ) <EOL> def _create_user ( self , ** kwargs ) : <EOL> domain_id = None <EOL> if self . identity_version not in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> domain = self . cloud . get_domain ( '<STR_LIT:default>' ) <EOL> domain_id = domain [ '<STR_LIT:id>' ] <EOL> return self . cloud . create_user ( domain_id = domain_id , ** kwargs ) <EOL> def test_list_roles ( self ) : <EOL> roles = self . cloud . list_roles ( ) <EOL> self . assertIsNotNone ( roles ) <EOL> self . assertNotEqual ( [ ] , roles ) <EOL> def test_get_role ( self ) : <EOL> role = self . cloud . get_role ( '<STR_LIT>' ) <EOL> self . assertIsNotNone ( role ) <EOL> self . assertIn ( '<STR_LIT:id>' , role ) <EOL> self . assertIn ( '<STR_LIT:name>' , role ) <EOL> self . assertEqual ( '<STR_LIT>' , role [ '<STR_LIT:name>' ] ) <EOL> def test_search_roles ( self ) : <EOL> roles = self . cloud . search_roles ( filters = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assertIsNotNone ( roles ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( roles ) ) <EOL> self . assertEqual ( '<STR_LIT>' , roles [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> def test_create_role ( self ) : <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> self . assertIsNotNone ( role ) <EOL> self . assertIn ( '<STR_LIT:id>' , role ) <EOL> self . assertIn ( '<STR_LIT:name>' , role ) <EOL> self . assertEqual ( role_name , role [ '<STR_LIT:name>' ] ) <EOL> def test_delete_role ( self ) : <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> self . assertIsNotNone ( role ) <EOL> self . assertTrue ( self . cloud . delete_role ( role_name ) ) <EOL> def test_list_role_assignments ( self ) : <EOL> if self . identity_version in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> assignments = self . cloud . list_role_assignments ( ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) > <NUM_LIT:0> ) <EOL> def test_list_role_assignments_v2 ( self ) : <EOL> user = self . cloud . get_user ( '<STR_LIT>' ) <EOL> project = self . cloud . get_project ( '<STR_LIT>' ) <EOL> assignments = self . cloud . list_role_assignments ( <EOL> filters = { '<STR_LIT:user>' : user [ '<STR_LIT:id>' ] , '<STR_LIT>' : project [ '<STR_LIT:id>' ] } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) > <NUM_LIT:0> ) <EOL> def test_grant_revoke_role_user_project ( self ) : <EOL> user_name = self . user_prefix + '<STR_LIT>' <EOL> user_email = '<STR_LIT>' <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> user = self . _create_user ( name = user_name , <EOL> email = user_email , <EOL> default_project = '<STR_LIT>' ) <EOL> self . assertTrue ( self . cloud . grant_role ( <EOL> role_name , user = user [ '<STR_LIT:id>' ] , project = '<STR_LIT>' , wait = True ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : user [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_project ( '<STR_LIT>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:1> ) <EOL> self . assertTrue ( self . cloud . revoke_role ( <EOL> role_name , user = user [ '<STR_LIT:id>' ] , project = '<STR_LIT>' , wait = True ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : user [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_project ( '<STR_LIT>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:0> ) <EOL> def test_grant_revoke_role_group_project ( self ) : <EOL> if self . identity_version in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> group_name = self . group_prefix + '<STR_LIT>' <EOL> group = self . cloud . create_group ( name = group_name , <EOL> description = '<STR_LIT>' , <EOL> domain = '<STR_LIT:default>' ) <EOL> self . assertTrue ( self . cloud . grant_role ( <EOL> role_name , group = group [ '<STR_LIT:id>' ] , project = '<STR_LIT>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : group [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_project ( '<STR_LIT>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:1> ) <EOL> self . assertTrue ( self . cloud . revoke_role ( <EOL> role_name , group = group [ '<STR_LIT:id>' ] , project = '<STR_LIT>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : group [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_project ( '<STR_LIT>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:0> ) <EOL> def test_grant_revoke_role_user_domain ( self ) : <EOL> if self . identity_version in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> user_name = self . user_prefix + '<STR_LIT>' <EOL> user_email = '<STR_LIT>' <EOL> user = self . _create_user ( name = user_name , <EOL> email = user_email , <EOL> default_project = '<STR_LIT>' ) <EOL> self . assertTrue ( self . cloud . grant_role ( <EOL> role_name , user = user [ '<STR_LIT:id>' ] , domain = '<STR_LIT:default>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : user [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_domain ( '<STR_LIT:default>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:1> ) <EOL> self . assertTrue ( self . cloud . revoke_role ( <EOL> role_name , user = user [ '<STR_LIT:id>' ] , domain = '<STR_LIT:default>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : user [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_domain ( '<STR_LIT:default>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:0> ) <EOL> def test_grant_revoke_role_group_domain ( self ) : <EOL> if self . identity_version in ( '<STR_LIT:2>' , '<STR_LIT>' ) : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> role_name = self . role_prefix + '<STR_LIT>' <EOL> role = self . cloud . create_role ( role_name ) <EOL> group_name = self . group_prefix + '<STR_LIT>' <EOL> group = self . cloud . create_group ( name = group_name , <EOL> description = '<STR_LIT>' , <EOL> domain = '<STR_LIT:default>' ) <EOL> self . assertTrue ( self . cloud . grant_role ( <EOL> role_name , group = group [ '<STR_LIT:id>' ] , domain = '<STR_LIT:default>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : group [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_domain ( '<STR_LIT:default>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:1> ) <EOL> self . assertTrue ( self . cloud . revoke_role ( <EOL> role_name , group = group [ '<STR_LIT:id>' ] , domain = '<STR_LIT:default>' ) ) <EOL> assignments = self . cloud . list_role_assignments ( { <EOL> '<STR_LIT>' : role [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : group [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : self . cloud . get_domain ( '<STR_LIT:default>' ) [ '<STR_LIT:id>' ] <EOL> } ) <EOL> self . assertIsInstance ( assignments , list ) <EOL> self . assertTrue ( len ( assignments ) == <NUM_LIT:0> ) </s>
