Skip to content

Commit

Permalink
feat(lambda): postConfirmation called in signUp
Browse files Browse the repository at this point in the history
If the PreSignUp trigger returns autoConfirmUser then also call the PostConfirmation trigger after the user has been saved
  • Loading branch information
jagregory committed Nov 29, 2021
1 parent af955a1 commit ddb2b77
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 19 deletions.
138 changes: 120 additions & 18 deletions src/targets/signUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,130 @@ describe("SignUp target", () => {
).rejects.toBeInstanceOf(UserLambdaValidationError);
});

it("confirms the user if the lambda returns autoConfirmUser=true", async () => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);
mockTriggers.preSignUp.mockResolvedValue({
autoConfirmUser: true,
describe("autoConfirmUser=true", () => {
beforeEach(() => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);
mockTriggers.preSignUp.mockResolvedValue({
autoConfirmUser: true,
});
});

await signUp({
ClientId: "clientId",
ClientMetadata: {
client: "metadata",
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
ValidationData: [{ Name: "another", Value: "attribute" }],
it("confirms the user", async () => {
await signUp({
ClientId: "clientId",
ClientMetadata: {
client: "metadata",
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
ValidationData: [{ Name: "another", Value: "attribute" }],
});

expect(mockUserPoolService.saveUser).toHaveBeenCalledWith(
expect.objectContaining({
UserStatus: "CONFIRMED",
})
);
});

expect(mockUserPoolService.saveUser).toHaveBeenCalledWith(
expect.objectContaining({
UserStatus: "CONFIRMED",
})
);
describe("when PostConfirmation trigger is enabled", () => {
beforeEach(() => {
mockTriggers.enabled.mockImplementation(
(trigger) =>
trigger === "PreSignUp" || trigger === "PostConfirmation"
);
});

it("calls the PostConfirmation trigger lambda", async () => {
await signUp({
ClientId: "clientId",
ClientMetadata: {
client: "metadata",
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
ValidationData: [{ Name: "another", Value: "attribute" }],
});

expect(mockTriggers.postConfirmation).toHaveBeenCalledWith({
clientId: "clientId",
clientMetadata: {
client: "metadata",
},
source: "PostConfirmation_ConfirmSignUp",
userAttributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
],
userPoolId: "test",
username: "user-supplied",
validationData: undefined,
});
});

it("throws if the PostConfirmation lambda fails", async () => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);
mockTriggers.postConfirmation.mockRejectedValue(
new UserLambdaValidationError()
);

await expect(
signUp({
ClientId: "clientId",
ClientMetadata: {
client: "metadata",
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
ValidationData: [{ Name: "another", Value: "attribute" }],
})
).rejects.toBeInstanceOf(UserLambdaValidationError);
});
});
});

describe("autoConfirmUser=false", () => {
beforeEach(() => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);
mockTriggers.preSignUp.mockResolvedValue({
autoConfirmUser: false,
});
});

it("does not confirm the user", async () => {
await signUp({
ClientId: "clientId",
ClientMetadata: {
client: "metadata",
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
ValidationData: [{ Name: "another", Value: "attribute" }],
});

expect(mockUserPoolService.saveUser).toHaveBeenCalledWith(
expect.objectContaining({
UserStatus: "UNCONFIRMED",
})
);
});

it("does not call the PostConfirmation trigger lambda", async () => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);

await signUp({
ClientId: "clientId",
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
});

expect(mockTriggers.postConfirmation).not.toHaveBeenCalled();
});
});

it("verifies the user's email if the lambda returns autoVerifyEmail=true and the user has an email attribute", async () => {
Expand Down
14 changes: 13 additions & 1 deletion src/targets/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,19 @@ export const SignUp =
ConfirmationCode: code,
});

// TODO: call PostConfirmation if PreSignUp confirms auto confirms the user
if (
user.UserStatus === "CONFIRMED" &&
triggers.enabled("PostConfirmation")
) {
await triggers.postConfirmation({
clientId: req.ClientId,
clientMetadata: req.ClientMetadata,
source: "PostConfirmation_ConfirmSignUp",
userAttributes: user.Attributes,
username: user.Username,
userPoolId: userPool.config.Id,
});
}

return {
CodeDeliveryDetails: deliveryDetails ?? undefined,
Expand Down

0 comments on commit ddb2b77

Please sign in to comment.