Search in sources :

Example 1 with AddOperation

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))))));
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ReplaceOperation(org.dspace.app.rest.model.patch.ReplaceOperation) RemoveOperation(org.dspace.app.rest.model.patch.RemoveOperation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) EPerson(org.dspace.eperson.EPerson) Date(java.util.Date) Item(org.dspace.content.Item) Collection(org.dspace.content.Collection) ResourcePolicy(org.dspace.authorize.ResourcePolicy) SimpleDateFormat(java.text.SimpleDateFormat) Community(org.dspace.content.Community) Test(org.junit.Test) AbstractControllerIntegrationTest(org.dspace.app.rest.test.AbstractControllerIntegrationTest)

Example 2 with AddOperation

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))))));
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ReplaceOperation(org.dspace.app.rest.model.patch.ReplaceOperation) RemoveOperation(org.dspace.app.rest.model.patch.RemoveOperation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) EPerson(org.dspace.eperson.EPerson) Date(java.util.Date) Item(org.dspace.content.Item) Collection(org.dspace.content.Collection) ResourcePolicy(org.dspace.authorize.ResourcePolicy) SimpleDateFormat(java.text.SimpleDateFormat) Community(org.dspace.content.Community) Test(org.junit.Test) AbstractControllerIntegrationTest(org.dspace.app.rest.test.AbstractControllerIntegrationTest)

Example 3 with AddOperation

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());
    }
}
Also used : Bitstream(org.dspace.content.Bitstream) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplaceOperation(org.dspace.app.rest.model.patch.ReplaceOperation) RemoveOperation(org.dspace.app.rest.model.patch.RemoveOperation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) EPerson(org.dspace.eperson.EPerson) Collection(org.dspace.content.Collection) Map(java.util.Map) HashMap(java.util.HashMap) WorkspaceItem(org.dspace.content.WorkspaceItem) AbstractControllerIntegrationTest(org.dspace.app.rest.test.AbstractControllerIntegrationTest) Test(org.junit.Test)

Example 4 with AddOperation

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));
}
Also used : ArrayList(java.util.ArrayList) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) ReplaceOperation(org.dspace.app.rest.model.patch.ReplaceOperation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Patch(org.dspace.app.rest.model.patch.Patch) Test(org.junit.Test)

Example 5 with AddOperation

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());
}
Also used : ArrayList(java.util.ArrayList) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) EPerson(org.dspace.eperson.EPerson) Test(org.junit.Test) AbstractControllerIntegrationTest(org.dspace.app.rest.test.AbstractControllerIntegrationTest)

Aggregations

ArrayList (java.util.ArrayList)84 AddOperation (org.dspace.app.rest.model.patch.AddOperation)84 Operation (org.dspace.app.rest.model.patch.Operation)84 ReplaceOperation (org.dspace.app.rest.model.patch.ReplaceOperation)80 Test (org.junit.Test)80 AbstractControllerIntegrationTest (org.dspace.app.rest.test.AbstractControllerIntegrationTest)77 RemoveOperation (org.dspace.app.rest.model.patch.RemoveOperation)67 Collection (org.dspace.content.Collection)65 WorkspaceItem (org.dspace.content.WorkspaceItem)53 Community (org.dspace.content.Community)51 HashMap (java.util.HashMap)48 EPerson (org.dspace.eperson.EPerson)30 InputStream (java.io.InputStream)27 Map (java.util.Map)23 Item (org.dspace.content.Item)14 Bitstream (org.dspace.content.Bitstream)12 SimpleDateFormat (java.text.SimpleDateFormat)11 Date (java.util.Date)11 ResourcePolicy (org.dspace.authorize.ResourcePolicy)10 Group (org.dspace.eperson.Group)7