Automate AWS Cognito SignUp and Confirm User flows using Java

Introduction

As we all know, AWS facilitates the Developers and QA Engineers with AWS CLI to register and confirm new users in AWS. This AWS CLI executes the simple command which takes a few parameters like Username, Password, Email and AWS clientId, etc. to register new users. This is a simple yet repetitive task in your testing cycles. Moreover, you may need to test your app with different scenarios like Create User, Confirm the newly Registered User, and Perform actions with the new user in the AUT (Application Under Test). It is also important to automate the AWS Sign up and Confirm User flows. There are multiple tools available to automate this procedure, and if you choose to do it with Rest Assured + Java, this article is going to help you with end to end solution. Let’s first revisit the signup and confirm user flows using AWS CLI and then understand how to automate the procedures using Java.

  1. This will create a new user in AWS Cognito and the user will receive the OTP/Verification code in the email address provided in the above command. Using the OTP/Verification code, we need to confirm the user identity.
  2. Confirm User:
    aws cognito-idp sign-up \
    — client-id <AWS clientId> \
    — username eliminatorMP \
    — confirmation-code 123456
package com.AWSRestAssured.util;import com.amazonaws.auth.SystemPropertiesCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder;
import com.amazonaws.services.cognitoidp.model.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY;
import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY;
import static com.digitalfuel.core.Config.EMAIL_TEMPLATES_JSON;
public class AWSCognitoUtils {

public static String accessKey = null;
public static String secretKey = null;
public static String clientId = null ;
public static String poolId = null;
public static AWSCognitoIdentityProvider provider;
public static Regions regions;

public void signUp(String username, String password, String emailAddress) {
setupCognito();
SignUpRequest signUpRequest = new SignUpRequest()
.withUsername(username)
.withClientId(clientId)
.withPassword(password)
.withUserAttributes(getUserAttributes(emailAddress));
provider.signUp(signUpRequest);
}
public void confirmSignup(String username){
MailReader mailReader = new MailReader();
setupCognito();
String otpEmailBody;
String otpCode;
String otpEmailSubject = "Your verification code";

//You need to create MailReader util method which read the OTP from email body as per your email format.
otpCode = mailReader.readEmail(otpEmailSubject);;

provider.confirmSignUp(new ConfirmSignUpRequest().withUsername(username).withClientId(clientId).withConfirmationCode(otpCode));
}
public static List<AttributeType> getUserAttributes(String emailAddress) {
ArrayList<AttributeType> attributes = new ArrayList<>();
attributes.add(new AttributeType().withName("email").withValue(emailAddress));
return attributes;
}
public static void setupCognito(){
regions = <Provide AWS Region name e.g. Regions.EU_WEST_1>;
accessKey = “<Replace your accessKey>”;
secretKey = “<Replace your secretKey>”;
clientId = “<Replace your clientId>”;
poolId = “<Replace your poolId>”;
System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, accessKey); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, secretKey);

provider = AWSCognitoIdentityProviderClientBuilder.standard()
.withRegion(regions).withCredentials(new SystemPropertiesCredentialsProvider()).build();
}
}

Automation and Functional QA Evangelist