Skip to content

Commit

Permalink
Added test case for IdentityWalletBindingController, IdentityKeyBindi…
Browse files Browse the repository at this point in the history
…ngServiceImpl, IdentityKeyBindingRequestValidatorTest (#1119)

* added test case for VCITransactionHelper.class and AuthTransactionHelper.class

* added testcase for authtransactionhelper and vcitransactionhelper

Signed-off-by: Mohd Kaif Siddique <[email protected]>

* added test case

Signed-off-by: Mohd Kaif Siddique <[email protected]>

* added test case for IdentityWalletBindingController, IdentityKeyBindingServiceImpl, IdentityKeyBindingRequestValidatorTest

Signed-off-by: Mohd Kaif Siddique <[email protected]>

---------

Signed-off-by: Mohd Kaif Siddique <[email protected]>
  • Loading branch information
kaifk468 authored Oct 27, 2023
1 parent f0d008f commit 5cdb93f
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package io.mosip.authentication.service.kyc.controller;


import io.mosip.authentication.common.service.helper.AuditHelper;
import io.mosip.authentication.common.service.helper.AuthTransactionHelper;
import io.mosip.authentication.common.service.util.TestHttpServletRequest;
import io.mosip.authentication.core.constant.IdAuthCommonConstants;
import io.mosip.authentication.core.exception.IdAuthenticationAppException;
import io.mosip.authentication.core.exception.IdAuthenticationBusinessException;
import io.mosip.authentication.core.exception.IdAuthenticationDaoException;
import io.mosip.authentication.core.indauth.dto.*;
import io.mosip.authentication.core.spi.indauth.facade.IdentityKeyBindingFacade;
import io.mosip.authentication.core.spi.partner.service.PartnerService;
import io.mosip.authentication.core.util.IdTypeUtil;
import io.mosip.authentication.service.kyc.validator.IdentityKeyBindingRequestValidator;
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.springframework.test.context.junit4.SpringRunner;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

@RunWith(SpringRunner.class)
public class IdentityWalletBindingControllerTest {

/** The auth facade. */
@Mock
IdentityKeyBindingFacade keyIdentityFacade;

@Mock
AuditHelper auditHelper;

@Mock
IdTypeUtil idTypeUtil;

@Mock
AuthTransactionHelper authTransactionHelper;

@Mock
PartnerService partnerService;

/** The KycExchangeRequestValidator */
@Mock
IdentityKeyBindingRequestValidator identityKeyBindingRequestValidator;

@InjectMocks
IdentityWalletBindingController identityWalletBindingController;

Errors errors = new BindException(IdentityKeyBindingRequestDTO.class, "identityKeyBindingRequestDTO");


IdentityKeyBindingDTO identityKeyBindingDTO;

IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO;

AuthResponseDTO authResponseDTO;

IdentityKeyBindingResponseDto keyBindingResponseDto;

IdentityKeyBindingRespDto identityKeyBindingRespDto;

@Before
public void before() {
identityKeyBindingDTO = new IdentityKeyBindingDTO();
identityKeyBindingDTO.setPublicKeyJWK(null);
identityKeyBindingDTO.setAuthFactorType("WLA");

RequestDTO requestDTO = new RequestDTO();
requestDTO.setDemographics(null);
requestDTO.setBiometrics(null);
requestDTO.setOtp(null);
requestDTO.setStaticPin(null);
requestDTO.setTimestamp(null);

identityKeyBindingRequestDTO = new IdentityKeyBindingRequestDTO();

identityKeyBindingRequestDTO.setIdentityKeyBinding(identityKeyBindingDTO);

identityKeyBindingRequestDTO.setRequest(requestDTO);
identityKeyBindingRequestDTO.setConsentObtained(false);
identityKeyBindingRequestDTO.setRequestHMAC(null);
identityKeyBindingRequestDTO.setRequestSessionKey(null);
identityKeyBindingRequestDTO.setMetadata(null);
identityKeyBindingRequestDTO.setIndividualIdType("UIN");


keyBindingResponseDto = new IdentityKeyBindingResponseDto();

IdentityKeyBindingRespDto identityKeyBindingRespDto = new IdentityKeyBindingRespDto();
identityKeyBindingRespDto.setIdentityCertificate(null);
identityKeyBindingRespDto.setBindingAuthStatus(true);
identityKeyBindingRespDto.setAuthToken("token");
keyBindingResponseDto.setResponse(identityKeyBindingRespDto);

authResponseDTO= new AuthResponseDTO();
authResponseDTO.setId("123");
authResponseDTO.setResponseTime("123");
authResponseDTO.setResponse(null);

}

@Test
public void processIdKeyBindingTest() throws IdAuthenticationBusinessException, IdAuthenticationDaoException, IdAuthenticationAppException {


Mockito.when(partnerService.getPartner(Mockito.anyString(),Mockito.anyMap())).thenReturn(null);
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(Mockito.any(),Mockito.anyBoolean(),Mockito.any())).thenReturn(null);

TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest();
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data");;
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info");

Mockito.when(keyIdentityFacade.authenticateIndividual(Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenReturn(authResponseDTO);

Mockito.when(keyIdentityFacade.processIdentityKeyBinding(Mockito.any(),Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenReturn(keyBindingResponseDto);
IdentityKeyBindingResponseDto identityKeyBindingResponseDto = identityWalletBindingController.processIdKeyBinding(identityKeyBindingRequestDTO, errors, "123", "123", "123", requestWithMetadata);
Assert.assertEquals(keyBindingResponseDto,identityKeyBindingResponseDto);
}

@Test
public void processIdKeyBindingWithInvalidDetails_thenFail() throws IdAuthenticationBusinessException, IdAuthenticationDaoException, IdAuthenticationAppException {

Mockito.when(partnerService.getPartner(Mockito.anyString(),Mockito.anyMap())).thenReturn(null);
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(Mockito.any(),Mockito.anyBoolean(),Mockito.any())).thenReturn(null);
//Mockito.when(identityKeyBindingRequestValidator.validateIdvId(Mockito.anyString(),Mockito.anyString(),errors)).thenReturn(null);

TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest();
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data");;
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info");

Mockito.when(keyIdentityFacade.authenticateIndividual(Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenThrow(IdAuthenticationBusinessException.class);

keyBindingResponseDto.setResponse(identityKeyBindingRespDto);
Mockito.when(keyIdentityFacade.processIdentityKeyBinding(Mockito.any(),Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenReturn(keyBindingResponseDto);
Mockito.when(authTransactionHelper.createDataValidationException(Mockito.any(),Mockito.any(),Mockito.any())).thenThrow(IdAuthenticationAppException.class);
try{
Errors errors = new BindException(identityKeyBindingRequestDTO, "identityKeyBindingRequestDTO");
errors.rejectValue("id", "errorCode", "defaultMessage");
IdentityKeyBindingResponseDto identityKeyBindingResponseDto = identityWalletBindingController.processIdKeyBinding(identityKeyBindingRequestDTO, errors, "123", "123", "123", requestWithMetadata);
Assert.fail();
}catch (Exception e){}

}

@Test
public void processIdKeyBindingTest2() throws IdAuthenticationBusinessException, IdAuthenticationDaoException, IdAuthenticationAppException {
Mockito.when(partnerService.getPartner(Mockito.anyString(),Mockito.anyMap())).thenReturn(null);
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(Mockito.any(),Mockito.anyBoolean(),Mockito.any())).thenReturn(null);

TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest();
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data");;
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info");

Mockito.when(keyIdentityFacade.authenticateIndividual(Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenThrow(new IdAuthenticationBusinessException("IDA-IKB-004","error"));
Mockito.when(keyIdentityFacade.processIdentityKeyBinding(Mockito.any(),Mockito.any(),Mockito.anyString(),Mockito.anyString(),Mockito.any())).thenReturn(keyBindingResponseDto);
try{
IdentityKeyBindingResponseDto identityKeyBindingResponseDto = identityWalletBindingController.processIdKeyBinding(identityKeyBindingRequestDTO, errors, "123", "123", "123", requestWithMetadata);
Assert.fail();
}catch (Exception e){}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package io.mosip.authentication.service.kyc.impl;

import io.mosip.authentication.common.service.config.IDAMappingConfig;
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.IdentityInfoDTO;
import io.mosip.authentication.core.indauth.dto.IdentityKeyBindingDTO;
import io.mosip.authentication.core.indauth.dto.IdentityKeyBindingRequestDTO;
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.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import java.security.cert.CertificateEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
public class IdentityKeyBindingServiceImplTest {

@Mock
IDAMappingConfig idMappingConfig;

@Mock
IdentityBindingCertificateRepository bindingCertificateRepo;

@Mock
IdAuthSecurityManager securityManager;

@InjectMocks
IdentityKeyBindingServiceImpl identityKeyBindingServiceImpl;

Map<String,Object> pubblicKeyMap;

@Before
public void initialize() {
pubblicKeyMap = new HashMap<>();
pubblicKeyMap.put("n", "isAXe1AStinOg3KSCyTDAvu38KRS7ZmKv3Etmt7lSy3SPEg1jOqycdpL4YfFf2uh4rrUEMwsizyIlvWrN6C_ytEx8Non6noXnYfuuePRvL6kaTGdd_lbrC7eh1FI2c2cPzWRTq-CMBCSAdxmjD6PIqaVk5WtliU4qt27F5xfo7lG8lMlREgLb7u0HB9W7B8PjxvWmZ6cDle6eSnb1zOxAAFzB-GbGhRpPF-6ki25mdUrWJGlEkXGSCW1SohSM3YKPJW_xY6_520XdSeHFS9X84f6BXEz_fYTQcBPiNKaxObRkqZ-24PnRzy5vOytjeEnwusenBUHtri4aj1rKkTmIQ");
pubblicKeyMap.put("e", "AQAB");
pubblicKeyMap.put("kid", "zcbgDyrQdhwLlaEPW_JeKTE5CiUCMLdDvftRC5Y8h8U");
pubblicKeyMap.put("alg", "RS256");
pubblicKeyMap.put("exp", "exp");
}



@Test
public void isPublicKeyBindedWithValidDetails_thenPass() throws IdAuthenticationBusinessException {

Mockito.when(securityManager.hash(Mockito.anyString())).thenReturn("idVidHash");
Mockito.when(bindingCertificateRepo.countPublicKeysByIdHash(Mockito.anyString(),Mockito.any())).thenReturn(1);

boolean flag=identityKeyBindingServiceImpl.isPublicKeyBinded("idVid", pubblicKeyMap);
Assert.assertTrue(flag);

}

@Test
public void createAndSaveKeyBindingCertificateWithValidDetails_thenPass() throws CertificateEncodingException, IdAuthenticationBusinessException {

ReflectionTestUtils.setField(identityKeyBindingServiceImpl,"defaultLangCode","eng");
IdentityKeyBindingDTO identityKeyBindingDTO=new IdentityKeyBindingDTO();
identityKeyBindingDTO.setPublicKeyJWK(pubblicKeyMap);

IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO=new IdentityKeyBindingRequestDTO();
identityKeyBindingRequestDTO.setIdentityKeyBinding(identityKeyBindingDTO);

Map<String, List<IdentityInfoDTO>> identityInfo=new HashMap<>();
List<IdentityInfoDTO> identityInfoDTOList=new ArrayList<>();
IdentityInfoDTO identityInfoDTO=new IdentityInfoDTO();
identityInfoDTO.setLanguage("eng");
identityInfoDTO.setValue("value");
identityInfoDTOList.add(identityInfoDTO);
identityInfo.put("name",identityInfoDTOList);

Map.Entry<String, String> certificateEntry=Map.entry("certThumbprint","certificateData");

Mockito.when(securityManager.generateKeyBindingCertificate(Mockito.any(),Mockito.any())).thenReturn(certificateEntry);
Mockito.when(securityManager.hash(Mockito.anyString())).thenReturn("idVidHash");
List<String> names=new ArrayList<>();
names.add("name");
Mockito.when(idMappingConfig.getName()).thenReturn(names);

identityKeyBindingServiceImpl.createAndSaveKeyBindingCertificate(identityKeyBindingRequestDTO,identityInfo,"token","partnerId");

}

@Test
public void createAndSaveKeyBindingCertificateWithInValidIdentityName_thenFail() throws CertificateEncodingException, IdAuthenticationBusinessException {

ReflectionTestUtils.setField(identityKeyBindingServiceImpl,"defaultLangCode","eng");
IdentityKeyBindingDTO identityKeyBindingDTO=new IdentityKeyBindingDTO();
identityKeyBindingDTO.setPublicKeyJWK(pubblicKeyMap);

IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO=new IdentityKeyBindingRequestDTO();
identityKeyBindingRequestDTO.setIdentityKeyBinding(identityKeyBindingDTO);

Map<String, List<IdentityInfoDTO>> identityInfo=new HashMap<>();
List<IdentityInfoDTO> identityInfoDTOList=new ArrayList<>();
IdentityInfoDTO identityInfoDTO=new IdentityInfoDTO();
identityInfoDTO.setLanguage("eng");
identityInfoDTO.setValue("value");
identityInfoDTOList.add(identityInfoDTO);
identityInfo.put("name",identityInfoDTOList);

Map.Entry<String, String> certificateEntry=Map.entry("certThumbprint","certificateData");

Mockito.when(securityManager.generateKeyBindingCertificate(Mockito.any(),Mockito.any())).thenReturn(certificateEntry);
Mockito.when(securityManager.hash(Mockito.anyString())).thenReturn("idVidHash");
List<String> names=new ArrayList<>();
names.add("name");

try{
identityKeyBindingServiceImpl.createAndSaveKeyBindingCertificate(identityKeyBindingRequestDTO,identityInfo,"token","partnerId");
Assert.fail();
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-IKB-004",e.getErrorCode());
}
}

@Test
public void createAndSaveKeyBindingCertificateWithInValidCertificateEntry_thenFail() throws CertificateEncodingException, IdAuthenticationBusinessException {

ReflectionTestUtils.setField(identityKeyBindingServiceImpl,"defaultLangCode","eng");
IdentityKeyBindingDTO identityKeyBindingDTO=new IdentityKeyBindingDTO();
identityKeyBindingDTO.setPublicKeyJWK(pubblicKeyMap);

IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO=new IdentityKeyBindingRequestDTO();
identityKeyBindingRequestDTO.setIdentityKeyBinding(identityKeyBindingDTO);

Map<String, List<IdentityInfoDTO>> identityInfo=new HashMap<>();
List<IdentityInfoDTO> identityInfoDTOList=new ArrayList<>();
IdentityInfoDTO identityInfoDTO=new IdentityInfoDTO();
identityInfoDTO.setLanguage("eng");
identityInfoDTO.setValue("value");
identityInfoDTOList.add(identityInfoDTO);
identityInfo.put("name",identityInfoDTOList);

Mockito.when(securityManager.generateKeyBindingCertificate(Mockito.any(),Mockito.any())).thenThrow(CertificateEncodingException.class);
Mockito.when(securityManager.hash(Mockito.anyString())).thenReturn("idVidHash");
List<String> names=new ArrayList<>();
names.add("name");
Mockito.when(idMappingConfig.getName()).thenReturn(names);

try{
identityKeyBindingServiceImpl.createAndSaveKeyBindingCertificate(identityKeyBindingRequestDTO,identityInfo,"token","partnerId");
Assert.fail();
}catch (IdAuthenticationBusinessException e){
Assert.assertEquals("IDA-IKB-005",e.getErrorCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.mosip.authentication.service.kyc.validator;


import io.mosip.authentication.common.service.helper.IdInfoHelper;
import io.mosip.authentication.core.indauth.dto.IdentityKeyBindingRequestDTO;
import io.mosip.authentication.core.indauth.dto.RequestDTO;
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.springframework.test.context.junit4.SpringRunner;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import static org.mockito.Mockito.mock;

@RunWith(SpringRunner.class)
public class IdentityKeyBindingRequestValidatorTest {

@Mock
IdInfoHelper idInfoHelper;

@Mock
Errors errors;

@InjectMocks
IdentityKeyBindingRequestValidator identityKeyBindingRequestValidator;

@Test
public void validateWithValidDetails_thenPass(){

IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO = new IdentityKeyBindingRequestDTO();
identityKeyBindingRequestDTO.setIdentityKeyBinding(null);
identityKeyBindingRequestDTO.setIndividualIdType("UIN");
identityKeyBindingRequestDTO.setIndividualId("123456789012");
RequestDTO requestDTO = new RequestDTO();
requestDTO.setBiometrics(null);
requestDTO.setOtp("123456");
requestDTO.setTimestamp("2019-02-20T10:00:00.000Z");
identityKeyBindingRequestDTO.setRequest(requestDTO);
//Mockito.when(errors.hasErrors()).thenReturn(false);
Errors errors = new BeanPropertyBindingResult(identityKeyBindingRequestDTO, "identityKeyBindingRequestDTO");
//Mockito.when(idInfoHelper.isMatchtypeEnabled(Mockito.any())).thenReturn(Boolean.TRUE);
identityKeyBindingRequestValidator.validate(identityKeyBindingRequestDTO, errors);

}

@Test
public void testValidateWithInvalidTarget() {
IdentityKeyBindingRequestDTO identityKeyBindingRequestDTO = new IdentityKeyBindingRequestDTO();
errors = new BeanPropertyBindingResult(identityKeyBindingRequestDTO, "target");
identityKeyBindingRequestValidator.validate(null, errors);
Assert.assertTrue(errors.hasErrors());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public void GetAuthTokenWithValidDetails_thenPass() throws Exception {
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.ok()
.header("authorization", expectedAuthToken)
.build();

when(restTemplate.exchange(Mockito.any(RequestEntity.class), Mockito.any(ParameterizedTypeReference.class)))
.thenReturn(responseEntity);

String authToken = authTransactionHelper.getAuthToken();
Assert.assertEquals(expectedAuthToken, authToken);
}
Expand Down

0 comments on commit 5cdb93f

Please sign in to comment.