Skip to content

Commit

Permalink
Fixed authControllerTest
Browse files Browse the repository at this point in the history
Signed-off-by: ase-101 <[email protected]>
  • Loading branch information
ase-101 committed Oct 23, 2023
1 parent d4f91c5 commit e7b74d2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import io.mosip.authentication.common.service.builder.AuthTransactionBuilder;
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.partner.dto.PartnerDTO;
import io.mosip.authentication.core.spi.indauth.facade.AuthFacade;
import io.mosip.authentication.core.spi.partner.service.PartnerService;
import io.mosip.authentication.core.util.IdTypeUtil;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -53,7 +62,6 @@
*
* @author Prem Kumar
*/
@Ignore
@RunWith(SpringRunner.class)
@WebMvcTest
@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class })
Expand All @@ -62,9 +70,9 @@ public class AuthControllerTest {
@Mock
private RestHelper restHelper;

@Autowired
@Mock
EnvUtil env;

@Autowired
Environment environment;

Expand Down Expand Up @@ -92,17 +100,31 @@ public class AuthControllerTest {
@Mock
private AuthRequestValidator authRequestValidator;

@Mock
private IdTypeUtil idTypeUtil;

@Mock
private AuthTransactionHelper authTransactionHelper;

@Mock
private PartnerService partnerService;

Errors error = new BindException(AuthRequestDTO.class, "authReqDTO");
Errors errors = new BindException(EkycAuthRequestDTO.class, "kycAuthReqDTO");

TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest();

@Before
public void before() {
ReflectionTestUtils.setField(env, "env", environment);
ReflectionTestUtils.setField(auditFactory, "env", env);
//ReflectionTestUtils.setField(auditFactory, "env", env);
ReflectionTestUtils.setField(restFactory, "env", env);
ReflectionTestUtils.invokeMethod(authController, "initAuthRequestBinder", binder);
ReflectionTestUtils.setField(authController, "authFacade", authFacade);
ReflectionTestUtils.setField(authFacade, "env", env);

requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data");
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info");
}

/*
Expand All @@ -116,7 +138,17 @@ public void showRequestValidator()
authReqDTO.setIndividualIdType(IdType.UIN.getType());
Errors error = new BindException(authReqDTO, "authReqDTO");
error.rejectValue("id", "errorCode", "defaultMessage");
authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", null);
TestHttpServletRequest requestWithMetadata = new TestHttpServletRequest();
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_DATA, "identity data");
requestWithMetadata.putMetadata(IdAuthCommonConstants.IDENTITY_INFO, "identity info");
Optional<PartnerDTO> partner = Optional.empty();
Mockito.when(partnerService.getPartner("partnerId", authReqDTO.getMetadata())).thenReturn(partner);
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(authReqDTO, !true, partner))
.thenReturn(AuthTransactionBuilder.newInstance());
Mockito.when(authTransactionHelper.createDataValidationException(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(new IdAuthenticationAppException(IdAuthenticationErrorConstants.DATA_VALIDATION_FAILED));

authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", requestWithMetadata);

}

Expand All @@ -125,9 +157,17 @@ public void authenticationFailed()
throws IdAuthenticationAppException, IdAuthenticationBusinessException, IdAuthenticationDaoException {
AuthRequestDTO authReqDTO = new AuthRequestDTO();
authReqDTO.setIndividualIdType(IdType.UIN.getType());
Optional<PartnerDTO> partner = Optional.empty();
AuthTransactionBuilder authTransactionBuilder = AuthTransactionBuilder.newInstance();
Mockito.when(partnerService.getPartner("partnerId", authReqDTO.getMetadata())).thenReturn(partner);
Mockito.when(authTransactionHelper.createAndSetAuthTxnBuilderMetadataToRequest(authReqDTO, !true, partner))
.thenReturn(authTransactionBuilder);
Mockito.when(authTransactionHelper.createUnableToProcessException(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(new IdAuthenticationAppException( IdAuthenticationErrorConstants.UNABLE_TO_PROCESS));
Mockito.when(authFacade.authenticateIndividual(Mockito.any(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any()))
.thenThrow(new IdAuthenticationBusinessException(IdAuthenticationErrorConstants.UIN_DEACTIVATED));
authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", null);

authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", requestWithMetadata);

}

Expand All @@ -137,29 +177,29 @@ public void authenticationSuccess()
AuthRequestDTO authReqDTO = new AuthRequestDTO();
authReqDTO.setIndividualIdType(IdType.UIN.getType());
Mockito.when(authFacade.authenticateIndividual(authReqDTO, true, "123456", "12345", true, new TestObjectWithMetadata())).thenReturn(new AuthResponseDTO());
authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", null);
authController.authenticateIndividual(authReqDTO, error, "123456", "123456","1234567", requestWithMetadata);

}

@Test
public void TestValidOtpRequest()
throws IdAuthenticationAppException, IdAuthenticationBusinessException, IdAuthenticationDaoException {
AuthRequestDTO authRequestDTO = getRequestDto();
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", null);
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", requestWithMetadata);
}

@Test
public void TestValidDemoRequest()
throws IdAuthenticationAppException, IdAuthenticationBusinessException, IdAuthenticationDaoException {
AuthRequestDTO authRequestDTO = getRequestDto();
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", null);
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", requestWithMetadata);
}

@Test
public void TestValidPinRequest()
throws IdAuthenticationAppException, IdAuthenticationBusinessException, IdAuthenticationDaoException {
AuthRequestDTO authRequestDTO = getRequestDto();
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", null);
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", requestWithMetadata);
}

@Test
Expand Down Expand Up @@ -194,7 +234,7 @@ public void TestValidBioFingerPrintRequest()

request.setBiometrics(bioIdentityList);
authRequestDTO.setRequest(request);
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", null);
authController.authenticateIndividual(authRequestDTO, error, "123456", "123456","1234567", requestWithMetadata);
}

private AuthRequestDTO getRequestDto() {
Expand All @@ -203,7 +243,7 @@ private AuthRequestDTO getRequestDto() {
authRequestDTO.setIndividualId("274390482564");
authRequestDTO.setIndividualIdType(IdType.UIN.getType());
authRequestDTO.setRequestTime(Instant.now().atOffset(ZoneOffset.of("+0530")) // offset
.format(DateTimeFormatter.ofPattern(EnvUtil.getDateTimePattern())).toString());
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")).toString());
authRequestDTO.setTransactionID("1234567890");
authRequestDTO.setVersion("1.0");
return authRequestDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.authentication.service.controller;
package io.mosip.authentication.service.kyc.controller;

import io.mosip.authentication.common.service.builder.AuthTransactionBuilder;
import io.mosip.authentication.common.service.helper.AuthTransactionHelper;
Expand Down Expand Up @@ -88,11 +88,10 @@ public void delegatedVCExchange_withValidInput_thenPass() throws Exception {
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);
.thenReturn(AuthTransactionBuilder.newInstance());

Mockito.when(vciFacade.processVciExchange(vciExchangeRequestDTO, "auth-partner-id",
"oidc-client-id", vciExchangeRequestDTO.getMetadata(), requestWithMetadata)).thenReturn(vciExchangeResponseDTO);
Expand Down

0 comments on commit e7b74d2

Please sign in to comment.