Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES-523 #10

Merged
merged 13 commits into from
Jan 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

public class SignUpException extends EsignetException {

public SignUpException() {super(ErrorConstants.UNKNOWN_ERROR);}

public SignUpException(String errorCode){
super(errorCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private String generateOTPChallenge(String challengeTransactionId) {
RestResponseWrapper<OtpResponse> restResponseWrapper = selfTokenRestTemplate
.exchange(generateChallengeUrl, HttpMethod.POST,
new HttpEntity<>(restRequestWrapper),
new ParameterizedTypeReference<RestResponseWrapper<OtpResponse>>() {}).getBody();
new ParameterizedTypeReference<RestResponseWrapper<OtpResponse>>() {})
.getBody();

if (restResponseWrapper != null && restResponseWrapper.getResponse() != null &&
!StringUtils.isEmpty(restResponseWrapper.getResponse().getOtp()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChallengeInfo> 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();
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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{

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<RegisterRequest> wrapper = new RequestWrapper<RegisterRequest>();
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<RegisterRequest> wrapper = new RequestWrapper<RegisterRequest>();
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);
Expand Down Expand Up @@ -994,6 +1101,45 @@ public void register_withBlankPassword_returnErrorResponse() throws Exception{
registerRequest.setUserInfo(userInfo);
registerRequest.setUsername("+85512345678");
registerRequest.setConsent("AGREE");
registerRequest.setPassword("");


RequestWrapper<RegisterRequest> wrapper = new RequestWrapper<RegisterRequest>();
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<LanguageTaggedValue> 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<RegisterRequest> wrapper = new RequestWrapper<RegisterRequest>();
wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime());
Expand Down Expand Up @@ -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<RegisterRequest> wrapper = new RequestWrapper<RegisterRequest>();
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<LanguageTaggedValue> fullNames = new ArrayList<>();
fullNames.add(new LanguageTaggedValue("khm", "ងន់ ម៉េងលាង"));
userInfo.setFullName(fullNames);

RegisterRequest registerRequest = new RegisterRequest();
registerRequest.setUserInfo(userInfo);
registerRequest.setConsent("AGREE");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<OtpResponse> 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<RestResponseWrapper<OtpResponse>>(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);
Expand Down
Loading
Loading