Skip to content

Commit

Permalink
PSA-171 fix for allowing one of the available channels when both chan…
Browse files Browse the repository at this point in the history
…nels specified.
  • Loading branch information
Loganathan Sekar committed Aug 22, 2023
1 parent 6a13ab4 commit 111f8b7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> channelList = new ArrayList<String>();
List<String> channelList = List.of("PHONE");
otpRequestDto.setOtpChannel(channelList);
otpRequestDto.setIndividualId("2345678901234");
otpRequestDto.setIndividualIdType(IdType.UIN.getType());
Expand All @@ -246,39 +247,137 @@ 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<String> channelList = new ArrayList<String>();
List<String> 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<String, Object> valueMap = new HashMap<>();
Map<String, Object> idInfo = new HashMap<>();
idInfo.put("email", "[email protected]");
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<Map> response = new ResponseWrapper<>();
Map<String, Object> 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<String> 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<String, Object> valueMap = new HashMap<>();
Map<String, Object> 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<Map> response = new ResponseWrapper<>();
Map<String, Object> 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<String> 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<String, Object> valueMap = new HashMap<>();
Map<String, Object> 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<Map> response = new ResponseWrapper<>();
Map<String, Object> 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<String> channelList = List.of("PHONE", "EMAIL");
otpRequestDto.setOtpChannel(channelList);
String individualId = "2345678901234";
otpRequestDto.setIndividualId(individualId);
otpRequestDto.setIndividualIdType(IdType.UIN.getType());
otpRequestDto.setRequestTime("2019-02-18T18:17:48.923+05:30");
Map<String, Object> valueMap = new HashMap<>();
Map<String, List<IdentityInfoDTO>> idInfo = new HashMap<>();
List<IdentityInfoDTO> mailList = new ArrayList<>();
IdentityInfoDTO identityInfoDTO = new IdentityInfoDTO();
identityInfoDTO.setValue("[email protected]");
mailList.add(identityInfoDTO);
List<IdentityInfoDTO> 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);
Expand All @@ -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)
Expand Down Expand Up @@ -361,7 +466,7 @@ private OtpRequestDTO getOtpRequestDTO() {
// otpRequestDto.setRequestTime(new SimpleDateFormat(env.getDateTimePattern()).format(new Date()));
otpRequestDto.setTransactionID("1234567890");
ArrayList<String> channelList = new ArrayList<String>();
channelList.add("MOBILE");
channelList.add("PHONE");
channelList.add("EMAIL");
otpRequestDto.setOtpChannel(channelList);
otpRequestDto.setIndividualId("2345678901234");
Expand Down

0 comments on commit 111f8b7

Please sign in to comment.