Skip to content

Commit

Permalink
[ES-1347]added test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Venkata Saidurga Polamraju <[email protected]>
  • Loading branch information
pvsaidurga committed Jul 26, 2024
1 parent bd2e009 commit b3b9512
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.mosip.esignet.services;


import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.esignet.api.dto.*;
import io.mosip.esignet.api.dto.claim.ClaimDetail;
import io.mosip.esignet.api.dto.claim.Claims;
Expand All @@ -17,6 +18,7 @@
import io.mosip.esignet.core.dto.*;
import io.mosip.esignet.core.exception.EsignetException;
import io.mosip.esignet.core.exception.InvalidTransactionException;
import io.mosip.esignet.core.spi.TokenService;
import io.mosip.esignet.core.util.AuthenticationContextClassRefUtil;
import io.mosip.esignet.core.util.CaptchaHelper;
import io.mosip.kernel.core.keymanager.spi.KeyStore;
Expand All @@ -34,6 +36,8 @@

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

Expand Down Expand Up @@ -72,6 +76,14 @@ public class AuthorizationHelperServiceTest {

@Mock
private AuditPlugin auditWrapper;

@Mock
private TokenService tokenService;

@Mock
private HttpServletRequest httpServletRequest;

ObjectMapper objectMapper=new ObjectMapper();

@Test
public void validateSendOtpCaptchaToken_withEmptyToken_thenFail() {
Expand Down Expand Up @@ -275,6 +287,19 @@ public void delegateAuthenticateRequest_withInvalidResult_thenFail() throws KycA
}
}

@Test
public void delegateAuthenticateRequest_withInValidDetails_thenFail() throws KycAuthException {
String transactionId = "transaction-id";
String individualId = "individual-id";
List<AuthChallenge> challengeList = new ArrayList<>();
OIDCTransaction oidcTransaction = new OIDCTransaction();
oidcTransaction.setRelyingPartyId("rp-id");
oidcTransaction.setClientId("client-id");
oidcTransaction.setAuthTransactionId("auth-transaction-id");
oidcTransaction.setRequestedClaims(new Claims());
Mockito.when(authenticationWrapper.doKycAuth(Mockito.anyString(), Mockito.anyString(), anyBoolean(), any(KycAuthDto.class))).thenThrow(KycAuthException.class);
Assert.assertThrows(EsignetException.class,()->authorizationHelperService.delegateAuthenticateRequest(transactionId, individualId, challengeList, oidcTransaction));
}

@Test
public void validateAcceptedClaims_withEmptyAcceptedClaims_thenPass() {
Expand Down Expand Up @@ -463,6 +488,20 @@ public void delegateSendOtpRequest_withInvalidTransactionId_thenFail() throws Se
}
}

@Test
public void delegateSendOtpRequest_withInvalidDetail_thenFail() throws SendOtpException {
OtpRequest otpRequest = new OtpRequest();
otpRequest.setIndividualId("individual-id");
otpRequest.setOtpChannels(Arrays.asList("email"));
OIDCTransaction oidcTransaction = new OIDCTransaction();
oidcTransaction.setAuthTransactionId("auth-transaction-id");
oidcTransaction.setRelyingPartyId("rpid");
oidcTransaction.setClientId("client-id");
SendOtpResult sendOtpResult = new SendOtpResult("temp-id", "masked-email", "masked-mobile");
Mockito.when(authenticationWrapper.sendOtp(Mockito.anyString(), Mockito.anyString(), any(SendOtpDto.class))).thenThrow(SendOtpException.class);
Assert.assertThrows(EsignetException.class,()->authorizationHelperService.delegateSendOtpRequest(otpRequest, oidcTransaction));
}

@Test
public void getProvidedAuthFactors_withValidInput_thenPass() {
Claims resolvedClaims = new Claims();
Expand Down Expand Up @@ -572,4 +611,55 @@ public void setGetIndividualId_withSecureStoreFlagSetToFalse() {
String result = authorizationHelperService.getIndividualId(oidcTransaction);
Assert.assertEquals(individualId, result);
}

@Test
public void testHandleInternalAuthenticateRequest_ValidDetails_thenPass(){
ReflectionTestUtils.setField(authorizationHelperService, "objectMapper",objectMapper);

AuthChallenge authChallenge = new AuthChallenge();
authChallenge.setChallenge("eyJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUo5LmV5SnpkV0lpT2lKemRXSnFaV04wSW4wLjl0MG5GMkNtVWZaeTlCYlA3cjM4bElhSlJSeTNaSk41MnBRNlpLSl9qVWMifQ==");
OIDCTransaction transaction = new OIDCTransaction();
transaction.setIndividualId("individualId");
Mockito.doNothing().when(tokenService).verifyIdToken(any(), any());
Mockito.when(httpServletRequest.getCookies()).thenReturn(createMockCookies("subject"));

OIDCTransaction haltedTransaction = new OIDCTransaction();
haltedTransaction.setIndividualId("individualId");
haltedTransaction.setTransactionId("transactionId");
haltedTransaction.setServerNonce("subject");
Mockito.when(cacheUtilService.getHaltedTransaction(Mockito.anyString())).thenReturn(haltedTransaction);

KycAuthResult result = authorizationHelperService.handleInternalAuthenticateRequest(authChallenge, transaction, httpServletRequest);

Assert.assertNotNull(result);
Assert.assertEquals("subject", result.getKycToken());
Assert.assertEquals("subject", result.getPartnerSpecificUserToken());
Assert.assertEquals("individualId", transaction.getIndividualId());
}

@Test
public void testHandleInternalAuthenticateRequest_NoCookie_thenFail() {

AuthChallenge authChallenge = new AuthChallenge();
authChallenge.setChallenge("base64encodedchallenge");
OIDCTransaction transaction = new OIDCTransaction();
HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
Assert.assertThrows(EsignetException.class, () ->
authorizationHelperService.handleInternalAuthenticateRequest(authChallenge, transaction, httpServletRequest));
}

@Test
public void testHandleInternalAuthenticateRequest_NoHaltedTransaction_thenFail() {
AuthChallenge authChallenge = new AuthChallenge();
authChallenge.setChallenge("base64encodedchallenge");
OIDCTransaction transaction = new OIDCTransaction();
Assert.assertThrows(EsignetException.class, () ->
authorizationHelperService.handleInternalAuthenticateRequest(authChallenge, transaction, httpServletRequest));
}

private Cookie[] createMockCookies(String subject) {
Cookie cookie = new Cookie(subject, "subject");
return new Cookie[]{cookie};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.esignet.api.dto.AuthChallenge;
import io.mosip.esignet.api.dto.SendOtpDto;
import io.mosip.esignet.api.dto.SendOtpResult;
import io.mosip.esignet.api.dto.claim.ClaimDetail;
import io.mosip.esignet.api.dto.claim.Claims;
import io.mosip.esignet.api.dto.KycAuthResult;
Expand Down Expand Up @@ -83,6 +85,10 @@ public class AuthorizationServiceTest {
@Mock
HttpServletRequest httpServletRequest;


@InjectMocks
AuthorizationHelperService authorizationHelperService;

private final ObjectMapper objectMapper = new ObjectMapper();


Expand All @@ -93,12 +99,9 @@ public void setUp() {
claims.put("profile", Arrays.asList("given_name", "profile_picture", "name", "phone_number", "email"));
claims.put("email", Arrays.asList("email","email_verified"));
claims.put("phone", Arrays.asList("phone_number","phone_number_verified"));
AuthorizationHelperService authorizationHelperService = new AuthorizationHelperService();
ReflectionTestUtils.setField(authorizationHelperService, "credentialScopes", Arrays.asList("sample_ldp_vc"));
ReflectionTestUtils.setField(authorizationHelperService, "authorizeScopes", Arrays.asList("resident-service"));
ReflectionTestUtils.setField(authorizationHelperService, "authenticationContextClassRefUtil", authenticationContextClassRefUtil);
ReflectionTestUtils.setField(authorizationHelperService, "authenticationWrapper", authenticationWrapper);
ReflectionTestUtils.setField(authorizationHelperService, "auditWrapper", auditWrapper);
ReflectionTestUtils.setField(authorizationHelperService,"captchaRequired",Arrays.asList("pwd"));

ReflectionTestUtils.setField(authorizationServiceImpl, "claims", claims);
ReflectionTestUtils.setField(authorizationServiceImpl, "objectMapper", new ObjectMapper());
Expand Down Expand Up @@ -1150,6 +1153,16 @@ public void getAuthCode_withValidInput_thenPass() {
Assert.assertEquals(authorizationServiceImpl.getAuthCode(authCodeRequest).getState(), "test-state");
}

@Test
public void getAuthCode_withInValidTransaction_thenFail() {
AuthCodeRequest authCodeRequest = new AuthCodeRequest();
authCodeRequest.setTransactionId("987654321");
authCodeRequest.setAcceptedClaims(Arrays.asList("fullName"));
authCodeRequest.setPermittedAuthorizeScopes(Arrays.asList("test-scope"));
Mockito.when(cacheUtilService.getAuthenticatedTransaction(Mockito.anyString())).thenReturn(null);
Assert.assertThrows(InvalidTransactionException.class,()->authorizationServiceImpl.getAuthCode(authCodeRequest));
}

@Test
public void getConsentDetails_withValidTransaction_thenPass(){
OIDCTransaction transaction=new OIDCTransaction();
Expand Down Expand Up @@ -1177,6 +1190,78 @@ public void getConsentDetails_withInvalidTransaction_thenFail(){
}
}

@Test
public void testSendOtp_ValidRequest_thenPass() throws Exception {
OtpRequest otpRequest = new OtpRequest();
otpRequest.setCaptchaToken("captchaToken");
otpRequest.setTransactionId("transactionId");
otpRequest.setIndividualId("individualId");
ArrayList<String> otpChannels=new ArrayList<>();
otpRequest.setOtpChannels(otpChannels);

OIDCTransaction transaction = new OIDCTransaction();
transaction.setTransactionId("transactionId");
transaction.setIndividualIdHash("individualIdHash");
transaction.setRelyingPartyId("relyingPartyId");
transaction.setClientId("clientId");
transaction.setAuthTransactionId("transactionId");
when(cacheUtilService.getPreAuthTransaction(Mockito.anyString())).thenReturn(transaction);
when(cacheUtilService.updateIndividualIdHashInPreAuthCache(Mockito.anyString(), Mockito.anyString())).thenReturn(transaction);
when(cacheUtilService.isIndividualIdBlocked(Mockito.anyString())).thenReturn(false);

SendOtpResult sendOtpResult = new SendOtpResult();
sendOtpResult.setTransactionId("transactionId");
sendOtpResult.setMaskedEmail("maskedEmail");
sendOtpResult.setMaskedMobile("maskedMobile");

SendOtpDto sendOtpDto=new SendOtpDto();
sendOtpDto.setTransactionId("transactionId");
sendOtpDto.setIndividualId("individualId");
ArrayList<String> otpChannel=new ArrayList<>();
sendOtpDto.setOtpChannels(otpChannel);

Mockito.when(authenticationWrapper.sendOtp("relyingPartyId","clientId",sendOtpDto)).thenReturn(sendOtpResult);
OtpResponse otpResponse = authorizationServiceImpl.sendOtp(otpRequest);
Assert.assertNotNull(otpResponse);
Assert.assertEquals("transactionId", otpResponse.getTransactionId());
Assert.assertEquals("maskedEmail", otpResponse.getMaskedEmail());
Assert.assertEquals("maskedMobile", otpResponse.getMaskedMobile());
}

@Test
public void sendOtp_whenIndividualIdBlocked_thenFail() throws Exception {
OtpRequest otpRequest = new OtpRequest();
otpRequest.setCaptchaToken("captchaToken");
otpRequest.setTransactionId("transactionId");
otpRequest.setIndividualId("individualId");
ArrayList<String> otpChannels=new ArrayList<>();
otpRequest.setOtpChannels(otpChannels);

OIDCTransaction transaction = new OIDCTransaction();
transaction.setTransactionId("transactionId");
transaction.setIndividualIdHash("individualIdHash");
transaction.setRelyingPartyId("relyingPartyId");
transaction.setClientId("clientId");
transaction.setAuthTransactionId("transactionId");
when(cacheUtilService.getPreAuthTransaction(Mockito.anyString())).thenReturn(transaction);
when(cacheUtilService.updateIndividualIdHashInPreAuthCache(Mockito.anyString(), Mockito.anyString())).thenReturn(transaction);
when(cacheUtilService.isIndividualIdBlocked(Mockito.anyString())).thenReturn(true);
try {
authorizationServiceImpl.sendOtp(otpRequest);
}catch(EsignetException e)
{
Assert.assertEquals(ErrorConstants.INDIVIDUAL_ID_BLOCKED,e.getErrorCode());
}
}

@Test
public void sendOtp_invalidTransactionId_thenFail() throws Exception {
OtpRequest otpRequest = new OtpRequest();
otpRequest.setTransactionId("invalidTransactionId");
when(cacheUtilService.getPreAuthTransaction("invalidTransactionId")).thenReturn(null);
assertThrows(InvalidTransactionException.class, () -> authorizationServiceImpl.sendOtp(otpRequest));
}

private OIDCTransaction createIdpTransaction(String[] acrs) {
OIDCTransaction oidcTransaction = new OIDCTransaction();
Map<String, ClaimDetail> idClaims = new HashMap<>();
Expand Down

0 comments on commit b3b9512

Please sign in to comment.