Skip to content

Commit

Permalink
MOSIP-24848-if-we-provide-invalid-schema-type-even-though-we-are-gett…
Browse files Browse the repository at this point in the history
…ing-attribute-names-list (mosip#719)

* MOSIP-24848 added validation on schemaType

* MOSIP-24848 added junit

* removed unused boolean variable

---------

Co-authored-by: Ritik Jain <[email protected]>
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
2 people authored and kameshsr committed Oct 17, 2023
1 parent d2d4e05 commit 4d18e7b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import io.mosip.resident.exception.ApisResourceAccessException;
import io.mosip.resident.exception.InvalidInputException;
import io.mosip.resident.exception.ResidentServiceCheckedException;
import io.mosip.resident.exception.ResidentServiceException;
import io.mosip.resident.service.IdentityService;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.util.EventEnum;
import io.mosip.resident.validator.RequestValidator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -44,6 +46,9 @@ public class IdentityController {
@Autowired
private IdentityService idServiceImpl;

@Autowired
private RequestValidator validator;

@Value("${resident.identity.info.id}")
private String residentIdentityInfoId;

Expand All @@ -66,16 +71,18 @@ public ResponseWrapper<Object> getInputAttributeValues(@PathVariable("schemaType
throws ResidentServiceCheckedException, ApisResourceAccessException, IOException {
logger.debug("IdentityController::getInputAttributeValues()::entry");
auditUtil.setAuditRequestDto(EventEnum.GET_INPUT_ATTRIBUTES);
try {
validator.validateSchemaType(schemaType);
} catch (InvalidInputException e) {
throw new ResidentServiceException(e.getErrorCode(), e.getErrorText(), e,
Map.of(ResidentConstants.REQ_RES_ID, residentIdentityInfoId));
}
ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
try{
String id = getIdFromUser();
Map<String, ?> propertiesResponse = idServiceImpl.getIdentityAttributes(id, true, schemaType, List.of());
Map<String, ?> propertiesResponse = idServiceImpl.getIdentityAttributes(id, schemaType, List.of());
auditUtil.setAuditRequestDto(EventEnum.GET_INPUT_ATTRIBUTES_SUCCESS);
logger.debug("IdentityController::getInputAttributeValues()::exit");
responseWrapper.setResponse(propertiesResponse);
}catch (Exception exception){
throw new InvalidInputException(ResidentConstants.SCHEMA_TYPE);
}
responseWrapper.setId(residentIdentityInfoId);
responseWrapper.setVersion(residentIdentityInfoVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,16 @@ public interface IdentityService {
* Get ID-Repo api data by id.
*
* @param id
* @param schemaType
* @return Map
* @throws ResidentServiceCheckedException
* @throws IOException
*/
Map<String, ?> getIdentityAttributes(String id,String schemaType) throws ResidentServiceCheckedException, IOException;

/**
* Get ID-Repo api data by id, type and includeUin.
*
* @param id
* @param includeUin
* @return Map
* @throws ResidentServiceCheckedException
*/
Map<String, ?> getIdentityAttributes(String id, boolean includeUin,String schemaType)
throws ResidentServiceCheckedException;
Map<String, Object> getIdentityAttributes(String id, String schemaType) throws ResidentServiceCheckedException, IOException;

public String getResidentIndvidualId() throws ApisResourceAccessException;

Map<String, Object> getIdentityAttributes(String id, boolean includeUin, String schemaType,
Map<String, Object> getIdentityAttributes(String id, String schemaType,
List<String> additionalAttributes) throws ResidentServiceCheckedException;

String getUinForIndividualId(String idvid) throws ResidentServiceCheckedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public IdentityDTO getIdentity(String id, boolean fetchFace, String langCode) th
IdentityDTO identityDTO = new IdentityDTO();
try {
Map<String, Object> identity =
getIdentityAttributes(id, true,env.getProperty(ResidentConstants.RESIDENT_IDENTITY_SCHEMATYPE));
getIdentityAttributes(id, env.getProperty(ResidentConstants.RESIDENT_IDENTITY_SCHEMATYPE));
/**
* It is assumed that in the UI schema the UIN is added.
*/
Expand Down Expand Up @@ -208,20 +208,14 @@ public IdentityDTO getIdentity(String id, boolean fetchFace, String langCode) th
}

@Override
public Map<String, ?> getIdentityAttributes(String id,String schemaType) throws ResidentServiceCheckedException, IOException {
Map<String, ?> identityAttributes = getIdentityAttributes(id, false,schemaType);
return identityAttributes;
}

@Override
public Map<String, Object> getIdentityAttributes(String id, boolean includeUin,String schemaType) throws ResidentServiceCheckedException {
return getIdentityAttributes(id, includeUin, schemaType, List.of(
public Map<String, Object> getIdentityAttributes(String id, String schemaType) throws ResidentServiceCheckedException, IOException {
return getIdentityAttributes(id, schemaType, List.of(
Objects.requireNonNull(env.getProperty(ResidentConstants.ADDITIONAL_ATTRIBUTE_TO_FETCH))
.split(ResidentConstants.COMMA)));
}

@Override
public Map<String, Object> getIdentityAttributes(String id, boolean includeUin, String schemaType,
public Map<String, Object> getIdentityAttributes(String id, String schemaType,
List<String> additionalAttributes) throws ResidentServiceCheckedException {
logger.debug("IdentityServiceImpl::getIdentityAttributes()::entry");
Map<String, String> pathsegments = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.mosip.resident.service.impl;

import java.util.Optional;

public enum UISchemaTypes {
UPDATE_DEMOGRAPHICS("update-demographics"),
PERSONALIZED_CARD("personalized-card"),
Expand All @@ -16,4 +18,12 @@ public String getFileIdentifier() {
return fileIdentifier;
}

public static Optional<UISchemaTypes> getUISchemaTypeFromString(String schemaTypeString) {
for (UISchemaTypes uiSchemaType : values()) {
if (uiSchemaType.getFileIdentifier().equals(schemaTypeString)) {
return Optional.of(uiSchemaType);
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.mosip.resident.repository.ResidentTransactionRepository;
import io.mosip.resident.service.impl.IdentityServiceImpl;
import io.mosip.resident.service.impl.ResidentServiceImpl;
import io.mosip.resident.service.impl.UISchemaTypes;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.util.EventEnum;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -966,6 +967,15 @@ private void validateServiceType(String serviceType, String requestServiceHistor
}
}

public void validateSchemaType(String schemaType) {
Optional<UISchemaTypes> uiSchemaTypeOptional = UISchemaTypes.getUISchemaTypeFromString(schemaType);
if (uiSchemaTypeOptional.isEmpty()) {
audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.INPUT_INVALID,
ResidentConstants.SCHEMA_TYPE, "Validating schema type"));
throw new InvalidInputException(ResidentConstants.SCHEMA_TYPE);
}
}

public void validateEventId(String eventId) {
validateMissingInputParameter(eventId, TemplateVariablesConstants.EVENT_ID);
if (isNumeric(eventId) || eventId.length()!=EVENT_ID_LENGTH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.IOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.LinkedHashMap;
Expand All @@ -22,6 +23,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand All @@ -32,13 +34,18 @@
import io.mosip.resident.controller.DocumentController;
import io.mosip.resident.controller.IdAuthController;
import io.mosip.resident.controller.IdentityController;
import io.mosip.resident.exception.ApisResourceAccessException;
import io.mosip.resident.exception.InvalidInputException;
import io.mosip.resident.exception.ResidentServiceCheckedException;
import io.mosip.resident.exception.ResidentServiceException;
import io.mosip.resident.helper.ObjectStoreHelper;
import io.mosip.resident.service.ProxyIdRepoService;
import io.mosip.resident.service.ResidentVidService;
import io.mosip.resident.service.impl.IdentityServiceImpl;
import io.mosip.resident.service.impl.ResidentServiceImpl;
import io.mosip.resident.test.ResidentTestBootApplication;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.validator.RequestValidator;

/**
* Resident identity controller test class.
Expand All @@ -65,6 +72,9 @@ public class IdentityControllerTest {
@Mock
private AuditUtil auditUtil;

@Mock
private RequestValidator validator;

@MockBean
@Qualifier("selfTokenRestTemplate")
private RestTemplate residentRestTemplate;
Expand Down Expand Up @@ -107,6 +117,7 @@ public void setUp() throws Exception {
responseWrapper.setVersion("v1");
responseWrapper.setId("1");
responseWrapper.setResponse(identityMap);
ReflectionTestUtils.setField(identityController, "residentIdentityInfoId", "identity.id");
}

@Test
Expand All @@ -115,4 +126,10 @@ public void testGetInputAttributeValues() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/identity/info/type/schemaType")).andExpect(status().isOk());
}

@Test(expected = ResidentServiceException.class)
public void testGetInputAttributeValuesWithInvalidInputException()
throws ApisResourceAccessException, ResidentServiceCheckedException, IOException {
Mockito.doThrow(new InvalidInputException()).when(validator).validateSchemaType(Mockito.anyString());
identityController.getInputAttributeValues("schema-type");
}
}

0 comments on commit 4d18e7b

Please sign in to comment.