diff --git a/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/filter/KycExchangeFilterTest.java b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/filter/KycExchangeFilterTest.java new file mode 100644 index 00000000000..ec668900b72 --- /dev/null +++ b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/filter/KycExchangeFilterTest.java @@ -0,0 +1,68 @@ +package io.mosip.authentication.service.kyc.filter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.authentication.common.service.util.EnvUtil; +import io.mosip.authentication.core.exception.IdAuthenticationAppException; +import io.mosip.authentication.core.partner.dto.AuthPolicy; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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.test.util.ReflectionTestUtils; +import org.springframework.web.context.WebApplicationContext; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.Collections; +import java.util.HashMap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +@RunWith(SpringRunner.class) +@WebMvcTest +@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class }) +@Import(EnvUtil.class) +public class KycExchangeFilterTest { + @Autowired + EnvUtil env; + KycExchangeFilter kycExchangeFilter = new KycExchangeFilter(); + @Autowired + ObjectMapper mapper; + + byte[] key = {48, -126, 1, 34, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 3, -126, 1, 15, 0, 48, + -126, 1, 10, 2, -126, 1, 1, 0, -56, 41, -49, 92, 30, -78, 87, 22, -103, -23, -14, 106, -89, 84, -73, 51, + -69, -10, 75, -88, 94, 23, -106, -67, -4, 53, -91, -74, -64, 101, 70, 113, 100, 14, 67, 22, -27, -121, -45, + -11, -107, 64, -56, -101, 97, 62, 64, 65, 57, -18, -47, 96, -88, 38, -77, 107, 125, 39, -52, -83, -67, -27, + -20, -9, 27, -15, 69, 78, 74, -36, -114, 20, -121, -119, -55, 26, -50, -69, 16, -21, 84, 6, 66, 117, -39, 0, + 17, -39, -15, 49, -114, -101, -106, -113, -98, -81, 3, 18, -109, -122, -57, -19, 27, 2, 53, 8, -53, -11, + -73, -84, 9, 55, -33, 8, -93, 16, -103, -4, 117, -35, -63, 43, -97, -74, 48, 101, -108, 38, -54, 18, -36, + 105, -39, 21, 117, -81, 42, -15, -95, 79, -124, -59, -128, 64, 82, 85, -68, -79, 24, -84, 25, -113, 125, + -17, -20, -57, 50, -63, -13, -79, -60, 81, -104, 111, -84, 62, 123, -40, 12, -7, 65, -5, 23, 3, -91, -17, 2, + 49, -56, 73, 35, 46, -97, 38, -18, 14, 10, 26, 11, 122, 124, 124, -20, -110, -9, 26, 122, 59, 74, -123, -86, + 97, 0, 48, -14, 65, -50, -49, 40, 90, 65, 127, 75, 110, -76, 127, -41, 80, 6, 30, 61, -4, 27, -63, -100, + 115, -79, -87, 107, 66, 73, -14, 13, -98, -108, 55, 26, 58, -72, -103, -35, 46, -15, 45, 23, 84, 93, 31, 44, + -112, -41, 95, 22, 14, -114, 15, 2, 3, 1, 0, 1}; + @Before + public void before() { + ReflectionTestUtils.setField(kycExchangeFilter, "mapper", mapper); + ReflectionTestUtils.setField(kycExchangeFilter, "env", env); + } + @Test + public void checkAllowedAuthTypeBasedOnPolicyTest() { + AuthPolicy authPolicy = new AuthPolicy(); + authPolicy.setAuthType("demo"); + authPolicy.setMandatory(true); + try { + ReflectionTestUtils.invokeMethod(kycExchangeFilter, "checkAllowedAuthTypeBasedOnPolicy", new HashMap<>(), Collections.singletonList(authPolicy)); + } catch (UndeclaredThrowableException e) { + String detailMessage = e.getUndeclaredThrowable().getMessage(); + String[] error = detailMessage.split("-->"); + assertEquals("IDA-MPA-026", error[0].trim()); + assertEquals("Partner is unauthorised for KYC-Exchange", error[1].trim()); + assertTrue(e.getCause().getClass().equals(IdAuthenticationAppException.class)); + } + } +} + \ No newline at end of file diff --git a/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/util/VCSchemaProviderUtilTest.java b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/util/VCSchemaProviderUtilTest.java new file mode 100644 index 00000000000..60db674d8e8 --- /dev/null +++ b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/util/VCSchemaProviderUtilTest.java @@ -0,0 +1,101 @@ +package io.mosip.authentication.service.kyc.util; + +import com.apicatalog.jsonld.JsonLdError; +import com.apicatalog.jsonld.document.JsonDocument; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.authentication.core.exception.IdAuthUncheckedException; +import io.mosip.authentication.core.exception.IdAuthenticationBusinessException; +import org.json.simple.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +@RunWith(MockitoJUnitRunner.class) +public class VCSchemaProviderUtilTest { + + @InjectMocks + private VCSchemaProviderUtil vcSchemaProviderUtil; + + @Mock + private RestTemplate restTemplate; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetVCContextSchema() { + String configServerFileStorageUrl = "http://example.com"; + String uri = "vc-context-schema.json"; + String vcContextJson = "{\"vc\": \"context\"}"; + JsonDocument expectedJsonDocument = null; + try { + expectedJsonDocument = JsonDocument.of(new StringReader(vcContextJson)); + } catch (JsonLdError e) { + throw new RuntimeException(e); + } + + Mockito.when(restTemplate.getForObject(configServerFileStorageUrl + uri, String.class)) + .thenReturn(vcContextJson); + JsonDocument result = vcSchemaProviderUtil.getVCContextSchema(configServerFileStorageUrl, uri); + Assert.assertEquals(expectedJsonDocument.getJsonContent(), result.getJsonContent()); + Mockito.verify(restTemplate).getForObject(configServerFileStorageUrl + uri, String.class); + } + + @Test + public void testGetVCContextSchema_throwsException() { + String configServerFileStorageUrl = "http://example.com"; + String uri = "vc-context-schema.json"; + String vcContextJson = ""; + Mockito.when(restTemplate.getForObject(configServerFileStorageUrl + uri, String.class)) + .thenReturn(vcContextJson); + Assert.assertThrows(IdAuthUncheckedException.class,()->vcSchemaProviderUtil.getVCContextSchema(configServerFileStorageUrl, uri)); + } + + @Test + public void testGetVCContextData() throws IdAuthenticationBusinessException { + String configServerFileStorageUrl = "http://example.com"; + String uri = "/vc-context-data.json"; + String vcContextData = "{\"vc\": \"data\"}"; + ObjectMapper objectMapper = new ObjectMapper(); + Map expectedMap; + + try { + expectedMap = objectMapper.readValue(vcContextData, new TypeReference>(){}); + } catch (IOException e) { + Assert.fail("Error parsing JSON: " + e.getMessage()); + return; + } + + JSONObject expectedJsonObject = new JSONObject(expectedMap); + Mockito.when(restTemplate.getForObject(configServerFileStorageUrl + uri, String.class)) + .thenReturn(vcContextData); + JSONObject result = vcSchemaProviderUtil.getVCContextData(configServerFileStorageUrl, uri, objectMapper); + Assert.assertEquals(expectedJsonObject, result); + Mockito.verify(restTemplate).getForObject(configServerFileStorageUrl + uri, String.class); + } + + @Test + public void testGetVCContextData_throwsException() throws IdAuthenticationBusinessException{ + String configServerFileStorageUrl = "http://example.com"; + String uri = "/vc-context-data.json"; + String vcContextData = ""; + Mockito.when(restTemplate.getForObject(configServerFileStorageUrl + uri, String.class)) + .thenReturn(vcContextData); + Assert.assertThrows(IdAuthenticationBusinessException.class,()->vcSchemaProviderUtil.getVCContextData(configServerFileStorageUrl,uri, new ObjectMapper())); + } + +} \ No newline at end of file diff --git a/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/validator/KycExchangeRequestValidatorTest.java b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/validator/KycExchangeRequestValidatorTest.java new file mode 100644 index 00000000000..ba9b619a332 --- /dev/null +++ b/authentication/authentication-service/src/test/java/io/mosip/authentication/service/kyc/validator/KycExchangeRequestValidatorTest.java @@ -0,0 +1,114 @@ +package io.mosip.authentication.service.kyc.validator; + +import io.mosip.authentication.common.service.helper.IdInfoHelper; +import io.mosip.authentication.common.service.util.EnvUtil; +import io.mosip.authentication.common.service.validator.AuthRequestValidator; +import io.mosip.authentication.core.indauth.dto.KycExchangeRequestDTO; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +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.test.util.ReflectionTestUtils; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.validation.Errors; +import org.springframework.web.context.WebApplicationContext; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +@RunWith(SpringRunner.class) +@WebMvcTest +@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class }) +@Import(EnvUtil.class) +public class KycExchangeRequestValidatorTest { + @InjectMocks + KycExchangeRequestValidator kycExchangeRequestValidator; + @Mock + IdInfoHelper idInfoHelper; + @InjectMocks + AuthRequestValidator authRequestValidator; + @Before + public void before() { + ReflectionTestUtils.setField(kycExchangeRequestValidator, "idInfoHelper", idInfoHelper); + ReflectionTestUtils.setField(authRequestValidator, "idInfoHelper", idInfoHelper); + } + @Test + public void testSupportTrue() { + assertTrue(kycExchangeRequestValidator.supports(KycExchangeRequestDTO.class)); + } + @Test + public void testSupportFalse() { + assertFalse(kycExchangeRequestValidator.supports(KycAuthRequestValidator.class)); + } + @Test + @Ignore + public void testValidate_ValidRequest_NoErrors() { + KycExchangeRequestDTO kycExchangeRequestDTO = new KycExchangeRequestDTO(); + kycExchangeRequestDTO.setId("id"); + kycExchangeRequestDTO.setRequestTime(ZonedDateTime.now() + .format(DateTimeFormatter.ofPattern(EnvUtil.getDateTimePattern())).toString()); + kycExchangeRequestDTO.setKycToken("token"); + kycExchangeRequestDTO.setTransactionID("1234567890"); + List consentObtained=new ArrayList<>(); + consentObtained.add("phone"); + consentObtained.add("email"); + kycExchangeRequestDTO.setConsentObtained(consentObtained); + List locales=new ArrayList<>(); + locales.add("en"); + kycExchangeRequestDTO.setLocales(locales); + kycExchangeRequestDTO.setRespType("abc"); + Map metadata=new HashMap<>(); + kycExchangeRequestDTO.setMetadata(metadata); + Errors errors = new BeanPropertyBindingResult(kycExchangeRequestDTO, "kycExchangeRequestDTO"); + Mockito.when(idInfoHelper.isMatchtypeEnabled(Mockito.any())).thenReturn(Boolean.TRUE); + kycExchangeRequestValidator.validate(kycExchangeRequestDTO, errors); + Assert.assertFalse(errors.hasErrors()); + } + @Test + public void testValidate_NullRequest_InvalidInputParameterError() { + KycExchangeRequestDTO kycExchangeRequestDTO = new KycExchangeRequestDTO(); + Errors errors = new BeanPropertyBindingResult(kycExchangeRequestDTO, "kycExchangeRequestDTO"); + kycExchangeRequestValidator.validate(kycExchangeRequestDTO, errors); + Mockito.when(idInfoHelper.isMatchtypeEnabled(Mockito.any())).thenReturn(Boolean.TRUE); + Assert.assertTrue(errors.hasErrors()); + Assert.assertEquals("IDA-MLC-006", errors.getFieldError().getCode()); + } + @Test + public void testValidate_InvalidKycToken_MissingInputParameterError() { + KycExchangeRequestDTO request = new KycExchangeRequestDTO(); + request.setRequestTime("2023-10-31 10:00:00"); + request.setKycToken(null); + request.setTransactionID("1234567890"); + Errors errors = new BeanPropertyBindingResult(request, "kycExchangeRequestDTO"); + kycExchangeRequestValidator.validate(request, errors); + Mockito.when(idInfoHelper.isMatchtypeEnabled(Mockito.any())).thenReturn(Boolean.TRUE); + Assert.assertTrue(errors.hasErrors()); + Assert.assertEquals("IDA-MLC-009", errors.getFieldError().getCode()); + } + @Test + public void testValidate_EmptyConsentObtainedList_MissingInputParameterError() { + KycExchangeRequestDTO request = new KycExchangeRequestDTO(); + request.setRequestTime("2023-10-31 10:00:00"); + request.setKycToken("exampleToken"); + request.setTransactionID("exampleTransactionID"); + request.setConsentObtained(new ArrayList<>()); + Errors errors = new BeanPropertyBindingResult(request, "kycExchangeRequestDTO"); + Mockito.when(idInfoHelper.isMatchtypeEnabled(Mockito.any())).thenReturn(Boolean.TRUE); + kycExchangeRequestValidator.validate(request, errors); + Assert.assertTrue(errors.hasErrors()); + Assert.assertEquals("IDA-MLC-009", errors.getFieldError().getCode()); + } +} \ No newline at end of file diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java new file mode 100644 index 00000000000..ceda8fd7c41 --- /dev/null +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java @@ -0,0 +1,160 @@ +package io.mosip.authentication.esignet.integration.service; + +import io.mosip.esignet.api.dto.AuditDTO; +import io.mosip.esignet.api.util.Action; +import io.mosip.esignet.api.util.ActionStatus; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.authentication.esignet.integration.dto.AuditResponse; +import io.mosip.authentication.esignet.integration.helper.AuthTransactionHelper; +import io.mosip.kernel.core.http.ResponseWrapper; +import org.mockito.*; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; +import static org.mockito.ArgumentMatchers.*; + +@RunWith(MockitoJUnitRunner.class) +public class IdaAuditPluginImplTest { + @InjectMocks + private IdaAuditPluginImpl idaAuditPlugin; + @Mock + private AuthTransactionHelper authTransactionHelper; + @Mock + private ObjectMapper objectMapper; + @Mock + private RestTemplate restTemplate; + @Test + public void logAudit_WithValidDetails_ThenPass() { + Action action = Action.AUTHENTICATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + @Test + public void logAudit_WithThrowable_ThenPass() { + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + @Test + public void logAudit_WithUsername_WithValidDetails_ThenPass() { + String username = "username"; + Action action = Action.OIDC_CLIENT_UPDATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(username, action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUsername_WithThrowable() throws Exception { + String username = "username"; + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + @Test + public void logAudit_WithValidStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.ok(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + @Test + public void logAudit_WithUnauthorizedStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + @Test + public void logAudit_WithForbiddenStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.FORBIDDEN).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } +} \ No newline at end of file