use of org.dspace.app.rest.model.patch.AddOperation in project DSpace by DSpace.
the class ResourcePolicyRestRepositoryIT method patchAddEndDataTest.
@Test
public void patchAddEndDataTest() throws Exception {
context.turnOffAuthorisationSystem();
EPerson eperson1 = EPersonBuilder.createEPerson(context).withEmail("eperson1@mail.com").withPassword("qwerty01").build();
Community community = CommunityBuilder.createCommunity(context).build();
Collection collection = CollectionBuilder.createCollection(context, community).withAdminGroup(eperson1).build();
Item publicItem1 = ItemBuilder.createItem(context, collection).withTitle("Public item").build();
ResourcePolicy resourcePolicy = ResourcePolicyBuilder.createResourcePolicy(context).withAction(Constants.READ).withDspaceObject(publicItem1).withGroup(EPersonServiceFactory.getInstance().getGroupService().findByName(context, Group.ANONYMOUS)).withPolicyType(ResourcePolicy.TYPE_CUSTOM).build();
context.restoreAuthSystemState();
Calendar newCalendar = Calendar.getInstance();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
Date newDate = new Date();
List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/endDate", formatDate.format(newDate));
ops.add(addOperation);
String patchBody = getPatchContent(ops);
String authToken = getAuthToken(eperson1.getEmail(), "qwerty01");
getClient(authToken).perform(patch("/api/authz/resourcepolicies/" + resourcePolicy.getID()).content(patchBody).contentType(MediaType.APPLICATION_JSON_PATCH_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.allOf(hasJsonPath("$.name", is(resourcePolicy.getRpName())), hasJsonPath("$.description", is(resourcePolicy.getRpDescription())), hasJsonPath("$.action", is(Constants.actionText[resourcePolicy.getAction()])), hasJsonPath("$.startDate", is(resourcePolicy.getStartDate())), hasJsonPath("$.endDate", is(formatDate.format(newDate))))));
getClient(authToken).perform(get("/api/authz/resourcepolicies/" + resourcePolicy.getID())).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.allOf(hasJsonPath("$.action", is(Constants.actionText[resourcePolicy.getAction()])), hasJsonPath("$.endDate", is(formatDate.format(newDate))))));
}
use of org.dspace.app.rest.model.patch.AddOperation in project DSpace by DSpace.
the class ResourcePolicyRestRepositoryIT method patchAddStartDataTest.
@Test
public void patchAddStartDataTest() throws Exception {
context.turnOffAuthorisationSystem();
EPerson eperson1 = EPersonBuilder.createEPerson(context).withEmail("eperson1@mail.com").withPassword("qwerty01").build();
Community community = CommunityBuilder.createCommunity(context).build();
Collection collection = CollectionBuilder.createCollection(context, community).withAdminGroup(eperson1).build();
Item publicItem1 = ItemBuilder.createItem(context, collection).withTitle("Public item").build();
ResourcePolicy resourcePolicy = ResourcePolicyBuilder.createResourcePolicy(context).withAction(Constants.READ).withDspaceObject(publicItem1).withGroup(EPersonServiceFactory.getInstance().getGroupService().findByName(context, Group.ANONYMOUS)).withPolicyType(ResourcePolicy.TYPE_CUSTOM).build();
context.restoreAuthSystemState();
Calendar newCalendar = Calendar.getInstance();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
newCalendar.set(Calendar.YEAR, 2019);
newCalendar.set(Calendar.MONTH, 9);
newCalendar.set(Calendar.DATE, 31);
Date newDate = newCalendar.getTime();
List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/startDate", formatDate.format(newDate));
ops.add(addOperation);
String patchBody = getPatchContent(ops);
String authToken = getAuthToken(eperson1.getEmail(), "qwerty01");
getClient(authToken).perform(patch("/api/authz/resourcepolicies/" + resourcePolicy.getID()).content(patchBody).contentType(MediaType.APPLICATION_JSON_PATCH_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.allOf(hasJsonPath("$.name", is(resourcePolicy.getRpName())), hasJsonPath("$.description", is(resourcePolicy.getRpDescription())), hasJsonPath("$.action", is(Constants.actionText[resourcePolicy.getAction()])), hasJsonPath("$.startDate", is(formatDate.format(newDate))), hasJsonPath("$.endDate", is(resourcePolicy.getEndDate())))));
getClient(authToken).perform(get("/api/authz/resourcepolicies/" + resourcePolicy.getID())).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.allOf(hasJsonPath("$.action", is(Constants.actionText[resourcePolicy.getAction()])), hasJsonPath("$.startDate", is(formatDate.format(newDate))))));
}
use of org.dspace.app.rest.model.patch.AddOperation in project DSpace by DSpace.
the class WorkflowItemRestRepositoryIT method whenWorkspaceitemBecomeWorkflowitemWithAccessConditionsTheBitstremMustBeDownloableTest.
@Test
public void whenWorkspaceitemBecomeWorkflowitemWithAccessConditionsTheBitstremMustBeDownloableTest() throws Exception {
context.turnOffAuthorisationSystem();
EPerson submitter = EPersonBuilder.createEPerson(context).withEmail("submitter@example.com").withPassword(password).build();
EPerson reviewer1 = EPersonBuilder.createEPerson(context).withEmail("reviewer1@example.com").withPassword(password).build();
parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
Collection collection1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").withWorkflowGroup(1, reviewer1).build();
Bitstream bitstream = null;
WorkspaceItem witem = null;
String bitstreamContent = "0123456789";
AtomicReference<Integer> idRef = new AtomicReference<Integer>();
try (InputStream is = IOUtils.toInputStream(bitstreamContent, Charset.defaultCharset())) {
context.setCurrentUser(submitter);
witem = WorkspaceItemBuilder.createWorkspaceItem(context, collection1).withTitle("Test WorkspaceItem").withIssueDate("2019-10-01").build();
bitstream = BitstreamBuilder.createBitstream(context, witem.getItem(), is).withName("Test bitstream").withDescription("This is a bitstream to test range requests").withMimeType("text/plain").build();
context.restoreAuthSystemState();
String tokenEPerson = getAuthToken(eperson.getEmail(), password);
String tokenSubmitter = getAuthToken(submitter.getEmail(), password);
String tokenReviewer1 = getAuthToken(reviewer1.getEmail(), password);
// submitter can download the bitstream
getClient(tokenSubmitter).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
// reviewer can't still download the bitstream
getClient(tokenReviewer1).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
// others can't download the bitstream
getClient(tokenEPerson).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
// create a list of values to use in add operation
List<Operation> addAccessCondition = new ArrayList<>();
List<Map<String, String>> accessConditions = new ArrayList<Map<String, String>>();
Map<String, String> value = new HashMap<>();
value.put("name", "administrator");
accessConditions.add(value);
addAccessCondition.add(new AddOperation("/sections/upload/files/0/accessConditions", accessConditions));
String patchBody = getPatchContent(addAccessCondition);
getClient(tokenSubmitter).perform(patch("/api/submission/workspaceitems/" + witem.getID()).content(patchBody).contentType(MediaType.APPLICATION_JSON_PATCH_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
// verify that the patch changes have been persisted
getClient(tokenSubmitter).perform(get("/api/submission/workspaceitems/" + witem.getID())).andExpect(status().isOk()).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
// submit the workspaceitem to start the workflow
getClient(tokenSubmitter).perform(post(BASE_REST_SERVER_URL + "/api/workflow/workflowitems").content("/api/submission/workspaceitems/" + witem.getID()).contentType(textUriContentType)).andExpect(status().isCreated()).andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id")));
// check that the workflowitem is persisted
getClient(tokenSubmitter).perform(get("/api/workflow/workflowitems/" + idRef.get())).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.is(WorkflowItemMatcher.matchItemWithTitleAndDateIssued(null, "Test WorkspaceItem", "2019-10-01")))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
// reviewer can download the bitstream
getClient(tokenReviewer1).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
// submitter can download the bitstream
getClient(tokenSubmitter).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
// others can't download the bitstream
getClient(tokenEPerson).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
} finally {
// remove the workflowitem if any
WorkflowItemBuilder.deleteWorkflowItem(idRef.get());
}
}
use of org.dspace.app.rest.model.patch.AddOperation in project DSpace by DSpace.
the class EPersonRestPermissionEvaluatorPluginTest method testHasPatchPermissionAuthOk.
@Test
public void testHasPatchPermissionAuthOk() throws Exception {
List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/password", "testpass");
ops.add(addOperation);
Patch patch = new Patch(ops);
assertTrue(ePersonRestPermissionEvaluatorPlugin.hasPatchPermission(authentication, null, null, patch));
}
use of org.dspace.app.rest.model.patch.AddOperation in project DSpace by DSpace.
the class ShibbolethLoginFilterIT method patchPassword.
@Test
public void patchPassword() throws Exception {
context.turnOffAuthorisationSystem();
EPerson ePerson = EPersonBuilder.createEPerson(context).withNameInMetadata("John", "Doe").withEmail("Johndoe@example.com").withPassword(password).build();
context.restoreAuthSystemState();
String newPassword = "newpassword";
List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/password", newPassword);
ops.add(addOperation);
String patchBody = getPatchContent(ops);
// login through shibboleth
String token = getClient().perform(get("/api/authn/shibboleth").requestAttr("SHIB-MAIL", eperson.getEmail())).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("http://localhost:4000")).andReturn().getResponse().getHeader("Authorization");
getClient(token).perform(get("/api/authn/status")).andExpect(status().isOk()).andExpect(jsonPath("$.authenticated", is(true))).andExpect(jsonPath("$.authenticationMethod", is("shibboleth")));
// updates password
getClient(token).perform(patch("/api/eperson/epersons/" + ePerson.getID()).content(patchBody).contentType(MediaType.APPLICATION_JSON_PATCH_JSON)).andExpect(status().isForbidden());
}
Aggregations