-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): post confirmation lambda trigger
- Loading branch information
Showing
13 changed files
with
359 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { NotAuthorizedError } from "../../errors"; | ||
import { Lambda } from "../lambda"; | ||
import { UserPool } from "../userPool"; | ||
import { PostConfirmation, PostConfirmationTrigger } from "./postConfirmation"; | ||
|
||
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; | ||
|
||
describe("PostConfirmation trigger", () => { | ||
let mockLambda: jest.Mocked<Lambda>; | ||
let mockUserPool: jest.Mocked<UserPool>; | ||
let postConfirmation: PostConfirmationTrigger; | ||
|
||
beforeEach(() => { | ||
mockLambda = { | ||
enabled: jest.fn(), | ||
invoke: jest.fn(), | ||
}; | ||
mockUserPool = { | ||
getUserByUsername: jest.fn(), | ||
getUserPoolIdForClientId: jest.fn(), | ||
saveUser: jest.fn(), | ||
}; | ||
|
||
postConfirmation = PostConfirmation({ | ||
lambda: mockLambda, | ||
userPool: mockUserPool, | ||
}); | ||
}); | ||
|
||
describe.each([ | ||
"PostConfirmation_ConfirmSignUp", | ||
"PostConfirmation_ConfirmForgotPassword", | ||
])("%s", (source) => { | ||
describe("when lambda invoke fails", () => { | ||
it("quietly completes", async () => { | ||
mockLambda.invoke.mockRejectedValue( | ||
new Error("Something bad happened") | ||
); | ||
|
||
await postConfirmation({ | ||
userPoolId: "userPoolId", | ||
clientId: "clientId", | ||
username: "username", | ||
userAttributes: [], | ||
source: source as any, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when lambda invoke succeeds", () => { | ||
it("quietly completes", async () => { | ||
mockLambda.invoke.mockResolvedValue({}); | ||
|
||
await postConfirmation({ | ||
userPoolId: "userPoolId", | ||
clientId: "clientId", | ||
username: "[email protected]", | ||
userAttributes: [{ Name: "email", Value: "[email protected]" }], | ||
source: source as any, | ||
}); | ||
|
||
expect(mockLambda.invoke).toHaveBeenCalledWith("PostConfirmation", { | ||
clientId: "clientId", | ||
triggerSource: source, | ||
userAttributes: { email: "[email protected]" }, | ||
userPoolId: "userPoolId", | ||
username: "[email protected]", | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { UserPool } from "../index"; | ||
import { Lambda } from "../lambda"; | ||
import { attributesToRecord } from "../userPool"; | ||
|
||
export type PostConfirmationTrigger = (params: { | ||
source: | ||
| "PostConfirmation_ConfirmSignUp" | ||
| "PostConfirmation_ConfirmForgotPassword"; | ||
userPoolId: string; | ||
clientId: string; | ||
username: string; | ||
userAttributes: readonly { Name: string; Value: string }[]; | ||
}) => Promise<void>; | ||
|
||
export const PostConfirmation = ({ | ||
lambda, | ||
}: { | ||
lambda: Lambda; | ||
userPool: UserPool; | ||
}): PostConfirmationTrigger => async ({ | ||
source, | ||
userPoolId, | ||
clientId, | ||
username, | ||
userAttributes, | ||
}): Promise<void> => { | ||
try { | ||
await lambda.invoke("PostConfirmation", { | ||
userPoolId, | ||
clientId, | ||
username, | ||
triggerSource: source, | ||
userAttributes: attributesToRecord(userAttributes), | ||
}); | ||
} catch (ex) { | ||
console.error(ex); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { advanceBy, advanceTo } from "jest-date-mock"; | ||
import { CodeMismatchError, UserNotFoundError } from "../errors"; | ||
import { | ||
CodeMismatchError, | ||
ResourceNotFoundError, | ||
UserNotFoundError, | ||
} from "../errors"; | ||
import { UserPool } from "../services"; | ||
import { Triggers } from "../services/triggers"; | ||
import { | ||
|
@@ -26,6 +30,7 @@ describe("ConfirmForgotPassword target", () => { | |
mockCodeDelivery = jest.fn(); | ||
mockTriggers = { | ||
enabled: jest.fn(), | ||
postConfirmation: jest.fn(), | ||
userMigration: jest.fn(), | ||
}; | ||
|
||
|
@@ -36,7 +41,21 @@ describe("ConfirmForgotPassword target", () => { | |
}); | ||
}); | ||
|
||
it("throws if can't find user pool by client id", async () => { | ||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue(null); | ||
|
||
await expect( | ||
confirmForgotPassword({ | ||
ClientId: "clientId", | ||
Username: "janice", | ||
ConfirmationCode: "1234", | ||
Password: "newPassword", | ||
}) | ||
).rejects.toBeInstanceOf(ResourceNotFoundError); | ||
}); | ||
|
||
it("throws if user doesn't exist", async () => { | ||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue("userPoolId"); | ||
mockDataStore.getUserByUsername.mockResolvedValue(null); | ||
|
||
await expect( | ||
|
@@ -50,6 +69,7 @@ describe("ConfirmForgotPassword target", () => { | |
}); | ||
|
||
it("throws if confirmation code doesn't match stored value", async () => { | ||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue("userPoolId"); | ||
mockDataStore.getUserByUsername.mockResolvedValue({ | ||
Attributes: [{ Name: "email", Value: "[email protected]" }], | ||
ConfirmationCode: "4567", | ||
|
@@ -73,6 +93,7 @@ describe("ConfirmForgotPassword target", () => { | |
|
||
describe("when code matches", () => { | ||
it("updates the user's password", async () => { | ||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue("userPoolId"); | ||
mockDataStore.getUserByUsername.mockResolvedValue({ | ||
Attributes: [{ Name: "email", Value: "[email protected]" }], | ||
ConfirmationCode: "4567", | ||
|
@@ -106,5 +127,70 @@ describe("ConfirmForgotPassword target", () => { | |
Username: "0000-0000", | ||
}); | ||
}); | ||
|
||
describe("when PostConfirmation trigger configured", () => { | ||
it("invokes the trigger", async () => { | ||
mockTriggers.enabled.mockReturnValue(true); | ||
|
||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue("userPoolId"); | ||
mockDataStore.getUserByUsername.mockResolvedValue({ | ||
Attributes: [{ Name: "email", Value: "[email protected]" }], | ||
ConfirmationCode: "4567", | ||
Enabled: true, | ||
Password: "pwd", | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
UserStatus: "UNCONFIRMED", | ||
Username: "0000-0000", | ||
}); | ||
|
||
await confirmForgotPassword({ | ||
ClientId: "clientId", | ||
Username: "janice", | ||
ConfirmationCode: "4567", | ||
Password: "newPassword", | ||
}); | ||
|
||
expect(mockTriggers.postConfirmation).toHaveBeenCalledWith({ | ||
clientId: "clientId", | ||
source: "PostConfirmation_ConfirmForgotPassword", | ||
userAttributes: [ | ||
{ | ||
Name: "email", | ||
Value: "[email protected]", | ||
}, | ||
], | ||
userPoolId: "userPoolId", | ||
username: "0000-0000", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when PostConfirmation trigger not configured", () => { | ||
it("doesn't invoke the trigger", async () => { | ||
mockTriggers.enabled.mockReturnValue(false); | ||
|
||
mockDataStore.getUserPoolIdForClientId.mockResolvedValue("userPoolId"); | ||
mockDataStore.getUserByUsername.mockResolvedValue({ | ||
Attributes: [{ Name: "email", Value: "[email protected]" }], | ||
ConfirmationCode: "4567", | ||
Enabled: true, | ||
Password: "pwd", | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
UserStatus: "UNCONFIRMED", | ||
Username: "0000-0000", | ||
}); | ||
|
||
await confirmForgotPassword({ | ||
ClientId: "clientId", | ||
Username: "janice", | ||
ConfirmationCode: "4567", | ||
Password: "newPassword", | ||
}); | ||
|
||
expect(mockTriggers.postConfirmation).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.