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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations