Skip to content

Commit

Permalink
Bxc 4063 clear thumbnail api (#1609)
Browse files Browse the repository at this point in the history
* BXC-4063 add tests and generalize PID string request variable

* BXC-4063 add controller and tests pass

* BXC-4063 fixing inconsistent code and backing out of PID changes

---------

Co-authored-by: Sharon Luong <[email protected]>
  • Loading branch information
sharonluong and Sharon Luong authored Sep 8, 2023
1 parent 1c3f330 commit f63ff64
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -36,7 +37,8 @@
/**
* Controller for handling thumbnail requests, including:
* upload submission for collection display thumbnails,
* assigning a child object to use as a thumbnail
* assigning a child object to use as a thumbnail,
* and deleting an assigned child object thumbnail
*
* @author lfarrell
*
Expand Down Expand Up @@ -121,4 +123,34 @@ public ResponseEntity<Object> assignThumbnail(@PathVariable("pidString") String
}
return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping(value = "/edit/deleteThumbnail/{pidString}")
@ResponseBody
public ResponseEntity<Object> deleteThumbnail(@PathVariable("pidString") String pidString) {
PID pid = PIDs.get(pidString);

AccessGroupSet principals = getAgentPrincipals().getPrincipals();
aclService.assertHasAccess("Insufficient permissions to assign thumbnail for " + pidString,
pid, principals, Permission.editDescription);

var object = repositoryObjectLoader.getRepositoryObject(pid);
if (!(object instanceof FileObject)) {
log.error("Error object is not a file: {}", pidString);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

var agent = AgentPrincipalsImpl.createFromThread();
var request = new ThumbnailRequest();
request.setAgent(agent);
request.setFilePidString(pidString);
request.setAction(ThumbnailRequest.DELETE);
try {
thumbnailRequestSender.sendToQueue(request);
} catch (IOException e) {
log.error("Error deleting assigned thumbnail for {}", request.getFilePidString(), e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.nio.file.Path;

import edu.unc.lib.boxc.model.api.objects.RepositoryObjectLoader;
Expand Down Expand Up @@ -209,6 +209,49 @@ public void assignThumbnailPidIsNotAFile() throws Exception {
verify(thumbnailRequestSender, never()).sendMessage(any(Document.class));
}

@Test
public void deleteThumbnailSuccess() throws Exception {
var pid = makePid();
var filePidString = pid.getId();
var file = repositoryObjectFactory.createFileObject(pid, null);
when(repositoryObjectLoader.getRepositoryObject(pid)).thenReturn(file);
mvc.perform(delete("/edit/deleteThumbnail/" + filePidString))
.andExpect(status().is2xxSuccessful())
.andReturn();

verify(thumbnailRequestSender).sendToQueue(requestCaptor.capture());
ThumbnailRequest request = requestCaptor.getValue();
assertEquals(filePidString, request.getFilePidString());
assertEquals(ThumbnailRequest.DELETE, request.getAction());
}

@Test
public void deleteThumbnailNoAccess() throws Exception {
var pid = makePid();
var filePidString = pid.getId();

doThrow(new AccessRestrictionException()).when(aclService)
.assertHasAccess(anyString(), eq(pid), any(AccessGroupSetImpl.class), eq(editDescription));

mvc.perform(delete("/edit/deleteThumbnail/" + filePidString))
.andExpect(status().isForbidden())
.andReturn();
verify(thumbnailRequestSender, never()).sendMessage(any(Document.class));
}

@Test
public void deleteThumbnailPidIsNotAFile() throws Exception {
var pid = makePid();
var workPidString = pid.getId();
var work = repositoryObjectFactory.createWorkObject(pid, null);
when(repositoryObjectLoader.getRepositoryObject(pid)).thenReturn(work);

mvc.perform(delete("/edit/deleteThumbnail/" + workPidString))
.andExpect(status().isBadRequest())
.andReturn();
verify(thumbnailRequestSender, never()).sendMessage(any(Document.class));
}

private byte[] textStream() {
return "I am not an image".getBytes();
}
Expand Down

0 comments on commit f63ff64

Please sign in to comment.