use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-authentication-api by alphagov.
the class SendOtpNotificationHandlerTest method shouldReturn400WhenAccountAlreadyExistsWithGivenEmail.
@Test
void shouldReturn400WhenAccountAlreadyExistsWithGivenEmail() {
when(dynamoService.userExists(eq(TEST_EMAIL_ADDRESS))).thenReturn(true);
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of());
event.setBody(format("{ \"email\": \"%s\", \"notificationType\": \"%s\" }", TEST_EMAIL_ADDRESS, VERIFY_EMAIL));
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertEquals(400, result.getStatusCode());
assertThat(result, hasJsonBody(ErrorResponse.ERROR_1009));
verifyNoInteractions(auditService);
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-authentication-api by alphagov.
the class SendOtpNotificationHandlerTest method shouldReturn400IfRequestIsMissingEmail.
@Test
void shouldReturn400IfRequestIsMissingEmail() {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of());
event.setBody("{ }");
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertEquals(400, result.getStatusCode());
assertThat(result, hasJsonBody(ErrorResponse.ERROR_1001));
verifyNoInteractions(auditService);
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-authentication-api by alphagov.
the class SendOtpNotificationHandlerTest method shouldReturn400IfPhoneNumberIsInvalid.
@Test
void shouldReturn400IfPhoneNumberIsInvalid() {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of());
event.setBody(format("{ \"email\": \"%s\", \"notificationType\": \"%s\", \"phoneNumber\": \"%s\" }", TEST_EMAIL_ADDRESS, VERIFY_PHONE_NUMBER, "12345"));
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertEquals(400, result.getStatusCode());
assertThat(result, hasJsonBody(ErrorResponse.ERROR_1012));
verifyNoInteractions(auditService);
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-authentication-api by alphagov.
the class SendOtpNotificationHandlerTest method shouldReturn204AndPutMessageOnQueueForAValidEmailRequest.
@Test
void shouldReturn204AndPutMessageOnQueueForAValidEmailRequest() throws Json.JsonException {
String persistentIdValue = "some-persistent-session-id";
NotifyRequest notifyRequest = new NotifyRequest(TEST_EMAIL_ADDRESS, VERIFY_EMAIL, TEST_SIX_DIGIT_CODE);
String serialisedRequest = objectMapper.writeValueAsString(notifyRequest);
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of(PersistentIdHelper.PERSISTENT_ID_HEADER_NAME, persistentIdValue));
event.setRequestContext(contextWithSourceIp("123.123.123.123"));
event.setBody(format("{ \"email\": \"%s\", \"notificationType\": \"%s\" }", TEST_EMAIL_ADDRESS, VERIFY_EMAIL));
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertEquals(204, result.getStatusCode());
verify(awsSqsClient).send(serialisedRequest);
verify(codeStorageService).saveOtpCode(TEST_EMAIL_ADDRESS, TEST_SIX_DIGIT_CODE, CODE_EXPIRY_TIME, VERIFY_EMAIL);
verify(auditService).submitAuditEvent(AccountManagementAuditableEvent.SEND_OTP, context.getAwsRequestId(), AuditService.UNKNOWN, AuditService.UNKNOWN, AuditService.UNKNOWN, TEST_EMAIL_ADDRESS, "123.123.123.123", null, persistentIdValue, pair("notification-type", VERIFY_EMAIL));
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-authentication-api by alphagov.
the class UpdateEmailHandlerTest method shouldReturn204ForValidUpdateEmailRequest.
@Test
void shouldReturn204ForValidUpdateEmailRequest() throws Json.JsonException {
String persistentIdValue = "some-persistent-session-id";
UserProfile userProfile = new UserProfile().setPublicSubjectID(SUBJECT.getValue());
when(dynamoService.getUserProfileByEmail(EXISTING_EMAIL_ADDRESS)).thenReturn(userProfile);
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setBody(format("{\"existingEmailAddress\": \"%s\", \"replacementEmailAddress\": \"%s\", \"otp\": \"%s\" }", EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS, OTP));
event.setHeaders(Map.of(PersistentIdHelper.PERSISTENT_ID_HEADER_NAME, persistentIdValue));
APIGatewayProxyRequestEvent.ProxyRequestContext proxyRequestContext = new APIGatewayProxyRequestEvent.ProxyRequestContext();
Map<String, Object> authorizerParams = new HashMap<>();
authorizerParams.put("principalId", SUBJECT.getValue());
proxyRequestContext.setIdentity(identityWithSourceIp("123.123.123.123"));
proxyRequestContext.setAuthorizer(authorizerParams);
event.setRequestContext(proxyRequestContext);
when(codeStorageService.isValidOtpCode(NEW_EMAIL_ADDRESS, OTP, VERIFY_EMAIL)).thenReturn(true);
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertThat(result, hasStatus(204));
verify(dynamoService).updateEmail(EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS);
NotifyRequest notifyRequest = new NotifyRequest(NEW_EMAIL_ADDRESS, EMAIL_UPDATED);
verify(sqsClient).send(objectMapper.writeValueAsString(notifyRequest));
verify(auditService).submitAuditEvent(AccountManagementAuditableEvent.UPDATE_EMAIL, context.getAwsRequestId(), AuditService.UNKNOWN, AuditService.UNKNOWN, userProfile.getSubjectID(), NEW_EMAIL_ADDRESS, "123.123.123.123", userProfile.getPhoneNumber(), persistentIdValue);
}
Aggregations