Search in sources :

Example 96 with APIGatewayProxyResponseEvent

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);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 97 with APIGatewayProxyResponseEvent

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);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 98 with APIGatewayProxyResponseEvent

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);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 99 with APIGatewayProxyResponseEvent

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));
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NotifyRequest(uk.gov.di.accountmanagement.entity.NotifyRequest) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 100 with APIGatewayProxyResponseEvent

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);
}
Also used : UserProfile(uk.gov.di.authentication.shared.entity.UserProfile) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) NotifyRequest(uk.gov.di.accountmanagement.entity.NotifyRequest) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Aggregations

APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)260 Test (org.junit.jupiter.api.Test)214 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)182 HashMap (java.util.HashMap)56 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)30 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)22 URI (java.net.URI)21 NotifyRequest (uk.gov.di.authentication.shared.entity.NotifyRequest)17 UserProfile (uk.gov.di.authentication.shared.entity.UserProfile)17 Map (java.util.Map)16 ClientRegistry (uk.gov.di.authentication.shared.entity.ClientRegistry)14 ClientSession (uk.gov.di.authentication.shared.entity.ClientSession)14 Context (com.amazonaws.services.lambda.runtime.Context)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)13 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)13 NotifyRequest (uk.gov.di.accountmanagement.entity.NotifyRequest)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Subject (com.nimbusds.oauth2.sdk.id.Subject)12 URIBuilder (org.apache.http.client.utils.URIBuilder)11