Best Python code snippet using tempest_python
test_roles.py
Source:test_roles.py  
...22    def setUpClass(cls):23        for _ in xrange(5):24            resp, role = cls.client.create_role(rand_name('role-'))25            cls.data.roles.append(role)26    def _get_role_params(self):27        self.data.setup_test_user()28        self.data.setup_test_role()29        user = self.get_user_by_name(self.data.test_user)30        tenant = self.get_tenant_by_name(self.data.test_tenant)31        role = self.get_role_by_name(self.data.test_role)32        return (user, tenant, role)33    def test_list_roles(self):34        """Return a list of all roles"""35        resp, body = self.client.list_roles()36        found = [role for role in body if role in self.data.roles]37        self.assertTrue(any(found))38        self.assertEqual(len(found), len(self.data.roles))39    def test_list_roles_by_unauthorized_user(self):40        """Non admin user should not be able to list roles"""41        self.assertRaises(exceptions.Unauthorized,42                          self.non_admin_client.list_roles)43    def test_list_roles_request_without_token(self):44        """Request to list roles without a valid token should fail"""45        token = self.client.get_auth()46        self.client.delete_token(token)47        self.assertRaises(exceptions.Unauthorized, self.client.list_roles)48        self.client.clear_auth()49    def test_role_create_delete(self):50        """Role should be created, verified, and deleted"""51        role_name = rand_name('role-test-')52        resp, body = self.client.create_role(role_name)53        self.assertTrue('status' in resp)54        self.assertTrue(resp['status'].startswith('2'))55        self.assertEqual(role_name, body['name'])56        resp, body = self.client.list_roles()57        found = [role for role in body if role['name'] == role_name]58        self.assertTrue(any(found))59        resp, body = self.client.delete_role(found[0]['id'])60        self.assertTrue('status' in resp)61        self.assertTrue(resp['status'].startswith('2'))62        resp, body = self.client.list_roles()63        found = [role for role in body if role['name'] == role_name]64        self.assertFalse(any(found))65    def test_role_create_blank_name(self):66        """Should not be able to create a role with a blank name"""67        self.assertRaises(exceptions.BadRequest, self.client.create_role, '')68    def test_role_create_duplicate(self):69        """Role names should be unique"""70        role_name = rand_name('role-dup-')71        resp, body = self.client.create_role(role_name)72        role1_id = body.get('id')73        self.assertTrue('status' in resp)74        self.assertTrue(resp['status'].startswith('2'))75        try:76            resp, body = self.client.create_role(role_name)77            # this should raise an exception78            self.fail('Should not be able to create a duplicate role name.'79                      ' %s' % role_name)80        except exceptions.Duplicate:81            pass82        self.client.delete_role(role1_id)83class RolesTestJSON(base.BaseIdentityAdminTestJSON,84                    RolesTestBase):85    @classmethod86    def setUpClass(cls):87        super(RolesTestJSON, cls).setUpClass()88        RolesTestBase.setUpClass(cls)89class RolesTestXML(base.BaseIdentityAdminTestXML,90                   RolesTestBase):91    @classmethod92    def setUpClass(cls):93        super(RolesTestXML, cls).setUpClass()94        RolesTestBase.setUpClass(cls)95class UserRolesTestBase(RolesTestBase):96    def test_assign_user_role(self):97        """Assign a role to a user on a tenant"""98        (user, tenant, role) = self._get_role_params()99        self.client.assign_user_role(tenant['id'], user['id'], role['id'])100        resp, roles = self.client.list_user_roles(tenant['id'], user['id'])101        self.assertEquals(1, len(roles))102        self.assertEquals(roles[0]['id'], role['id'])103    def test_assign_user_role_by_unauthorized_user(self):104        """Non admin user should not be authorized to assign a role to user"""105        (user, tenant, role) = self._get_role_params()106        self.assertRaises(exceptions.Unauthorized,107                          self.non_admin_client.assign_user_role,108                          tenant['id'], user['id'], role['id'])109    def test_assign_user_role_request_without_token(self):110        """Request to assign a role to a user without a valid token"""111        (user, tenant, role) = self._get_role_params()112        token = self.client.get_auth()113        self.client.delete_token(token)114        self.assertRaises(exceptions.Unauthorized,115                          self.client.assign_user_role, tenant['id'],116                          user['id'], role['id'])117        self.client.clear_auth()118    def test_assign_user_role_for_non_existent_user(self):119        """Attempt to assign a role to a non existent user should fail"""120        (user, tenant, role) = self._get_role_params()121        self.assertRaises(exceptions.NotFound, self.client.assign_user_role,122                          tenant['id'], 'junk-user-id-999', role['id'])123    def test_assign_user_role_for_non_existent_role(self):124        """Attempt to assign a non existent role to user should fail"""125        (user, tenant, role) = self._get_role_params()126        self.assertRaises(exceptions.NotFound, self.client.assign_user_role,127                          tenant['id'], user['id'], 'junk-role-id-12345')128    def test_assign_user_role_for_non_existent_tenant(self):129        """Attempt to assign a role on a non existent tenant should fail"""130        (user, tenant, role) = self._get_role_params()131        self.assertRaises(exceptions.NotFound, self.client.assign_user_role,132                          'junk-tenant-1234', user['id'], role['id'])133    def test_assign_duplicate_user_role(self):134        """Duplicate user role should not get assigned"""135        (user, tenant, role) = self._get_role_params()136        self.client.assign_user_role(tenant['id'], user['id'], role['id'])137        self.assertRaises(exceptions.Duplicate, self.client.assign_user_role,138                          tenant['id'], user['id'], role['id'])139    def test_remove_user_role(self):140        """Remove a role assigned to a user on a tenant"""141        (user, tenant, role) = self._get_role_params()142        resp, user_role = self.client.assign_user_role(tenant['id'],143                                                       user['id'], role['id'])144        resp, body = self.client.remove_user_role(tenant['id'], user['id'],145                                                  user_role['id'])146        self.assertEquals(resp['status'], '204')147    def test_remove_user_role_by_unauthorized_user(self):148        """Non admin user should not be authorized to remove a user's role"""149        (user, tenant, role) = self._get_role_params()150        resp, user_role = self.client.assign_user_role(tenant['id'],151                                                       user['id'],152                                                       role['id'])153        self.assertRaises(exceptions.Unauthorized,154                          self.non_admin_client.remove_user_role,155                          tenant['id'], user['id'], role['id'])156    def test_remove_user_role_request_without_token(self):157        """Request to remove a user's role without a valid token"""158        (user, tenant, role) = self._get_role_params()159        resp, user_role = self.client.assign_user_role(tenant['id'],160                                                       user['id'],161                                                       role['id'])162        token = self.client.get_auth()163        self.client.delete_token(token)164        self.assertRaises(exceptions.Unauthorized,165                          self.client.remove_user_role, tenant['id'],166                          user['id'], role['id'])167        self.client.clear_auth()168    def test_remove_user_role_non_existant_user(self):169        """Attempt to remove a role from a non existent user should fail"""170        (user, tenant, role) = self._get_role_params()171        resp, user_role = self.client.assign_user_role(tenant['id'],172                                                       user['id'],173                                                       role['id'])174        self.assertRaises(exceptions.NotFound, self.client.remove_user_role,175                          tenant['id'], 'junk-user-id-123', role['id'])176    def test_remove_user_role_non_existant_role(self):177        """Attempt to delete a non existent role from a user should fail"""178        (user, tenant, role) = self._get_role_params()179        resp, user_role = self.client.assign_user_role(tenant['id'],180                                                       user['id'],181                                                       role['id'])182        self.assertRaises(exceptions.NotFound, self.client.remove_user_role,183                          tenant['id'], user['id'], 'junk-user-role-123')184    def test_remove_user_role_non_existant_tenant(self):185        """Attempt to remove a role from a non existent tenant should fail"""186        (user, tenant, role) = self._get_role_params()187        resp, user_role = self.client.assign_user_role(tenant['id'],188                                                       user['id'],189                                                       role['id'])190        self.assertRaises(exceptions.NotFound, self.client.remove_user_role,191                          'junk-tenant-id-123', user['id'], role['id'])192    def test_list_user_roles(self):193        """List roles assigned to a user on tenant"""194        (user, tenant, role) = self._get_role_params()195        self.client.assign_user_role(tenant['id'], user['id'], role['id'])196        resp, roles = self.client.list_user_roles(tenant['id'], user['id'])197        self.assertEquals(1, len(roles))198        self.assertEquals(role['id'], roles[0]['id'])199    def test_list_user_roles_by_unauthorized_user(self):200        """Non admin user should not be authorized to list a user's roles"""201        (user, tenant, role) = self._get_role_params()202        self.client.assign_user_role(tenant['id'], user['id'], role['id'])203        self.assertRaises(exceptions.Unauthorized,204                          self.non_admin_client.list_user_roles, tenant['id'],205                          user['id'])206    def test_list_user_roles_request_without_token(self):207        """Request to list user's roles without a valid token should fail"""208        (user, tenant, role) = self._get_role_params()209        token = self.client.get_auth()210        self.client.delete_token(token)211        try:212            self.assertRaises(exceptions.Unauthorized,213                              self.client.list_user_roles, tenant['id'],214                              user['id'])215        finally:216            self.client.clear_auth()217    def test_list_user_roles_for_non_existent_user(self):218        """Attempt to list roles of a non existent user should fail"""219        (user, tenant, role) = self._get_role_params()220        self.assertRaises(exceptions.NotFound, self.client.list_user_roles,221                          tenant['id'], 'junk-role-aabbcc11')222class UserRolesTestJSON(RolesTestJSON,223                        UserRolesTestBase):224    @classmethod225    def setUpClass(cls):226        super(UserRolesTestJSON, cls).setUpClass()227class UserRolesTestXML(RolesTestXML,228                       UserRolesTestBase):229    @classmethod230    def setUpClass(cls):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
