diff --git a/src/__tests__/mockMessages.ts b/src/__tests__/mockMessages.ts index 64260274..745ffc9a 100644 --- a/src/__tests__/mockMessages.ts +++ b/src/__tests__/mockMessages.ts @@ -1,8 +1,5 @@ import { Messages } from "../services"; export const newMockMessages = (): jest.Mocked => ({ - adminCreateUser: jest.fn(), - authentication: jest.fn(), - forgotPassword: jest.fn(), - signUp: jest.fn(), + create: jest.fn(), }); diff --git a/src/services/messages.test.ts b/src/services/messages.test.ts index e544edb6..19d6a9bc 100644 --- a/src/services/messages.test.ts +++ b/src/services/messages.test.ts @@ -27,8 +27,9 @@ describe("messages service", () => { }); const messages = new MessagesService(mockTriggers); - const message = await messages.authentication( + const message = await messages.create( TestContext, + "Authentication", "clientId", "userPoolId", user, @@ -67,8 +68,9 @@ describe("messages service", () => { mockTriggers.customMessage.mockResolvedValue(null); const messages = new MessagesService(mockTriggers); - const message = await messages.authentication( + const message = await messages.create( TestContext, + "Authentication", "clientId", "userPoolId", user, @@ -102,8 +104,9 @@ describe("messages service", () => { mockTriggers.enabled.mockReturnValue(false); const messages = new MessagesService(mockTriggers); - const message = await messages.authentication( + const message = await messages.create( TestContext, + "Authentication", "clientId", "userPoolId", user, @@ -136,8 +139,9 @@ describe("messages service", () => { }); const messages = new MessagesService(mockTriggers); - const message = await messages.forgotPassword( + const message = await messages.create( TestContext, + "ForgotPassword", "clientId", "userPoolId", user, @@ -176,8 +180,9 @@ describe("messages service", () => { mockTriggers.customMessage.mockResolvedValue(null); const messages = new MessagesService(mockTriggers); - const message = await messages.forgotPassword( + const message = await messages.create( TestContext, + "ForgotPassword", "clientId", "userPoolId", user, @@ -211,8 +216,9 @@ describe("messages service", () => { mockTriggers.enabled.mockReturnValue(false); const messages = new MessagesService(mockTriggers); - const message = await messages.forgotPassword( + const message = await messages.create( TestContext, + "ForgotPassword", "clientId", "userPoolId", user, @@ -245,8 +251,9 @@ describe("messages service", () => { }); const messages = new MessagesService(mockTriggers); - const message = await messages.signUp( + const message = await messages.create( TestContext, + "SignUp", "clientId", "userPoolId", user, @@ -285,8 +292,9 @@ describe("messages service", () => { mockTriggers.customMessage.mockResolvedValue(null); const messages = new MessagesService(mockTriggers); - const message = await messages.signUp( + const message = await messages.create( TestContext, + "SignUp", "clientId", "userPoolId", user, @@ -320,8 +328,9 @@ describe("messages service", () => { mockTriggers.enabled.mockReturnValue(false); const messages = new MessagesService(mockTriggers); - const message = await messages.signUp( + const message = await messages.create( TestContext, + "SignUp", "clientId", "userPoolId", user, diff --git a/src/services/messages.ts b/src/services/messages.ts index c139c6f7..25bf11a3 100644 --- a/src/services/messages.ts +++ b/src/services/messages.ts @@ -11,33 +11,20 @@ export interface Message { smsMessage?: string; } +type MessageSource = + | "AdminCreateUser" + | "Authentication" + | "ForgotPassword" + | "ResendCode" + | "SignUp" + | "UpdateUserAttribute" + | "VerifyUserAttribute"; + export interface Messages { - adminCreateUser( - ctx: Context, - userPoolId: string, - user: User, - temporaryPassword: string, - clientMetadata: Record | undefined - ): Promise; - authentication( - ctx: Context, - clientId: string, - userPoolId: string, - user: User, - code: string, - clientMetadata: Record | undefined - ): Promise; - forgotPassword( + create( ctx: Context, - clientId: string, - userPoolId: string, - user: User, - code: string, - clientMetadata: Record | undefined - ): Promise; - signUp( - ctx: Context, - clientId: string, + source: MessageSource, + clientId: string | null, userPoolId: string, user: User, code: string, @@ -52,101 +39,10 @@ export class MessagesService implements Messages { this.triggers = triggers; } - public async adminCreateUser( - ctx: Context, - userPoolId: string, - user: User, - temporaryPassword: string, - clientMetadata: Record | undefined - ): Promise { - if (this.triggers.enabled("CustomMessage")) { - const message = await this.triggers.customMessage(ctx, { - clientId: AWS_ADMIN_CLIENT_ID, - clientMetadata, - 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( - ctx: Context, - clientId: string, - userPoolId: string, - user: User, - code: string, - clientMetadata: Record | undefined - ): Promise { - if (this.triggers.enabled("CustomMessage")) { - const message = await this.triggers.customMessage(ctx, { - clientId, - clientMetadata, - code, - source: "CustomMessage_Authentication", - userAttributes: user.Attributes, - username: user.Username, - userPoolId, - }); - - return { - __code: code, - ...message, - }; - } - - // TODO: What should the default message be? - return { - __code: code, - }; - } - - public async forgotPassword( - ctx: Context, - clientId: string, - userPoolId: string, - user: User, - code: string, - clientMetadata: Record | undefined - ): Promise { - if (this.triggers.enabled("CustomMessage")) { - const message = await this.triggers.customMessage(ctx, { - clientId, - clientMetadata, - code, - source: "CustomMessage_ForgotPassword", - userAttributes: user.Attributes, - username: user.Username, - userPoolId, - }); - - return { - __code: code, - ...message, - }; - } - - // TODO: What should the default message be? - return { - __code: code, - }; - } - - public async signUp( + public async create( ctx: Context, - clientId: string, + source: MessageSource, + clientId: string | null, userPoolId: string, user: User, code: string, @@ -154,10 +50,10 @@ export class MessagesService implements Messages { ): Promise { if (this.triggers.enabled("CustomMessage")) { const message = await this.triggers.customMessage(ctx, { - clientId, + clientId: clientId ?? AWS_ADMIN_CLIENT_ID, clientMetadata, code, - source: "CustomMessage_SignUp", + source: `CustomMessage_${source}`, userAttributes: user.Attributes, username: user.Username, userPoolId, diff --git a/src/targets/adminCreateUser.test.ts b/src/targets/adminCreateUser.test.ts index 08048ddb..fa176495 100644 --- a/src/targets/adminCreateUser.test.ts +++ b/src/targets/adminCreateUser.test.ts @@ -92,7 +92,7 @@ describe("AdminCreateUser target", () => { describe("messages", () => { describe("DesiredDeliveryMediums=EMAIL", () => { it("sends a welcome email to the user", async () => { - mockMessages.adminCreateUser.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ emailMessage: "email message", emailSubject: "email subject", }); @@ -108,8 +108,10 @@ describe("AdminCreateUser target", () => { UserPoolId: "test", }); - expect(mockMessages.adminCreateUser).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "AdminCreateUser", + null, "test", { ...response.User, Password: "pwd", RefreshTokens: [] }, "pwd", @@ -148,14 +150,14 @@ describe("AdminCreateUser target", () => { ) ); - expect(mockMessages.adminCreateUser).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); describe("DesiredDeliveryMediums=SMS", () => { it("sends a welcome sms to the user", async () => { - mockMessages.adminCreateUser.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ smsMessage: "sms message", }); @@ -170,8 +172,10 @@ describe("AdminCreateUser target", () => { UserPoolId: "test", }); - expect(mockMessages.adminCreateUser).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "AdminCreateUser", + null, "test", { ...response.User, Password: "pwd", RefreshTokens: [] }, "pwd", @@ -210,14 +214,14 @@ describe("AdminCreateUser target", () => { ) ); - expect(mockMessages.adminCreateUser).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); describe("DesiredDeliveryMediums=default", () => { it("sends a welcome sms to the user", async () => { - mockMessages.adminCreateUser.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ smsMessage: "sms message", }); @@ -231,8 +235,10 @@ describe("AdminCreateUser target", () => { UserPoolId: "test", }); - expect(mockMessages.adminCreateUser).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "AdminCreateUser", + null, "test", { ...response.User, Password: "pwd", RefreshTokens: [] }, "pwd", @@ -273,14 +279,14 @@ describe("AdminCreateUser target", () => { ) ); - expect(mockMessages.adminCreateUser).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); describe("DesiredDeliveryMediums=EMAIL and SMS", () => { it("sends a welcome sms to a user with a phone_number and an email", async () => { - mockMessages.adminCreateUser.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ smsMessage: "sms message", }); @@ -298,8 +304,10 @@ describe("AdminCreateUser target", () => { UserPoolId: "test", }); - expect(mockMessages.adminCreateUser).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "AdminCreateUser", + null, "test", { ...response.User, Password: "pwd", RefreshTokens: [] }, "pwd", @@ -324,7 +332,7 @@ describe("AdminCreateUser target", () => { }); it("sends a welcome email to a user without a phone_number but with an email", async () => { - mockMessages.adminCreateUser.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ emailMessage: "email message", emailSubject: "email subject", }); @@ -340,8 +348,10 @@ describe("AdminCreateUser target", () => { UserPoolId: "test", }); - expect(mockMessages.adminCreateUser).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "AdminCreateUser", + null, "test", { ...response.User, Password: "pwd", RefreshTokens: [] }, "pwd", @@ -380,7 +390,7 @@ describe("AdminCreateUser target", () => { ) ); - expect(mockMessages.adminCreateUser).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); diff --git a/src/targets/adminCreateUser.ts b/src/targets/adminCreateUser.ts index 9b98540f..bb261f73 100644 --- a/src/targets/adminCreateUser.ts +++ b/src/targets/adminCreateUser.ts @@ -87,8 +87,10 @@ const deliverWelcomeMessage = async ( ); } - const message = await messages.adminCreateUser( + const message = await messages.create( ctx, + "AdminCreateUser", + null, userPool.config.Id, user, temporaryPassword, diff --git a/src/targets/forgotPassword.test.ts b/src/targets/forgotPassword.test.ts index 6638d912..e817b7c4 100644 --- a/src/targets/forgotPassword.test.ts +++ b/src/targets/forgotPassword.test.ts @@ -23,7 +23,7 @@ describe("ForgotPassword target", () => { mockUserPoolService = newMockUserPoolService(); mockMessageDelivery = newMockMessageDelivery(); mockMessages = newMockMessages(); - mockMessages.forgotPassword.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ emailSubject: "Mock message", }); mockOtp = jest.fn().mockReturnValue("1234"); diff --git a/src/targets/forgotPassword.ts b/src/targets/forgotPassword.ts index 2b397b00..ce70c3ec 100644 --- a/src/targets/forgotPassword.ts +++ b/src/targets/forgotPassword.ts @@ -47,8 +47,9 @@ export const ForgotPassword = }; const code = otp(); - const message = await messages.forgotPassword( + const message = await messages.create( ctx, + "ForgotPassword", req.ClientId, userPool.config.Id, user, diff --git a/src/targets/initiateAuth.test.ts b/src/targets/initiateAuth.test.ts index f24af138..592451ad 100644 --- a/src/targets/initiateAuth.test.ts +++ b/src/targets/initiateAuth.test.ts @@ -36,7 +36,7 @@ describe("InitiateAuth target", () => { mockUserPoolService = newMockUserPoolService(); mockMessageDelivery = newMockMessageDelivery(); mockMessages = newMockMessages(); - mockMessages.authentication.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ emailSubject: "Mock message", }); mockOtp = jest.fn().mockReturnValue("1234"); @@ -304,8 +304,9 @@ describe("InitiateAuth target", () => { expect(output).toBeDefined(); - expect(mockMessages.authentication).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "Authentication", "clientId", "test", user, diff --git a/src/targets/initiateAuth.ts b/src/targets/initiateAuth.ts index 3cdf5dcd..e2d8a9bc 100644 --- a/src/targets/initiateAuth.ts +++ b/src/targets/initiateAuth.ts @@ -62,8 +62,9 @@ const verifyMfaChallenge = async ( } const code = services.otp(); - const message = await services.messages.authentication( + const message = await services.messages.create( ctx, + "Authentication", req.ClientId, userPool.config.Id, user, diff --git a/src/targets/signUp.test.ts b/src/targets/signUp.test.ts index 8fd8633e..955845da 100644 --- a/src/targets/signUp.test.ts +++ b/src/targets/signUp.test.ts @@ -35,7 +35,7 @@ describe("SignUp target", () => { mockUserPoolService = newMockUserPoolService(); mockMessageDelivery = newMockMessageDelivery(); mockMessages = newMockMessages(); - mockMessages.signUp.mockResolvedValue({ + mockMessages.create.mockResolvedValue({ emailSubject: "Mock message", }); mockOtp = jest.fn(); @@ -478,8 +478,9 @@ describe("SignUp target", () => { RefreshTokens: [], }; - expect(mockMessages.signUp).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "SignUp", "clientId", "test", createdUser, @@ -514,7 +515,7 @@ describe("SignUp target", () => { ) ); - expect(mockMessages.signUp).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); @@ -552,8 +553,9 @@ describe("SignUp target", () => { RefreshTokens: [], }; - expect(mockMessages.signUp).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "SignUp", "clientId", "test", createdUser, @@ -588,7 +590,7 @@ describe("SignUp target", () => { ) ); - expect(mockMessages.signUp).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); @@ -633,8 +635,9 @@ describe("SignUp target", () => { RefreshTokens: [], }; - expect(mockMessages.signUp).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "SignUp", "clientId", "test", createdUser, @@ -683,8 +686,9 @@ describe("SignUp target", () => { RefreshTokens: [], }; - expect(mockMessages.signUp).toHaveBeenCalledWith( + expect(mockMessages.create).toHaveBeenCalledWith( TestContext, + "SignUp", "clientId", "test", createdUser, @@ -719,7 +723,7 @@ describe("SignUp target", () => { ) ); - expect(mockMessages.signUp).not.toHaveBeenCalled(); + expect(mockMessages.create).not.toHaveBeenCalled(); expect(mockMessageDelivery.deliver).not.toHaveBeenCalled(); }); }); diff --git a/src/targets/signUp.ts b/src/targets/signUp.ts index b5bc81ee..fcb9ef22 100644 --- a/src/targets/signUp.ts +++ b/src/targets/signUp.ts @@ -83,8 +83,9 @@ const deliverWelcomeMessage = async ( ); } - const message = await messages.signUp( + const message = await messages.create( ctx, + "SignUp", clientId, userPool.config.Id, user,