Search in sources :

Example 1 with SendNotificationRequest

use of uk.gov.di.accountmanagement.entity.SendNotificationRequest in project di-authentication-api by alphagov.

the class SendOtpNotificationIntegrationTest method shouldReturn400ForVerifyEmailRequestWhenUserAlreadyExists.

@Test
void shouldReturn400ForVerifyEmailRequestWhenUserAlreadyExists() throws Exception {
    String password = "password-1";
    userStore.signUp(TEST_EMAIL, password);
    var response = makeRequest(Optional.of(new SendNotificationRequest(TEST_EMAIL, VERIFY_EMAIL, TEST_PHONE_NUMBER)), Collections.emptyMap(), Collections.emptyMap());
    assertThat(response, hasStatus(HttpStatus.SC_BAD_REQUEST));
    assertThat(response, hasBody(new ObjectMapper().writeValueAsString(ErrorResponse.ERROR_1009)));
    NotificationAssertionHelper.assertNoNotificationsReceived(notificationsQueue);
    assertNoAuditEventsReceived(auditTopic);
}
Also used : SendNotificationRequest(uk.gov.di.accountmanagement.entity.SendNotificationRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) ApiGatewayHandlerIntegrationTest(uk.gov.di.authentication.sharedtest.basetest.ApiGatewayHandlerIntegrationTest)

Example 2 with SendNotificationRequest

use of uk.gov.di.accountmanagement.entity.SendNotificationRequest in project di-authentication-api by alphagov.

the class SendOtpNotificationHandler method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    return isWarming(input).orElseGet(() -> {
        String sessionId = RequestHeaderHelper.getHeaderValueOrElse(input.getHeaders(), SESSION_ID_HEADER, "");
        attachSessionIdToLogs(sessionId);
        LOG.info("Request received in SendOtp Lambda");
        try {
            SendNotificationRequest sendNotificationRequest = objectMapper.readValue(input.getBody(), SendNotificationRequest.class);
            switch(sendNotificationRequest.getNotificationType()) {
                case VERIFY_EMAIL:
                    LOG.info("NotificationType is VERIFY_EMAIL");
                    Optional<ErrorResponse> emailErrorResponse = validationService.validateEmailAddress(sendNotificationRequest.getEmail());
                    if (emailErrorResponse.isPresent()) {
                        return generateApiGatewayProxyErrorResponse(400, emailErrorResponse.get());
                    }
                    if (dynamoService.userExists(sendNotificationRequest.getEmail())) {
                        return generateApiGatewayProxyErrorResponse(400, ErrorResponse.ERROR_1009);
                    }
                    return handleNotificationRequest(sendNotificationRequest.getEmail(), sendNotificationRequest, input, context);
                case VERIFY_PHONE_NUMBER:
                    LOG.info("NotificationType is VERIFY_PHONE_NUMBER");
                    Optional<ErrorResponse> phoneNumberValidationError = validationService.validatePhoneNumber(sendNotificationRequest.getPhoneNumber());
                    if (phoneNumberValidationError.isPresent()) {
                        return generateApiGatewayProxyErrorResponse(400, phoneNumberValidationError.get());
                    }
                    return handleNotificationRequest(sendNotificationRequest.getPhoneNumber(), sendNotificationRequest, input, context);
            }
            return generateApiGatewayProxyErrorResponse(400, ERROR_1002);
        } catch (SdkClientException ex) {
            LOG.error("Error sending message to queue", ex);
            return generateApiGatewayProxyResponse(500, "Error sending message to queue");
        } catch (JsonProcessingException e) {
            return generateApiGatewayProxyErrorResponse(400, ERROR_1001);
        }
    });
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) SendNotificationRequest(uk.gov.di.accountmanagement.entity.SendNotificationRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ErrorResponse(uk.gov.di.authentication.shared.entity.ErrorResponse) ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse(uk.gov.di.authentication.shared.helpers.ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse)

Example 3 with SendNotificationRequest

use of uk.gov.di.accountmanagement.entity.SendNotificationRequest in project di-authentication-api by alphagov.

the class SendOtpNotificationHandler method sendOtpRequestHandler.

public APIGatewayProxyResponseEvent sendOtpRequestHandler(APIGatewayProxyRequestEvent input, Context context) {
    return isWarming(input).orElseGet(() -> {
        String sessionId = RequestHeaderHelper.getHeaderValueOrElse(input.getHeaders(), SESSION_ID_HEADER, "");
        attachSessionIdToLogs(sessionId);
        LOG.info("Request received in SendOtp Lambda");
        try {
            SendNotificationRequest sendNotificationRequest = objectMapper.readValue(input.getBody(), SendNotificationRequest.class);
            switch(sendNotificationRequest.getNotificationType()) {
                case VERIFY_EMAIL:
                    LOG.info("NotificationType is VERIFY_EMAIL");
                    Optional<ErrorResponse> emailErrorResponse = ValidationHelper.validateEmailAddress(sendNotificationRequest.getEmail());
                    if (emailErrorResponse.isPresent()) {
                        return generateApiGatewayProxyErrorResponse(400, emailErrorResponse.get());
                    }
                    if (dynamoService.userExists(sendNotificationRequest.getEmail())) {
                        return generateApiGatewayProxyErrorResponse(400, ErrorResponse.ERROR_1009);
                    }
                    return handleNotificationRequest(sendNotificationRequest.getEmail(), sendNotificationRequest, input, context);
                case VERIFY_PHONE_NUMBER:
                    LOG.info("NotificationType is VERIFY_PHONE_NUMBER");
                    Optional<ErrorResponse> phoneNumberValidationError = ValidationHelper.validatePhoneNumber(sendNotificationRequest.getPhoneNumber());
                    if (phoneNumberValidationError.isPresent()) {
                        return generateApiGatewayProxyErrorResponse(400, phoneNumberValidationError.get());
                    }
                    return handleNotificationRequest(sendNotificationRequest.getPhoneNumber(), sendNotificationRequest, input, context);
            }
            return generateApiGatewayProxyErrorResponse(400, ERROR_1002);
        } catch (SdkClientException ex) {
            LOG.error("Error sending message to queue", ex);
            return generateApiGatewayProxyResponse(500, "Error sending message to queue");
        } catch (JsonException e) {
            return generateApiGatewayProxyErrorResponse(400, ERROR_1001);
        }
    });
}
Also used : JsonException(uk.gov.di.authentication.shared.serialization.Json.JsonException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) SendNotificationRequest(uk.gov.di.accountmanagement.entity.SendNotificationRequest) ErrorResponse(uk.gov.di.authentication.shared.entity.ErrorResponse) ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse(uk.gov.di.authentication.shared.helpers.ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse)

Aggregations

SendNotificationRequest (uk.gov.di.accountmanagement.entity.SendNotificationRequest)3 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 ErrorResponse (uk.gov.di.authentication.shared.entity.ErrorResponse)2 ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse (uk.gov.di.authentication.shared.helpers.ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Test (org.junit.jupiter.api.Test)1 JsonException (uk.gov.di.authentication.shared.serialization.Json.JsonException)1 ApiGatewayHandlerIntegrationTest (uk.gov.di.authentication.sharedtest.basetest.ApiGatewayHandlerIntegrationTest)1