Skip to content

Commit

Permalink
fix(api): adminCreateUser handles duplicate users
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Nov 25, 2021
1 parent 4996aa4 commit 7529971
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/targets/adminCreateUser.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ClockFake } from "../__tests__/clockFake";
import { MockUserPoolClient } from "../__tests__/mockUserPoolClient";
import { UUID } from "../__tests__/patterns";
import { UsernameExistsError } from "../errors";
import { CognitoClient } from "../services";
import { AdminCreateUser, AdminCreateUserTarget } from "./adminCreateUser";
import * as TDB from "../__tests__/testDataBuilder";

describe("AdminCreateUser target", () => {
let adminCreateUser: AdminCreateUserTarget;
Expand Down Expand Up @@ -52,7 +54,21 @@ describe("AdminCreateUser target", () => {
it.todo("can create an alias to an existing user");
it.todo("can resend the welcome message");
it.todo("can suppress the welcome message");
it.todo("handles creating a duplicate user");

it("handles creating a duplicate user", async () => {
const existingUser = TDB.user();
MockUserPoolClient.getUserByUsername.mockResolvedValue(existingUser);

await expect(
adminCreateUser({
TemporaryPassword: "pwd",
UserAttributes: existingUser.Attributes,
Username: existingUser.Username,
UserPoolId: "test",
})
).rejects.toEqual(new UsernameExistsError());
});

it.todo("invokes the PreSignIn lambda");
it.todo("saves a user with a generated temporary password");
it.todo("sends a welcome message to the user");
Expand Down
10 changes: 8 additions & 2 deletions src/targets/adminCreateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AdminCreateUserResponse,
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import uuid from "uuid";
import { UnsupportedError } from "../errors";
import { UnsupportedError, UsernameExistsError } from "../errors";
import { Services } from "../services";
import { attributesInclude, User } from "../services/userPoolClient";

Expand All @@ -22,6 +22,12 @@ export const AdminCreateUser = ({
}

const userPool = await cognitoClient.getUserPool(req.UserPoolId);
const existingUser = await userPool.getUserByUsername(req.Username);
if (existingUser && req.MessageAction === "RESEND") {
throw new UnsupportedError("AdminCreateUser with MessageAction=RESEND");
} else if (existingUser) {
throw new UsernameExistsError();
}

const attributes = attributesInclude("sub", req.UserAttributes)
? req.UserAttributes ?? []
Expand All @@ -41,7 +47,7 @@ export const AdminCreateUser = ({
};
await userPool.saveUser(user);

// TODO: should handle existing users
// TODO: should throw InvalidParameterException when a non-email is supplied as the Username when the pool has email as a UsernameAttribute
// TODO: should send a message unless MessageAction=="SUPPRESS"
// TODO: support MessageAction=="RESEND"
// TODO: should generate a TemporaryPassword if one isn't set
Expand Down

0 comments on commit 7529971

Please sign in to comment.