DURACOM-136 improve handling and testing of invalid mimetype

This commit is contained in:
Andrea Bollini
2023-06-06 16:01:47 +02:00
parent 14bb32036c
commit 2b523ba5ac
2 changed files with 29 additions and 3 deletions

View File

@@ -19,9 +19,12 @@ import org.dspace.app.rest.model.hateoas.ProcessResource;
import org.dspace.app.rest.repository.ScriptRestRepository;
import org.dspace.app.rest.utils.ContextUtil;
import org.dspace.core.Context;
import org.dspace.scripts.configuration.ScriptConfiguration;
import org.dspace.scripts.service.ScriptService;
import org.dspace.services.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.ControllerUtils;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -50,6 +53,9 @@ public class ScriptProcessesController {
@Autowired
private ScriptRestRepository scriptRestRepository;
@Autowired
private ScriptService scriptService;
@Autowired
private RequestService requestService;
@@ -80,9 +86,17 @@ public class ScriptProcessesController {
@RequestMapping(method = RequestMethod.POST, consumes = "!" + MediaType.MULTIPART_FORM_DATA_VALUE)
@PreAuthorize("hasAuthority('AUTHENTICATED')")
public ResponseEntity<RepresentationModel<?>> startProcessInvalidMimeType(
@PathVariable(name = "name") String scriptName,
@RequestParam(name = "file", required = false) List<MultipartFile> files)
@PathVariable(name = "name") String scriptName)
throws Exception {
if (log.isTraceEnabled()) {
log.trace("Starting Process for Script with name: " + scriptName);
}
Context context = ContextUtil.obtainContext(requestService.getCurrentRequest().getHttpServletRequest());
ScriptConfiguration scriptToExecute = scriptService.getScriptConfiguration(scriptName);
if (scriptToExecute == null) {
throw new ResourceNotFoundException("The script for name: " + scriptName + " wasn't found");
}
throw new DSpaceBadRequestException("Invalid mimetype");
}

View File

@@ -134,6 +134,13 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
.andExpect(jsonPath("$.page.totalElements", is(0)));
}
@Test
public void findAllScriptsAnonymousUserTest() throws Exception {
getClient().perform(get("/api/system/scripts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.totalElements", is(0)));
}
@Test
public void findAllScriptsLocalAdminsTest() throws Exception {
context.turnOffAuthorisationSystem();
@@ -543,8 +550,13 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
public void postProcessAdminWithWrongContentTypeBadRequestException() throws Exception {
String token = getAuthToken(admin.getEmail(), password);
getClient(token)
.perform(post("/api/system/scripts/mock-script/processes"))
.andExpect(status().isBadRequest());
getClient(token).perform(post("/api/system/scripts/mock-script-invalid/processes"))
.andExpect(status().isBadRequest());
.andExpect(status().isNotFound());
}
@Test