Skip to content

Commit

Permalink
Merged in task/dspace-cris-2023_02_x/DSC-1739 (pull request DSpace#2675)
Browse files Browse the repository at this point in the history
[DSC-1739] create collection/community logo with the same polices as parent collection/community

Approved-by: Stefano Maffei
  • Loading branch information
eskander17 authored and steph-ieffam committed Oct 2, 2024
2 parents 9b57b6d + f1a818c commit 85f0e68
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ public Bitstream setLogo(Context context, Collection collection, InputStream is)

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, collection, Constants.READ);
List<ResourcePolicy> policies = authorizeService.getPolicies(context, collection);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ public Bitstream setLogo(Context context, Community community, InputStream is)

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, community, Constants.READ);
List<ResourcePolicy> policies = authorizeService.getPolicies(context, community);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
*/
package org.dspace.app.rest;

import static org.dspace.app.matcher.ResourcePolicyMatcher.matches;
import static org.dspace.authorize.ResourcePolicy.TYPE_CUSTOM;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.service.BitstreamService;
import org.dspace.core.Constants;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.GroupService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;
Expand All @@ -33,6 +47,15 @@ public class CollectionLogoControllerIT extends AbstractControllerIntegrationTes
private MockMultipartFile bitstreamFile;
private Collection childCollection;

@Autowired
private GroupService groupService;

@Autowired
private AuthorizeService authorizeService;

@Autowired
private BitstreamService bitstreamService;

@Before
public void createStructure() throws Exception {
context.turnOffAuthorisationSystem();
Expand Down Expand Up @@ -142,6 +165,37 @@ public void deleteLogoNoRights() throws Exception {
.andExpect(status().isForbidden());
}

@Test
public void collectionLogoPoliciesTest() throws Exception {
context.turnOffAuthorisationSystem();
Group group = groupService.findByName(context, Group.ANONYMOUS);
authorizeService.removeAllPolicies(context, childCollection);
authorizeService.addPolicy(context, childCollection, Constants.READ, group, ResourcePolicy.TYPE_CUSTOM);
authorizeService.addPolicy(context, childCollection, Constants.WRITE, group, ResourcePolicy.TYPE_CUSTOM);
context.commit();
context.restoreAuthSystemState();

String postUuid = createLogoInternal();
assert (postUuid != null);

childCollection = context.reloadEntity(childCollection);

assertThat(childCollection.getResourcePolicies(), hasSize(2));
assertThat(childCollection.getResourcePolicies(), Matchers.hasItems(
matches(Constants.READ, group, TYPE_CUSTOM),
matches(Constants.WRITE, group, TYPE_CUSTOM)
));

Bitstream logo = bitstreamService.find(context, UUID.fromString(postUuid));

// logo polices are equal to parent collection polices
assertThat(logo.getResourcePolicies(), hasSize(2));
assertThat(logo.getResourcePolicies(), Matchers.hasItems(
matches(Constants.READ, group, TYPE_CUSTOM),
matches(Constants.WRITE, group, TYPE_CUSTOM)
));
}

private String getLogoUrlTemplate(String uuid) {
return "/api/core/collections/" + uuid + "/logo";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@
*/
package org.dspace.app.rest;

import static org.dspace.app.matcher.ResourcePolicyMatcher.matches;
import static org.dspace.authorize.ResourcePolicy.TYPE_CUSTOM;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.builder.CommunityBuilder;
import org.dspace.content.Bitstream;
import org.dspace.content.service.BitstreamService;
import org.dspace.core.Constants;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.GroupService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;
Expand All @@ -30,6 +44,15 @@ public class CommunityLogoControllerIT extends AbstractControllerIntegrationTest
private String bitstreamContent;
private MockMultipartFile bitstreamFile;

@Autowired
private GroupService groupService;

@Autowired
private AuthorizeService authorizeService;

@Autowired
private BitstreamService bitstreamService;

@Before
public void createStructure() throws Exception {
context.turnOffAuthorisationSystem();
Expand Down Expand Up @@ -137,6 +160,37 @@ public void deleteLogoNoRights() throws Exception {
.andExpect(status().isForbidden());
}

@Test
public void communityLogoPoliciesTest() throws Exception {
context.turnOffAuthorisationSystem();
Group group = groupService.findByName(context, Group.ANONYMOUS);
authorizeService.removeAllPolicies(context, parentCommunity);
authorizeService.addPolicy(context, parentCommunity, Constants.READ, group, ResourcePolicy.TYPE_CUSTOM);
authorizeService.addPolicy(context, parentCommunity, Constants.WRITE, group, ResourcePolicy.TYPE_CUSTOM);
context.commit();
context.restoreAuthSystemState();

String postUuid = createLogoInternal();
assert (postUuid != null);

parentCommunity = context.reloadEntity(parentCommunity);

assertThat(parentCommunity.getResourcePolicies(), hasSize(2));
assertThat(parentCommunity.getResourcePolicies(), Matchers.hasItems(
matches(Constants.READ, group, TYPE_CUSTOM),
matches(Constants.WRITE, group, TYPE_CUSTOM)
));

Bitstream logo = bitstreamService.find(context, UUID.fromString(postUuid));

// logo polices are equal to parent community polices
assertThat(logo.getResourcePolicies(), hasSize(2));
assertThat(logo.getResourcePolicies(), Matchers.hasItems(
matches(Constants.READ, group, TYPE_CUSTOM),
matches(Constants.WRITE, group, TYPE_CUSTOM)
));
}

private String getLogoUrlTemplate(String uuid) {
return "/api/core/communities/" + uuid + "/logo";
}
Expand Down

0 comments on commit 85f0e68

Please sign in to comment.