Search in sources :

Example 6 with APIGatewayProxyRequestEvent

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

the class AuthorizationCodeHandlerTest method shouldPersistPassportCheckDaoWithInValidGpg45Score.

@Test
void shouldPersistPassportCheckDaoWithInValidGpg45Score() throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, JOSEException, ParseException, EmptyDcsResponseException {
    DcsSignedEncryptedResponse dcsSignedEncryptedResponse = new DcsSignedEncryptedResponse("TEST_PAYLOAD");
    when(passportService.dcsPassportCheck(any(JWSObject.class))).thenReturn(dcsSignedEncryptedResponse);
    when(dcsCryptographyService.preparePayload(any(PassportAttributes.class))).thenReturn(jwsObject);
    when(dcsCryptographyService.unwrapDcsResponse(any(DcsSignedEncryptedResponse.class))).thenReturn(invalidDcsResponse);
    when(authorizationCodeService.generateAuthorizationCode()).thenReturn(authorizationCode);
    when(authRequestValidator.validateRequest(anyMap(), anyString())).thenReturn(Optional.empty());
    var event = new APIGatewayProxyRequestEvent();
    Map<String, String> params = new HashMap<>();
    params.put(OAuth2RequestParams.REDIRECT_URI, "http://example.com");
    params.put(OAuth2RequestParams.CLIENT_ID, "12345");
    params.put(OAuth2RequestParams.RESPONSE_TYPE, "code");
    params.put(OAuth2RequestParams.SCOPE, "openid");
    event.setQueryStringParameters(params);
    event.setHeaders(Map.of("user_id", "test-user-id"));
    event.setBody(objectMapper.writeValueAsString(validPassportFormData));
    underTest.handleRequest(event, context);
    ArgumentCaptor<PassportCheckDao> persistedPassportCheckDao = ArgumentCaptor.forClass(PassportCheckDao.class);
    verify(passportService).persistDcsResponse(persistedPassportCheckDao.capture());
    assertEquals(validPassportFormData.get("passportNumber"), persistedPassportCheckDao.getValue().getAttributes().getPassportNumber());
    assertEquals(INVALID_GPG45_SCORE.getStrength(), persistedPassportCheckDao.getValue().getGpg45Score().getStrength());
    assertEquals(INVALID_GPG45_SCORE.getValidity(), persistedPassportCheckDao.getValue().getGpg45Score().getValidity());
    assertEquals(invalidDcsResponse, persistedPassportCheckDao.getValue().getAttributes().getDcsResponse());
}
Also used : DcsSignedEncryptedResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JWSObject(com.nimbusds.jose.JWSObject) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao) Test(org.junit.jupiter.api.Test)

Example 7 with APIGatewayProxyRequestEvent

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

the class AuthorizationCodeHandlerTest method shouldReturn200WithCorrectFormData.

@Test
void shouldReturn200WithCorrectFormData() throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, JOSEException, ParseException, EmptyDcsResponseException, SqsException {
    DcsSignedEncryptedResponse dcsSignedEncryptedResponse = new DcsSignedEncryptedResponse("TEST_PAYLOAD");
    when(passportService.dcsPassportCheck(any(JWSObject.class))).thenReturn(dcsSignedEncryptedResponse);
    when(dcsCryptographyService.preparePayload(any(PassportAttributes.class))).thenReturn(jwsObject);
    when(dcsCryptographyService.unwrapDcsResponse(any(DcsSignedEncryptedResponse.class))).thenReturn(validDcsResponse);
    when(authorizationCodeService.generateAuthorizationCode()).thenReturn(authorizationCode);
    when(authRequestValidator.validateRequest(any(), anyString())).thenReturn(Optional.empty());
    var event = new APIGatewayProxyRequestEvent();
    Map<String, String> params = new HashMap<>();
    params.put(OAuth2RequestParams.REDIRECT_URI, "http://example.com");
    params.put(OAuth2RequestParams.CLIENT_ID, "12345");
    params.put(OAuth2RequestParams.RESPONSE_TYPE, "code");
    params.put(OAuth2RequestParams.SCOPE, "openid");
    event.setQueryStringParameters(params);
    event.setHeaders(Map.of("user_id", "test-user-id"));
    event.setBody(objectMapper.writeValueAsString(validPassportFormData));
    var response = underTest.handleRequest(event, context);
    verify(auditService).sendAuditEvent(AuditEventTypes.PASSPORT_REQUEST_SENT_TO_DCS);
    assertEquals(HttpStatus.SC_OK, response.getStatusCode());
}
Also used : DcsSignedEncryptedResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JWSObject(com.nimbusds.jose.JWSObject) Test(org.junit.jupiter.api.Test)

Example 8 with APIGatewayProxyRequestEvent

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

the class AuthorizationCodeHandlerTest method shouldPersistPassportCheckDaoWithValidGpg45Score.

@Test
void shouldPersistPassportCheckDaoWithValidGpg45Score() throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, JOSEException, ParseException, EmptyDcsResponseException {
    DcsSignedEncryptedResponse dcsSignedEncryptedResponse = new DcsSignedEncryptedResponse("TEST_PAYLOAD");
    when(passportService.dcsPassportCheck(any(JWSObject.class))).thenReturn(dcsSignedEncryptedResponse);
    when(dcsCryptographyService.preparePayload(any(PassportAttributes.class))).thenReturn(jwsObject);
    when(dcsCryptographyService.unwrapDcsResponse(any(DcsSignedEncryptedResponse.class))).thenReturn(validDcsResponse);
    when(authorizationCodeService.generateAuthorizationCode()).thenReturn(authorizationCode);
    when(authRequestValidator.validateRequest(anyMap(), anyString())).thenReturn(Optional.empty());
    var event = new APIGatewayProxyRequestEvent();
    Map<String, String> params = new HashMap<>();
    params.put(OAuth2RequestParams.REDIRECT_URI, "http://example.com");
    params.put(OAuth2RequestParams.CLIENT_ID, "12345");
    params.put(OAuth2RequestParams.RESPONSE_TYPE, "code");
    params.put(OAuth2RequestParams.SCOPE, "openid");
    event.setQueryStringParameters(params);
    event.setHeaders(Map.of("user_id", "test-user-id"));
    event.setBody(objectMapper.writeValueAsString(validPassportFormData));
    underTest.handleRequest(event, context);
    ArgumentCaptor<PassportCheckDao> persistedPassportCheckDao = ArgumentCaptor.forClass(PassportCheckDao.class);
    verify(passportService).persistDcsResponse(persistedPassportCheckDao.capture());
    assertEquals(validPassportFormData.get("passportNumber"), persistedPassportCheckDao.getValue().getAttributes().getPassportNumber());
    assertEquals(VALID_GPG45_SCORE.getStrength(), persistedPassportCheckDao.getValue().getGpg45Score().getStrength());
    assertEquals(VALID_GPG45_SCORE.getValidity(), persistedPassportCheckDao.getValue().getGpg45Score().getValidity());
    assertEquals(validDcsResponse, persistedPassportCheckDao.getValue().getAttributes().getDcsResponse());
}
Also used : DcsSignedEncryptedResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) HashMap(java.util.HashMap) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JWSObject(com.nimbusds.jose.JWSObject) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao) Test(org.junit.jupiter.api.Test)

Example 9 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 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 10 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 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

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