use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent 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.APIGatewayProxyResponseEvent in project serverless-java-frameworks-samples by aws-samples.
the class CreateProductFunction method apply.
@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
if (!requestEvent.getHttpMethod().equals(HttpMethod.PUT.name())) {
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
}
try {
String id = requestEvent.getPathParameters().get("id");
String jsonPayload = requestEvent.getBody();
Product product = objectMapper.readValue(jsonPayload, Product.class);
if (!product.getId().equals(id)) {
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
}
productDao.putProduct(product);
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
} catch (Exception e) {
e.printStackTrace();
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error :: " + e.getMessage());
}
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project serverless-java-frameworks-samples by aws-samples.
the class CreateProductHandler method handleRequest.
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent requestEvent, Context context) {
if (!requestEvent.getHttpMethod().equals(SdkHttpMethod.PUT.name())) {
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
}
try {
String id = requestEvent.getPathParameters().get("id");
String jsonPayload = requestEvent.getBody();
Product product = objectMapper.readValue(jsonPayload, Product.class);
if (!product.getId().equals(id)) {
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
}
productDao.putProduct(product);
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
} catch (Exception e) {
e.printStackTrace();
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error");
}
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent 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.APIGatewayProxyResponseEvent 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());
}
Aggregations