Search in sources :

Example 11 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent 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)

Example 12 with APIGatewayProxyResponseEvent

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

the class SessionHandlerTest method shouldCreateAndSaveAddressSession.

@Test
void shouldCreateAndSaveAddressSession() throws SessionValidationException, ClientConfigurationException, JsonProcessingException {
    when(eventProbe.counterMetric(anyString())).thenReturn(eventProbe);
    UUID sessionId = UUID.randomUUID();
    when(sessionRequest.getClientId()).thenReturn("ipv-core");
    when(apiGatewayProxyRequestEvent.getBody()).thenReturn("some json");
    when(addressSessionService.validateSessionRequest("some json")).thenReturn(sessionRequest);
    when(addressSessionService.createAndSaveAddressSession(sessionRequest)).thenReturn(sessionId);
    APIGatewayProxyResponseEvent responseEvent = sessionHandler.handleRequest(apiGatewayProxyRequestEvent, null);
    assertEquals(HttpStatus.SC_CREATED, responseEvent.getStatusCode());
    Map responseBody = new ObjectMapper().readValue(responseEvent.getBody(), Map.class);
    assertEquals(sessionId.toString(), responseBody.get(SESSION_ID));
    verify(eventProbe).addDimensions(Map.of("issuer", "ipv-core"));
    verify(eventProbe).counterMetric("session_created");
}
Also used : UUID(java.util.UUID) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 13 with APIGatewayProxyResponseEvent

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

the class GetExpensesHandlerTest method testGetExpenses.

@Test
@DisplayName("Test that request to function returns list of expenses")
void testGetExpenses() {
    APIGatewayProxyResponseEvent result = testHandler.handleRequest(new APIGatewayProxyRequestEvent(), testContext);
    assertThat(result.getStatusCode()).isEqualTo(200);
    try {
        List<Expense> expenses = OBJECT_MAPPER.readValue(result.getBody(), List.class);
        assertThat(expenses).asList().isNotEmpty();
        assertThat(expenses).asList().size().isGreaterThanOrEqualTo(3);
    } catch (JsonProcessingException e) {
        fail("did not expect json error");
    }
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 14 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project saas-tenant-isolation-architecture by aws-samples.

the class ApiGatewayHandler method handlePostRequest.

public APIGatewayProxyResponseEvent handlePostRequest(final APIGatewayProxyRequestEvent input, final Context context) {
    // we vending the token by extracting the tenant ID from the JWT token contained in
    // the request headers
    TokenVendor tokenVendor = new TokenVendor();
    final AwsCredentialsProvider awsCredentialsProvider = tokenVendor.vendTokenJwt(input.getHeaders());
    // we parse the body of the POST request, currently we only accept a 'data' parameter to
    // be written to DynamoDB, anything else will be ignored
    Map<String, String> body;
    try {
        TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
        };
        body = mapper.readValue(input.getBody(), typeRef);
    } catch (JsonProcessingException e) {
        logger.error("Error parsing JSON body.", e);
        throw new RuntimeException(createBadRequestResponse(context.getAwsRequestId(), "Error parsing JSON body."));
    }
    String tenant = tokenVendor.getTenant();
    logger.info("TENANT ID: " + tenant);
    // TenantProduct class encapsulates writing to DynamoDB using the enhanced DynamoDB
    // client, which allows us to use POJOs
    TenantProduct tenantProduct = new TenantProduct(awsCredentialsProvider, tenant, body.get("data"));
    tenantProduct.save();
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    return new APIGatewayProxyResponseEvent().withHeaders(headers).withStatusCode(201);
}
Also used : TokenVendor(tenant.vendinglayer.TokenVendor) TenantProduct(tenant.export.models.TenantProduct) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 15 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project saas-tenant-isolation-architecture by aws-samples.

the class ApiGatewayHandler method handleGetRequest.

public APIGatewayProxyResponseEvent handleGetRequest(final APIGatewayProxyRequestEvent input, final Context context) {
    // we vending the token by extracting the tenant ID from the JWT token contained in
    // the request headers
    TokenVendor tokenVendor = new TokenVendor();
    final AwsCredentialsProvider awsCredentialsProvider = tokenVendor.vendTokenJwt(input.getHeaders());
    String tenant = tokenVendor.getTenant();
    logger.info("TENANT ID: " + tenant);
    // TenantProduct class encapsulates writing to DynamoDB using the enhanced DynamoDB
    // client, which allows us to use POJOs
    TenantProduct tenantProduct = new TenantProduct(awsCredentialsProvider, tenant);
    tenantProduct = tenantProduct.load(tenantProduct);
    String body;
    try {
        body = mapper.writeValueAsString(tenantProduct);
    } catch (JsonProcessingException e) {
        logger.error("Error parsing JSON body.", e);
        throw new RuntimeException(createBadRequestResponse(context.getAwsRequestId(), "Error parsing JSON body."));
    }
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    return new APIGatewayProxyResponseEvent().withHeaders(headers).withBody(body).withStatusCode(200);
}
Also used : TokenVendor(tenant.vendinglayer.TokenVendor) TenantProduct(tenant.export.models.TenantProduct) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

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