AWS Cognito Test Environment

I am currently working on using AWS Cognito to manage user authentication for our application. One problem I am facing is a good way to implement a โ€œtestโ€ or โ€œqaโ€ environment.

I have many automated tests for my APIs that will create users with random data. Obviously, I do not want Cognito to send actual SMS or email messages in this environment. Also, during manual testing, we will create many users with fake phone numbers and emails. Is there a way to turn the user pool into development mode, where all messages are simply logged in some way?

+4
source share
2 answers

Here's what I did to create an AWS Cognito "staging" environment that doesn't send real notifications to users. Actually there were several different parts, but I think I was able to cover everything. It would be nice if Cognito simply provided the "User Pool" parameter to disable all notifications, so I donโ€™t need to write the environment logic in my code.

Deny user invitations

AdminCreateUser , . . , MessageAction: 'SUPPRESS' . :

let params = {
    UserPoolId: config.cognitoUserPoolId,
    Username: uuid.v4(),
    MessageAction: 'SUPPRESS', /* IMPORTANT! */
    TemporaryPassword: user.phone_number.slice(-6),
    UserAttributes: [
        { Name: 'given_name', Value: user.first_name },
        { Name: 'family_name', Value: user.last_name },
        { Name: 'phone_number', Value: user.phone_number }
    ]
};

cognito.adminCreateUser(params).promise().then(data => {
    console.log(data);
});

: http://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html

, , . . " ?" .

User Pool Settings

+2

ุจamba , autoConfirmUser. Cognito SMS- . lambda (http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#aws-lambda-triggers-pre-registration-example).

exports.handler = function(event, context) {
    // This Lambda function returns a flag to indicate if a user should be auto-confirmed.

    // Perform any necessary validations.

    // Impose a condition that the minimum length of the username of 5 is imposed on all user pools.
    if (event.userName.length < 5) {
        var error = new Error('failed!');
        context.done(error, event);
    }

    // Access your resource which contains the list of emails of users who were invited to sign up

    // Compare the list of email IDs from the request to the approved list
    if(event.userPoolId === "yourSpecialUserPool") {
        if (event.request.userAttributes.email in listOfEmailsInvited) {
            event.response.autoConfirmUser = true;
        }
    }
    // Return result to Cognito
    context.done(null, event);
};
+2

Source: https://habr.com/ru/post/1665388/


All Articles