Best Python code snippet using localstack_python
cli.py
Source:cli.py
...56 # it's ok if user is already added to group57 iam_client.add_user_to_group(GroupName=group_name, UserName=user_name)58 time.sleep(10)59 logger.info("User %s attached to group %s." % (user_name, group_name))60def create_cognito_user_pool(cognito_idp_client, user_pool_config):61 # type: ('boto3.client("cognito-idp")', dict) -> str62 # noinspection PyBroadException,PyUnusedLocal63 kwargs = {64 "MaxResults": 6065 }66 while True:67 response = cognito_idp_client.list_user_pools(**kwargs)68 # yield from response['events']69 try:70 for user_pool in response['UserPools']:71 if user_pool['Name'] == user_pool_config['PoolName']:72 return user_pool['Id']73 kwargs['NextToken'] = response['NextToken']74 except KeyError:75 break76 response = cognito_idp_client.create_user_pool(**user_pool_config)77 return response['UserPool']['Id']78def fix_identity_pool_id(trust_policy_str, identity_pool_id):79 # type: (str, str) -> str80 trust_policy_dict = json.loads(trust_policy_str)81 trust_policy_dict['Statement'][0]['Condition']['StringEquals'][82 'cognito-identity.amazonaws.com:aud'] = identity_pool_id83 return json.dumps(trust_policy_dict)84def get_cognito_identity_pool_id(cognito_identity_client, identity_pool_name):85 # type: ('boto3.client("cognito-identity")', str) -> str86 identity_pools = cognito_identity_client.list_identity_pools(MaxResults=60)87 identity_pool_id = None88 for identity_pool in identity_pools['IdentityPools']:89 if identity_pool['IdentityPoolName'] == identity_pool_name:90 identity_pool_id = identity_pool['IdentityPoolId']91 return identity_pool_id92# noinspection PyUnusedLocal93def create_cognito_identity_pool(cognito_identity_client,94 iam_client,95 identity_pool_name,96 allow_unauthenticated_identities,97 cognito_praktikos_trust_policy_auth_file,98 cognito_praktikos_trust_policy_unauth_file99 ):100 # type: ('boto3.client("cognito-identity")', 'boto3.client("iam")', str, bool, str, str) -> None101 identity_pool_id = get_cognito_identity_pool_id(cognito_identity_client, identity_pool_name)102 if not identity_pool_id:103 response = cognito_identity_client.create_identity_pool(104 IdentityPoolName=identity_pool_name,105 AllowUnauthenticatedIdentities=allow_unauthenticated_identities106 )107 identity_pool_id = response['IdentityPoolId']108 time.sleep(10)109 response = cognito_identity_client.get_identity_pool_roles(IdentityPoolId=identity_pool_id)110 if 'Roles' not in response:111 # noinspection PyBroadException112 try:113 auth_role = iam_client.get_role(RoleName="Cognito_praktikosAuth_Role")114 except Exception as ex:115 json_policy = get_file_contents(cognito_praktikos_trust_policy_auth_file)116 auth_role = create_iam_role(iam_client,117 "Cognito_praktikosAuth_Role",118 "Cognito Authenticated role for Praktikos Lambda Functions",119 fix_identity_pool_id(json_policy, identity_pool_id)120 )121 # noinspection PyBroadException122 try:123 unauth_role = iam_client.get_role(RoleName="Cognito_praktikosUnauth_Role")124 except Exception as ex:125 json_policy = get_file_contents(cognito_praktikos_trust_policy_unauth_file)126 unauth_role = create_iam_role(iam_client,127 "Cognito_praktikosUnauth_Role",128 "Cognito Unauthenticated role for Praktikos Lambda Functions",129 fix_identity_pool_id(json_policy, identity_pool_id)130 )131 kwargs = {132 "IdentityPoolId": identity_pool_id,133 "Roles": {134 "authenticated": auth_role['Role']['Arn'],135 "unauthenticated": unauth_role['Role']['Arn']136 }137 }138 cognito_identity_client.set_identity_pool_roles(**kwargs)139 return identity_pool_id140def create_cognito_user_pool_client(cognito_idp_client, user_pool_id, client_name, cognito_user_pool_client_file):141 # type: ('boto3.client("cognito-idp")', str, str, str) -> None142 user_pool_clients = cognito_idp_client.list_user_pool_clients(UserPoolId=user_pool_id, MaxResults=60)143 for user_pool_client in user_pool_clients['UserPoolClients']:144 if user_pool_client['ClientName'] == client_name:145 return146 # user pool client not found, create it147 kwargs = json.loads(get_file_contents(cognito_user_pool_client_file))148 kwargs['UserPoolId'] = user_pool_id149 kwargs['ClientName'] = client_name150 cognito_idp_client.create_user_pool_client(**kwargs)151def create_cognito_user_pool_group(cognito_idp_client, user_pool_id, group_name):152 kwargs = {153 "UserPoolId": user_pool_id,154 "Limit": 60155 }156 while True:157 response = cognito_idp_client.list_groups(**kwargs)158 for group in response['Groups']:159 if group['GroupName'] == group_name:160 return161 # yield from response['events']162 try:163 kwargs['NextToken'] = response['NextToken']164 except KeyError:165 break166 # group not found, create it167 cognito_idp_client.create_group(168 GroupName=group_name,169 UserPoolId=user_pool_id170 )171def update_iam_user_praktikos(iam_client, user_name, group_name="praktikos"):172 # type: ('boto3.client("iam")', str, str) -> None173 filename = path.abspath(path.join(configs_directory, "iam_user_and_group_policy_arns.json"))174 with open(filename, 'r') as fd:175 policy_arns = json.loads(fd.read())176 update_iam_user_and_group_policies(iam_client, user_name, group_name, policy_arns)177def create_cognito_user_pool_praktikos(cognito_idp_client):178 filename = path.abspath(path.join(configs_directory, "cognito_user_pool.json"))179 with open(filename, 'r') as fd:180 cognito_user_pool = json.loads(fd.read())181 user_pool_id = create_cognito_user_pool(cognito_idp_client, cognito_user_pool)182 filename = path.abspath(path.join(configs_directory, "cognito_user_pool_client.json"))183 create_cognito_user_pool_client(cognito_idp_client, user_pool_id, "praktikos", filename)184 create_cognito_user_pool_group(cognito_idp_client, user_pool_id, "authenticated")185 return user_pool_id186def create_cognito_identity_pool_praktikos(cognito_identity_client, iam_client):187 auth_filename = path.abspath(path.join(configs_directory, "trust_policy_cognito_auth.json"))188 unauth_filename = path.abspath(path.join(configs_directory, "trust_policy_cognito_unauth.json"))189 return create_cognito_identity_pool(cognito_identity_client,190 iam_client,191 "praktikos",192 True,193 auth_filename,194 unauth_filename195 )...
apigateway_fixtures.py
Source:apigateway_fixtures.py
...82 return response.get("basePath"), response.get("stage")83def create_rest_api_deployment(apigateway_client, **kwargs):84 response = apigateway_client.create_deployment(**kwargs)85 assert_response_is_200(response)86def create_cognito_user_pool(cognito_idp, **kwargs):87 response = cognito_idp.create_user_pool(**kwargs)88 assert_response_is_200(response)89 return response.get("UserPool").get("Id"), response.get("UserPool").get("Arn")90def delete_cognito_user_pool(cognito_idp, **kwargs):91 response = cognito_idp.delete_user_pool(**kwargs)92 assert_response_is_200(response)93def create_cognito_user_pool_client(cognito_idp, **kwargs):94 response = cognito_idp.create_user_pool_client(**kwargs)95 assert_response_is_200(response)96 return (97 response.get("UserPoolClient").get("ClientId"),98 response.get("UserPoolClient").get("ClientName"),99 )100def create_cognito_user(cognito_idp, **kwargs):...
aws_auth_service_stack.py
Source:aws_auth_service_stack.py
...32 COG_APP_OAUTH = aws_cognito.OAuthSettings(flows=COG_APP_OAUTH_FLOWS,33 scopes=COG_APP_OAUTH_SCOPES,34 callback_urls=COG_APP_OAUTH_CALLBACK_URL,35 logout_urls=COG_APP_OAUTH_LOGOUT_URL)36 def create_cognito_user_pool(self):37 return aws_cognito.UserPool(self,38 id=self.COG_USER_POOL_ID,39 password_policy=self.COG_PASSWORD_POLICY,40 self_sign_up_enabled=self.COG_SELF_SIGNUP_ENABLED,41 sign_in_aliases=self.COG_SIGNIN_ALIASES,42 sign_in_case_sensitive=self.COG_SIGNIN_CASE_SENSITIVE,43 standard_attributes=self.COG_STANDARD_ATTRIBUTES,44 user_pool_name=self.COG_USER_POOL_NAME,45 lambda_triggers=self.COG_LAMBDA_TRIGGERS46 )47 def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:48 super().__init__(scope, construct_id, **kwargs)49 cognito_user_pool = self.create_cognito_user_pool()50 cognito_user_pool.add_domain(id=self.COG_DOMAIN_ID, cognito_domain=self.COG_DOMAIN_OPTIONS)51 cognito_user_pool.add_client(id=self.COG_APPID_ID,52 user_pool_client_name=self.COG_APP_NAME,53 generate_secret=self.COG_APP_GENERATE_SECRET,54 auth_flows=self.COG_APP_AUTH_FLOWS,55 o_auth=self.COG_APP_OAUTH...
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!!