diff --git a/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java b/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java new file mode 100644 index 000000000000..1cc470e18e9a --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java @@ -0,0 +1,89 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.administer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.dspace.AbstractIntegrationTestWithDatabase; +import org.dspace.builder.EPersonBuilder; +import org.dspace.content.clarin.ClarinUserRegistration; +import org.dspace.content.factory.ClarinServiceFactory; +import org.dspace.content.service.clarin.ClarinUserRegistrationService; +import org.dspace.eperson.EPerson; +import org.dspace.eperson.Group; +import org.dspace.eperson.factory.EPersonServiceFactory; +import org.dspace.eperson.service.EPersonService; +import org.dspace.eperson.service.GroupService; +import org.junit.After; +import org.junit.Test; + +/** + * Runs the create-administrator command and checks the CLARIN user registration it creates. + */ +public class CreateAdministratorIT extends AbstractIntegrationTestWithDatabase { + + private static final String EMAIL = "create-administrator-it@example.com"; + + private final EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService(); + private final GroupService groupService = EPersonServiceFactory.getInstance().getGroupService(); + private final ClarinUserRegistrationService clarinUserRegistrationService = + ClarinServiceFactory.getInstance().getClarinUserRegistration(); + + /** + * Removes the administrator the command created, the command commits with its own context. + */ + @After + public void deleteCreatedAdministrator() throws Exception { + EPerson created = ePersonService.findByEmail(context, EMAIL); + if (created != null) { + EPersonBuilder.deleteEPerson(created.getID()); + } + } + + /** + * -o ends up in user_registration.organization of the new administrator. + */ + @Test + public void organizationOptionIsStoredInUserRegistration() throws Exception { + CreateAdministrator.main(new String[] {"-e", EMAIL, "-f", "First", "-l", "Last", "-c", "en", + "-p", password, "-o", "MyOrg"}); + + context.turnOffAuthorisationSystem(); + EPerson created = ePersonService.findByEmail(context, EMAIL); + assertNotNull(created); + assertTrue(groupService.isMember(context, created, Group.ADMIN)); + + List registrations = + clarinUserRegistrationService.findByEPersonUUID(context, created.getID()); + assertEquals(1, registrations.size()); + assertEquals("MyOrg", registrations.get(0).getOrganization()); + assertEquals(EMAIL, registrations.get(0).getEmail()); + assertTrue(registrations.get(0).isConfirmation()); + } + + /** + * Without -o the registration gets the unknown organization, and -c still sets the language. + */ + @Test + public void missingOrganizationOptionFallsBackToUnknown() throws Exception { + CreateAdministrator.main(new String[] {"-e", EMAIL, "-f", "First", "-l", "Last", "-c", "cs", + "-p", password}); + + context.turnOffAuthorisationSystem(); + EPerson created = ePersonService.findByEmail(context, EMAIL); + assertEquals("cs", created.getLanguage()); + List registrations = + clarinUserRegistrationService.findByEPersonUUID(context, created.getID()); + assertEquals(1, registrations.size()); + assertEquals(ClarinUserRegistration.UNKNOWN_USER_REGISTRATION, registrations.get(0).getOrganization()); + } +} diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index 3b0cfcba59f5..d05d9196efad 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -43,14 +43,18 @@ import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.ItemBuilder; +import org.dspace.builder.ResourcePolicyBuilder; +import org.dspace.content.Bitstream; import org.dspace.content.Collection; import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; import org.dspace.content.clarin.ClarinUserRegistration; import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; +import org.dspace.core.Constants; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Before; @@ -507,6 +511,82 @@ public void findAllBitstreamsAttachedToLicense() throws Exception { .andExpect(jsonPath("$.bitstreams", is(2))); } + /** + * PUT /api/core/items/{uuid}/bundles?licenseID= moves every ORIGINAL bitstream from its license to the new one + * and writes the dc.rights metadata of the item; an unknown id (-1) only detaches. + */ + @Test + public void updateLicenseForBundleAttachesAndDetachesLicense() throws Exception { + context.turnOffAuthorisationSystem(); + Collection col = CollectionBuilder.createCollection(context, parentCommunity).withName("Bundle col").build(); + Item item = ItemBuilder.createItem(context, col).withTitle("Item without license").build(); + List bitstreams = List.of( + BitstreamBuilder.createBitstream(context, item, toInputStream("test", UTF_8)) + .withName("file.txt") + .build(), + BitstreamBuilder.createBitstream(context, item, toInputStream("test 2", UTF_8)) + .withName("file2.txt") + .build()); + context.restoreAuthSystemState(); + String tokenAdmin = getAuthToken(admin.getEmail(), password); + String itemUri = "/api/core/items/" + item.getID(); + + // a valid license replaces the current one on every bitstream of the ORIGINAL bundle and in the metadata + getClient(tokenAdmin).perform(put(itemUri + "/bundles") + .param("licenseID", String.valueOf(secondCLicense.getID()))) + .andExpect(status().isOk()); + getClient(tokenAdmin).perform(put(itemUri + "/bundles") + .param("licenseID", String.valueOf(firstCLicense.getID()))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.type", is("item"))) + .andExpect(jsonPath("$.id", is(item.getID().toString()))); + getClient(tokenAdmin).perform(get(itemUri)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.metadata['dc.rights'][0].value", is(firstCLicense.getName()))) + .andExpect(jsonPath("$.metadata['dc.rights.uri'][0].value", is(firstCLicense.getDefinition()))) + .andExpect(jsonPath("$.metadata['dc.rights.label'][0].value", + is(firstCLicense.getNonExtendedClarinLicenseLabel().getLabel()))); + for (Bitstream bitstream : bitstreams) { + List mappings = + clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()); + Assert.assertEquals(1, mappings.size()); + Assert.assertEquals(firstCLicense.getID(), mappings.get(0).getLicense().getID()); + } + + // an unknown license id only detaches the current license + getClient(tokenAdmin).perform(put(itemUri + "/bundles").param("licenseID", "-1")) + .andExpect(status().isOk()); + getClient(tokenAdmin).perform(get(itemUri)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.metadata['dc.rights']").doesNotExist()) + .andExpect(jsonPath("$.metadata['dc.rights.uri']").doesNotExist()) + .andExpect(jsonPath("$.metadata['dc.rights.label']").doesNotExist()); + for (Bitstream bitstream : bitstreams) { + Assert.assertTrue(clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()) + .isEmpty()); + } + } + + /** + * The license PUT needs ADD on the item: 403 for a user with only WRITE, 401 for an anonymous call. + */ + @Test + public void updateLicenseForBundleNeedsAddRightsOnItem() throws Exception { + context.turnOffAuthorisationSystem(); + ResourcePolicyBuilder.createResourcePolicy(context, eperson, null) + .withDspaceObject(publicItem2) + .withAction(Constants.WRITE) + .build(); + context.restoreAuthSystemState(); + String tokenEPerson = getAuthToken(eperson.getEmail(), password); + getClient(tokenEPerson).perform(put("/api/core/items/" + publicItem2.getID() + "/bundles") + .param("licenseID", String.valueOf(firstCLicense.getID()))) + .andExpect(status().isForbidden()); + getClient().perform(put("/api/core/items/" + publicItem2.getID() + "/bundles") + .param("licenseID", String.valueOf(firstCLicense.getID()))) + .andExpect(status().isUnauthorized()); + } + private ClarinLicenseLabel getNonExtendedLicenseLabel(List clarinLicenseLabelList) { for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) { if (clarinLicenseLabel.isExtended()) {