use of org.dspace.authorize.ResourcePolicy 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.authorize.ResourcePolicy in project DSpace by DSpace.
the class ResourcePolicyRestRepositoryIT method patchReplaceStartDataForbiddenTest.
@Test
public void patchReplaceStartDataForbiddenTest() 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).build();
Item item = ItemBuilder.createItem(context, collection).withTitle("Public item").build();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2019);
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 31);
Date date = calendar.getTime();
ResourcePolicy resourcePolicy = ResourcePolicyBuilder.createResourcePolicy(context).withAction(Constants.READ).withDspaceObject(item).withGroup(EPersonServiceFactory.getInstance().getGroupService().findByName(context, Group.ANONYMOUS)).withStartDate(date).withPolicyType(ResourcePolicy.TYPE_CUSTOM).build();
context.restoreAuthSystemState();
Calendar calendar2 = Calendar.getInstance();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
calendar2.set(Calendar.YEAR, 2020);
calendar2.set(Calendar.MONTH, 0);
calendar2.set(Calendar.DATE, 1);
Date newData = calendar2.getTime();
List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/startDate", formatDate.format(newData));
ops.add(replaceOperation);
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().isForbidden());
String authToken2 = getAuthToken(admin.getEmail(), password);
getClient(authToken2).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(date))), hasJsonPath("$.description", is(resourcePolicy.getRpDescription())))));
}
use of org.dspace.authorize.ResourcePolicy 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.authorize.ResourcePolicy in project DSpace by DSpace.
the class ResourcePolicyRestRepositoryIT method patchReplaceEndDateBeforeStartDataTest.
@Test
public void patchReplaceEndDateBeforeStartDataTest() 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();
Calendar calendarStartDate = Calendar.getInstance();
calendarStartDate.set(Calendar.YEAR, 2019);
calendarStartDate.set(Calendar.MONTH, 10);
calendarStartDate.set(Calendar.DATE, 21);
Date startDate = calendarStartDate.getTime();
Calendar calendarEndDate = Calendar.getInstance();
calendarEndDate.set(Calendar.YEAR, 2020);
calendarEndDate.set(Calendar.MONTH, 10);
calendarEndDate.set(Calendar.DATE, 21);
Date endDate = calendarEndDate.getTime();
ResourcePolicy resourcePolicy = ResourcePolicyBuilder.createResourcePolicy(context).withAction(Constants.READ).withDspaceObject(publicItem1).withUser(eperson1).withStartDate(startDate).withEndDate(endDate).withPolicyType(ResourcePolicy.TYPE_CUSTOM).build();
context.restoreAuthSystemState();
Calendar newEndDateCalendar = Calendar.getInstance();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
newEndDateCalendar.set(Calendar.YEAR, 2018);
newEndDateCalendar.set(Calendar.MONTH, 10);
newEndDateCalendar.set(Calendar.DATE, 21);
Date newEndDate = newEndDateCalendar.getTime();
List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/endDate", formatDate.format(newEndDate));
ops.add(replaceOperation);
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().isBadRequest());
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(startDate))), hasJsonPath("$.endDate", is(formatDate.format(endDate))))));
}
use of org.dspace.authorize.ResourcePolicy in project DSpace by DSpace.
the class ResourcePolicyRestRepositoryIT method patchReplaceStartDataTest.
@Test
public void patchReplaceStartDataTest() 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();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2019);
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 31);
Date data = calendar.getTime();
ResourcePolicy resourcePolicy = ResourcePolicyBuilder.createResourcePolicy(context).withAction(Constants.READ).withDspaceObject(publicItem1).withGroup(EPersonServiceFactory.getInstance().getGroupService().findByName(context, Group.ANONYMOUS)).withStartDate(data).withPolicyType(ResourcePolicy.TYPE_CUSTOM).build();
context.restoreAuthSystemState();
Calendar newCalendar = Calendar.getInstance();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
newCalendar.set(Calendar.YEAR, 2020);
newCalendar.set(Calendar.MONTH, 0);
newCalendar.set(Calendar.DATE, 1);
Date newDate = newCalendar.getTime();
List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/startDate", formatDate.format(newDate));
ops.add(replaceOperation);
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))))));
}
Aggregations