diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/OTPServiceImpl.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/OTPServiceImpl.java index e6a89075dcb..69c057eda35 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/OTPServiceImpl.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/OTPServiceImpl.java @@ -224,7 +224,22 @@ private OtpResponseDTO doGenerateOTP(OtpRequestDTO otpRequestDto, String partner valueMap.put(IdAuthCommonConstants.PHONE_NUMBER, phoneNumber); valueMap.put(IdAuthCommonConstants.EMAIL, email); - if((phoneNumber == null || phoneNumber.isEmpty()) && (email == null || email.isEmpty())) { + List otpChannel = otpRequestDto.getOtpChannel(); + if ((phoneNumber == null || phoneNumber.isEmpty()) && otpChannel.contains(PHONE) && !otpChannel.contains(EMAIL)) { + throw new IdAuthenticationBusinessException( + IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), + IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + + ". Phone Number is not found in identity data."); + } + + if ((email == null || email.isEmpty()) && otpChannel.contains(EMAIL) && !otpChannel.contains(PHONE)) { + throw new IdAuthenticationBusinessException( + IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), + IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + + ". Email ID is not found in identity data."); + } + + if((phoneNumber == null || phoneNumber.isEmpty()) && (email == null || email.isEmpty()) && (otpChannel.contains(PHONE) && otpChannel.contains(EMAIL))) { throw new IdAuthenticationBusinessException( IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() diff --git a/authentication/authentication-common/src/test/java/io/mosip/authentication/common/service/impl/OTPServiceImplTest.java b/authentication/authentication-common/src/test/java/io/mosip/authentication/common/service/impl/OTPServiceImplTest.java index 2687322ba8f..ceba1209f56 100644 --- a/authentication/authentication-common/src/test/java/io/mosip/authentication/common/service/impl/OTPServiceImplTest.java +++ b/authentication/authentication-common/src/test/java/io/mosip/authentication/common/service/impl/OTPServiceImplTest.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -215,12 +216,12 @@ public void TestgenerateOtp() throws IdAuthenticationBusinessException, RestServ @SuppressWarnings("rawtypes") @Test - public void TestPhonenumberisNull() throws IdAuthenticationBusinessException, RestServiceException { + public void TestPhonenumberisNull_Phone_Channel_Alone() throws IdAuthenticationBusinessException, RestServiceException { OtpRequestDTO otpRequestDto = new OtpRequestDTO(); otpRequestDto.setId("id"); otpRequestDto.setRequestTime(new SimpleDateFormat(EnvUtil.getDateTimePattern()).format(new Date())); otpRequestDto.setTransactionID("1234567890"); - ArrayList channelList = new ArrayList(); + List channelList = List.of("PHONE"); otpRequestDto.setOtpChannel(channelList); otpRequestDto.setIndividualId("2345678901234"); otpRequestDto.setIndividualIdType(IdType.UIN.getType()); @@ -246,20 +247,130 @@ public void TestPhonenumberisNull() throws IdAuthenticationBusinessException, Re Mockito.any())).thenReturn(true); try { otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + Assert.fail(); } catch(IdAuthenticationBusinessException ex) { assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), ex.getErrorCode()); - assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + ". Both Phone Number and Email ID are not found in identity data.", ex.getErrorText()); + assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + ". Phone Number is not found in identity data.", ex.getErrorText()); } } - - @Test(expected = IdAuthenticationBusinessException.class) - public void TestPhoneorEmailisNull() throws IdAuthenticationBusinessException, RestServiceException { + + @SuppressWarnings("rawtypes") + @Test + public void TestPhonenumberisNull_bothChannels() throws IdAuthenticationBusinessException, RestServiceException { OtpRequestDTO otpRequestDto = new OtpRequestDTO(); otpRequestDto.setId("id"); otpRequestDto.setRequestTime(new SimpleDateFormat(EnvUtil.getDateTimePattern()).format(new Date())); otpRequestDto.setTransactionID("1234567890"); - ArrayList channelList = new ArrayList(); + List channelList = List.of("PHONE", "EMAIL"); + otpRequestDto.setOtpChannel(channelList); + otpRequestDto.setIndividualId("2345678901234"); + otpRequestDto.setIndividualIdType(IdType.UIN.getType()); + otpRequestDto.setRequestTime("2019-02-18T18:17:48.923+05:30"); + Map valueMap = new HashMap<>(); + Map idInfo = new HashMap<>(); + idInfo.put("email", "abc@test.com"); + valueMap.put("response", idInfo); + Mockito.when(idAuthService.processIdType(Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anySet())) + .thenReturn(valueMap); + Mockito.when(idAuthService.getToken(Mockito.any())).thenReturn("2345678901234"); + Mockito.when(autntxnrepository.countRequestDTime(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1); + Mockito.when(uinHashSaltRepo.retrieveSaltById(Mockito.anyInt())).thenReturn("2344"); + Mockito.when(idAuthSecurityManager.getUser()).thenReturn("ida_app_user"); + RestRequestDTO value = getRestDto(); + Mockito.when(restRequestFactory.buildRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(value); + ResponseWrapper response = new ResponseWrapper<>(); + Map map = new HashMap<>(); + map.put("otp", "123456"); + response.setResponse(map); + Mockito.when(restHelper.requestSync(Mockito.any())).thenReturn(response); + Mockito.when(otpManager.sendOtp(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), + Mockito.any())).thenReturn(true); + otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + } + + @SuppressWarnings("rawtypes") + @Test + public void TestEmailIdisNull_Email_Channel_Alone() throws IdAuthenticationBusinessException, RestServiceException { + OtpRequestDTO otpRequestDto = new OtpRequestDTO(); + otpRequestDto.setId("id"); + otpRequestDto.setRequestTime(new SimpleDateFormat(EnvUtil.getDateTimePattern()).format(new Date())); + otpRequestDto.setTransactionID("1234567890"); + List channelList = List.of("EMAIL"); + otpRequestDto.setOtpChannel(channelList); + otpRequestDto.setIndividualId("2345678901234"); + otpRequestDto.setIndividualIdType(IdType.UIN.getType()); + otpRequestDto.setRequestTime("2019-02-18T18:17:48.923+05:30"); + Map valueMap = new HashMap<>(); + Map idInfo = new HashMap<>(); + idInfo.put("phone", "9292292934"); + valueMap.put("response", idInfo); + Mockito.when(idAuthService.processIdType(Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anySet())) + .thenReturn(valueMap); + Mockito.when(idAuthService.getToken(Mockito.any())).thenReturn("2345678901234"); + Mockito.when(autntxnrepository.countRequestDTime(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1); + Mockito.when(uinHashSaltRepo.retrieveSaltById(Mockito.anyInt())).thenReturn("2344"); + Mockito.when(idAuthSecurityManager.getUser()).thenReturn("ida_app_user"); + RestRequestDTO value = getRestDto(); + Mockito.when(restRequestFactory.buildRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(value); + ResponseWrapper response = new ResponseWrapper<>(); + Map map = new HashMap<>(); + map.put("otp", "123456"); + response.setResponse(map); + Mockito.when(restHelper.requestSync(Mockito.any())).thenReturn(response); + Mockito.when(otpManager.sendOtp(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), + Mockito.any())).thenReturn(true); + try { + otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + Assert.fail(); + } + catch(IdAuthenticationBusinessException ex) { + assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), ex.getErrorCode()); + assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + ". Email ID is not found in identity data.", ex.getErrorText()); + } + } + + @SuppressWarnings("rawtypes") + @Test + public void TestEmailIdisNull_bothChannels() throws IdAuthenticationBusinessException, RestServiceException { + OtpRequestDTO otpRequestDto = new OtpRequestDTO(); + otpRequestDto.setId("id"); + otpRequestDto.setRequestTime(new SimpleDateFormat(EnvUtil.getDateTimePattern()).format(new Date())); + otpRequestDto.setTransactionID("1234567890"); + List channelList = List.of("PHONE", "EMAIL"); + otpRequestDto.setOtpChannel(channelList); + otpRequestDto.setIndividualId("2345678901234"); + otpRequestDto.setIndividualIdType(IdType.UIN.getType()); + otpRequestDto.setRequestTime("2019-02-18T18:17:48.923+05:30"); + Map valueMap = new HashMap<>(); + Map idInfo = new HashMap<>(); + idInfo.put("phone", "9384848384"); + valueMap.put("response", idInfo); + Mockito.when(idAuthService.processIdType(Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anySet())) + .thenReturn(valueMap); + Mockito.when(idAuthService.getToken(Mockito.any())).thenReturn("2345678901234"); + Mockito.when(autntxnrepository.countRequestDTime(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1); + Mockito.when(uinHashSaltRepo.retrieveSaltById(Mockito.anyInt())).thenReturn("2344"); + Mockito.when(idAuthSecurityManager.getUser()).thenReturn("ida_app_user"); + RestRequestDTO value = getRestDto(); + Mockito.when(restRequestFactory.buildRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(value); + ResponseWrapper response = new ResponseWrapper<>(); + Map map = new HashMap<>(); + map.put("otp", "123456"); + response.setResponse(map); + Mockito.when(restHelper.requestSync(Mockito.any())).thenReturn(response); + Mockito.when(otpManager.sendOtp(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), + Mockito.any())).thenReturn(true); + otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + } + + @Test + public void TestPhoneorEmailisNull_both_channels_provided() throws IdAuthenticationBusinessException, RestServiceException { + OtpRequestDTO otpRequestDto = new OtpRequestDTO(); + otpRequestDto.setId("id"); + otpRequestDto.setRequestTime(new SimpleDateFormat(EnvUtil.getDateTimePattern()).format(new Date())); + otpRequestDto.setTransactionID("1234567890"); + List channelList = List.of("PHONE", "EMAIL"); otpRequestDto.setOtpChannel(channelList); String individualId = "2345678901234"; otpRequestDto.setIndividualId(individualId); @@ -267,18 +378,6 @@ public void TestPhoneorEmailisNull() throws IdAuthenticationBusinessException, R otpRequestDto.setRequestTime("2019-02-18T18:17:48.923+05:30"); Map valueMap = new HashMap<>(); Map> idInfo = new HashMap<>(); - List mailList = new ArrayList<>(); - IdentityInfoDTO identityInfoDTO = new IdentityInfoDTO(); - identityInfoDTO.setValue("abc@bc.com"); - mailList.add(identityInfoDTO); - List phoneList = new ArrayList<>(); - IdentityInfoDTO identityInfoDTO1 = new IdentityInfoDTO(); - identityInfoDTO1.setValue("9876543210"); - phoneList.add(identityInfoDTO1); - idInfo.put("email", mailList); - idInfo.put("mobile", phoneList); - valueMap.put("uin", "426789089018"); - valueMap.put("phone", "426789089018"); valueMap.put("response", idInfo); Mockito.when(idAuthService.processIdType(Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anySet())) .thenReturn(valueMap); @@ -301,7 +400,13 @@ public void TestPhoneorEmailisNull() throws IdAuthenticationBusinessException, R Mockito.when(restHelper.requestSync(Mockito.any())).thenThrow(new RestServiceException( IdRepoErrorConstants.CLIENT_ERROR, response.toString(), response)); - otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + try { + otpServiceImpl.generateOtp(otpRequestDto, "1234567890", new TestObjectWithMetadata()); + Assert.fail(); + } catch (IdAuthenticationBusinessException ex) { + assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorCode(), ex.getErrorCode()); + assertEquals(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED.getErrorMessage() + ". Both Phone Number and Email ID are not found in identity data.", ex.getErrorText()); + } } @Test(expected = IdAuthenticationBusinessException.class) @@ -361,7 +466,7 @@ private OtpRequestDTO getOtpRequestDTO() { // otpRequestDto.setRequestTime(new SimpleDateFormat(env.getDateTimePattern()).format(new Date())); otpRequestDto.setTransactionID("1234567890"); ArrayList channelList = new ArrayList(); - channelList.add("MOBILE"); + channelList.add("PHONE"); channelList.add("EMAIL"); otpRequestDto.setOtpChannel(channelList); otpRequestDto.setIndividualId("2345678901234");