How to set AWS Cognito user password in CloudFormation template for testing
Asked Answered
O

1

10

I'm creating a Cognito User Pool and a test user using CloudFormation templates. I'm doing this for automated testing of login, user management code, and token code.

However, there doesn't appear to be a way to set the password to some known value so that the test code can proceed with login, which would be the first step in testing everything else.

related question but answer is "check your email account", which won't work for automated testing: Cloud Formation AWS::Cognito::UserPoolUser temporary password\

I can use the API to administratively set the password (at least the CLI can) but I want my test code to be as black box as possible. I want to specify all the preconditions in the Cloud Formation Template, as I do for everything else I'm testing.

This AWS forum answer says "use a custom resource", but provides no help in how to do so:

Okeefe answered 3/6, 2019 at 18:38 Comment(3)
Adding users in a CFT might be an anti-pattern, because it's so useless to do so. This might be a better way to create users for automated testing: https://mcmap.net/q/125604/-how-to-change-user-status-force_change_passwordOkeefe
it can be that AWS forum answer similar to thing to the linked answer, that is creating a CF custom resource with a lambda that would go use AWS.CognitoIdentityServiceProvider to perform docs.aws.amazon.com/cognito-user-identity-pools/latest/… , I wouldn't say it's specifically anti-pattern, it doesn't have any reasonable upside here to me comparing to the approach in linked answer, but it is directly tied up to CF stack lifetime, which is often what we want to achieveStandford
at this point I've moved on. I now have my test harness create users with random email addresses and use the Cognito API to set their password. Yes Cognito fills up, but sufficient randomness and the lifetime of the users is that of the CFT cover up most issues, it's good enough. The downside is there's a lot of latency in creating users and logging them in, so the tests run slower than I would like.Okeefe
E
0

You may find that using cdk-userpool-user suits your purposes. It packages up using a custom resource fairly cleanly.

new UserPoolUser(this, 'Test', {
    userPool,
    username: 'testuser',
    password: 'Passw0$rd',
});
Everick answered 7/10, 2023 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.