Skip to content

Commit

Permalink
feat(api): adminCreateUser delivers welcome message
Browse files Browse the repository at this point in the history
- Invokes the CustomMessage lambda with CustomMessage_AdminCreateUser
- Respects DesiredDeliveryMediums parameter
  • Loading branch information
jagregory committed Nov 27, 2021
1 parent 084c253 commit d49aa80
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ cognito-local how to connect to your local Lambda server:
| --------------------------- | --------------------------------- | ------- |
| CreateAuthChallenge | \* ||
| CustomEmailSender | \* ||
| CustomMessage | AdminCreateUser | |
| CustomMessage | AdminCreateUser | |
| CustomMessage | Authentication ||
| CustomMessage | ForgotPassword ||
| CustomMessage | ResendCode ||
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/aws-sdk/adminCreateUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe(

const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",

Expand All @@ -32,6 +33,7 @@ describe(
Name: "sub",
Value: expect.stringMatching(UUID),
},
{ Name: "phone_number", Value: "0400000000" },
],
Enabled: true,
UserCreateDate: roundedDate,
Expand Down
1 change: 1 addition & 0 deletions integration-tests/aws-sdk/adminDeleteUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe(
// create the user
const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",

Expand Down
1 change: 1 addition & 0 deletions integration-tests/aws-sdk/adminGetUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe(
// create the user
const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",

Expand Down
1 change: 1 addition & 0 deletions integration-tests/aws-sdk/adminSetUserPassword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe(
// create the user
const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",

Expand Down
3 changes: 1 addition & 2 deletions integration-tests/aws-sdk/deleteUser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { UUID } from "../../src/__tests__/patterns";
import { UserNotFoundError } from "../../src/errors";
import { attributeValue } from "../../src/services/userPoolService";
import { withCognitoSdk } from "./setup";

describe(
Expand All @@ -20,6 +18,7 @@ describe(
// create a user
await client
.adminCreateUser({
DesiredDeliveryMediums: ["EMAIL"],
TemporaryPassword: "def",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
Username: "abc",
Expand Down
1 change: 1 addition & 0 deletions integration-tests/aws-sdk/initiateAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe(

const createUserResponse = await client
.adminCreateUser({
DesiredDeliveryMediums: ["EMAIL"],
TemporaryPassword: "def",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
Username: "abc",
Expand Down
5 changes: 4 additions & 1 deletion integration-tests/aws-sdk/listUsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ describe(

const createUserResult = await client
.adminCreateUser({
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
Username: "abc",
UserPoolId: "test",
TemporaryPassword: "TemporaryPassword", // TODO: shouldn't need to supply this

// TODO: shouldn't need to supply this
TemporaryPassword: "TemporaryPassword",
})
.promise();

Expand Down
1 change: 1 addition & 0 deletions integration-tests/aws-sdk/respondToAuthChallenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe(

await client
.adminCreateUser({
DesiredDeliveryMediums: ["EMAIL"],
TemporaryPassword: "def",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
Username: "abc",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/mockMessages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Messages } from "../services";

export const newMockMessages = (): jest.Mocked<Messages> => ({
adminCreateUser: jest.fn(),
authentication: jest.fn(),
forgotPassword: jest.fn(),
signUp: jest.fn(),
Expand Down
34 changes: 34 additions & 0 deletions src/services/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Triggers } from "./triggers";
import { User } from "./userPoolService";

const AWS_ADMIN_CLIENT_ID = "CLIENT_ID_NOT_APPLICABLE";

export interface Message {
__code?: string; // not really part of the message, but we pass it around for convenience logging to the console
emailMessage?: string;
Expand All @@ -9,6 +11,11 @@ export interface Message {
}

export interface Messages {
adminCreateUser(
userPoolId: string,
user: User,
temporaryPassword: string
): Promise<Message>;
authentication(
clientId: string,
userPoolId: string,
Expand Down Expand Up @@ -36,6 +43,33 @@ export class MessagesService implements Messages {
this.triggers = triggers;
}

public async adminCreateUser(
userPoolId: string,
user: User,
temporaryPassword: string
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage({
clientId: AWS_ADMIN_CLIENT_ID,
code: temporaryPassword,
source: "CustomMessage_AdminCreateUser",
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
});

return {
__code: temporaryPassword,
...message,
};
}

// TODO: What should the default message be?
return {
__code: temporaryPassword,
};
}

public async authentication(
clientId: string,
userPoolId: string,
Expand Down
Loading

0 comments on commit d49aa80

Please sign in to comment.