From 7bb38c6318204dcce811132d1ad42b970d917c0f Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Sun, 13 Oct 2024 22:59:13 +0530 Subject: [PATCH] [ES-1613] Added test cases Signed-off-by: Venkata Saidurga Polamraju --- .../mock/service/MockHelperServiceTest.java | 210 ++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/mock-plugin/src/test/java/io/mosip/esignet/plugin/mock/service/MockHelperServiceTest.java b/mock-plugin/src/test/java/io/mosip/esignet/plugin/mock/service/MockHelperServiceTest.java index cfc49ea..0db00b0 100644 --- a/mock-plugin/src/test/java/io/mosip/esignet/plugin/mock/service/MockHelperServiceTest.java +++ b/mock-plugin/src/test/java/io/mosip/esignet/plugin/mock/service/MockHelperServiceTest.java @@ -61,6 +61,7 @@ public void setUp() throws Exception { supportedKycAuthFormats.put("BIO", List.of("encoded-json")); supportedKycAuthFormats.put("WLA", List.of("jwt")); supportedKycAuthFormats.put("KBA", List.of("base64url-encoded-json")); + supportedKycAuthFormats.put("PWD", List.of("alpha-numeric")); // Get the field Field field = MockHelperService.class.getDeclaredField("supportedKycAuthFormats"); @@ -130,6 +131,215 @@ public void doKycAuthMock_withValidDetails_thenPass() throws KycAuthException { Assert.assertEquals("partner_token", result.getPartnerSpecificUserToken()); } + @Test + public void doKycAuthMock_withAuthFactorAsWLA_thenPass() throws KycAuthException { + ReflectionTestUtils.setField(mockHelperService, "kycAuthUrl", "http://localhost:8080/kyc/auth"); + ReflectionTestUtils.setField(mockHelperService, "objectMapper", new ObjectMapper()); + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + KycAuthResponseDtoV2 response = new KycAuthResponseDtoV2(); + Map> claimMetaData=new HashMap<>(); + + ObjectNode verificationDetail = objectMapper.createObjectNode(); + verificationDetail.put("trust_framework", "test_trust_framework"); + claimMetaData.put("name",List.of(verificationDetail)); + + response.setClaimMetadata(claimMetaData); + response.setAuthStatus(true); + response.setKycToken("test_token"); + response.setPartnerSpecificUserToken("partner_token"); + responseWrapper.setResponse(response); + ResponseEntity> responseEntity= new ResponseEntity<>(responseWrapper, HttpStatus.OK); + + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(new ParameterizedTypeReference>() { + }) + )).thenReturn(responseEntity); + + KycAuthDto kycAuthDto = new KycAuthDto(); + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setAuthFactorType("WLA"); + authChallenge.setChallenge("validjwt"); + authChallenge.setFormat("jwt"); + kycAuthDto.setChallengeList(List.of(authChallenge)); + KycAuthResult result = mockHelperService.doKycAuthMock("relyingPartyId", "clientId", kycAuthDto, true); + + Assert.assertNotNull(result); + Assert.assertEquals("test_token", result.getKycToken()); + Assert.assertEquals("partner_token", result.getPartnerSpecificUserToken()); + } + + @Test + public void doKycAuthMock_withAuthFactorAsPIN_thenPass() throws KycAuthException { + ReflectionTestUtils.setField(mockHelperService, "kycAuthUrl", "http://localhost:8080/kyc/auth"); + ReflectionTestUtils.setField(mockHelperService, "objectMapper", new ObjectMapper()); + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + KycAuthResponseDtoV2 response = new KycAuthResponseDtoV2(); + Map> claimMetaData=new HashMap<>(); + + ObjectNode verificationDetail = objectMapper.createObjectNode(); + verificationDetail.put("trust_framework", "test_trust_framework"); + claimMetaData.put("name",List.of(verificationDetail)); + + response.setClaimMetadata(claimMetaData); + response.setAuthStatus(true); + response.setKycToken("test_token"); + response.setPartnerSpecificUserToken("partner_token"); + responseWrapper.setResponse(response); + ResponseEntity> responseEntity= new ResponseEntity<>(responseWrapper, HttpStatus.OK); + + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(new ParameterizedTypeReference>() { + }) + )).thenReturn(responseEntity); + + KycAuthDto kycAuthDto = new KycAuthDto(); + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setAuthFactorType("PIN"); + authChallenge.setChallenge("111111"); + authChallenge.setFormat("number"); + kycAuthDto.setChallengeList(List.of(authChallenge)); + KycAuthResult result = mockHelperService.doKycAuthMock("relyingPartyId", "clientId", kycAuthDto, true); + + Assert.assertNotNull(result); + Assert.assertEquals("test_token", result.getKycToken()); + Assert.assertEquals("partner_token", result.getPartnerSpecificUserToken()); + } + + @Test + public void doKycAuthMock_withAuthFactorAsPWD_thenPass() throws KycAuthException { + ReflectionTestUtils.setField(mockHelperService, "kycAuthUrl", "http://localhost:8080/kyc/auth"); + ReflectionTestUtils.setField(mockHelperService, "objectMapper", new ObjectMapper()); + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + KycAuthResponseDtoV2 response = new KycAuthResponseDtoV2(); + Map> claimMetaData=new HashMap<>(); + + ObjectNode verificationDetail = objectMapper.createObjectNode(); + verificationDetail.put("trust_framework", "test_trust_framework"); + claimMetaData.put("name",List.of(verificationDetail)); + + response.setClaimMetadata(claimMetaData); + response.setAuthStatus(true); + response.setKycToken("test_token"); + response.setPartnerSpecificUserToken("partner_token"); + responseWrapper.setResponse(response); + ResponseEntity> responseEntity= new ResponseEntity<>(responseWrapper, HttpStatus.OK); + + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(new ParameterizedTypeReference>() { + }) + )).thenReturn(responseEntity); + + KycAuthDto kycAuthDto = new KycAuthDto(); + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setAuthFactorType("PWD"); + authChallenge.setChallenge("Mosip@12"); + authChallenge.setFormat("alpha-numeric"); + kycAuthDto.setChallengeList(List.of(authChallenge)); + KycAuthResult result = mockHelperService.doKycAuthMock("relyingPartyId", "clientId", kycAuthDto, true); + + Assert.assertNotNull(result); + Assert.assertEquals("test_token", result.getKycToken()); + Assert.assertEquals("partner_token", result.getPartnerSpecificUserToken()); + } + + @Test + public void doKycAuthMock_withInvalidChallenge_thenFail() { + ReflectionTestUtils.setField(mockHelperService, "kycAuthUrl", "http://localhost:8080/kyc/auth"); + ReflectionTestUtils.setField(mockHelperService, "objectMapper", new ObjectMapper()); + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + KycAuthResponseDtoV2 response = new KycAuthResponseDtoV2(); + Map> claimMetaData=new HashMap<>(); + ObjectNode verificationDetail = objectMapper.createObjectNode(); + verificationDetail.put("trust_framework", "test_trust_framework"); + claimMetaData.put("name",List.of(verificationDetail)); + response.setClaimMetadata(claimMetaData); + response.setAuthStatus(true); + response.setKycToken("test_token"); + response.setPartnerSpecificUserToken("partner_token"); + responseWrapper.setResponse(response); + ResponseEntity> responseEntity= new ResponseEntity<>(responseWrapper, HttpStatus.OK); + + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(new ParameterizedTypeReference>() { + }) + )).thenReturn(responseEntity); + + KycAuthDto kycAuthDto = new KycAuthDto(); + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setAuthFactorType("abc"); + authChallenge.setChallenge("123456"); + authChallenge.setFormat("alpha-numeric"); + kycAuthDto.setChallengeList(List.of(authChallenge)); + try { + mockHelperService.doKycAuthMock("relyingPartyId", "clientId", kycAuthDto, true); + Assert.fail(); + }catch (KycAuthException e) + { + Assert.assertEquals("invalid_auth_challenge",e.getMessage()); + } + } + + @Test + public void doKycAuthMock_withInvalidChallengeFormat_thenFail() { + ReflectionTestUtils.setField(mockHelperService, "kycAuthUrl", "http://localhost:8080/kyc/auth"); + ReflectionTestUtils.setField(mockHelperService, "objectMapper", new ObjectMapper()); + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + KycAuthResponseDtoV2 response = new KycAuthResponseDtoV2(); + Map> claimMetaData=new HashMap<>(); + ObjectNode verificationDetail = objectMapper.createObjectNode(); + verificationDetail.put("trust_framework", "test_trust_framework"); + claimMetaData.put("name",List.of(verificationDetail)); + response.setClaimMetadata(claimMetaData); + + response.setAuthStatus(true); + response.setKycToken("test_token"); + response.setPartnerSpecificUserToken("partner_token"); + responseWrapper.setResponse(response); + ResponseEntity> responseEntity= new ResponseEntity<>(responseWrapper, HttpStatus.OK); + + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(new ParameterizedTypeReference>() { + }) + )).thenReturn(responseEntity); + + KycAuthDto kycAuthDto = new KycAuthDto(); + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setAuthFactorType("OTP"); + authChallenge.setChallenge("123456"); + authChallenge.setFormat("invalidFormat"); + kycAuthDto.setChallengeList(List.of(authChallenge)); + try { + mockHelperService.doKycAuthMock("relyingPartyId", "clientId", kycAuthDto, true); + Assert.fail(); + }catch (KycAuthException e) + { + Assert.assertEquals("invalid_challenge_format",e.getMessage()); + } + } + + @Test + public void getUTCDateTime_withValidDetails_thenPass() { + LocalDateTime utcDateTime = mockHelperService.getUTCDateTime(); + Assert.assertNotNull(utcDateTime); + } + + @Test + public void getEpochSeconds_withValidDetails_thenPass() { + long epochSeconds = mockHelperService.getEpochSeconds(); + Assert.assertTrue(epochSeconds > 0); + } + + @Test + public void isSupportedOtpChannel_withValidChannel_thenPass() { + ReflectionTestUtils.setField(mockHelperService,"otpChannels",List.of("sms")); + Assert.assertTrue(mockHelperService.isSupportedOtpChannel("sms")); + } + @Test public void doKycAuthMock_withEmptyResponse_thenFail() throws KycAuthException {