Search in sources :

Example 1 with APIGatewayProxyResponseEvent

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

the class IssueCredentialHandlerTest method shouldReturnErrorResponseWhenTokenIsMissingBearerPrefix.

@Test
void shouldReturnErrorResponseWhenTokenIsMissingBearerPrefix() throws JsonProcessingException {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    Map<String, String> headers = Collections.singletonMap("Authorization", "11111111");
    event.setHeaders(headers);
    setRequestBodyAsPlainJWT(event);
    APIGatewayProxyResponseEvent response = issueCredentialHandler.handleRequest(event, mockContext);
    responseBody = objectMapper.readValue(response.getBody(), new TypeReference<>() {
    });
    assertEquals(BearerTokenError.INVALID_REQUEST.getHTTPStatusCode(), response.getStatusCode());
    assertEquals(BearerTokenError.INVALID_REQUEST.getCode(), responseBody.get("error"));
    assertEquals(BearerTokenError.INVALID_REQUEST.getDescription(), responseBody.get("error_description"));
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 2 with APIGatewayProxyResponseEvent

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

the class IssueCredentialHandlerTest method shouldReturnErrorResponseWhenInvalidAccessTokenProvided.

@Test
void shouldReturnErrorResponseWhenInvalidAccessTokenProvided() throws JsonProcessingException {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    AccessToken accessToken = new BearerAccessToken();
    Map<String, String> headers = Collections.singletonMap("Authorization", accessToken.toAuthorizationHeader());
    event.setHeaders(headers);
    setRequestBodyAsPlainJWT(event);
    when(mockAccessTokenService.getResourceIdByAccessToken(anyString())).thenReturn(null);
    APIGatewayProxyResponseEvent response = issueCredentialHandler.handleRequest(event, mockContext);
    Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), new TypeReference<>() {
    });
    assertEquals(403, response.getStatusCode());
    assertEquals(OAuth2Error.ACCESS_DENIED.getCode(), responseBody.get("error"));
    assertEquals(OAuth2Error.ACCESS_DENIED.appendDescription(" - The supplied access token was not found in the database").getDescription(), responseBody.get("error_description"));
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 3 with APIGatewayProxyResponseEvent

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

the class IssueCredentialHandlerTest method shouldReturn200OnSuccessfulDcsCredentialRequest.

@Test
void shouldReturn200OnSuccessfulDcsCredentialRequest() throws SqsException {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    AccessToken accessToken = new BearerAccessToken();
    Map<String, String> headers = Collections.singletonMap("Authorization", accessToken.toAuthorizationHeader());
    event.setHeaders(headers);
    setRequestBodyAsPlainJWT(event);
    when(mockAccessTokenService.getResourceIdByAccessToken(anyString())).thenReturn(TEST_RESOURCE_ID);
    when(mockDcsPassportCheckService.getDcsPassportCheck(anyString())).thenReturn(dcsCredential);
    APIGatewayProxyResponseEvent response = issueCredentialHandler.handleRequest(event, mockContext);
    verify(mockAuditService).sendAuditEvent(AuditEventTypes.PASSPORT_CREDENTIAL_ISSUED);
    assertEquals(200, response.getStatusCode());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 4 with APIGatewayProxyResponseEvent

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

the class AccessTokenHandlerTest method shouldReturn400WhenInvalidTokenRequestProvided.

@Test
void shouldReturn400WhenInvalidTokenRequestProvided() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String invalidTokenRequest = "invalid-token-request";
    event.setBody(invalidTokenRequest);
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
    assertEquals(OAuth2Error.INVALID_REQUEST.getCode(), errorResponse.getCode());
    assertEquals(OAuth2Error.INVALID_REQUEST.getDescription() + ": Missing grant_type parameter", errorResponse.getDescription());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 5 with APIGatewayProxyResponseEvent

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

the class AccessTokenHandlerTest method shouldReturn400IfAccessTokenServiceDeemsRequestInvalid.

@Test
void shouldReturn400IfAccessTokenServiceDeemsRequestInvalid() throws ParseException {
    when(mockAccessTokenService.validateTokenRequest(any())).thenReturn(new ValidationResult<>(false, OAuth2Error.UNSUPPORTED_GRANT_TYPE));
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=authorization_code&client_id=test_client_id";
    event.setBody(tokenRequestBody);
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
    assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE.getCode(), errorResponse.getCode());
    assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE.getDescription(), errorResponse.getDescription());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

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