Search in sources :

Example 31 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.

the class IPVCallbackHandlerTest method shouldThrowWhenSessionIsNotFoundInRedis.

@Test
void shouldThrowWhenSessionIsNotFoundInRedis() {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    event.setQueryStringParameters(Collections.emptyMap());
    event.setHeaders(Map.of(COOKIE, buildCookieString()));
    RuntimeException expectedException = assertThrows(RuntimeException.class, () -> handler.handleRequest(event, context), "Expected to throw exception");
    assertThat(expectedException.getMessage(), containsString("Session not found"));
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Test(org.junit.jupiter.api.Test)

Example 32 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.

the class TokenHandlerTest method generateApiGatewayRequest.

private APIGatewayProxyResponseEvent generateApiGatewayRequest(PrivateKeyJWT privateKeyJWT, String authorisationCode, String redirectUri) {
    Map<String, List<String>> customParams = new HashMap<>();
    customParams.put("grant_type", Collections.singletonList(GrantType.AUTHORIZATION_CODE.getValue()));
    customParams.put("client_id", Collections.singletonList(CLIENT_ID));
    customParams.put("code", Collections.singletonList(authorisationCode));
    customParams.put("redirect_uri", Collections.singletonList(redirectUri));
    Map<String, List<String>> privateKeyParams = privateKeyJWT.toParameters();
    privateKeyParams.putAll(customParams);
    String requestParams = URLUtils.serializeParameters(privateKeyParams);
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    event.setBody(requestParams);
    return handler.handleRequest(event, context);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 33 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.

the class IPVAuthorisationHandlerTest method shouldReturn200AndRedirectURIWithClaims.

@Test
void shouldReturn200AndRedirectURIWithClaims() throws JsonProcessingException, UnsupportedEncodingException {
    usingValidSession();
    usingValidClientSession(TEST_CLIENT_ID);
    Map<String, String> headers = new HashMap<>();
    headers.put(PersistentIdHelper.PERSISTENT_ID_HEADER_NAME, PERSISTENT_SESSION_ID);
    headers.put("Session-Id", session.getSessionId());
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    event.setHeaders(headers);
    event.setBody(format("{ \"email\": \"%s\"}", TEST_EMAIL_ADDRESS));
    event.setRequestContext(contextWithSourceIp("123.123.123.123"));
    APIGatewayProxyResponseEvent response = makeHandlerRequest(event);
    assertThat(response, hasStatus(200));
    IPVAuthorisationResponse body = new ObjectMapper().readValue(response.getBody(), IPVAuthorisationResponse.class);
    assertThat(body.getRedirectUri(), startsWith(IPV_AUTHORISATION_URI + "/authorize"));
    assertThat(splitQuery(body.getRedirectUri()).get("claims"), equalTo(claimsSetRequest.toJSONString()));
    verify(authorisationService).storeState(eq(session.getSessionId()), any(State.class));
}
Also used : IPVAuthorisationResponse(uk.gov.di.authentication.ipv.entity.IPVAuthorisationResponse) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) State(com.nimbusds.oauth2.sdk.id.State) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 34 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.

the class CheckUserExistsHandlerTest method shouldReturn200IfUserTransitionsFromUserNotFoundAndUserDoesNotExist.

@Test
public void shouldReturn200IfUserTransitionsFromUserNotFoundAndUserDoesNotExist() throws JsonProcessingException {
    usingValidSession();
    when(authenticationService.userExists(eq("joe.bloggs"))).thenReturn(false);
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    event.setBody("{ \"email\": \"joe.bloggs\" }");
    event.setHeaders(Map.of("Session-Id", session.getSessionId()));
    event.setRequestContext(contextWithSourceIp("123.123.123.123"));
    APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
    assertEquals(200, result.getStatusCode());
    CheckUserExistsResponse checkUserExistsResponse = objectMapper.readValue(result.getBody(), CheckUserExistsResponse.class);
    assertEquals("joe.bloggs", checkUserExistsResponse.getEmail());
    assertFalse(checkUserExistsResponse.doesUserExist());
    verify(auditService).submitAuditEvent(FrontendAuditableEvent.CHECK_USER_NO_ACCOUNT_WITH_EMAIL, "aws-session-id", session.getSessionId(), "", auditService.UNKNOWN, "joe.bloggs", "123.123.123.123", AuditService.UNKNOWN, PersistentIdHelper.PERSISTENT_ID_UNKNOWN_VALUE);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) CheckUserExistsResponse(uk.gov.di.authentication.frontendapi.entity.CheckUserExistsResponse) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 35 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.

the class SendNotificationHandlerTest method shouldReturn400WhenPhoneNumberIsInvalid.

@Test
void shouldReturn400WhenPhoneNumberIsInvalid() {
    when(validationService.validatePhoneNumber(eq("123456789"))).thenReturn(Optional.of(ErrorResponse.ERROR_1012));
    usingValidSession();
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    event.setHeaders(Map.of("Session-Id", session.getSessionId()));
    event.setBody(format("{ \"email\": \"%s\", \"notificationType\": \"%s\", \"phoneNumber\": \"%s\" }", TEST_EMAIL_ADDRESS, VERIFY_PHONE_NUMBER, "123456789"));
    APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
    assertEquals(400, result.getStatusCode());
    assertThat(result, hasJsonBody(ErrorResponse.ERROR_1012));
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Aggregations

APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)239 Test (org.junit.jupiter.api.Test)217 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)182 HashMap (java.util.HashMap)70 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)33 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)29 NotifyRequest (uk.gov.di.authentication.shared.entity.NotifyRequest)17 URI (java.net.URI)15 Map (java.util.Map)15 UserProfile (uk.gov.di.authentication.shared.entity.UserProfile)14 Context (com.amazonaws.services.lambda.runtime.Context)13 NotifyRequest (uk.gov.di.accountmanagement.entity.NotifyRequest)13 Subject (com.nimbusds.oauth2.sdk.id.Subject)12 Instant (java.time.Instant)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 ClientID (com.nimbusds.oauth2.sdk.id.ClientID)10 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 JWSObject (com.nimbusds.jose.JWSObject)8