Skip to content

Commit

Permalink
Merge pull request #399 from jagregory/fix-race-conditions
Browse files Browse the repository at this point in the history
fix!: user pool creation race conditions
  • Loading branch information
jagregory authored Feb 11, 2025
2 parents f2f3a19 + 69ee1e1 commit ef9669c
Show file tree
Hide file tree
Showing 37 changed files with 607 additions and 413 deletions.
15 changes: 11 additions & 4 deletions integration-tests/aws-sdk/adminConfirmSignUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ describe(
it("confirms a user", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

const upc = await client
.createUserPoolClient({
UserPoolId: "test",
UserPoolId: userPoolId,
ClientName: "test",
})
.promise();
Expand All @@ -24,7 +31,7 @@ describe(

let user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand All @@ -33,14 +40,14 @@ describe(

await client
.adminConfirmSignUp({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();

user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand Down
29 changes: 26 additions & 3 deletions integration-tests/aws-sdk/adminCreateUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ describe(
it("creates a user with only the required parameters", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand All @@ -45,12 +52,20 @@ describe(
it("sends a welcome email", async () => {
const fakeMessageDelivery = messageDelivery();
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

const createUserResult = await client
.adminCreateUser({
DesiredDeliveryMediums: ["EMAIL"],
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand Down Expand Up @@ -88,11 +103,19 @@ describe(
it("creates a user without sending a welcome email if MessageAction=SUPPRESS is passed", async () => {
const fakeMessageDelivery = messageDelivery();
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

const createUserResult = await client
.adminCreateUser({
MessageAction: "SUPPRESS",
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand Down
31 changes: 22 additions & 9 deletions integration-tests/aws-sdk/adminDeleteUser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ClockFake } from "../../src/__tests__/clockFake";
import { UUID } from "../../src/__tests__/patterns";
import { UserNotFoundError } from "../../src/errors";
import { withCognitoSdk } from "./setup";

Expand All @@ -16,20 +15,27 @@ describe(
it("deletes a user", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

// create the user
const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

// verify they exist
const beforeUserResult = await client
.adminGetUser({
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand All @@ -46,7 +52,7 @@ describe(
await client
.adminDeleteUser({
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand All @@ -55,7 +61,7 @@ describe(
client
.adminGetUser({
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise()
).rejects.toEqual(new UserNotFoundError("User does not exist."));
Expand All @@ -64,6 +70,13 @@ describe(
it("deletes a user with an email address as a username", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

// create the user
const createUserResult = await client
.adminCreateUser({
Expand All @@ -72,15 +85,15 @@ describe(
{ Name: "phone_number", Value: "0400000000" },
],
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

// verify they exist
const beforeUserResult = await client
.adminGetUser({
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand All @@ -97,7 +110,7 @@ describe(
await client
.adminDeleteUser({
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand All @@ -106,7 +119,7 @@ describe(
client
.adminGetUser({
Username: "[email protected]",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise()
).rejects.toEqual(new UserNotFoundError("User does not exist."));
Expand Down
15 changes: 11 additions & 4 deletions integration-tests/aws-sdk/adminDeleteUserAttributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ describe(
it("updates a user's attributes", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

await client
.adminCreateUser({
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "custom:example", Value: "1" },
],
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
DesiredDeliveryMediums: ["EMAIL"],
})
.promise();

let user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand All @@ -34,15 +41,15 @@ describe(

await client
.adminDeleteUserAttributes({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
UserAttributeNames: ["custom:example"],
})
.promise();

user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand Down
13 changes: 10 additions & 3 deletions integration-tests/aws-sdk/adminDisableUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ describe(
it("updates a user's attributes", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

await client
.adminCreateUser({
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "custom:example", Value: "1" },
],
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
DesiredDeliveryMediums: ["EMAIL"],
})
.promise();

await client
.adminDisableUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();

const user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand Down
17 changes: 12 additions & 5 deletions integration-tests/aws-sdk/adminEnableUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ describe(
it("updates a user's attributes", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

await client
.adminCreateUser({
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "custom:example", Value: "1" },
],
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
DesiredDeliveryMediums: ["EMAIL"],
})
.promise();

await client
.adminDisableUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();

let user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand All @@ -36,14 +43,14 @@ describe(

await client
.adminEnableUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();

user = await client
.adminGetUser({
UserPoolId: "test",
UserPoolId: userPoolId,
Username: "abc",
})
.promise();
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/aws-sdk/adminGetUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ describe(
it("gets a user", async () => {
const client = Cognito();

const pool = await client
.createUserPool({
PoolName: "test",
})
.promise();
const userPoolId = pool.UserPool?.Id!!;

// create the user
const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

// verify they exist
const result = await client
.adminGetUser({
Username: "abc",
UserPoolId: "test",
UserPoolId: userPoolId,
})
.promise();

Expand Down
Loading

0 comments on commit ef9669c

Please sign in to comment.