Skip to content

Commit

Permalink
ES-430 (mosip#16)
Browse files Browse the repository at this point in the history
* ES-430-signup-backend-api-to-get-the-registration-status

* Add newline

* move to constant

* check case empty transactionId

* Remove idea configure file

---------

Co-authored-by: Sreang Rathanak <[email protected]>
Signed-off-by: Sreang Rathanak <[email protected]>
  • Loading branch information
rathanak-0080 and Sreang Rathanak committed Jan 15, 2024
1 parent 553d371 commit 565d3bf
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import io.mosip.esignet.core.dto.RequestWrapper;
import io.mosip.esignet.core.dto.ResponseWrapper;
import io.mosip.esignet.core.util.IdentityProviderUtil;
import io.mosip.signup.dto.RegistrationStatusResponse;
import io.mosip.signup.dto.VerifyChallengeRequest;
import io.mosip.signup.dto.VerifyChallengeResponse;
import io.mosip.signup.exception.SignUpException;
import io.mosip.signup.services.RegistrationService;
import io.mosip.signup.util.SignUpConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,11 +28,20 @@ public class RegistrationController {

@PostMapping("/verify-challenge")
public ResponseWrapper<VerifyChallengeResponse> verifyChallenge(@Valid @RequestBody RequestWrapper<VerifyChallengeRequest> requestWrapper,
@CookieValue("TRANSACTION_ID") String transactionId)
@CookieValue(SignUpConstants.TRANSACTION_ID) String transactionId)
throws SignUpException {
ResponseWrapper<VerifyChallengeResponse> responseWrapper = new ResponseWrapper<>();
responseWrapper.setResponseTime(IdentityProviderUtil.getUTCDateTime());
responseWrapper.setResponse(registrationService.verifyChallenge(requestWrapper.getRequest(),transactionId));
return responseWrapper;
}

@GetMapping("/status")
public ResponseWrapper<RegistrationStatusResponse> getRegistrationStatus(
@CookieValue(SignUpConstants.TRANSACTION_ID) String transactionId) {
ResponseWrapper<RegistrationStatusResponse> responseWrapper = new ResponseWrapper<RegistrationStatusResponse>();
responseWrapper.setResponse(registrationService.getRegistrationStatus(transactionId));
responseWrapper.setResponseTime(IdentityProviderUtil.getUTCDateTime());
return responseWrapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.mosip.signup.dto;

import io.mosip.signup.util.RegistrationStatus;
import lombok.Data;

@Data
public class RegistrationStatusResponse {
private RegistrationStatus status;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.signup.dto;

import io.mosip.signup.util.RegistrationStatus;
import lombok.Data;

import java.io.Serializable;
Expand All @@ -9,4 +10,5 @@ public class RegistrationTransaction implements Serializable {

String otp; //TODO temporary field. It will be removed later after integrate with OTP Manager service.
String identifier;
private RegistrationStatus registrationStatus;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.signup.services;

import io.mosip.signup.dto.RegistrationStatusResponse;
import io.mosip.signup.dto.RegistrationTransaction;
import io.mosip.signup.dto.VerifyChallengeRequest;
import io.mosip.signup.dto.VerifyChallengeResponse;
Expand Down Expand Up @@ -30,4 +31,19 @@ public VerifyChallengeResponse verifyChallenge(VerifyChallengeRequest verifyChal
verifyChallengeResponse.setStatus(ActionStatus.SUCCESS);
return verifyChallengeResponse;
}

public RegistrationStatusResponse getRegistrationStatus(String transactionId)
throws SignUpException {
if (transactionId == null || transactionId.isEmpty())
throw new InvalidTransactionException();

RegistrationTransaction registrationTransaction = cacheUtilService.getRegisteredTransaction(
transactionId);
if (registrationTransaction == null)
throw new InvalidTransactionException();

RegistrationStatusResponse registrationStatusResponse = new RegistrationStatusResponse();
registrationStatusResponse.setStatus(registrationTransaction.getRegistrationStatus());
return registrationStatusResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.mosip.signup.util;

public enum RegistrationStatus {
PENDING,
COMPLETED,
FAILED
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class SignUpConstants {
public static final String CHALLENGE_GENERATED = "challenge_generated";
public static final String CHALLENGE_VERIFIED = "challenge_verified";
public static final String REGISTERED_CACHE = "registered";
public static final String TRANSACTION_ID = "TRANSACTION_ID";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.esignet.core.dto.RequestWrapper;
import io.mosip.esignet.core.util.IdentityProviderUtil;
import io.mosip.signup.dto.ChallengeInfo;
import io.mosip.signup.dto.RegistrationTransaction;
import io.mosip.signup.dto.VerifyChallengeRequest;
import io.mosip.signup.dto.VerifyChallengeResponse;
import io.mosip.signup.dto.*;
import io.mosip.signup.exception.ChallengeFailedException;
import io.mosip.signup.exception.InvalidIdentifierException;
import io.mosip.signup.exception.InvalidTransactionException;
import io.mosip.signup.services.CacheUtilService;
import io.mosip.signup.services.RegistrationService;
import io.mosip.signup.util.ActionStatus;
import io.mosip.signup.util.RegistrationStatus;
import io.mosip.signup.util.SignUpConstants;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,6 +27,7 @@

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -72,7 +72,7 @@ public void verifyChallenge_thenPass() throws Exception {

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").value("SUCCESS"));
Expand All @@ -93,7 +93,7 @@ public void verifyChallenge_withInvalidChallenge_returnErrorResponse() throws Ex
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -118,7 +118,7 @@ public void verifyChallenge_withInvalidChallenge_ChallengeSizeMoreThen6_returnEr
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -143,7 +143,7 @@ public void verifyChallenge_withInvalidChallengeFormat_returnErrorResponse() thr
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -169,7 +169,7 @@ public void verifyChallenge_withInvalidChallengeFormat_allowlist_returnErrorResp
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -195,7 +195,7 @@ public void verifyChallenge_withInvalidTimestamp_returnErrorResponse() throws Ex
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -222,7 +222,7 @@ public void verifyChallenge_withLongIntervalTimestamp_returnErrorResponse() thro
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand All @@ -244,7 +244,7 @@ public void verifyChallenge_withInvalidChallengeInfo_returnErrorResponse() throw
wrapper.setRequest(verifyChallengeRequest);

String mockTransactionID = "123456789";
Cookie cookie = new Cookie("TRANSACTION_ID", mockTransactionID);
Cookie cookie = new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID);

mockMvc.perform(post("/registration/verify-challenge").cookie(cookie)
.content(objectMapper.writeValueAsString(wrapper))
Expand Down Expand Up @@ -279,7 +279,7 @@ public void verifyChallenge_withoutIdentifier_returnErrorResponse() throws Excep

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors").isNotEmpty())
Expand Down Expand Up @@ -311,7 +311,7 @@ public void verifyChallenge_withInvalidTransaction_returnErrorResponse() throws

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors").isNotEmpty())
Expand Down Expand Up @@ -343,7 +343,7 @@ public void verifyChallenge_withChallengeFailedException_returnErrorResponse() t

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors").isNotEmpty())
Expand Down Expand Up @@ -375,7 +375,7 @@ public void verifyChallenge_withInvalidIdentifierException_returnErrorResponse()

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors").isNotEmpty())
Expand Down Expand Up @@ -405,10 +405,58 @@ public void verifyChallenge_withMultipleInvalidRequest_returnErrorResponse() thr

mockMvc.perform(post("/registration/verify-challenge")
.content(objectMapper.writeValueAsString(wrapper))
.cookie(new Cookie("TRANSACTION_ID", mockTransactionID))
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors").isNotEmpty())
.andExpect(jsonPath("$.errors.length()").value(2));
}

@Test
public void getRegistrationStatus_returnCompletedResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.COMPLETED);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());

when(registrationService.getRegistrationStatus(mockTransactionID)).thenReturn(response);
mockMvc.perform(get("/registration/status")
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").value("COMPLETED"));
}

@Test
public void getRegistrationStatus_returnPendingResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.PENDING);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());

when(registrationService.getRegistrationStatus(mockTransactionID)).thenReturn(response);
mockMvc.perform(get("/registration/status")
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").value("PENDING"));
}

@Test
public void getRegistrationStatus_returnFailedResponse() throws Exception {
String mockTransactionID = "123456789";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.FAILED);
RegistrationStatusResponse response = new RegistrationStatusResponse();
response.setStatus(registrationTransaction.getRegistrationStatus());

when(registrationService.getRegistrationStatus(mockTransactionID)).thenReturn(response);
mockMvc.perform(get("/registration/status")
.cookie(new Cookie(SignUpConstants.TRANSACTION_ID, mockTransactionID))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").value("FAILED"));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.mosip.signup.services;

import io.mosip.signup.dto.ChallengeInfo;
import io.mosip.signup.dto.RegistrationTransaction;
import io.mosip.signup.dto.VerifyChallengeRequest;
import io.mosip.signup.dto.VerifyChallengeResponse;
import io.mosip.signup.dto.*;
import io.mosip.signup.exception.ChallengeFailedException;
import io.mosip.signup.exception.InvalidIdentifierException;
import io.mosip.signup.exception.InvalidTransactionException;
import io.mosip.signup.util.RegistrationStatus;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -114,4 +112,71 @@ public void verifyChallenge_withInvalidIdentifier_throwsException() throws Excep
Assert.assertEquals("invalid_identifier", invalidIdentifierException.getErrorCode());
}
}

@Test
public void getRegistrationStatus_withCompletedTransaction() {
String transactionId = "TRAN-1234";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.COMPLETED);
when(cacheUtilService.getRegisteredTransaction(transactionId)).thenReturn(registrationTransaction);
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(transactionId);

Assert.assertNotNull(registrationStatusResponse);
Assert.assertEquals(registrationStatusResponse.getStatus(), RegistrationStatus.COMPLETED);
}

@Test
public void getRegistrationStatus_withPendingTransaction() {
String transactionId = "TRAN-1234";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.PENDING);
when(cacheUtilService.getRegisteredTransaction(transactionId)).thenReturn(registrationTransaction);
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(transactionId);

Assert.assertNotNull(registrationStatusResponse);
Assert.assertEquals(registrationStatusResponse.getStatus(), RegistrationStatus.PENDING);
}

@Test
public void getRegistrationStatus_withFailedTransaction() {
String transactionId = "TRAN-1234";
RegistrationTransaction registrationTransaction = new RegistrationTransaction();
registrationTransaction.setRegistrationStatus(RegistrationStatus.FAILED);
when(cacheUtilService.getRegisteredTransaction(transactionId)).thenReturn(registrationTransaction);
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(transactionId);

Assert.assertNotNull(registrationStatusResponse);
Assert.assertEquals(registrationStatusResponse.getStatus(), RegistrationStatus.FAILED);
}

@Test
public void getRegistrationStatus_withInvalidTransaction() {
String transactionId = "TRAN-1234";
try {
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(transactionId);
Assert.fail();
} catch (InvalidTransactionException exception) {
Assert.assertEquals("invalid_transaction", exception.getErrorCode());
}
}

@Test
public void getRegistrationStatus_withEmptyTransactionId() {
try {
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus("");
Assert.fail();
} catch (InvalidTransactionException exception) {
Assert.assertEquals("invalid_transaction", exception.getErrorCode());
}
}

@Test
public void getRegistrationStatus_withNullTransactionId() {
try {
RegistrationStatusResponse registrationStatusResponse = registrationService.getRegistrationStatus(null);
Assert.fail();
} catch (InvalidTransactionException exception) {
Assert.assertEquals("invalid_transaction", exception.getErrorCode());
}
}
}

0 comments on commit 565d3bf

Please sign in to comment.