Skip to content

Commit

Permalink
feat(lambda): support for CustomMessage_SignUp
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Nov 25, 2021
1 parent 0c0edb4 commit ce69ea7
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ cognito-local how to connect to your local Lambda server:
| CustomMessage | Authentication ||
| CustomMessage | ForgotPassword ||
| CustomMessage | ResendCode ||
| CustomMessage | SignUp | |
| CustomMessage | SignUp | |
| CustomMessage | UpdateUserAttribute ||
| CustomMessage | VerifyUserAttribute ||
| DefineAuthChallenge | \* ||
Expand Down
91 changes: 90 additions & 1 deletion src/services/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,94 @@ describe("messages service", () => {
});
});

it.todo("signUp");
describe("signUp", () => {
describe("CustomMessage lambda is configured", () => {
describe("lambda returns a custom message", () => {
it("returns the custom message and code", async () => {
mockTriggers.enabled.mockImplementation((name) => {
return name === "CustomMessage";
});
mockTriggers.customMessage.mockResolvedValue({
smsMessage: "sms",
emailSubject: "email subject",
emailMessage: "email",
});

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
smsMessage: "sms",
emailSubject: "email subject",
emailMessage: "email",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_SignUp",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});

describe("lambda does not return a custom message", () => {
it("returns just the code", async () => {
mockTriggers.enabled.mockImplementation((name) => {
return name === "CustomMessage";
});
mockTriggers.customMessage.mockResolvedValue(null);

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_SignUp",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});
});

describe("CustomMessage lambda is not configured", () => {
it("returns just the code", async () => {
mockTriggers.enabled.mockReturnValue(false);

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).not.toHaveBeenCalled();
});
});
});
});
35 changes: 32 additions & 3 deletions src/services/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export interface Messages {
user: User,
code: string
): Promise<Message>;
signUp(code: string): Promise<Message>;
signUp(
clientId: string,
userPoolId: string,
user: User,
code: string
): Promise<Message>;
}

const stubMessage = (code: string) =>
Expand Down Expand Up @@ -92,7 +97,31 @@ export class MessagesService implements Messages {
};
}

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

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

// TODO: What should the default message be?
return {
__code: code,
};
}
}
32 changes: 20 additions & 12 deletions src/targets/signUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,27 @@ describe("SignUp target", () => {
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
});

const createdUser = {
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
],
Enabled: true,
Password: "pwd",
UserCreateDate: now.getTime(),
UserLastModifiedDate: now.getTime(),
UserStatus: "UNCONFIRMED",
Username: "user-supplied",
};

expect(mockMessages.signUp).toHaveBeenCalledWith(
"clientId",
"test",
createdUser,
"1234"
);
expect(mockMessageDelivery.deliver).toHaveBeenCalledWith(
{
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
],
Enabled: true,
Password: "pwd",
UserCreateDate: now.getTime(),
UserLastModifiedDate: now.getTime(),
UserStatus: "UNCONFIRMED",
Username: "user-supplied",
},
createdUser,
{
AttributeName: "email",
DeliveryMedium: "EMAIL",
Expand Down
7 changes: 6 additions & 1 deletion src/targets/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export const SignUp = (
};

const code = otp();
const message = await messages.signUp(code);
const message = await messages.signUp(
req.ClientId,
userPool.config.Id,
user,
code
);
await messageDelivery.deliver(user, deliveryDetails, message);

await userPool.saveUser({
Expand Down

0 comments on commit ce69ea7

Please sign in to comment.