Skip to content

Commit

Permalink
Added test case for coverage (#1123)
Browse files Browse the repository at this point in the history
* added test cases for idaVCIssuancePluginImpl

* test cases added for idaVCIssuancePluginImpl

* added test cases for idaVCIssuancePluginImpl

* added langCode converter in idaVCIssuancePluginImpl

* fix the build failed

* fix issue with lan code like en-US

* added test case

* added test cases for coverage

* added test case

* test case for coverage
  • Loading branch information
kaifk468 authored Nov 3, 2023
1 parent 3bcb2e0 commit 2c870cb
Show file tree
Hide file tree
Showing 7 changed files with 919 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package io.mosip.authentication.common.service.helper;

import io.mosip.authentication.common.service.entity.KycTokenData;
import io.mosip.authentication.common.service.repository.KycTokenDataRepository;
import io.mosip.authentication.common.service.repository.OIDCClientDataRepository;
import io.mosip.authentication.core.exception.IdAuthenticationBusinessException;
import io.mosip.authentication.core.spi.indauth.service.KycService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.*;

@RunWith(SpringRunner.class)
public class TokenValidationHelperTest {

/** The Kyc Service */
@Mock
private KycService kycService;

@Mock
private KycTokenDataRepository kycTokenDataRepo;

@Mock
private IdInfoHelper idInfoHelper;

@Mock
private OIDCClientDataRepository oidcClientDataRepo;

@InjectMocks
TokenValidationHelper tokenValidationHelper;


@Test
public void findAndValidateIssuedTokenTestWithValidDetails_thenPass() throws IdAuthenticationBusinessException {

KycTokenData kycTokenData = new KycTokenData();
kycTokenData.setPsuToken("1234567890");
kycTokenData.setKycToken("1234567890");
kycTokenData.setKycTokenStatus("ACTIVE");
kycTokenData.setOidcClientId("12345");
kycTokenData.setTokenIssuedDateTime(LocalDateTime.now());
kycTokenData.setIdVidHash("1234567");
kycTokenData.setRequestTransactionId("123456");
Mockito.when(kycTokenDataRepo.findByKycToken(Mockito.anyString())).thenReturn(Optional.of(kycTokenData));
Mockito.when( kycService.isKycTokenExpire(Mockito.any(),Mockito.anyString())).thenReturn(false);

tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123456", "1234567");

}

@Test
public void findAndValidateIssuedTokenTestWithInValidTokenDetails_thenFail() throws IdAuthenticationBusinessException {

Mockito.when(kycTokenDataRepo.findByKycToken(Mockito.anyString())).thenReturn(Optional.empty());
Mockito.when( kycService.isKycTokenExpire(Mockito.any(),Mockito.anyString())).thenReturn(false);

try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12346", "123456", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-001"));
}

}

@Test
public void findAndValidateIssuedTokenTestWithExpiredTokenDetails_thenFail() throws IdAuthenticationBusinessException {

KycTokenData kycTokenData = new KycTokenData();
kycTokenData.setPsuToken("1234567890");
kycTokenData.setKycToken("1234567890");
kycTokenData.setKycTokenStatus("ACTIVE");
kycTokenData.setOidcClientId("12345");
kycTokenData.setTokenIssuedDateTime(LocalDateTime.now());
kycTokenData.setIdVidHash("1234567");
kycTokenData.setRequestTransactionId("123456");
Mockito.when(kycTokenDataRepo.findByKycToken(Mockito.anyString())).thenReturn(Optional.of(kycTokenData));
Mockito.when( kycService.isKycTokenExpire(Mockito.any(),Mockito.anyString())).thenReturn(true);

try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123456", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-002"));
}

}

@Test
public void findAndValidateIssuedTokenTestWithInValidDetails_thenPass() throws IdAuthenticationBusinessException {

KycTokenData kycTokenData = new KycTokenData();
kycTokenData.setPsuToken("1234567890");
kycTokenData.setKycToken("1234567890");
kycTokenData.setKycTokenStatus("ACTIVE");
kycTokenData.setOidcClientId("12345");
kycTokenData.setTokenIssuedDateTime(LocalDateTime.now());
kycTokenData.setIdVidHash("1234567");
kycTokenData.setRequestTransactionId("123456");
Mockito.when(kycTokenDataRepo.findByKycToken(Mockito.anyString())).thenReturn(Optional.of(kycTokenData));
Mockito.when( kycService.isKycTokenExpire(Mockito.any(),Mockito.anyString())).thenReturn(false);


try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12346", "123456", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-004"));
}

try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123457", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-005"));
}

try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123456", "1234568");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-007"));
}

kycTokenData.setKycTokenStatus("EXPIRED");
try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123456", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-002"));
}

kycTokenData.setKycTokenStatus("PROCESSED");
try{
tokenValidationHelper.findAndValidateIssuedToken("1234567890", "12345", "123456", "1234567");
}catch (IdAuthenticationBusinessException e){
assert(e.getErrorCode().equalsIgnoreCase("IDA-KYE-003"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.mosip.authentication.common.service.impl;


import io.mosip.authentication.common.service.builder.MatchInputBuilder;
import io.mosip.authentication.common.service.config.IDAMappingConfig;
import io.mosip.authentication.common.service.helper.IdInfoHelper;
import io.mosip.authentication.common.service.repository.IdentityBindingCertificateRepository;
import io.mosip.authentication.common.service.transaction.manager.IdAuthSecurityManager;
import io.mosip.authentication.core.exception.IdAuthenticationBusinessException;
import io.mosip.authentication.core.indauth.dto.AuthRequestDTO;
import io.mosip.authentication.core.indauth.dto.IdentityInfoDTO;
import io.mosip.authentication.core.spi.indauth.match.MatchOutput;
import org.apache.commons.collections.map.HashedMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
public class KeyBindedTokenAuthServiceImplTest {

@InjectMocks
IdInfoHelper idInfoHelper;

/** The id info helper. */
@Mock
MatchInputBuilder matchInputBuilder;

/** The ida mapping config. */
@Mock
IDAMappingConfig idaMappingConfig;

@Mock
IdAuthSecurityManager securityManager;

@Mock
IdentityBindingCertificateRepository identityBindingCertificateRepository;

@InjectMocks
KeyBindedTokenAuthServiceImpl keyBindedTokenAuthService;

@Test
public void authenticateTestWithValidDetails_thenPass() throws IdAuthenticationBusinessException {
AuthRequestDTO authRequestDTO = new AuthRequestDTO();

ReflectionTestUtils.setField(keyBindedTokenAuthService,"idInfoHelper",idInfoHelper);
IdentityInfoDTO identityInfoDTO=new IdentityInfoDTO();
identityInfoDTO.setValue("value");
identityInfoDTO.setLanguage("end");
List<IdentityInfoDTO> list=new ArrayList<>();
list.add(identityInfoDTO);
Map<String, List<IdentityInfoDTO>> idInfo= new HashedMap();
idInfo.put("key",list);

MatchOutput matchOutput=new MatchOutput(5,true,"fingerpring",null,"end","idName");

matchOutput.setLanguage("end");
List<MatchOutput> matchOutputList=new ArrayList<>();
matchOutputList.add(matchOutput);
keyBindedTokenAuthService.authenticate(authRequestDTO,"individualId",idInfo,"partnerId");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package io.mosip.authentication.common.service.util;

import io.mosip.authentication.core.exception.IdAuthenticationBusinessException;
import io.mosip.kernel.keymanagerservice.util.KeymanagerUtil;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
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.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RunWith(SpringRunner.class)
public class KeyBindedTokenMatcherUtilTest {

@Mock
KeymanagerUtil keymanagerUtil;
@InjectMocks
KeyBindedTokenMatcherUtil keyBindedTokenMatcherUtil;


@Test
public void matchTestWithInValidToken_thenFail() {

Map<String, Object> properties =new HashMap<>();
Map<String, String> bindingCertificates =new HashMap<>();
Map<String, String> input =new HashMap<>();
input.put("individualId","individualId");
input.put("type","type");
input.put("format","jwt");
input.put("token",".eyJ");

try{
keyBindedTokenMatcherUtil.match(input, bindingCertificates, properties);
}catch (IdAuthenticationBusinessException e){
}
}
@Test
public void matchTestWithExpiredToken_thenFail() {

Map<String, Object> properties =new HashMap<>();
Map<String, String> bindingCertificates =new HashMap<>();
Map<String, String> input =new HashMap<>();
input.put("individualId","individualId");
input.put("type","type");
input.put("format","jwt");
input.put("token","eyJ0eXAiOiJKV1QiLCJ4NXQjUzI1NiI6IjBFSmtKMDYyWnZNZ0dKSk9BRVNYWFo1Tl9hamRDOG04Y0hPTXVKVVRGWUEiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2OTg5ODgyMTcsIm5iZiI6MTY5ODk4ODIxNywiZXhwIjoxNjk4OTg4ODIyLCJqdGkiOiJYZkpRaGVfU3RuNTNmaWc3YVV3V3MiLCJhdWQiOiJpZGEtYmluZGluZyIsInN1YiI6IjQxNTg2MTI2MDkiLCJpc3MiOiJwb3N0bWFuLWluamkifQ.bSqcJZlq5PyAExwPoww41OF-vBIyaADZ8OsXzA_7gtowNl0kChVAB11eIPEcjuFvYeQiSpQgNZsS2-w84ZBdiqh72kkJQLjN7ItMKNf-cekNRmG6XFf1os1vom7CwrguataoYvboiiXYw0WUfsZTmnhcOKC8XN3qAsB2YAyYEnBJBeKy5aCNAfJiOULTMrqAqcu-A1MA_wtAkaCJggiNxf1-5bJWjZYyQOkis0nHmbgWjzzThdd6TzMkLnUyNxzO2n1E9A19OJ2ZH0ZN1d46c8QBMsYmGX-Kz8B8GBDnDlwC4M5g4hmxuXCN6sBcVjAONl92LxI1htSZ6muv3xL1YQ");

try{
keyBindedTokenMatcherUtil.match(input, bindingCertificates, properties);
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-KBT-002",e.getErrorCode());
}
}

@Test
public void matchTestWithInValidThumbprint_thenFail() {

Map<String, Object> properties =new HashMap<>();
Map<String, String> bindingCertificates =new HashMap<>();
Map<String, String> input =new HashMap<>();
input.put("individualId","individualId");
input.put("type","type");
input.put("format","jwt");
input.put("token","eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJrdHkiOiJSU0EiLCJlIjoiQVFBQiIsInVzZSI6InNpZyIsImtpZCI6IkFBNzBWbUQ2OGltaUU2NDhSdDBzRWNhc1N3VkJDNGxtbno4ZUFPZmRYQzgiLCJhbGciOiJSUzI1NiIsIm4iOiJzZFNtY0o1bzJZX1JMOURORXFEQl9fV0xqV1RUY3JvRHpMaXRFRUxSb3h3MF9talV1azAyd1NlcTY3YlR4YzR0b2JMTS04Y19DNkVsNUgtQzlsTFF3VURZYnZ1VmxpX2lBLWtvZ0Rxa1dOTEEwbWZaLW9FRUtEYTlJV280VmY4MHl2Z09EN2hfSlpCWG53dHZjaTRqUlNaekc1R05sU2VJY25ZU0tiNVpvTTBOVDEtb0VrLU1tQnIxcmZGYWpGZ0hvMXhQSTQwUkN6UFhNWnkwOXY5Y2R5SVdTbWFzd2VRMmcxbEtsVHhqcVhibEtiTjZMQzNhUXc2azlFVVEzNjJ5QW5FMmZWOWpkcmNFb29RT0ZYZk1ZTHQ2UlFRMmJQSjIxb01aT0RGUjU0cHlVT0RxdlowMnczcnZMS0ozU2dvTHEtLVlDUTEzNi04cWJqNXpTVnNwMlEiLCJpYXQiOjE2OTg5OTQzMzR9.J4DNzGDNWE6AIIg7PAF8CZufFOOKA97ngBn1xMU05T9X_eqV9mfSk3G-fDXRRGS3ucS25gB2k6kOh7vt0eoVYEgw1lOQ2ERM2UoT7sWzUYvt0zedkP2zgkcubkeOwC-dY65_NiFRZ4iXudu38iLd2iQcAdwnp9e5HBfCFxiVkxIByMfGac6SwrCByNnPQnaiYn_988UKW7YVoqK4NK2kIJ405bz9kRWb8MMIRSTRskg0gYoQs9tCQGfD0QJWjJGk_Qqj2eDuH2pHresKELchjhe9hbbkajG021azpvPdq3t4PrYlqhiFajE-MRKwAR7Ey3_CfSHSoJ4mg2OBrhOVsA");

try{
keyBindedTokenMatcherUtil.match(input, bindingCertificates, properties);
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-MLC-009",e.getErrorCode());
}
}
@Test
public void matchTestWithInValidCerts_thenFail() throws IdAuthenticationBusinessException {
ReflectionTestUtils.setField(keyBindedTokenMatcherUtil, "iatAdjSeconds", 30000000);
Map<String, Object> properties =new HashMap<>();
Map<String, String> bindingCertificates =new HashMap<>();
Map<String, String> input =new HashMap<>();
input.put("individualId","individualId");
input.put("type","type");
input.put("format","jwt");
input.put("token","eyJ0eXAiOiJKV1QiLCJ4NXQjUzI1NiI6IjBFSmtKMDYyWnZNZ0dKSk9BRVNYWFo1Tl9hamRDOG04Y0hPTXVKVVRGWUEiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2OTg5ODgyMTcsIm5iZiI6MTY5ODk4ODIxNywiZXhwIjoxNjk4OTg4ODIyLCJqdGkiOiJYZkpRaGVfU3RuNTNmaWc3YVV3V3MiLCJhdWQiOiJpZGEtYmluZGluZyIsInN1YiI6IjQxNTg2MTI2MDkiLCJpc3MiOiJwb3N0bWFuLWluamkifQ.bSqcJZlq5PyAExwPoww41OF-vBIyaADZ8OsXzA_7gtowNl0kChVAB11eIPEcjuFvYeQiSpQgNZsS2-w84ZBdiqh72kkJQLjN7ItMKNf-cekNRmG6XFf1os1vom7CwrguataoYvboiiXYw0WUfsZTmnhcOKC8XN3qAsB2YAyYEnBJBeKy5aCNAfJiOULTMrqAqcu-A1MA_wtAkaCJggiNxf1-5bJWjZYyQOkis0nHmbgWjzzThdd6TzMkLnUyNxzO2n1E9A19OJ2ZH0ZN1d46c8QBMsYmGX-Kz8B8GBDnDlwC4M5g4hmxuXCN6sBcVjAONl92LxI1htSZ6muv3xL1YQ");

try {
keyBindedTokenMatcherUtil.match(input, bindingCertificates, properties);
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-KBT-001",e.getErrorCode());
}
}

@Test
public void matchTestWithValidCerts_thenFail() throws IdAuthenticationBusinessException {
ReflectionTestUtils.setField(keyBindedTokenMatcherUtil, "iatAdjSeconds", 300000000);
Map<String, Object> properties =new HashMap<>();
Map<String, String> bindingCertificates =new HashMap<>();
bindingCertificates.put("D04264274EB666F32018924E0044975D9E4DFDA8DD0BC9BC70738CB895131580-TYPE","X509");
Map<String, String> input =new HashMap<>();
input.put("individualId","individualId");
input.put("type","type");
input.put("format","jwt");
input.put("token","eyJ0eXAiOiJKV1QiLCJ4NXQjUzI1NiI6IjBFSmtKMDYyWnZNZ0dKSk9BRVNYWFo1Tl9hamRDOG04Y0hPTXVKVVRGWUEiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2OTg5ODgyMTcsIm5iZiI6MTY5ODk4ODIxNywiZXhwIjoxNjk4OTg4ODIyLCJqdGkiOiJYZkpRaGVfU3RuNTNmaWc3YVV3V3MiLCJhdWQiOiJpZGEtYmluZGluZyIsInN1YiI6IjQxNTg2MTI2MDkiLCJpc3MiOiJwb3N0bWFuLWluamkifQ.bSqcJZlq5PyAExwPoww41OF-vBIyaADZ8OsXzA_7gtowNl0kChVAB11eIPEcjuFvYeQiSpQgNZsS2-w84ZBdiqh72kkJQLjN7ItMKNf-cekNRmG6XFf1os1vom7CwrguataoYvboiiXYw0WUfsZTmnhcOKC8XN3qAsB2YAyYEnBJBeKy5aCNAfJiOULTMrqAqcu-A1MA_wtAkaCJggiNx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IYXGkcNwi7naSpo\n" +
"VULtcruR7Q3yCnXLJC4hyT7q8dp2GsmUiB/xP5jw2WtxwJZy60Syea0h2e8GEAmn\n" +
"K25CO5bPD/lEVhvwEzY2ZWg7ZMp1FE02fhFbSXpbF9BACy8UsEZ0Pcr0daWUtXTC\n" +
"5xbRhnAbNF0ixcSvZFcZvPhHDSnmnjABuHmuCUAQ\n" +
"-----END CERTIFICATE-----";
Certificate certificate=convertToCertificate(certificateString);
Mockito.when(keymanagerUtil.convertToCertificate(Mockito.anyString())).thenReturn(certificate);
try{
keyBindedTokenMatcherUtil.match(input, bindingCertificates, properties);
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-KBT-003",e.getErrorCode());
}
}

public static Certificate convertToCertificate(String certData) {
try {
StringReader strReader = new StringReader(certData);
PemReader pemReader = new PemReader(strReader);
PemObject pemObject = pemReader.readPemObject();
if (Objects.isNull(pemObject)) {
throw new RuntimeException("Invalid certificate");
}
byte[] certBytes = pemObject.getContent();
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
return certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
} catch (IOException | CertificateException e) {
throw new RuntimeException("Invalid certificate");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.mosip.authentication.common.service.util;


import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
public class TokenEncoderUtilTest {



@Test
public void encodeBase58TestWithVaidDetails_thenPass(){
String st="string";
String result=TokenEncoderUtil.encodeBase58(st.getBytes());
Assert.assertEquals("zVbyBrMk",result);
}

@Test
public void encodeBase58TestWithNull_thenFail(){
String st="string";
String result=TokenEncoderUtil.encodeBase58(null);
}

}
Loading

0 comments on commit 2c870cb

Please sign in to comment.