-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ase-101 <[email protected]>
- Loading branch information
Showing
6 changed files
with
437 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...n-service/src/test/java/io/mosip/authentication/service/controller/VCIControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package io.mosip.authentication.service.controller; | ||
|
||
import io.mosip.authentication.common.service.builder.AuthTransactionBuilder; | ||
import io.mosip.authentication.common.service.helper.AuthTransactionHelper; | ||
import io.mosip.authentication.common.service.util.EnvUtil; | ||
import io.mosip.authentication.common.service.util.TestHttpServletRequest; | ||
import io.mosip.authentication.core.constant.IdAuthCommonConstants; | ||
import io.mosip.authentication.core.exception.IDDataValidationException; | ||
import io.mosip.authentication.core.exception.IdAuthenticationAppException; | ||
import io.mosip.authentication.core.exception.IdAuthenticationBusinessException; | ||
import io.mosip.authentication.core.indauth.dto.IdType; | ||
import io.mosip.authentication.core.indauth.dto.VciCredentialsDefinitionRequestDTO; | ||
import io.mosip.authentication.core.indauth.dto.VciExchangeRequestDTO; | ||
import io.mosip.authentication.core.indauth.dto.VciExchangeResponseDTO; | ||
import io.mosip.authentication.core.partner.dto.PartnerDTO; | ||
import io.mosip.authentication.core.spi.indauth.facade.VciFacade; | ||
import io.mosip.authentication.core.spi.partner.service.PartnerService; | ||
import io.mosip.authentication.core.util.IdTypeUtil; | ||
import io.mosip.authentication.service.kyc.controller.VCIController; | ||
import io.mosip.authentication.service.kyc.validator.VciExchangeRequestValidator; | ||
import org.apache.struts.mock.MockHttpServletRequest; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.validation.BeanPropertyBindingResult; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
|
||
@RunWith(SpringRunner.class) | ||
@WebMvcTest(value = VCIController.class) | ||
@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class }) | ||
@Import(EnvUtil.class) | ||
public class VCIControllerTest { | ||
|
||
@Mock | ||
private VciFacade vciFacade; | ||
|
||
@Mock | ||
private IdTypeUtil idTypeUtil; | ||
|
||
@Mock | ||
private AuthTransactionHelper authTransactionHelper; | ||
|
||
@Mock | ||
private PartnerService partnerService; | ||
|
||
@Mock | ||
private VciExchangeRequestValidator vciExchangeRequestValidator; | ||
|
||
@InjectMocks | ||
VCIController vciController; | ||
|
||
|
||
@Test | ||
public void delegatedVCExchange_withValidInput_thenPass() throws Exception { | ||
VciExchangeRequestDTO vciExchangeRequestDTO = new VciExchangeRequestDTO(); | ||
vciExchangeRequestDTO.setRequestTime(Instant.now().atOffset(ZoneOffset.UTC) | ||
.format(DateTimeFormatter.ofPattern(EnvUtil.getDateTimePattern())).toString()); | ||
vciExchangeRequestDTO.setTransactionID("transactio"); | ||
vciExchangeRequestDTO.setVcFormat("ldp_vc"); | ||
vciExchangeRequestDTO.setVcAuthToken("vc-auth-token"); | ||
vciExchangeRequestDTO.setCredSubjectId("did:jwk:eyJrdHkiOiJSU0EiLCJlIjoiQVFBQiIsInVzZSI6InNpZyIsImtpZCI6Inc4VUY3QnE0dDFSeVMxdFJTOHhvVllHUjMySVdiMFZyU3I4M0dEdno3d28iLCJhbGciOiJSUzI1NiIsIm4iOiJ5SGY1RjZYMFI5RDNxWm5WaUJORDZRV25pUmVnR2hjQ3NqakVJSENlTWp1UWJHek1LaFB6aFZVWGNtaTBMbGVQVWdUdlhjOWlrRmNnTXM3ckFhckI1dlJEcTh1Mjd2WHNBVjdiOUlZaVVGY3U1ZFZpdTd0Q0F1N0V5cXlLWVlUX20xMzhlZjQxVmU4X29LZVNvT0RRaGxyc0RJTmltX0JwWHBvc0xQVV96MXpfODNxX0ZRU05ydDE2dGhHa0hZeUZsRnhxZnNWZElPTkdoMzRFY3dubFZUY0lQUE5xZVY2RkJ3MENlR2NuaUlSRDZVMzVCbFNnT2loaHE2dl9LTll1aktJS2hmOERLY1AzWHY3Yy00ZUcwQ1Q2eFNGdDBpbzlvVGRQT0ZJNEt4RlJ0eGNIa3NxV2FsN1ZON3p5QUlNblJrMlJDbXRZLVUyVkVDSVgydzJOSlEifQ=="); | ||
VciCredentialsDefinitionRequestDTO vciCredentialsDefinitionRequestDTO = new VciCredentialsDefinitionRequestDTO(); | ||
vciCredentialsDefinitionRequestDTO.setType(Arrays.asList("VerifiableCredential", "MOSIPVerifiableCredential")); | ||
vciExchangeRequestDTO.setCredentialsDefinition(vciCredentialsDefinitionRequestDTO); | ||
|
||
Errors errors = new BeanPropertyBindingResult(vciExchangeRequestDTO, "vciExchangeRequestDTO"); | ||
|
||
TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest(); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data"); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info"); | ||
|
||
VciExchangeResponseDTO vciExchangeResponseDTO = new VciExchangeResponseDTO(); | ||
|
||
vciExchangeRequestDTO.setIndividualIdType(IdType.UIN.getType()); | ||
AuthTransactionBuilder authTxnBuilder = AuthTransactionBuilder.newInstance(); | ||
Optional<PartnerDTO> partner = Optional.empty(); | ||
Mockito.when(partnerService.getPartner("partnerId", vciExchangeRequestDTO.getMetadata())).thenReturn(partner); | ||
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(vciExchangeRequestDTO, !false, partner)) | ||
.thenReturn(authTxnBuilder); | ||
|
||
Mockito.when(vciFacade.processVciExchange(vciExchangeRequestDTO, "auth-partner-id", | ||
"oidc-client-id", vciExchangeRequestDTO.getMetadata(), requestWithMetadata)).thenReturn(vciExchangeResponseDTO); | ||
|
||
vciExchangeResponseDTO = vciController.vciExchange(vciExchangeRequestDTO, errors, "license-key", "auth-partner-id", | ||
"oidc-client-id", requestWithMetadata); | ||
|
||
Assert.assertNotNull(vciExchangeResponseDTO); | ||
} | ||
|
||
@Test(expected = IdAuthenticationBusinessException.class) | ||
public void delegatedVCExchange_withInvalidInput_thenFail() throws Exception { | ||
VciExchangeRequestDTO vciExchangeRequestDTO = new VciExchangeRequestDTO(); | ||
Errors errors = new BeanPropertyBindingResult(vciExchangeRequestDTO, "vciExchangeRequestDTO"); | ||
vciController.vciExchange(vciExchangeRequestDTO, errors, "license-key", "auth-partner-id", | ||
"oidc-client-id", new MockHttpServletRequest()); | ||
} | ||
|
||
@Test(expected = IdAuthenticationAppException.class) | ||
public void delegatedVCExchange_withInternalError_thenFail() throws Exception { | ||
VciExchangeRequestDTO vciExchangeRequestDTO = new VciExchangeRequestDTO(); | ||
Errors errors = new BeanPropertyBindingResult(vciExchangeRequestDTO, "vciExchangeRequestDTO"); | ||
TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest(); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data"); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info"); | ||
|
||
vciExchangeRequestDTO.setIndividualIdType(IdType.UIN.getType()); | ||
AuthTransactionBuilder authTxnBuilder = AuthTransactionBuilder.newInstance(); | ||
Optional<PartnerDTO> partner = Optional.empty(); | ||
Mockito.when(partnerService.getPartner("partnerId", vciExchangeRequestDTO.getMetadata())).thenReturn(partner); | ||
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(vciExchangeRequestDTO, !false, partner)) | ||
.thenReturn(authTxnBuilder); | ||
IDDataValidationException idDataValidationException = new IDDataValidationException("error-code","error-message"); | ||
Mockito.when(authTransactionHelper.createDataValidationException(null, idDataValidationException, requestWithMetadata)) | ||
.thenReturn(new IdAuthenticationAppException()); | ||
|
||
Mockito.when(vciFacade.processVciExchange(vciExchangeRequestDTO, "auth-partner-id", | ||
"oidc-client-id", vciExchangeRequestDTO.getMetadata(), requestWithMetadata)) | ||
.thenThrow(idDataValidationException); | ||
|
||
vciController.vciExchange(vciExchangeRequestDTO, errors, "license-key", "auth-partner-id", | ||
"oidc-client-id", requestWithMetadata); | ||
} | ||
|
||
@Test(expected = IdAuthenticationAppException.class) | ||
public void delegatedVCIExchange_withInternalError2_thenFail() throws Exception { | ||
VciExchangeRequestDTO vciExchangeRequestDTO = new VciExchangeRequestDTO(); | ||
Errors errors = new BeanPropertyBindingResult(vciExchangeRequestDTO, "vciExchangeRequestDTO"); | ||
TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest(); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data"); | ||
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info"); | ||
|
||
vciExchangeRequestDTO.setIndividualIdType(IdType.UIN.getType()); | ||
AuthTransactionBuilder authTxnBuilder = AuthTransactionBuilder.newInstance(); | ||
Optional<PartnerDTO> partner = Optional.empty(); | ||
Mockito.when(partnerService.getPartner("partnerId", vciExchangeRequestDTO.getMetadata())).thenReturn(partner); | ||
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(vciExchangeRequestDTO, !false, partner)) | ||
.thenReturn(authTxnBuilder); | ||
IdAuthenticationBusinessException idAuthenticationBusinessException = new IdAuthenticationBusinessException("error-code","error-message"); | ||
Mockito.when(vciFacade.processVciExchange(vciExchangeRequestDTO, "auth-partner-id", | ||
"oidc-client-id", vciExchangeRequestDTO.getMetadata(), requestWithMetadata)) | ||
.thenThrow(idAuthenticationBusinessException); | ||
|
||
vciController.vciExchange(vciExchangeRequestDTO, errors, "license-key", "auth-partner-id", | ||
"oidc-client-id", requestWithMetadata); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...rvice/src/test/java/io/mosip/authentication/service/kyc/filter/VciExchangeFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.mosip.authentication.service.kyc.filter; | ||
|
||
import io.mosip.authentication.common.service.util.EnvUtil; | ||
import io.mosip.authentication.core.exception.IdAuthenticationAppException; | ||
import io.mosip.authentication.core.partner.dto.AuthPolicy; | ||
import io.mosip.authentication.core.partner.dto.MispPolicyDTO; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.util.Arrays; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebMvcTest | ||
@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class }) | ||
@Import(EnvUtil.class) | ||
public class VciExchangeFilterTest { | ||
|
||
VciExchangeFilter vciExchangeFilter = new VciExchangeFilter(); | ||
|
||
@Test | ||
public void test_defaultMethods() { | ||
Assert.assertTrue(vciExchangeFilter.isPartnerCertificateNeeded()); | ||
Assert.assertTrue(vciExchangeFilter.isSigningRequired()); | ||
Assert.assertTrue(vciExchangeFilter.isSignatureVerificationRequired()); | ||
Assert.assertTrue(vciExchangeFilter.isTrustValidationRequired()); | ||
Assert.assertTrue(vciExchangeFilter.needStoreAuthTransaction()); | ||
Assert.assertTrue(vciExchangeFilter.needStoreAnonymousProfile()); | ||
Assert.assertTrue(vciExchangeFilter.isMispPolicyValidationRequired()); | ||
Assert.assertTrue(vciExchangeFilter.isCertificateValidationRequired()); | ||
Assert.assertFalse(vciExchangeFilter.isAMRValidationRequired()); | ||
} | ||
|
||
@Test | ||
public void test_checkAllowedAuthTypeBasedOnPolicy_withValidPolicy_thenPass() throws IdAuthenticationAppException { | ||
AuthPolicy authPolicy = new AuthPolicy(); | ||
authPolicy.setAuthType("vciexchange"); | ||
vciExchangeFilter.checkAllowedAuthTypeBasedOnPolicy(null, Arrays.asList(authPolicy)); | ||
} | ||
|
||
@Test(expected = IdAuthenticationAppException.class) | ||
public void test_checkAllowedAuthTypeBasedOnPolicy_withInvalidPolicy_thenFail() throws IdAuthenticationAppException { | ||
AuthPolicy authPolicy = new AuthPolicy(); | ||
authPolicy.setAuthType("kycexchange"); | ||
vciExchangeFilter.checkAllowedAuthTypeBasedOnPolicy(null, Arrays.asList(authPolicy)); | ||
} | ||
|
||
@Test(expected = IdAuthenticationAppException.class) | ||
public void test_checkMispPolicyAllowed_withInvalidPolicy_thenFail() | ||
throws IdAuthenticationAppException { | ||
MispPolicyDTO mispPolicyDTO = new MispPolicyDTO(); | ||
mispPolicyDTO.setAllowVciRequestDelegation(false); | ||
vciExchangeFilter.checkMispPolicyAllowed(mispPolicyDTO); | ||
} | ||
|
||
@Test | ||
public void test_checkMispPolicyAllowed_withValidPolicy_thenPass() | ||
throws IdAuthenticationAppException { | ||
MispPolicyDTO mispPolicyDTO = new MispPolicyDTO(); | ||
mispPolicyDTO.setAllowVciRequestDelegation(true); | ||
vciExchangeFilter.checkMispPolicyAllowed(mispPolicyDTO); | ||
} | ||
} |
Oops, something went wrong.