Skip to content

Commit

Permalink
ES-556 (mosip#51)
Browse files Browse the repository at this point in the history
Signed-off-by: pr <[email protected]>
Signed-off-by: Sreang Rathanak <[email protected]>
  • Loading branch information
panharith-0118 authored and Sreang Rathanak committed Jan 15, 2024
1 parent 6a19e9a commit 10ad337
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ public ResponseEntity handleExceptions(Exception ex, WebRequest request) {
}
return new ResponseEntity<ResponseWrapper>(getResponseWrapper(errors), HttpStatus.OK);
}
if(ex instanceof MissingServletRequestParameterException) {
return new ResponseEntity<ResponseWrapper>(getResponseWrapper(INVALID_REQUEST, ex.getMessage()),
HttpStatus.OK);
}
if(ex instanceof HttpMediaTypeNotAcceptableException) {
if(ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof HttpMediaTypeNotAcceptableException) {
return new ResponseEntity<ResponseWrapper>(getResponseWrapper(INVALID_REQUEST, ex.getMessage()),
HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.signup.dto;

import io.mosip.signup.util.Purpose;
import io.mosip.signup.validator.Identifier;
import io.mosip.signup.validator.Language;
import lombok.Data;
Expand All @@ -13,4 +14,6 @@ public class GenerateChallengeRequest {
@Language(required = false)
private String locale;
private boolean regenerate;

private Purpose purpose;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.mosip.signup.dto;

import io.mosip.esignet.core.util.IdentityProviderUtil;
import io.mosip.signup.util.ErrorConstants;
import io.mosip.signup.util.Purpose;
import io.mosip.signup.util.RegistrationStatus;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand All @@ -26,8 +30,9 @@ public class RegistrationTransaction implements Serializable {
private Map<String, RegistrationStatus> handlesStatus;
private RegistrationStatus registrationStatus;
private String locale;
private Purpose purpose;

public RegistrationTransaction(String identifier) {
public RegistrationTransaction(String identifier, Purpose purpose) {
this.identifier = IdentityProviderUtil.generateB64EncodedHash(IdentityProviderUtil.ALGO_SHA3_256,
identifier.toLowerCase(Locale.ROOT));
this.startedAt = LocalDateTime.now(ZoneOffset.UTC);
Expand All @@ -38,6 +43,7 @@ public RegistrationTransaction(String identifier) {
this.challengeHash = null;
this.challengeRetryAttempts = 0;
this.lastRetryAt = null;
this.purpose = purpose;
}

public long getLastRetryToNow() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import io.mosip.signup.exception.ChallengeFailedException;
import io.mosip.signup.exception.InvalidTransactionException;
import io.mosip.signup.exception.SignUpException;
import io.mosip.signup.util.ActionStatus;
import io.mosip.signup.util.ErrorConstants;
import io.mosip.signup.util.RegistrationStatus;
import io.mosip.signup.util.*;
import io.mosip.signup.exception.CaptchaException;
import io.mosip.signup.util.SignUpConstants;
import io.mosip.signup.exception.GenerateChallengeException;
import io.mosip.signup.helper.NotificationHelper;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -118,7 +115,7 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene

if(generateChallengeRequest.isRegenerate() == false) {
transactionId = IdentityProviderUtil.createTransactionId(null);
transaction = new RegistrationTransaction(identifier);
transaction = new RegistrationTransaction(identifier, generateChallengeRequest.getPurpose());
//Need to set cookie only when regenerate is false.
addCookieInResponse(transactionId, unauthenticatedTransactionTimeout);
}
Expand Down Expand Up @@ -184,6 +181,10 @@ public RegisterResponse register(RegisterRequest registerRequest, String transac
log.error("Transaction {} : given unsupported username in L1", transactionId);
throw new SignUpException(ErrorConstants.IDENTIFIER_MISMATCH);
}
if (!transaction.getPurpose().equals(Purpose.REGISTRATION)) {
log.error("Transaction {} : is not for Registration Purpose", transactionId);
throw new SignUpException(ErrorConstants.UNSUPPORTED_PURPOSE);
}
if(registerRequest.getConsent().equals(CONSENT_DISAGREE)) {
log.error("Transaction {} : disagrees consent", transactionId);
throw new SignUpException(ErrorConstants.CONSENT_REQUIRED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public class ErrorConstants {
public static final String GET_UIN_FAILED = "get_uin_failed";
public static final String INVALID_FULLNAME = "invalid_fullname";
public static final String TOO_EARLY_ATTEMPT = "too_early_attempt";
public static final String UNSUPPORTED_PURPOSE = "unsupported_purpose";

}
10 changes: 10 additions & 0 deletions signup-service/src/main/java/io/mosip/signup/util/Purpose.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.mosip.signup.util;

import java.util.Arrays;
import java.util.List;

public enum Purpose {

REGISTRATION,
RESET_PASSWORD;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import io.mosip.signup.exception.InvalidIdentifierException;
import io.mosip.signup.exception.InvalidTransactionException;
import io.mosip.signup.services.RegistrationService;
import io.mosip.signup.util.ActionStatus;
import io.mosip.signup.util.ErrorConstants;
import io.mosip.signup.util.SignUpConstants;
import io.mosip.signup.util.*;
import org.junit.Before;
import io.mosip.signup.util.RegistrationStatus;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -63,6 +60,7 @@ public class RegistrationControllerTest {
@Before
public void init() {
generateChallengeRequest = new GenerateChallengeRequest();
generateChallengeRequest.setPurpose(Purpose.REGISTRATION);
generateChallengeRequest.setIdentifier("+85577410541");
ZonedDateTime requestTime = ZonedDateTime.now(ZoneOffset.UTC);
wrapper = new RequestWrapper<>();
Expand Down Expand Up @@ -233,7 +231,7 @@ public void doVerifyChallenge_withoutIdentifier_returnErrorResponse() throws Exc
verifyRequestWrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("mock");
registrationTransaction.setIdentifier("mock");

Expand All @@ -252,7 +250,7 @@ public void doVerifyChallenge_withoutIdentifier_returnErrorResponse() throws Exc
@Test
public void doVerifyChallenge_withInvalidTransaction_returnErrorResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("mock");
registrationTransaction.setIdentifier("mock");

Expand All @@ -271,7 +269,7 @@ public void doVerifyChallenge_withInvalidTransaction_returnErrorResponse() throw
@Test
public void doVerifyChallenge_withVerifyChallengeRaiseChallengeFailedException_returnErrorResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("mock");
registrationTransaction.setIdentifier("mock");

Expand All @@ -290,7 +288,7 @@ public void doVerifyChallenge_withVerifyChallengeRaiseChallengeFailedException_r
@Test
public void doVerifyChallenge_withVerifyChallengeRaiseInvalidIdentifierException_returnErrorResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("mock");
registrationTransaction.setIdentifier("mock");

Expand All @@ -315,7 +313,7 @@ public void doVerifyChallenge_withMultipleInvalidRequest_returnErrorResponse() t
verifyRequestWrapper.setRequestTime(null);

String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123128", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("mock");
registrationTransaction.setIdentifier("mock");

Expand Down Expand Up @@ -386,10 +384,67 @@ public void doGenerateChallenge_withInvalidCaptchaToken_returnErrorResponse() th
.andExpect(jsonPath("$.errors[0].errorMessage").value(ErrorConstants.INVALID_CAPTCHA));
}

@Test
public void doGenerateChallenge_withRegistrationPurpose_thenPass() throws Exception {
String status = "SUCCESSFUL";
GenerateChallengeResponse generateChallengeResponse = new GenerateChallengeResponse(status);

generateChallengeRequest.setPurpose(Purpose.REGISTRATION);
when(registrationService.generateChallenge(generateChallengeRequest, ""))
.thenReturn(generateChallengeResponse);

mockMvc.perform(post("/registration/generate-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").isNotEmpty())
.andExpect(jsonPath("$.response.status").value(status))
.andExpect(jsonPath("$.errors").isEmpty());
}

@Test
public void doGenerateChallenge_withResetPasswordPurpose_thenPass() throws Exception {
String status = "SUCCESSFUL";
GenerateChallengeResponse generateChallengeResponse = new GenerateChallengeResponse(status);

generateChallengeRequest.setPurpose(Purpose.RESET_PASSWORD);
when(registrationService.generateChallenge(generateChallengeRequest, ""))
.thenReturn(generateChallengeResponse);

mockMvc.perform(post("/registration/generate-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").isNotEmpty())
.andExpect(jsonPath("$.response.status").value(status))
.andExpect(jsonPath("$.errors").isEmpty());
}

@Test
public void doGenerateChallenge_withInvalidPurpose_thenFail() throws Exception {
String status = "SUCCESSFUL";
GenerateChallengeResponse generateChallengeResponse = new GenerateChallengeResponse(status);

generateChallengeRequest.setPurpose(Purpose.REGISTRATION);
when(registrationService.generateChallenge(generateChallengeRequest, ""))
.thenReturn(generateChallengeResponse);

String requestBody = objectMapper.writeValueAsString(wrapper);
requestBody = requestBody.replace("REGISTRATION", "Invalid-purpose");

mockMvc.perform(post("/registration/generate-challenge")
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response").isEmpty())
.andExpect(jsonPath("$.errors").isNotEmpty())
.andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_REQUEST));
}

@Test
public void doGetRegistrationStatus_returnCompletedResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
registrationTransaction.setRegistrationStatus(RegistrationStatus.COMPLETED);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());
Expand All @@ -405,7 +460,7 @@ public void doGetRegistrationStatus_returnCompletedResponse() throws Exception {
@Test
public void doGetRegistrationStatus_returnPendingResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
registrationTransaction.setRegistrationStatus(RegistrationStatus.PENDING);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());
Expand All @@ -421,7 +476,7 @@ public void doGetRegistrationStatus_returnPendingResponse() throws Exception {
@Test
public void doGetRegistrationStatus_returnFailedResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
registrationTransaction.setRegistrationStatus(RegistrationStatus.FAILED);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mosip.signup.services;

import io.mosip.signup.dto.RegistrationTransaction;
import io.mosip.signup.util.Purpose;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -24,7 +25,7 @@ public class CacheUtilServiceTest {

@Test
public void test_RegistrationTransaction_cache() {
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123123");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123123", Purpose.REGISTRATION);
registrationTransaction.setChallengeHash("123456-HASH");

Mockito.when(cache.get("mock", RegistrationTransaction.class)).thenReturn(registrationTransaction);
Expand All @@ -44,7 +45,7 @@ public void test_RegistrationTransaction_cache() {

@Test
public void setChallengeTransaction_thenPass() {
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123123");
RegistrationTransaction registrationTransaction = new RegistrationTransaction("+85512123123", Purpose.REGISTRATION);
Assert.assertEquals(cacheUtilService.setChallengeGeneratedTransaction("mock-transaction", registrationTransaction), registrationTransaction);
Assert.assertNotNull(cacheUtilService.setChallengeGeneratedTransaction("mock-transaction", registrationTransaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.mosip.signup.dto.RestError;
import io.mosip.signup.dto.RestResponseWrapper;
import io.mosip.signup.exception.SignUpException;
import io.mosip.signup.util.Purpose;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void setUp() {

@Test
public void doGenerateChallenge_allValid_thenPass() throws SignUpException {
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
RestResponseWrapper<OtpResponse> challengeResponse = new RestResponseWrapper<>();
OtpResponse otpResponse = new OtpResponse();
otpResponse.setOtp("1111");
Expand All @@ -65,7 +66,7 @@ public void doGenerateChallenge_allValid_thenPass() throws SignUpException {

@Test
public void doGenerateChallenge_withApiResponseEmptyChallenge_thenFail() throws SignUpException {
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
RestResponseWrapper<OtpResponse> challengeResponse = new RestResponseWrapper<>();
OtpResponse otpResponse = new OtpResponse();
otpResponse.setOtp("");
Expand All @@ -87,7 +88,7 @@ public void doGenerateChallenge_withApiResponseEmptyChallenge_thenFail() throws

@Test
public void doGenerateChallenge_withApiNullResponse_thenFail() throws SignUpException {
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
when(selfTokenRestTemplate.exchange(
eq(generateChallengeUrl),
eq(HttpMethod.POST),
Expand All @@ -104,7 +105,7 @@ public void doGenerateChallenge_withApiNullResponse_thenFail() throws SignUpExce

@Test
public void doGenerateChallenge_withApiResponseErrors_thenFail() throws SignUpException {
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541");
RegistrationTransaction transaction = new RegistrationTransaction("+85577410541", Purpose.REGISTRATION);
ArrayList<RestError> errors= new ArrayList<RestError>();
errors.add(new RestError("401", "401"));
RestResponseWrapper<OtpResponse> challengeResponse = new RestResponseWrapper<>();
Expand Down
Loading

0 comments on commit 10ad337

Please sign in to comment.