Skip to content

Commit

Permalink
Merge pull request mosip#334 from ase-101/feature-ES-4
Browse files Browse the repository at this point in the history
ES-262
  • Loading branch information
vishwa-vyom authored Sep 1, 2023
2 parents 0853cb9 + 340f8b9 commit 3693514
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

Expand All @@ -15,7 +16,7 @@ public class CredentialDefinition {
@JsonProperty("@context")
private List<String> context;

@NotBlank(message = ErrorConstants.INVALID_REQUEST)
@NotEmpty(message = ErrorConstants.INVALID_REQUEST)
private List<String> type;

private Map<String, Object> credentialSubject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import io.mosip.esignet.core.constants.ErrorConstants;
import lombok.Data;
import lombok.NonNull;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@Data
public class CredentialRequest {
Expand All @@ -22,6 +20,7 @@ public class CredentialRequest {
* OPTIONAL.
* JSON object containing proof of possession of the key material the issued Credential shall be bound to.
*/
@Valid
@NotNull(message = ErrorConstants.INVALID_PROOF)
private CredentialProof proof;

Expand All @@ -35,6 +34,7 @@ public class CredentialRequest {
* types: REQUIRED. JSON array. This claim contains the type values the Wallet shall request
* in the subsequent Credential Request.
*/
@Valid
@NotNull(message = ErrorConstants.INVALID_REQUEST)
private CredentialDefinition credential_definition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ public ResponseEntity<VCError> handleVCIControllerExceptions(Exception ex) {
if(ex instanceof MethodArgumentNotValidException) {
FieldError fieldError = ((MethodArgumentNotValidException) ex).getBindingResult().getFieldError();
String message = fieldError != null ? fieldError.getDefaultMessage() : ex.getMessage();
return new ResponseEntity<VCError>(getVCErrorDto(INVALID_REQUEST, message), HttpStatus.BAD_REQUEST);
return new ResponseEntity<VCError>(getVCErrorDto(message, message), HttpStatus.BAD_REQUEST);
}
if(ex instanceof ConstraintViolationException) {
Set<ConstraintViolation<?>> violations = ((ConstraintViolationException) ex).getConstraintViolations();
String message = !violations.isEmpty() ? violations.stream().findFirst().get().getMessage() : ex.getMessage();
return new ResponseEntity<VCError>(getVCErrorDto(INVALID_REQUEST, message), HttpStatus.BAD_REQUEST);
return new ResponseEntity<VCError>(getVCErrorDto(message, message), HttpStatus.BAD_REQUEST);
}
if(ex instanceof NotAuthenticatedException) {
String errorCode = ((EsignetException) ex).getErrorCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Slf4j
Expand All @@ -25,7 +26,7 @@ public class VCIssuanceController {
* @throws EsignetException
*/
@PostMapping(value = "/credential",produces = "application/json")
public CredentialResponse getCredential(@RequestBody CredentialRequest credentialRequest) throws EsignetException {
public CredentialResponse getCredential(@Valid @RequestBody CredentialRequest credentialRequest) throws EsignetException {
return vcIssuanceService.getCredential(credentialRequest);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package io.mosip.esignet.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import foundation.identity.jsonld.JsonLDObject;
import io.mosip.esignet.api.spi.AuditPlugin;
import io.mosip.esignet.core.dto.vci.ParsedAccessToken;
import io.mosip.esignet.core.constants.ErrorConstants;
import io.mosip.esignet.core.dto.vci.*;
import io.mosip.esignet.core.spi.VCIssuanceService;
import io.mosip.esignet.services.CacheUtilService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -22,6 +27,8 @@
@WebMvcTest(value = VCIssuanceController.class)
public class VCIssuanceControllerTest {

ObjectMapper objectMapper = new ObjectMapper();

@Autowired
MockMvc mockMvc;

Expand All @@ -37,12 +44,87 @@ public class VCIssuanceControllerTest {
@MockBean
VCIssuanceService vcIssuanceService;

@InjectMocks
VCIssuanceController vcIssuanceController;
@Test
public void getVC_withValidDetails_thenPass() throws Exception {
CredentialDefinition credentialDefinition = new CredentialDefinition();
credentialDefinition.setType(Arrays.asList("VerifiableCredential", "SampleVerifiableCredential_ldp"));
credentialDefinition.setContext(Arrays.asList("https://www.w3.org/2018/credentials/v1"));
CredentialProof credentialProof = new CredentialProof();
credentialProof.setProof_type("jwt");
credentialProof.setJwt("dummy_jwt_proof");
CredentialRequest credentialRequest = new CredentialRequest();
credentialRequest.setFormat("ldp_vc");
credentialRequest.setProof(credentialProof);
credentialRequest.setCredential_definition(credentialDefinition);

CredentialResponse credentialResponse = new CredentialResponse<JsonLDObject>();
credentialResponse.setFormat("ldp_vc");
credentialResponse.setCredential(new JsonLDObject());
Mockito.when(vcIssuanceService.getCredential(credentialRequest)).thenReturn(credentialResponse);

mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.format").exists())
.andExpect(jsonPath("$.credential").exists());
}

@Test
public void getVC_withValidDetails_thenPass() {
//TODO WIP
Assert.assertTrue(true);
public void getVC_withInvalidFormat_thenFail() throws Exception {
CredentialRequest credentialRequest = new CredentialRequest();
credentialRequest.setFormat(null);
CredentialProof credentialProof = new CredentialProof();
credentialProof.setProof_type("jwt");
credentialRequest.setProof(credentialProof);
CredentialDefinition credentialDefinition = new CredentialDefinition();
credentialDefinition.setType(Arrays.asList("VerifiableCredential", "SampleVerifiableCredential_ldp"));
credentialRequest.setCredential_definition(credentialDefinition);

mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value(ErrorConstants.INVALID_VC_FORMAT));

credentialRequest.setFormat(" ");
mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value(ErrorConstants.INVALID_VC_FORMAT));
}

@Test
public void getVC_withInvalidProof_thenFail() throws Exception {
CredentialRequest credentialRequest = new CredentialRequest();
credentialRequest.setFormat("jwt_vc_json");
CredentialDefinition credentialDefinition = new CredentialDefinition();
credentialDefinition.setType(Arrays.asList("VerifiableCredential", "SampleVerifiableCredential_ldp"));
credentialRequest.setCredential_definition(credentialDefinition);

credentialRequest.setProof(null);
mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value(ErrorConstants.INVALID_PROOF));

CredentialProof credentialProof = new CredentialProof();
credentialRequest.setProof(credentialProof);
mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value(ErrorConstants.UNSUPPORTED_PROOF_TYPE));


credentialProof.setProof_type(" ");
credentialRequest.setProof(credentialProof);
mockMvc.perform(post("/vci/credential")
.content(objectMapper.writeValueAsBytes(credentialRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value(ErrorConstants.UNSUPPORTED_PROOF_TYPE));
}
}

0 comments on commit 3693514

Please sign in to comment.