diff --git a/signup-service/src/main/java/io/mosip/signup/exception/SignUpException.java b/signup-service/src/main/java/io/mosip/signup/exception/SignUpException.java index f914de4f..bd05dd71 100644 --- a/signup-service/src/main/java/io/mosip/signup/exception/SignUpException.java +++ b/signup-service/src/main/java/io/mosip/signup/exception/SignUpException.java @@ -5,8 +5,6 @@ public class SignUpException extends EsignetException { - public SignUpException() {super(ErrorConstants.UNKNOWN_ERROR);} - public SignUpException(String errorCode){ super(errorCode); } diff --git a/signup-service/src/main/java/io/mosip/signup/services/ChallengeManagerService.java b/signup-service/src/main/java/io/mosip/signup/services/ChallengeManagerService.java index 81c98cb6..a2c8bd85 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/ChallengeManagerService.java +++ b/signup-service/src/main/java/io/mosip/signup/services/ChallengeManagerService.java @@ -49,7 +49,8 @@ private String generateOTPChallenge(String challengeTransactionId) { RestResponseWrapper restResponseWrapper = selfTokenRestTemplate .exchange(generateChallengeUrl, HttpMethod.POST, new HttpEntity<>(restRequestWrapper), - new ParameterizedTypeReference>() {}).getBody(); + new ParameterizedTypeReference>() {}) + .getBody(); if (restResponseWrapper != null && restResponseWrapper.getResponse() != null && !StringUtils.isEmpty(restResponseWrapper.getResponse().getOtp()) && diff --git a/signup-service/src/main/java/io/mosip/signup/services/GoogleRecaptchaValidatorService.java b/signup-service/src/main/java/io/mosip/signup/services/GoogleRecaptchaValidatorService.java index a981dac1..281c4f71 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/GoogleRecaptchaValidatorService.java +++ b/signup-service/src/main/java/io/mosip/signup/services/GoogleRecaptchaValidatorService.java @@ -21,11 +21,6 @@ public class GoogleRecaptchaValidatorService implements CaptchaValidator { @Value("${mosip.signup.send-challenge.captcha-required}") private boolean requiredCaptcha; - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } - @Value("${mosip.signup.captcha-validator.url}") private String captchaVerifyUrl; diff --git a/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java b/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java index 0a883e45..ed7641c9 100644 --- a/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java +++ b/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java @@ -205,6 +205,30 @@ public void doVerifyChallenge_withInvalidChallengeFormat_returnErrorResponse() t .value("request.challengeInfo[0].format: invalid_challenge_format")); } + @Test + public void doVerifyChallenge_withBlankChallengeFormat_returnErrorResponse() throws Exception { + ChallengeInfo challengeInfo = new ChallengeInfo(); + challengeInfo.setFormat(""); + challengeInfo.setChallenge("111111"); + challengeInfo.setType("OTP"); + ArrayList challengeInfoArrayList = new ArrayList<>(); + challengeInfoArrayList.add(challengeInfo); + verifyChallengeRequest.setChallengeInfo(challengeInfoArrayList); + verifyRequestWrapper.setRequest(verifyChallengeRequest); + + String mockTransactionID = "123456789"; + Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID); + + mockMvc.perform(post("/registration/verify-challenge").cookie(cookie) + .content(objectMapper.writeValueAsString(verifyRequestWrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT)) + .andExpect(jsonPath("$.errors[0].errorMessage") + .value("request.challengeInfo[0].format: invalid_challenge_format")); + } + @Test public void doVerifyChallenge_withChallengeFormatNotInAllowlist_returnErrorResponse() throws Exception { ChallengeInfo challengeInfo = new ChallengeInfo(); @@ -309,6 +333,28 @@ public void doVerifyChallenge_withoutIdentifier_returnErrorResponse() throws Exc .value("request.identifier: invalid_identifier")); } + @Test + public void doVerifyChallenge_BlankIdentifier_returnErrorResponse() throws Exception { + verifyChallengeRequest.setIdentifier(""); + verifyRequestWrapper.setRequest(verifyChallengeRequest); + + String mockTransactionID = "123456789"; + RegistrationTransaction registrationTransaction = new RegistrationTransaction("", Purpose.REGISTRATION); + registrationTransaction.setChallengeHash("mock"); + registrationTransaction.setIdentifier("mock"); + + when(registrationService.verifyChallenge(verifyChallengeRequest, mockTransactionID)).thenThrow(new InvalidIdentifierException()); + + mockMvc.perform(post("/registration/verify-challenge") + .content(objectMapper.writeValueAsString(verifyRequestWrapper)) + .cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_IDENTIFIER)) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.identifier: invalid_identifier")); + } + @Test public void doVerifyChallenge_withInvalidTransaction_returnErrorResponse() throws Exception { String mockTransactionID = "123456789"; @@ -560,7 +606,7 @@ public void doGetRegistrationStatus_returnFailedResponse() throws Exception { .andExpect(jsonPath("$.response.status").value("FAILED")); } -// Register endpoint + // Register endpoint @Test public void register_thenPass() throws Exception{ @@ -594,7 +640,7 @@ public void register_thenPass() throws Exception{ } @Test - public void register_withBlankConsent_returnErrorResponse() throws Exception{ + public void register_withNullConsent_returnErrorResponse() throws Exception{ UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); @@ -688,7 +734,7 @@ public void register_withInvalidPhoneNumber_returnErrorResponse() throws Excepti } @Test - public void register_withBlankPhoneNumber_returnErrorResponse() throws Exception{ + public void register_withNullPhoneNumber_returnErrorResponse() throws Exception{ UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); @@ -717,12 +763,73 @@ public void register_withBlankPhoneNumber_returnErrorResponse() throws Exception .value("request.userInfo.phone: invalid_phone_number")); } + @Test + public void register_withBlankPhoneNumber_returnErrorResponse() throws Exception{ + + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង"))); + userInfo.setPhone(""); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+85512345678"); + registerRequest.setPassword("Password@2923"); + registerRequest.setConsent("AGREE"); + + RequestWrapper wrapper = new RequestWrapper(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(registerRequest); + + String mockTransactionID = "123456789"; + + mockMvc.perform(post("/registration/register") + .content(objectMapper.writeValueAsString(wrapper)) + .cookie(new Cookie("TRANSACTION_ID", mockTransactionID)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.response").isEmpty()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorMessage") + .value("request.userInfo.phone: invalid_phone_number")); + } + + @Test + public void register_withNullPreferredLang_returnErrorResponse() throws Exception{ + + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setFullName(List.of(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង"))); + userInfo.setPhone("+855123456789"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+85512345678"); + registerRequest.setPassword("Password@2023"); + registerRequest.setConsent("AGREE"); + + RequestWrapper wrapper = new RequestWrapper(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(registerRequest); + + String mockTransactionID = "123456789"; + + mockMvc.perform(post("/registration/register") + .content(objectMapper.writeValueAsString(wrapper)) + .cookie(new Cookie("TRANSACTION_ID", mockTransactionID)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.response").isEmpty()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.userInfo.preferredLang: unsupported_language")); + } + @Test public void register_withBlankPreferredLang_returnErrorResponse() throws Exception{ UserInfoMap userInfo = new UserInfoMap(); userInfo.setFullName(List.of(new LanguageTaggedValue("khm", "អាន បញ្ញារិទ្ធ"))); userInfo.setPhone("+855123456789"); + userInfo.setPreferredLang(""); RegisterRequest registerRequest = new RegisterRequest(); registerRequest.setUserInfo(userInfo); @@ -994,6 +1101,45 @@ public void register_withBlankPassword_returnErrorResponse() throws Exception{ registerRequest.setUserInfo(userInfo); registerRequest.setUsername("+85512345678"); registerRequest.setConsent("AGREE"); + registerRequest.setPassword(""); + + + RequestWrapper wrapper = new RequestWrapper(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(registerRequest); + + String mockTransactionID = "123456789"; + + RegisterResponse registerResponse = new RegisterResponse(); + registerResponse.setStatus(ActionStatus.PENDING); + when(registrationService.register(registerRequest, mockTransactionID)).thenReturn(registerResponse); + + mockMvc.perform(post("/registration/register") + .content(objectMapper.writeValueAsString(wrapper)) + .cookie(new Cookie("TRANSACTION_ID", mockTransactionID)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.response").isEmpty()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorMessage") + .value("request.password: invalid_password")); + } + + @Test + public void register_withNullPassword_returnErrorResponse() throws Exception{ + + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPhone("+855123456789"); + userInfo.setPreferredLang("khm"); + List fullNames = new ArrayList<>(); + fullNames.add(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង")); + userInfo.setFullName(fullNames); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+85512345678"); + registerRequest.setConsent("AGREE"); + RequestWrapper wrapper = new RequestWrapper(); wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); @@ -1026,6 +1172,42 @@ public void register_withBlankUsername_returnErrorResponse() throws Exception{ fullNames.add(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង")); userInfo.setFullName(fullNames); + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setConsent("AGREE"); + registerRequest.setPassword("Password@2023"); + registerRequest.setUsername(""); + + RequestWrapper wrapper = new RequestWrapper(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(registerRequest); + + String mockTransactionID = "123456789"; + + RegisterResponse registerResponse = new RegisterResponse(); + registerResponse.setStatus(ActionStatus.PENDING); + when(registrationService.register(registerRequest, mockTransactionID)).thenReturn(registerResponse); + + mockMvc.perform(post("/registration/register") + .content(objectMapper.writeValueAsString(wrapper)) + .cookie(new Cookie("TRANSACTION_ID", mockTransactionID)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.response").isEmpty()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.username: invalid_username")); + } + + @Test + public void register_withNullUsername_returnErrorResponse() throws Exception{ + + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPhone("+855123456789"); + userInfo.setPreferredLang("khm"); + List fullNames = new ArrayList<>(); + fullNames.add(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង")); + userInfo.setFullName(fullNames); + RegisterRequest registerRequest = new RegisterRequest(); registerRequest.setUserInfo(userInfo); registerRequest.setConsent("AGREE"); diff --git a/signup-service/src/test/java/io/mosip/signup/services/ChallengeManagerServiceTest.java b/signup-service/src/test/java/io/mosip/signup/services/ChallengeManagerServiceTest.java index e1b76225..8003aa8f 100644 --- a/signup-service/src/test/java/io/mosip/signup/services/ChallengeManagerServiceTest.java +++ b/signup-service/src/test/java/io/mosip/signup/services/ChallengeManagerServiceTest.java @@ -1,9 +1,9 @@ package io.mosip.signup.services; -import io.mosip.signup.dto.OtpResponse; -import io.mosip.signup.dto.RegistrationTransaction; -import io.mosip.signup.dto.RestError; -import io.mosip.signup.dto.RestResponseWrapper; +import brave.Response; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; +import io.mosip.signup.dto.*; import io.mosip.signup.exception.SignUpException; import io.mosip.signup.util.Purpose; import org.junit.Assert; @@ -37,6 +37,8 @@ public class ChallengeManagerServiceTest { @Mock RestTemplate selfTokenRestTemplate; + ObjectMapper objectMapper = new ObjectMapper(); + private String generateChallengeUrl = "https://api.net/v1/otpmanager/otp/generate"; @Before @@ -45,7 +47,6 @@ public void setUp() { ReflectionTestUtils.setField(challengeManagerService, "supportedGenerateChallengeType", "OTP"); } - @Test public void doGenerateChallenge_allValid_thenPass() throws SignUpException { RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION); @@ -64,6 +65,18 @@ public void doGenerateChallenge_allValid_thenPass() throws SignUpException { Assert.assertEquals(challenge, "1111"); } + @Test + public void doGenerateChallenge_withUnsupportedChallengeType_thenFail() throws SignUpException { + RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION); + ReflectionTestUtils.setField(challengeManagerService, "supportedGenerateChallengeType", "TELEGRAM"); + try{ + challengeManagerService.generateChallenge(transaction); + Assert.fail(); + } catch (SignUpException ex){ + Assert.assertEquals("unsupported_challenge_type", ex.getErrorCode()); + } + } + @Test public void doGenerateChallenge_withApiResponseEmptyChallenge_thenFail() throws SignUpException { RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION); @@ -86,6 +99,25 @@ public void doGenerateChallenge_withApiResponseEmptyChallenge_thenFail() throws } } + @Test + public void doGenerateChallenge_withApiResponseNullChallenge_thenFail() throws SignUpException, IOException { + RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION); + RestResponseWrapper challengeResponse = objectMapper.readValue("{\"id\":\"string\",\"version\":\"string\",\"responsetime\":\"2023-11-14T10:59:16.574Z\",\"metadata\":null,\"response\":{\"otp\": \"null\"},\"errors\":null}", TypeFactory.defaultInstance().constructParametricType(RestResponseWrapper.class, OtpResponse.class)); + + when(selfTokenRestTemplate.exchange( + eq(generateChallengeUrl), + eq(HttpMethod.POST), + any(), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity>(challengeResponse, HttpStatus.OK)); + + try { + challengeManagerService.generateChallenge(transaction); + Assert.fail(); + } catch (SignUpException ex) { + Assert.assertEquals("generate_challenge_failed", ex.getErrorCode()); + } + } + @Test public void doGenerateChallenge_withApiNullResponse_thenFail() throws SignUpException { RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION); diff --git a/signup-service/src/test/java/io/mosip/signup/services/GoogleRecaptchaValidatorServiceTest.java b/signup-service/src/test/java/io/mosip/signup/services/GoogleRecaptchaValidatorServiceTest.java new file mode 100644 index 00000000..4e3ba54b --- /dev/null +++ b/signup-service/src/test/java/io/mosip/signup/services/GoogleRecaptchaValidatorServiceTest.java @@ -0,0 +1,75 @@ +package io.mosip.signup.services; + +import io.mosip.signup.dto.ReCaptchaResponse; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.when; + + +class GoogleRecaptchaValidatorServiceTest { + + @InjectMocks + GoogleRecaptchaValidatorService googleRecaptchaValidatorService; + + @Mock + RestTemplate restTemplate; + + private String captchaVerifyUrl = "mockCaptchaVerifyUrl"; + private String verifierSecret = "mockVerifierSecret"; + + + @BeforeEach + public void setUp(){ + MockitoAnnotations.initMocks(this); + ReflectionTestUtils.setField(googleRecaptchaValidatorService, + "requiredCaptcha", + true); + ReflectionTestUtils.setField(googleRecaptchaValidatorService, + "captchaVerifyUrl", + captchaVerifyUrl); + ReflectionTestUtils.setField(googleRecaptchaValidatorService, + "verifierSecret", + verifierSecret); + } + + @Test + void validateCaptcha_withNotRequiredCaptcha_thenTrue() { + ReflectionTestUtils.setField(googleRecaptchaValidatorService, + "requiredCaptcha", + false); + assertEquals(true, googleRecaptchaValidatorService.validateCaptcha("mockToken")); + } + + @Test + void validateCaptcha_withRecaptchaValidatorSuccess_thenTrue() { + + ReCaptchaResponse reCaptchaResponse = new ReCaptchaResponse(); + reCaptchaResponse.setSuccess(true); + + when(restTemplate.postForObject(eq(captchaVerifyUrl), any(), any())).thenReturn(reCaptchaResponse); + assertEquals(true, googleRecaptchaValidatorService.validateCaptcha("mockToken")); + } + + @Test + void validateCaptcha_withRecaptchaValidatorFailure_thenFalse() { + + ReCaptchaResponse reCaptchaResponse = new ReCaptchaResponse(); + reCaptchaResponse.setSuccess(false); + + when(restTemplate.postForObject(eq(captchaVerifyUrl), any(), any())).thenReturn(reCaptchaResponse); + assertEquals(false, googleRecaptchaValidatorService.validateCaptcha("mockToken")); + } + + @Test + void validateCaptcha_withRecaptchaValidatorNull_thenFalse() { + assertEquals(false, googleRecaptchaValidatorService.validateCaptcha("mockToken")); + } +} diff --git a/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java b/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java index 84be8564..cd69f80c 100644 --- a/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java +++ b/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java @@ -18,7 +18,6 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; @@ -130,7 +129,7 @@ public void doVerifyChallenge_thenPass() { } @Test - public void doVerifyChallenge_thenIdentifierAlreadyRegisterError() { + public void doVerifyChallenge_whenIdentifierAlreadyRegisterError_throwIdentityAlreadyRegister() { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); @@ -173,7 +172,7 @@ public void doVerifyChallenge_thenIdentifierAlreadyRegisterError() { } @Test - public void doVerifyChallenge_toResetPassword_thenIdentifierNotFound() { + public void doVerifyChallengeInResetPassword_whenResponseNoRecordFromIDRepo_throwIdentifierNotFound() { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); @@ -219,7 +218,7 @@ public void doVerifyChallenge_toResetPassword_thenIdentifierNotFound() { } @Test - public void doVerifyChallenge_toResetPassword_thenIdentifierNotFound_WhenIdRepoResponseAnotherErrorCode() { + public void doVerifyChallengeInResetPassword_whenErrorFromIDRepo_throwFetchFailed() { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); @@ -265,7 +264,7 @@ public void doVerifyChallenge_toResetPassword_thenIdentifierNotFound_WhenIdRepoR } @Test - public void doVerifyChallenge_toResetPassword_withOTPMismatchFormat_thenChallengeFormatAndTypeMismatch() { + public void doVerifyChallengeInResetPassword_withOTPMismatchFormat_throwChallengeFormatAndTypeMismatch() { ChallengeInfo challengeInfoOTP = new ChallengeInfo(); challengeInfoOTP.setFormat("alpha-encoded"); challengeInfoOTP.setChallenge("111111"); @@ -296,7 +295,7 @@ public void doVerifyChallenge_toResetPassword_withOTPMismatchFormat_thenChalleng } @Test - public void doVerifyChallenge_toResetPassword_withKBAMismatchFormat_thenChallengeFormatAndTypeMismatch() { + public void doVerifyChallengeInResetPassword_withKBAMismatchFormat_throwChallengeFormatAndTypeMismatch() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-json"); challengeInfoKBA.setChallenge("111111"); @@ -327,7 +326,7 @@ public void doVerifyChallenge_toResetPassword_withKBAMismatchFormat_thenChalleng } @Test - public void doVerifyChallenge_toResetPassword_thenInvalidKBAChallenge() { + public void doVerifyChallengeInResetPassword_withInvalidKBAChallenge_throwInvalidKBAChallenge() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-encoded-json"); @@ -386,7 +385,7 @@ public void doVerifyChallenge_toResetPassword_thenInvalidKBAChallenge() { } @Test - public void doVerifyChallenge_toResetPassword_thenFullNameMismatch() { + public void doVerifyChallengeInResetPassword_withFullNameMismatch_throwKnowledgeBaseMismatch() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-encoded-json"); @@ -445,7 +444,7 @@ public void doVerifyChallenge_toResetPassword_thenFullNameMismatch() { } @Test - public void doVerifyChallenge_toResetPassword_thenSuccess() { + public void doVerifyChallengeInResetPassword_thenSuccess() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-encoded-json"); @@ -502,7 +501,7 @@ public void doVerifyChallenge_toResetPassword_thenSuccess() { } @Test - public void doVerifyChallenge_withInvalidFormatForOTPChallenge_thenFail() { + public void doVerifyChallenge_withInvalidFormatForOTPChallenge_throwChallengeFormatAndTypeMismatch() { ChallengeInfo challengeInfoOTP = new ChallengeInfo(); challengeInfoOTP.setFormat("alpha-encoded"); challengeInfoOTP.setChallenge("1623"); @@ -533,7 +532,7 @@ public void doVerifyChallenge_withInvalidFormatForOTPChallenge_thenFail() { } @Test - public void doVerifyChallenge_toResetPassword_thenIdentityInactive() { + public void doVerifyChallengeInResetPassword_withInactiveIdentity_throwIdentityInactive() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-encoded-json"); @@ -591,7 +590,7 @@ public void doVerifyChallenge_toResetPassword_thenIdentityInactive() { } @Test - public void doVerifyChallenge_toResetPassword_thenKBAChallengeNotFound() { + public void doVerifyChallengeInResetPassword_withKBAChallengeNotFound_throwIdentityInactive() { ChallengeInfo challengeInfoOTP = new ChallengeInfo(); challengeInfoOTP.setFormat("alpha-numeric"); @@ -643,7 +642,7 @@ public void doVerifyChallenge_toResetPassword_thenKBAChallengeNotFound() { } @Test - public void doVerifyChallenge_toResetPassword_thenFetchIdentityFailed() { + public void doVerifyChallengeInResetPassword_whenGetIdentityNullResponse_throwFetchFailed() { ChallengeInfo challengeInfoKBA = new ChallengeInfo(); challengeInfoKBA.setFormat("base64url-encoded-json"); @@ -688,7 +687,7 @@ public void doVerifyChallenge_toResetPassword_thenFetchIdentityFailed() { } @Test - public void doVerifyChallenge_withNullTransaction_thenFail() throws Exception { + public void doVerifyChallenge_withNullTransaction_thenFail() throws SignUpException { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); challengeInfo.setChallenge("123456"); @@ -712,7 +711,7 @@ public void doVerifyChallenge_withNullTransaction_thenFail() throws Exception { } @Test - public void doVerifyChallenge_withChallengeNotMatch_thenFail() throws Exception { + public void doVerifyChallenge_withChallengeNotMatch_thenFail() throws SignUpException { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); challengeInfo.setChallenge("123456"); @@ -741,7 +740,7 @@ public void doVerifyChallenge_withChallengeNotMatch_thenFail() throws Exception } @Test - public void doVerifyChallenge_withIdentifierNotMatch_throwsException() throws Exception { + public void doVerifyChallenge_withIdentifierNotMatch_throwsIdentifierMismatch() throws SignUpException { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); challengeInfo.setChallenge("123456"); @@ -770,7 +769,7 @@ public void doVerifyChallenge_withIdentifierNotMatch_throwsException() throws Ex // ## register--------------------------------- @Test - public void register_thenPass() throws Exception { + public void register_thenPass() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -831,7 +830,7 @@ public void register_thenPass() throws Exception { } @Test - public void register_thenUinEndpointResponseNullBody_throwException() throws Exception { + public void register_whenUinEndpointResponseNullBody_throwGetUINFailed() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -867,7 +866,48 @@ public void register_thenUinEndpointResponseNullBody_throwException() throws Exc } @Test - public void register_thenUinEndpointResponseErrors_throwException() throws Exception { + public void register_whenUinEndpointResponseNullUIN_throwGetUINFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN(null); + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals(ErrorConstants.GET_UIN_FAILED, signUpException.getErrorCode()); + } + } + + @Test + public void register_whenUinEndpointResponseErrors_throwServerError() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -908,7 +948,46 @@ public void register_thenUinEndpointResponseErrors_throwException() throws Excep } @Test - public void register_thenGenerateHashEndpointResponseNullBody_throwException() throws Exception { + public void register_whenUinEndpointResponseWithNullErrors_throwGetUINFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper<>(); + mockRestResponseWrapperUINResponse.setErrors(null); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("get_uin_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_whenGenerateHashEndpointResponseNullBody_throwHashGenerateFailed() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -933,9 +1012,6 @@ public void register_thenGenerateHashEndpointResponseNullBody_throwException() t identityResponse.setStatus("ACTIVATED"); UINResponse uinResponse = new UINResponse(); uinResponse.setUIN("mockUIN"); - Password.PasswordHash passwordHash = new Password.PasswordHash(); - passwordHash.setSalt("mockSalt"); - passwordHash.setHashValue("mockHashValue"); RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); mockRestResponseWrapperUINResponse.setResponse(uinResponse); @@ -960,7 +1036,7 @@ public void register_thenGenerateHashEndpointResponseNullBody_throwException() t } @Test - public void register_thenGenerateHashEndpointResponseErrors_throwException() throws Exception { + public void register_whenGenerateHashEndpointResponseErrors_throwServerError() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -985,9 +1061,6 @@ public void register_thenGenerateHashEndpointResponseErrors_throwException() thr identityResponse.setStatus("ACTIVATED"); UINResponse uinResponse = new UINResponse(); uinResponse.setUIN("mockUIN"); - Password.PasswordHash passwordHash = new Password.PasswordHash(); - passwordHash.setSalt("mockSalt"); - passwordHash.setHashValue("mockHashValue"); RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); mockRestResponseWrapperUINResponse.setResponse(uinResponse); @@ -1016,7 +1089,166 @@ public void register_thenGenerateHashEndpointResponseErrors_throwException() thr } @Test - public void register_thenAddIdentityEndpointResponseNullBody_throwException() throws Exception { + public void register_whenGenerateHashEndpointResponseNullErrors_throwHashGenerateFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setStatus("ACTIVATED"); + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN("mockUIN"); + + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); + mockRestResponseWrapperPasswordHash.setErrors(null); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(generateHashEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperPasswordHash, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("hash_generate_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_whenGenerateHashEndpointResponseNullSaltedPassword_throwHashGenerateFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setStatus("ACTIVATED"); + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN("mockUIN"); + Password.PasswordHash passwordHash = new Password.PasswordHash(); + passwordHash.setSalt("mockSalt"); + passwordHash.setHashValue("mockHashValue"); + + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); + mockRestResponseWrapperPasswordHash.setResponse(new Password.PasswordHash()); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(generateHashEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperPasswordHash, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("hash_generate_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_whenGenerateHashEndpointResponseNullSalt_throwHashGenerateFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setStatus("ACTIVATED"); + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN("mockUIN"); + Password.PasswordHash passwordHash = new Password.PasswordHash(); + passwordHash.setSalt(null); + passwordHash.setHashValue("mockHashValue"); + + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); + mockRestResponseWrapperPasswordHash.setResponse(passwordHash); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(generateHashEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperPasswordHash, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("hash_generate_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_whenAddIdentityEndpointResponseNullBody_throwAddIdentityFailed() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1075,7 +1307,68 @@ public void register_thenAddIdentityEndpointResponseNullBody_throwException() th } @Test - public void register_thenAddIdentityEndpointResponseErrors_throwException() throws Exception { + public void register_whenAddIdentityEndpointResponseStatusNotEqualsACTIVATED_throwAddIdentityFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(userInfo.getPhone(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setStatus("mock"); + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN("mockUIN"); + Password.PasswordHash passwordHash = new Password.PasswordHash(); + passwordHash.setSalt("mockSalt"); + passwordHash.setHashValue("mockHashValue"); + + RestResponseWrapper mockRestResponseWrapperIdentityResponse = new RestResponseWrapper(); + mockRestResponseWrapperIdentityResponse.setResponse(identityResponse); + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); + mockRestResponseWrapperPasswordHash.setResponse(passwordHash); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(generateHashEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperPasswordHash, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(identityEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperIdentityResponse, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("add_identity_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_whenAddIdentityEndpointResponseErrors_throwServerError() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1104,10 +1397,10 @@ public void register_thenAddIdentityEndpointResponseErrors_throwException() thro passwordHash.setSalt("mockSalt"); passwordHash.setHashValue("mockHashValue"); - RestResponseWrapper mockRestResponseWrapperAddIdentityResponse = new RestResponseWrapper(); + RestResponseWrapper mockRestResponseWrapperIdentityResponse = new RestResponseWrapper(); ArrayList errors = new ArrayList<>(); errors.add(new RestError("server_error", "server_error")); - mockRestResponseWrapperAddIdentityResponse.setErrors(errors); + mockRestResponseWrapperIdentityResponse.setErrors(errors); RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); mockRestResponseWrapperUINResponse.setResponse(uinResponse); RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); @@ -1127,7 +1420,7 @@ public void register_thenAddIdentityEndpointResponseErrors_throwException() thro eq(identityEndpoint), eq(HttpMethod.POST), any(HttpEntity.class), - any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperAddIdentityResponse, HttpStatus.OK)); + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperIdentityResponse, HttpStatus.OK)); try { registrationService.register(registerRequest, mockTransactionID); @@ -1138,7 +1431,69 @@ public void register_thenAddIdentityEndpointResponseErrors_throwException() thro } @Test - public void register_withInvalidTransaction_throwException() throws Exception { + public void register_whenAddIdentityEndpointResponseNullErrors_throwAddIdentityFailed() throws SignUpException{ + UserInfoMap userInfo = new UserInfoMap(); + userInfo.setPreferredLang("khm"); + userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); + userInfo.setPhone("+855219718732"); + + RegisterRequest registerRequest = new RegisterRequest(); + registerRequest.setUserInfo(userInfo); + registerRequest.setUsername("+855219718732"); + registerRequest.setPassword("123123"); + registerRequest.setConsent("AGREE"); + + String mockTransactionID = "123456789"; + + RegistrationTransaction mockRegistrationTransaction = new RegistrationTransaction(registerRequest.getUsername(), Purpose.REGISTRATION); + mockRegistrationTransaction.setChallengeHash("123456"); + mockRegistrationTransaction.setIdentifier(userInfo.getPhone()); + + when(cacheUtilService.getChallengeVerifiedTransaction(mockTransactionID)) + .thenReturn(mockRegistrationTransaction); + + IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setStatus("ACTIVATED"); + UINResponse uinResponse = new UINResponse(); + uinResponse.setUIN("mockUIN"); + Password.PasswordHash passwordHash = new Password.PasswordHash(); + passwordHash.setSalt("mockSalt"); + passwordHash.setHashValue("mockHashValue"); + + RestResponseWrapper mockRestResponseWrapperIdentityResponse = new RestResponseWrapper(); + mockRestResponseWrapperIdentityResponse.setErrors(null); + RestResponseWrapper mockRestResponseWrapperUINResponse = new RestResponseWrapper(); + mockRestResponseWrapperUINResponse.setResponse(uinResponse); + RestResponseWrapper mockRestResponseWrapperPasswordHash = new RestResponseWrapper(); + mockRestResponseWrapperPasswordHash.setResponse(passwordHash); + + when(selfTokenRestTemplate.exchange( + eq(getUinEndpoint), + eq(HttpMethod.GET), + eq(null), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperUINResponse, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(generateHashEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperPasswordHash, HttpStatus.OK)); + when(selfTokenRestTemplate.exchange( + eq(identityEndpoint), + eq(HttpMethod.POST), + any(HttpEntity.class), + any(ParameterizedTypeReference.class))) + .thenReturn(new ResponseEntity<>(mockRestResponseWrapperIdentityResponse, HttpStatus.OK)); + + try{ + registrationService.register(registerRequest, mockTransactionID); + Assert.fail(); + }catch (SignUpException signUpException){ + Assert.assertEquals("add_identity_failed", signUpException.getErrorCode()); + } + } + + @Test + public void register_withInvalidTransaction_throwInvalidTransaction() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1161,7 +1516,7 @@ public void register_withInvalidTransaction_throwException() throws Exception { } @Test - public void register_withInvalidUsername_throwException() throws Exception { + public void register_withInvalidUsername_throwIdentifierMismatch() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1191,7 +1546,7 @@ public void register_withInvalidUsername_throwException() throws Exception { } @Test - public void register_withInvalidConsent_throwException() throws Exception { + public void register_withInvalidConsent_throwConsentRequired() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1221,7 +1576,7 @@ public void register_withInvalidConsent_throwException() throws Exception { } @Test - public void register_with_ResetPasswordPurposeTransaction_throwException() throws Exception { + public void register_with_ResetPasswordPurposeTransaction_throwUnsupportedPurpose() throws SignUpException { UserInfoMap userInfo = new UserInfoMap(); userInfo.setPreferredLang("khm"); userInfo.setFullName(List.of(new LanguageTaggedValue("eng", "Panharith AN"))); @@ -1299,7 +1654,7 @@ public void doGenerateChallenge_withTransactionId_thenPass() throws SignUpExcept } @Test - public void doGenerateChallenge_regenerateWithInvalidTransactionId_thenFail() throws SignUpException { + public void doGenerateChallenge_regenerateWithInvalidTransactionId_throwInvalidTransaction() throws SignUpException { String identifier = "+85577410541"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1322,7 +1677,7 @@ public void doGenerateChallenge_regenerateWithInvalidTransactionId_thenFail() th } @Test - public void doGenerateChallenge_withInvalidCaptcha_thenFail() throws EsignetException { + public void doGenerateChallenge_withInvalidCaptcha_throwInvalidCaptcha() throws EsignetException { String identifier = "12345678"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1338,7 +1693,7 @@ public void doGenerateChallenge_withInvalidCaptcha_thenFail() throws EsignetExce } @Test - public void doGenerateChallenge_withInvalidTransactionId_thenFail() throws SignUpException { + public void doGenerateChallenge_withInvalidTransactionId_throwInvalidTransaction() throws SignUpException { String identifier = "+85577410541"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1360,7 +1715,7 @@ public void doGenerateChallenge_withInvalidTransactionId_thenFail() throws SignU } @Test - public void doGenerateChallenge_withIdentifierNotMatchTransactionId_thenFail() throws SignUpException { + public void doGenerateChallenge_withIdentifierNotMatchTransactionId_throwIdentifierMismatch() throws SignUpException { String identifier = "+85577410541"; String other_identifier = "+85577410542"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); @@ -1383,7 +1738,7 @@ public void doGenerateChallenge_withIdentifierNotMatchTransactionId_thenFail() t } @Test - public void doGenerateChallenge_withTooManyAttemptTransactionId_thenFail() throws SignUpException { + public void doGenerateChallenge_withTooManyAttemptTransactionId_throwTooManyAttempts() throws SignUpException { String identifier = "+85577410541"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1406,7 +1761,7 @@ public void doGenerateChallenge_withTooManyAttemptTransactionId_thenFail() throw } @Test - public void doGenerateChallenge_withToEarlyAttemptTransactionId_thenFail() throws SignUpException { + public void doGenerateChallenge_withToEarlyAttemptTransactionId_throwTooEarlyAttempt() throws SignUpException { String identifier = "+85577410541"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1451,7 +1806,7 @@ public void doGenerateChallenge_withRegistrationPurpose_thenPass() throws SignUp } @Test - public void doGenerateChallenge_with_ResetPasswrodPurpose_thenPass() throws SignUpException, IOException { + public void doGenerateChallenge_with_ResetPasswordPurpose_thenPass() throws SignUpException, IOException { String identifier = "+85577410541"; GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); generateChallengeRequest.setIdentifier(identifier); @@ -1512,7 +1867,7 @@ public void doGetRegistrationStatus_withFailedTransaction_thenPass() { } @Test - public void doGetRegistrationStatus_withInvalidTransaction_thenFail() { + public void doGetRegistrationStatus_withInvalidTransaction_throwInvalidTransaction() { String transactionId = "TRAN-1234"; try { RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(transactionId); @@ -1523,7 +1878,7 @@ public void doGetRegistrationStatus_withInvalidTransaction_thenFail() { } @Test - public void doGetRegistrationStatus_withEmptyTransactionId_thenFail() { + public void doGetRegistrationStatus_withEmptyTransactionId_throwInvalidTransaction() { try { RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(""); Assert.fail(); @@ -1533,7 +1888,7 @@ public void doGetRegistrationStatus_withEmptyTransactionId_thenFail() { } @Test - public void doGetRegistrationStatus_withNullTransactionId_thenFail() { + public void doGetRegistrationStatus_withNullTransactionId_throwInvalidTransaction() { try { RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(null); Assert.fail(); @@ -1605,7 +1960,7 @@ public void doUpdatePassword_withInvalidTransaction_thenSuccess() { } @Test - public void doUpdatePassword_withIdentifierMismatch_thenFail() { + public void doUpdatePassword_withIdentifierMismatch_throwIdentifierMismatch() { String verifiedTransactionId = "VERIFIED_TRANSACTION_ID"; ResetPasswordRequest resetPasswordRequest = new ResetPasswordRequest(); @@ -1625,7 +1980,7 @@ public void doUpdatePassword_withIdentifierMismatch_thenFail() { } @Test - public void doUpdatePassword_thenFail_whenIdentityEndpointResponseNull() { + public void doUpdatePassword_whenIdentityEndpointResponseIsNull_throwResetPasswordFailed() { String verifiedTransactionId = "VERIFIED_TRANSACTION_ID"; ResetPasswordRequest resetPasswordRequest = new ResetPasswordRequest(); @@ -1665,7 +2020,7 @@ public void doUpdatePassword_thenFail_whenIdentityEndpointResponseNull() { } @Test - public void doUpdatePassword_thenFail_whenIdentityEndpointResponseContainResponseNull() { + public void doUpdatePassword_whenIdentityEndpointReturnInvalidResponse_throwResetPasswordFailed() { String verifiedTransactionId = "VERIFIED_TRANSACTION_ID"; ResetPasswordRequest resetPasswordRequest = new ResetPasswordRequest(); @@ -1708,7 +2063,7 @@ public void doUpdatePassword_thenFail_whenIdentityEndpointResponseContainRespons } @Test - public void doUpdatePassword_thenFail_whenIdentityEndpointResponseErrors() { + public void doUpdatePassword_whenIdentityEndpointReturnsError_throwErrorFromAnotherService() { String verifiedTransactionId = "VERIFIED_TRANSACTION_ID"; ResetPasswordRequest resetPasswordRequest = new ResetPasswordRequest(); resetPasswordRequest.setPassword("Password@2002"); @@ -1755,7 +2110,7 @@ public void doUpdatePassword_thenFail_whenIdentityEndpointResponseErrors() { } @Test - public void doVerifyChallenge_withExpiredChallenge_thenFail() throws Exception { + public void doVerifyChallenge_withExpiredChallenge_throwChallengeExpired() throws SignUpException { ChallengeInfo challengeInfo = new ChallengeInfo(); challengeInfo.setFormat("alpha-numeric"); challengeInfo.setChallenge("123456");