How to use _check_role_exists method in tempest

Best Python code snippet using tempest_python

iam.py

Source:iam.py Github

copy

Full Screen

...88 dict(Error=dict(89 Code='EntityAlreadyExists',90 Message=('Role with name %s already exists' % RoleName))),91 OperationName)92 def _check_role_exists(self, RoleName, OperationName):93 if RoleName not in self.mock_iam_roles:94 raise ClientError(95 dict(Error=dict(96 Code='NoSuchEntity',97 Message=('Role not found for %s' % RoleName))),98 OperationName)99 # attached role policies100 def attach_role_policy(self, PolicyArn, RoleName):101 self._check_role_exists(RoleName, 'AttachRolePolicy')102 arns = self.mock_iam_role_attached_policies.setdefault(RoleName, [])103 if PolicyArn not in arns:104 arns.append(PolicyArn)105 return {}106 def list_attached_role_policies(self, RoleName):107 self._check_role_exists(RoleName, 'ListAttachedRolePolicies')108 arns = self.mock_iam_role_attached_policies.get(RoleName, [])109 return dict(AttachedPolicies=[110 dict(PolicyArn=arn, PolicyName=arn.split('/')[-1])111 for arn in arns112 ])113 # instance profiles114 def create_instance_profile(self, InstanceProfileName):115 # Path not implemented116 # mock InstanceProfileIds are all the same117 self._check_instance_profile_does_not_exist(InstanceProfileName,118 'CreateInstanceProfile')119 profile = dict(120 Arn=('arn:aws:iam::012345678901:instance-profile/%s' %121 InstanceProfileName),122 CreateDate=_boto3_now(),123 InstanceProfileId='AIPAMOCKMOCKMOCKMOCK',124 InstanceProfileName=InstanceProfileName,125 Path='/',126 Roles=[],127 )128 self.mock_iam_instance_profiles[InstanceProfileName] = profile129 return dict(InstanceProfile=profile)130 def add_role_to_instance_profile(self, InstanceProfileName, RoleName):131 self._check_role_exists(RoleName, 'AddRoleToInstanceProfile')132 self._check_instance_profile_exists(133 InstanceProfileName, 'AddRoleToInstanceProfile')134 profile = self.mock_iam_instance_profiles[InstanceProfileName]135 if profile['Roles']:136 raise ClientError(137 dict(Error=dict(138 Code='LimitExceeded',139 Message=('Cannot exceed quota for'140 ' InstanceSessionsPerInstanceProfile: 1'),141 )),142 'AddRoleToInstanceProfile',143 )144 # just point straight at the mock role; never going to redefine it145 profile['Roles'] = [self.mock_iam_roles[RoleName]]...

Full Screen

Full Screen

test_roles_router.py

Source:test_roles_router.py Github

copy

Full Screen

...92 @patch('routers.roles.get_role_by_id')93 def tests_check_role_exists_helper_raises_when_does_not_pass(self, mock_get_role_by_id):94 mock_get_role_by_id.return_value.__next__.return_value = None95 with self.assertRaises(HTTPException) as exc:96 _check_role_exists(role_id=self.mock_base_role['role_id'])97 self.assertEqual(exc.exception.status_code, status.HTTP_400_BAD_REQUEST)98 self.assertEqual(exc.exception.detail, 'Role not found.')99 @patch('routers.roles.get_role_by_id')100 def test_returns_role_id_when_role_found(self, mock_get_role_by_id):101 mock_get_role_by_id.return_value = iter([self.mock_base_role])102 result = _check_role_exists(role_id=self.mock_base_role['role_id'])103 self.assertEqual(result, self.mock_base_role['role_id'])104 @patch('routers.roles.get_all_permissions')105 def test_raises_exception_when_permission_not_found(self, mock_get_all_permissions):106 mock_get_all_permissions.return_value = iter([{'permission_id': '3413cc55-6a78-4c09-a176-0387eac4d395'}])107 with self.assertRaises(HTTPException) as exc:108 _check_permissions_exists(permissions=['e1416696-62ef-42d1-a84c-18b36d35d7f9'])109 self.assertEqual(exc.exception.status_code, status.HTTP_400_BAD_REQUEST)110 self.assertEqual(exc.exception.detail, "One or more permissions not valid.") 111 @patch('routers.roles.get_all_permissions')112 def test_returns_permissions_when_all_are_valid(self, mock_get_all_permissions):113 mock_permission = {'permission_id': '3413cc55-6a78-4c09-a176-0387eac4d395'}114 mock_get_all_permissions.return_value = iter([mock_permission])115 result = _check_permissions_exists(permissions=[mock_permission['permission_id']])116 self.assertEqual(result, [mock_permission['permission_id']])

Full Screen

Full Screen

role_service.py

Source:role_service.py Github

copy

Full Screen

...20 def create_role(self, role_data):21 role_data, err = self._validate_data(role_data, role_schema)22 if err:23 return err, HTTPStatus.BAD_REQUEST24 role = self._check_role_exists(role_data['name'])25 if role:26 message = self._get_response('ROLE_EXISTS', role.name)27 return message, HTTPStatus.BAD_REQUEST28 self._save_role(role_data)29 return self._get_response('ROLE_CREATED', role_data['name']), \30 HTTPStatus.CREATED31 def delete_role(self, role_data):32 role_data, err = self._validate_data(role_data, role_schema)33 if err:34 return err, HTTPStatus.BAD_REQUEST35 role = self._check_role_exists(role_data['name'])36 if not role:37 message = self._get_response('ROLE_NOT_EXISTS', role_data['name'])38 return message, HTTPStatus.NOT_FOUND39 40 self._delete_role(role)41 return self._get_response('ROLE_DELETED', role_data['name'])42 def assign_user_role(self, user_uuid, role_name):43 role, err = self._validate_role(role_name)44 if not role:45 return err, HTTPStatus.BAD_REQUEST46 user, err = self._validate_user(user_uuid)47 if not user:48 return err, HTTPStatus.BAD_REQUEST49 err = self._assign_user_role(user, role)50 if err:51 return err, HTTPStatus.BAD_REQUEST52 return self._get_response('ROLE_ASSIGNED', role.name, user.id)53 def unassign_user_role(self, user_uuid, role_name):54 role, err = self._validate_role(role_name)55 if not role:56 return err, HTTPStatus.BAD_REQUEST57 user, err = self._validate_user(user_uuid)58 if not user:59 return err, HTTPStatus.BAD_REQUEST60 err = self._unassign_user_role(user, role)61 if err:62 return err, HTTPStatus.BAD_REQUEST63 return self._get_response('ROLE_UNASSIGNED', role.name, user.id)64 def _assign_user_role(self, user: User, role: Role):65 try:66 user.roles.append(role)67 db.session.commit()68 except IntegrityError:69 db.session.rollback()70 return {'message': f'{user.id} already has role {role.name}'}71 def _unassign_user_role(self, user: User, role: Role):72 try:73 user.roles.remove(role)74 db.session.commit()75 except ValueError as e:76 db.session.rollback()77 return {'message': f'{user.id} does not have role {role.name}'}78 def _validate_user(self, user_uuid):79 user = self._check_user_exists(user_uuid)80 if not user:81 message = self._get_response('USER_NOT_EXISTS', user_uuid)82 return None, message83 return user, {}84 def _validate_role(self, role_name):85 role = self._check_role_exists(role_name)86 if not role:87 message = self._get_response('ROLE_NOT_EXISTS', role_name)88 return None, message89 return role, {}90 def _check_user_exists(self, user_uuid):91 return User.query.filter(User.id == user_uuid).first()92 def _validate_data(self, data, schema):93 try:94 data = schema.load(data)95 except ValidationError as e:96 return {}, e.messages97 else:98 return data, {}99 def _check_role_exists(self, role):100 return Role.query.filter(Role.name == role).first()101 def _save_role(self, role_data):102 assert role_data.get('name', None) is not None103 role = Role(104 name=role_data.get('name'),105 description=role_data.get('description', None)106 )107 db.session.add(role)108 db.session.commit()109 def _delete_role(self, role):110 Role.query.filter(Role.name == role.name).delete()111 db.session.commit()112 def _get_response(self, label, *args):113 return {'message': self.RESPONSE_DESCRIPTIONS[label].format(*args)}...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful