From 538a81d871c62ca93cab686173a72cfa66d94f6f Mon Sep 17 00:00:00 2001 From: Alessandro Lattao Date: Wed, 21 Oct 2026 12:51:32 +0200 Subject: [PATCH] fix(cognito): pass ClientMetadata to the CustomMessage trigger for ResendCode and ForgotPassword (#6108) * fix(cognito): pass ClientMetadata to the CustomMessage trigger for ResendCode and ForgotPassword The developer guide lists ResendConfirmationCode and ForgotPassword among the calls whose ClientMetadata reaches the CustomMessage trigger, and Floci passed an empty map there. Measured on Cognito, the event's clientMetadata is also null rather than {} when the request carries none, and for a sign-in code (CustomMessage_Authentication) even when InitiateAuth sends one. The CustomMessage event now follows both through one helper, shared with the CustomEmailSender event, which applied the same rule inline, so the two cannot drift. * test(cognito): share one CustomMessage setup across the trigger tests The CustomMessage tests each built the same fixture by hand: the verification code mock, a pool whose CustomMessage trigger is the test function, its client, and the captured invocation. They now share customMessageService, customMessagePool, customMessageClient and customMessageAnswers, the four new tests of this change included, as the review asked. --- .../cognito/CognitoAuthFlowHandler.java | 21 +- .../services/cognito/CognitoService.java | 4 +- .../cognito/CognitoLambdaTriggersTest.java | 251 +++++++++--------- 3 files changed, 138 insertions(+), 138 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java index fc213b671d..69d33796d6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java @@ -1017,7 +1017,7 @@ final class CognitoAuthFlowHandler { VerificationCode.Purpose purpose = "EMAIL_OTP".equals(challengeName) ? VerificationCode.Purpose.EMAIL_OTP : VerificationCode.Purpose.SMS_OTP; Map customMessage = customEmailSenderTakesOver(pool, deliveryMedium) ? Map.of() - : fireCustomMessage(pool, client, user, "CustomMessage_Authentication", Map.of()); + : fireCustomMessage(pool, client, user, "CustomMessage_Authentication", null); Map challengeParams = new HashMap<>( service.issueSignInOtp(pool, client, user, purpose, attributeName, deliveryMedium, customMessage)); challengeParams.put("USERNAME", user.getUsername()); @@ -1873,14 +1873,15 @@ final class CognitoAuthFlowHandler { * Fires the CustomMessage trigger, if configured, so the function can override the * subject/body Cognito would otherwise deliver. Non-blocking: an unconfigured or * failing trigger falls back to the pool's default templated message, mirroring how - * {@link #firePostConfirmation} tolerates errors. + * {@link #firePostConfirmation} tolerates errors. {@code clientMetadata} is the request's + * ClientMetadata; a sign-in code's event carries none (measured, eu-west-1, 2026-10-20). */ Map fireCustomMessage(UserPool pool, UserPoolClient client, CognitoUser user, String triggerSource, Map clientMetadata) { Map req = new HashMap<>(); req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER); req.put("usernameParameter", user.getUsername()); - req.put("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata); + req.put("clientMetadata", requestClientMetadata(clientMetadata)); return customMessageResponse(pool, client, user, triggerSource, req); } @@ -1894,10 +1895,18 @@ final class CognitoAuthFlowHandler { Map req = new HashMap<>(); req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER); req.put("usernameParameter", CUSTOM_MESSAGE_USERNAME_PARAMETER); - req.put("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata); + req.put("clientMetadata", requestClientMetadata(clientMetadata)); return customMessageResponse(pool, null, user, "CustomMessage_AdminCreateUser", req); } + /** + * A custom message or sender event's {@code clientMetadata}: the request's, or null when it + * carries none, as Cognito sends it (measured, eu-west-1, 2026-10-16 and 2026-10-20). + */ + private static Map requestClientMetadata(Map clientMetadata) { + return clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata; + } + private Map customMessageResponse(UserPool pool, UserPoolClient client, CognitoUser user, String triggerSource, Map request) { TriggerResult result = invokeTrigger(pool, client, user, "CustomMessage", triggerSource, request); @@ -1932,7 +1941,7 @@ final class CognitoAuthFlowHandler { Map req = new HashMap<>(); req.put("type", "customEmailSenderRequestV1"); req.put("code", encryptedCode); - req.put("clientMetadata", clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata); + req.put("clientMetadata", requestClientMetadata(clientMetadata)); byte[] payload; try { payload = MAPPER.writeValueAsBytes( @@ -2186,7 +2195,7 @@ final class CognitoAuthFlowHandler { } boolean sms = "SMS_MFA".equals(factor); Map customMessage = customEmailSenderTakesOver(pool, sms ? "SMS" : "EMAIL") ? Map.of() - : fireCustomMessage(pool, client, user, "CustomMessage_Authentication", Map.of()); + : fireCustomMessage(pool, client, user, "CustomMessage_Authentication", null); Map parameters = new HashMap<>(service.issueMfaCode(pool, client, user, sms ? VerificationCode.Purpose.SMS_MFA : VerificationCode.Purpose.EMAIL_MFA, sms ? "phone_number" : "email", sms ? "SMS" : "EMAIL", customMessage)); diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java index 0f933ff08a..bd2bb08d64 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java @@ -3561,7 +3561,7 @@ public class CognitoService implements ResourceProvider { deliveryTarget.deliveryMedium(), "CustomEmailSender_ResendCode", VerificationCode.Purpose.SIGNUP_CONFIRMATION, code, clientMetadata)) { Map customMessage = authFlowHandler.fireCustomMessage( - pool, client, user, "CustomMessage_ResendCode", Map.of()); + pool, client, user, "CustomMessage_ResendCode", clientMetadata); messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.SIGNUP_CONFIRMATION, code, List.of(deliveryTarget.deliveryMedium()), customMessage); } @@ -3979,7 +3979,7 @@ public class CognitoService implements ResourceProvider { deliveryTarget.deliveryMedium(), "CustomEmailSender_ForgotPassword", VerificationCode.Purpose.PASSWORD_RESET, code, clientMetadata)) { Map customMessage = authFlowHandler.fireCustomMessage( - pool, client, user, "CustomMessage_ForgotPassword", Map.of()); + pool, client, user, "CustomMessage_ForgotPassword", clientMetadata); messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.PASSWORD_RESET, code, List.of(deliveryTarget.deliveryMedium()), customMessage); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java index 69bcc77f36..381b95214a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java @@ -2209,25 +2209,46 @@ class CognitoLambdaTriggersTest { // CustomMessage // ========================================================================= + private static final String CUSTOM_MESSAGE = "arn:aws:lambda:::custom-message"; + + /** A service that delivers through {@code ses} and issues {@code code} for every {@code purpose} code. */ + private CognitoService customMessageService(SesService ses, VerificationCode.Purpose purpose, String code) { + VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); + if (purpose != null) { + when(verificationCodeService.issue(any(), any(), eq(purpose), any())).thenReturn(code); + } + return createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService); + } + + /** A pool with {@code settings} whose CustomMessage trigger is {@link #CUSTOM_MESSAGE}. */ + private static UserPool customMessagePool(CognitoService svc, Map settings) { + Map request = new HashMap<>(settings); + request.put("PoolName", "trigger-pool"); + request.put("LambdaConfig", Map.of("CustomMessage", CUSTOM_MESSAGE)); + return svc.createUserPool(request, "us-east-1"); + } + + private static UserPoolClient customMessageClient(CognitoService svc, UserPool pool) { + return svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + } + + /** Answers the CustomMessage function with {@code response} and captures the events it is sent. */ + private ArgumentCaptor customMessageAnswers(Map response) { + ArgumentCaptor events = ArgumentCaptor.forClass(byte[].class); + when(lambdaService.invoke(anyString(), eq(CUSTOM_MESSAGE), events.capture(), + eq(InvocationType.RequestResponse))).thenReturn(ok(response)); + return events; + } + @Test void customMessageOverridesSignUpVerificationEmail() { SesService ses = mock(SesService.class); - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("246962"); - CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "AutoVerifiedAttributes", List.of("email"), - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); - - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - any(byte[].class), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of( - "emailSubject", "TRIGGER-FIRED", - "emailMessage", "TRIGGER-FIRED code={####}"))); + CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); + customMessageAnswers(Map.of( + "emailSubject", "TRIGGER-FIRED", + "emailMessage", "TRIGGER-FIRED code={####}")); svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of()); @@ -2237,26 +2258,15 @@ class CognitoLambdaTriggersTest { @Test @SuppressWarnings("unchecked") void customMessageSignUpTriggerReceivesCodeParameterAndUsername() throws Exception { - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("246962"); - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "AutoVerifiedAttributes", List.of("email"), - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); - - ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(byte[].class); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - payloadCaptor.capture(), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of())); + CognitoService svc = customMessageService(mock(SesService.class), + VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); + ArgumentCaptor events = customMessageAnswers(Map.of()); svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of()); - Map event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {}); + Map event = MAPPER.readValue(events.getValue(), new TypeReference<>() {}); assertEquals("CustomMessage_SignUp", event.get("triggerSource")); Map request = (Map) event.get("request"); assertEquals("{####}", request.get("codeParameter")); @@ -2265,27 +2275,16 @@ class CognitoLambdaTriggersTest { @Test void customMessageSignUpTriggerCarriesClientMetadata() throws Exception { - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("246962"); - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "AutoVerifiedAttributes", List.of("email"), - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); - - ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(byte[].class); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - payloadCaptor.capture(), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of())); + CognitoService svc = customMessageService(mock(SesService.class), + VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); + ArgumentCaptor events = customMessageAnswers(Map.of()); svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of("source", "signup-form")); - JsonNode event = MAPPER.readTree(payloadCaptor.getValue()); + JsonNode event = MAPPER.readTree(events.getValue()); assertEquals("CustomMessage_SignUp", event.path("triggerSource").asText()); assertEquals("signup-form", event.path("request").path("clientMetadata").path("source").asText()); } @@ -2293,18 +2292,10 @@ class CognitoLambdaTriggersTest { @Test void customMessageLambdaErrorFallsBackToDefaultSignUpMessage() { SesService ses = mock(SesService.class); - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("246962"); - CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "AutoVerifiedAttributes", List.of("email"), - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); - - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), any(byte[].class), any())) + CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); + when(lambdaService.invoke(anyString(), eq(CUSTOM_MESSAGE), any(byte[].class), any())) .thenReturn(lambdaError("Unhandled")); // SignUp must still succeed and deliver the default message when CustomMessage errors. @@ -2317,15 +2308,11 @@ class CognitoLambdaTriggersTest { @Test void customMessageDoesNotFireWhenNotConfigured() { SesService ses = mock(SesService.class); - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("246962"); - CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService); - + CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); UserPool pool = svc.createUserPool(Map.of( "PoolName", "trigger-pool", "AutoVerifiedAttributes", List.of("email")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + UserPoolClient client = customMessageClient(svc, pool); svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of()); @@ -2336,72 +2323,85 @@ class CognitoLambdaTriggersTest { @Test @SuppressWarnings("unchecked") - void customMessageFiresWithResendCodeTriggerSourceOnResendConfirmationCode() throws Exception { - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any())) - .thenReturn("111222"); - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "AutoVerifiedAttributes", List.of("email"), - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + void customMessageFiresWithResendCodeTriggerSourceAndClientMetadataOnResendConfirmationCode() throws Exception { + CognitoService svc = customMessageService(mock(SesService.class), + VerificationCode.Purpose.SIGNUP_CONFIRMATION, "111222"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of()); + ArgumentCaptor events = customMessageAnswers(Map.of()); - ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(byte[].class); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - payloadCaptor.capture(), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of())); + svc.resendConfirmationCode(client.getClientId(), "spike", Map.of("source", "resend")); - svc.resendConfirmationCode(client.getClientId(), "spike"); - - Map event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {}); + Map event = MAPPER.readValue(events.getValue(), new TypeReference<>() {}); assertEquals("CustomMessage_ResendCode", event.get("triggerSource")); + assertEquals(Map.of("source", "resend"), ((Map) event.get("request")).get("clientMetadata")); } @Test @SuppressWarnings("unchecked") - void customMessageFiresWithForgotPasswordTriggerSourceOnForgotPassword() throws Exception { - VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class); - when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.PASSWORD_RESET), any())) - .thenReturn("333444"); - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - verificationCodeService); - - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + void customMessageFiresWithForgotPasswordTriggerSourceAndClientMetadataOnForgotPassword() throws Exception { + CognitoService svc = customMessageService(mock(SesService.class), + VerificationCode.Purpose.PASSWORD_RESET, "333444"); + UserPool pool = customMessagePool(svc, Map.of()); + UserPoolClient client = customMessageClient(svc, pool); svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com", "email_verified", "true"), null); svc.adminSetUserPassword(pool.getId(), "spike", "Spike@Test1", true); + ArgumentCaptor events = customMessageAnswers(Map.of()); - ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(byte[].class); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - payloadCaptor.capture(), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of())); + svc.forgotPassword(client.getClientId(), "spike", Map.of("source", "forgot")); - svc.forgotPassword(client.getClientId(), "spike"); - - Map event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {}); + Map event = MAPPER.readValue(events.getValue(), new TypeReference<>() {}); assertEquals("CustomMessage_ForgotPassword", event.get("triggerSource")); + assertEquals(Map.of("source", "forgot"), ((Map) event.get("request")).get("clientMetadata")); + } + + /** Measured (eu-west-1, 2026-10-20): a request without ClientMetadata gives the event a null one, not {}. */ + @Test + void customMessageWithoutClientMetadataCarriesANullOne() throws Exception { + CognitoService svc = customMessageService(mock(SesService.class), + VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962"); + UserPoolClient client = customMessageClient(svc, + customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email")))); + ArgumentCaptor events = customMessageAnswers(Map.of()); + + svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of()); + + JsonNode request = MAPPER.readTree(events.getValue()).path("request"); + assertTrue(request.has("clientMetadata")); + assertTrue(request.path("clientMetadata").isNull()); + } + + /** Measured (eu-west-1, 2026-10-20): a sign-in code's event carries no ClientMetadata, though InitiateAuth sends one. */ + @Test + void customMessageForASignInCodeCarriesANullClientMetadata() throws Exception { + CognitoService svc = customMessageService(mock(SesService.class), VerificationCode.Purpose.EMAIL_OTP, "555666"); + UserPool pool = customMessagePool(svc, Map.of("Policies", Map.of("SignInPolicy", + Map.of("AllowedFirstAuthFactors", List.of("PASSWORD", "EMAIL_OTP"))))); + UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of(), + null, List.of(), null, List.of("ALLOW_USER_AUTH"), null, null, List.of(), null, List.of(), null, + null, null, List.of(), null, null); + svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com", "email_verified", "true"), null); + svc.adminSetUserPassword(pool.getId(), "spike", "Spike@Test1", true); + ArgumentCaptor events = customMessageAnswers(Map.of()); + + svc.initiateAuth(client.getClientId(), "USER_AUTH", + Map.of("USERNAME", "spike", "PREFERRED_CHALLENGE", "EMAIL_OTP"), Map.of("app", "web")); + + JsonNode event = MAPPER.readTree(events.getValue()); + assertEquals("CustomMessage_Authentication", event.path("triggerSource").asText()); + assertTrue(event.path("request").path("clientMetadata").isNull()); } @Test void customMessageOverridesTheAdminCreateUserInvitation() { SesService ses = mock(SesService.class); - CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), - inlineMock(VerificationCodeService.class)); - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - any(byte[].class), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of( - "emailSubject", "Welcome", - "emailMessage", "Sign in as {username} with {####}"))); + CognitoService svc = customMessageService(ses, null, null); + UserPool pool = customMessagePool(svc, Map.of()); + customMessageAnswers(Map.of( + "emailSubject", "Welcome", + "emailMessage", "Sign in as {username} with {####}")); svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com"), "Spike@Test1", null, false, List.of("EMAIL"), Map.of(), Map.of()); @@ -2412,20 +2412,14 @@ class CognitoLambdaTriggersTest { @Test @SuppressWarnings("unchecked") void customMessageAdminCreateUserTriggerReceivesPlaceholdersAndClientMetadata() throws Exception { - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - inlineMock(VerificationCodeService.class)); - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); - ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(byte[].class); - when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), - payloadCaptor.capture(), eq(InvocationType.RequestResponse))) - .thenReturn(ok(Map.of())); + CognitoService svc = customMessageService(mock(SesService.class), null, null); + UserPool pool = customMessagePool(svc, Map.of()); + ArgumentCaptor events = customMessageAnswers(Map.of()); svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com"), "Spike@Test1", null, false, List.of("EMAIL"), Map.of(), Map.of("source", "admin")); - Map event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {}); + Map event = MAPPER.readValue(events.getValue(), new TypeReference<>() {}); assertEquals("CustomMessage_AdminCreateUser", event.get("triggerSource")); assertEquals("CLIENT_ID_NOT_APPLICABLE", ((Map) event.get("callerContext")).get("clientId")); Map request = (Map) event.get("request"); @@ -2436,11 +2430,8 @@ class CognitoLambdaTriggersTest { @Test void customMessageDoesNotFireForAnInvitationWithNowhereToGo() { - CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), - inlineMock(VerificationCodeService.class)); - UserPool pool = svc.createUserPool(Map.of( - "PoolName", "trigger-pool", - "LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1"); + CognitoService svc = customMessageService(mock(SesService.class), null, null); + UserPool pool = customMessagePool(svc, Map.of()); // SMS, the default medium, and no phone number. svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com"), "Spike@Test1");