Best Python code snippet using localstack_python
test_iam.py
Source:test_iam.py
...31 )32 account_id = sts_client.get_caller_identity()["Account"]33 keys = iam_client.create_access_key(UserName=user_name)["AccessKey"]34 wait_for_user(keys)35 iam_client_as_user = create_client_with_keys("iam", keys=keys)36 user_response = iam_client_as_user.get_user()37 user = user_response["User"]38 assert user["UserName"] == user_name39 assert user["Arn"] == f"arn:aws:iam::{account_id}:user/{user_name}"40 @pytest.mark.only_localstack41 def test_get_user_without_username_as_root(self, iam_client, sts_client):42 """Test get_user on root account. Marked only localstack, since we usually cannot access as root directly"""43 account_id = sts_client.get_caller_identity()["Account"]44 user_response = iam_client.get_user()45 user = user_response["User"]46 assert user["UserId"] == account_id47 assert user["Arn"] == f"arn:aws:iam::{account_id}:root"48 def test_get_user_without_username_as_role(49 self, iam_client, create_role, wait_and_assume_role, sts_client50 ):51 role_name = f"role-{short_uid()}"52 policy_name = f"policy={short_uid()}"53 session_name = f"session-{short_uid()}"54 account_arn = sts_client.get_caller_identity()["Arn"]55 assume_policy_doc = {56 "Version": "2012-10-17",57 "Statement": [58 {59 "Action": "sts:AssumeRole",60 "Principal": {"AWS": account_arn},61 "Effect": "Allow",62 }63 ],64 }65 created_role_arn = create_role(66 RoleName=role_name, AssumeRolePolicyDocument=json.dumps(assume_policy_doc)67 )["Role"]["Arn"]68 iam_client.put_role_policy(69 RoleName=role_name, PolicyName=policy_name, PolicyDocument=GET_USER_POLICY_DOC70 )71 keys = wait_and_assume_role(role_arn=created_role_arn, session_name=session_name)72 iam_client_as_role = create_client_with_keys("iam", keys=keys)73 with pytest.raises(ClientError) as e:74 iam_client_as_role.get_user()75 e.match("Must specify userName when calling with non-User credentials")76 def test_create_user_with_permission_boundary(self, iam_client, create_user, create_policy):77 user_name = f"user-{short_uid()}"78 policy_name = f"policy-{short_uid()}"79 policy_arn = create_policy(PolicyName=policy_name, PolicyDocument=GET_USER_POLICY_DOC)[80 "Policy"81 ]["Arn"]82 create_user_reply = create_user(UserName=user_name, PermissionsBoundary=policy_arn)83 assert "PermissionsBoundary" in create_user_reply["User"]84 assert {85 "PermissionsBoundaryArn": policy_arn,86 "PermissionsBoundaryType": "Policy",...
util.py
Source:util.py
...32 if bucket["Name"] == bucket_name:33 return True34 return False35def wait_for_user(keys):36 sts_client = create_client_with_keys(service="sts", keys=keys)37 def is_user_ready():38 try:39 sts_client.get_caller_identity()40 return True41 except ClientError as e:42 if e.response["Error"]["Code"] == "InvalidClientTokenId":43 return False44 return True45 # wait until the given user is ready, takes AWS IAM a while...46 poll_condition(is_user_ready, interval=5, timeout=20)47def create_client_with_keys(48 service: str,49 keys: Dict[str, str],50 region_name: str = None,51 client_config: Config = None,52):53 """54 Create a boto client with the given access key, targeted against LS per default, but to AWS if TEST_TARGET is set55 accordingly.56 :param service: Service to create the Client for57 :param keys: Access Keys58 :param region_name: Region for the client59 :param client_config:60 :return:61 """...
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!!