Search in sources :

Example 11 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-ipv-cri-uk-passport-back by alphagov.

the class AccessTokenHandlerTest method shouldReturnAccessTokenOnSuccessfulExchange.

@Test
void shouldReturnAccessTokenOnSuccessfulExchange() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://example.com&grant_type=authorization_code&client_id=test_client_id";
    event.setBody(tokenRequestBody);
    AccessToken accessToken = new BearerAccessToken();
    TokenResponse tokenResponse = new AccessTokenResponse(new Tokens(accessToken, null));
    when(mockAccessTokenService.generateAccessToken(any())).thenReturn(tokenResponse);
    when(mockAuthorizationCodeService.getAuthCodeItem("12345")).thenReturn(TEST_AUTH_CODE_ITEM);
    when(mockAccessTokenService.validateTokenRequest(any())).thenReturn(ValidationResult.createValidResult());
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), new TypeReference<>() {
    });
    assertEquals(ContentType.APPLICATION_JSON.getType(), response.getHeaders().get("Content-Type"));
    assertEquals(200, response.getStatusCode());
    assertEquals(tokenResponse.toSuccessResponse().getTokens().getAccessToken().getValue(), responseBody.get("access_token").toString());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) Tokens(com.nimbusds.oauth2.sdk.token.Tokens) Test(org.junit.jupiter.api.Test)

Example 12 with APIGatewayProxyRequestEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-java-serverless by hermanlintvelt.

the class HandlerTest method handleTestHandler.

@Test
void handleTestHandler() {
    Map<String, Object> input = new HashMap<>();
    input.put("testKey", "test value");
    APIGatewayProxyRequestEvent requestEvent = new APIGatewayProxyRequestEvent();
    requestEvent.setBody(converToJson(input));
    ApiGatewayResponse response = subject.handleRequest(requestEvent, testContext);
    assertEquals(200, response.getStatusCode());
    Response expectedResponse = new Response("Go Serverless v1.x! Your function executed successfully!", converToJson(input));
    assertEquals(converToJson(expectedResponse), response.getBody());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 13 with APIGatewayProxyRequestEvent

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

the class AccessTokenHandlerTest method shouldReturnAccessTokenOnSuccessfulExchange.

@Test
void shouldReturnAccessTokenOnSuccessfulExchange() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=authorization_code&client_id=test_client_id";
    event.withBody(tokenRequestBody);
    AddressSessionItem addressSessionItem = mock(AddressSessionItem.class);
    AccessToken accessToken = new BearerAccessToken();
    tokenResponse = new AccessTokenResponse(new Tokens(accessToken, null));
    // TODO: This here as a placeholder pending the story that generates the authorization code
    TokenRequest tokenRequest = mock(TokenRequest.class);
    when(tokenRequest.getAuthorizationGrant()).thenReturn(new AuthorizationCodeGrant(new AuthorizationCode("12345"), URI.create("http://test.com"), null));
    when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenReturn(tokenRequest);
    when(mockAddressSessionService.createToken(any())).thenReturn(tokenResponse);
    when(mockAddressSessionService.getAddressSessionItemByValue(any())).thenReturn(addressSessionItem);
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), new TypeReference<>() {
    });
    assertEquals(ContentType.APPLICATION_JSON.getType(), response.getHeaders().get("Content-Type"));
    assertEquals(HttpStatus.SC_OK, response.getStatusCode());
    assertEquals(tokenResponse.toSuccessResponse().getTokens().getAccessToken().getValue(), responseBody.get("access_token").toString());
}
Also used : AuthorizationCode(com.nimbusds.oauth2.sdk.AuthorizationCode) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) AuthorizationCodeGrant(com.nimbusds.oauth2.sdk.AuthorizationCodeGrant) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) Tokens(com.nimbusds.oauth2.sdk.token.Tokens) Test(org.junit.jupiter.api.Test)

Example 14 with APIGatewayProxyRequestEvent

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

the class AccessTokenHandlerTest method shouldReturn400WhenInvalidGrantTypeProvided.

@Test
void shouldReturn400WhenInvalidGrantTypeProvided() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=" + GrantType.IMPLICIT.getValue() + "&client_id=test_client_id";
    event.withBody(tokenRequestBody);
    when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenThrow(new AccessTokenRequestException(OAuth2Error.UNSUPPORTED_GRANT_TYPE));
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
    assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE_CODE, errorResponse.getCode());
    assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE.getDescription(), errorResponse.getDescription());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 15 with APIGatewayProxyRequestEvent

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

the class AccessTokenHandlerTest method shouldReturn400WhenInvalidRedirectUriIsProvided.

@Test
void shouldReturn400WhenInvalidRedirectUriIsProvided() throws ParseException {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=authorization_code&client_id=test_client_id";
    event.withBody(tokenRequestBody);
    when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenThrow(new AccessTokenRequestException(OAuth2Error.INVALID_GRANT));
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
    assertEquals(OAuth2Error.INVALID_GRANT.getCode(), errorResponse.getCode());
    assertEquals(OAuth2Error.INVALID_GRANT.getDescription(), errorResponse.getDescription());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) 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