From 591d972f7058cbd0628b02f4a23341be9644ea50 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Sat, 17 Oct 2026 11:55:43 +0200 Subject: [PATCH 1/3] Add ITs for create-administrator -o and PUT items/{uuid}/bundles?licenseID Co-Authored-By: Claude Fable 5.1 --- .../administer/CreateAdministratorIT.java | 88 +++++++++++++++++++ .../rest/ClarinLicenseRestRepositoryIT.java | 55 ++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java 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..b521db333f2a --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java @@ -0,0 +1,88 @@ +/** + * 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. + */ + @Test + public void missingOrganizationOptionFallsBackToUnknown() throws Exception { + CreateAdministrator.main(new String[] {"-e", EMAIL, "-f", "First", "-l", "Last", "-c", "en", + "-p", password}); + + context.turnOffAuthorisationSystem(); + EPerson created = ePersonService.findByEmail(context, EMAIL); + 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..ac7d7a31cbf9 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,10 +43,12 @@ import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.ItemBuilder; +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; @@ -507,6 +509,59 @@ public void findAllBitstreamsAttachedToLicense() throws Exception { .andExpect(jsonPath("$.bitstreams", is(2))); } + /** + * PUT /api/core/items/{uuid}/bundles?licenseID= moves every ORIGINAL bitstream to the license 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(); + Bitstream bitstream = BitstreamBuilder.createBitstream(context, item, toInputStream("test", UTF_8)) + .withName("file.txt") + .build(); + context.restoreAuthSystemState(); + String tokenAdmin = getAuthToken(admin.getEmail(), password); + String bundlesUri = "/api/core/items/" + item.getID() + "/bundles"; + + // attach the license to every bitstream of the ORIGINAL bundle and to the item metadata + getClient(tokenAdmin).perform(put(bundlesUri).param("licenseID", String.valueOf(firstCLicense.getID()))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(item.getID().toString()))) + .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()))); + 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(bundlesUri).param("licenseID", "-1")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.metadata['dc.rights']").doesNotExist()) + .andExpect(jsonPath("$.metadata['dc.rights.uri']").doesNotExist()) + .andExpect(jsonPath("$.metadata['dc.rights.label']").doesNotExist()); + Assert.assertTrue(clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()) + .isEmpty()); + } + + /** + * The license PUT needs ADD on the item: 403 for a plain user, 401 for an anonymous call. + */ + @Test + public void updateLicenseForBundleNeedsAddRightsOnItem() throws Exception { + 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()) { From ebe58bb5762777ef6abad2a5c8a31d990862013e Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 23:16:28 +0100 Subject: [PATCH 2/3] Check -c, the stored metadata and every ORIGINAL file in the new ITs The test without -o now checks the language from -c. The licence PUT test uses two files and reads the item metadata with a GET after each PUT. Co-Authored-By: Claude Opus 5.5 --- .../administer/CreateAdministratorIT.java | 5 ++- .../rest/ClarinLicenseRestRepositoryIT.java | 39 ++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java b/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java index b521db333f2a..1cc470e18e9a 100644 --- a/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java +++ b/dspace-api/src/test/java/org/dspace/administer/CreateAdministratorIT.java @@ -71,15 +71,16 @@ public void organizationOptionIsStoredInUserRegistration() throws Exception { } /** - * Without -o the registration gets the unknown organization. + * 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", "en", + 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()); 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 ac7d7a31cbf9..d27037598181 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 @@ -518,34 +518,47 @@ 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(); - Bitstream bitstream = BitstreamBuilder.createBitstream(context, item, toInputStream("test", UTF_8)) - .withName("file.txt") - .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 bundlesUri = "/api/core/items/" + item.getID() + "/bundles"; + String itemUri = "/api/core/items/" + item.getID(); // attach the license to every bitstream of the ORIGINAL bundle and to the item metadata - getClient(tokenAdmin).perform(put(bundlesUri).param("licenseID", String.valueOf(firstCLicense.getID()))) + getClient(tokenAdmin).perform(put(itemUri + "/bundles") + .param("licenseID", String.valueOf(firstCLicense.getID()))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(item.getID().toString()))); + getClient(tokenAdmin).perform(get(itemUri)) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(item.getID().toString()))) .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()))); - List mappings = - clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()); - Assert.assertEquals(1, mappings.size()); - Assert.assertEquals(firstCLicense.getID(), mappings.get(0).getLicense().getID()); + 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(bundlesUri).param("licenseID", "-1")) + 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()); - Assert.assertTrue(clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()) - .isEmpty()); + for (Bitstream bitstream : bitstreams) { + Assert.assertTrue(clarinLicenseResourceMappingService.findByBitstreamUUID(context, bitstream.getID()) + .isEmpty()); + } } /** From ecda5e679d259e2da3f8a8cabd8ba34d04d71a7c Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 23:26:08 +0100 Subject: [PATCH 3/3] Check the ADD rule and licence replacement in the bundle licence IT A user with only WRITE now gets 403, and a valid licence must replace the one already on the files. The PUT response is checked to be an item. Co-Authored-By: Claude Opus 5.5 --- .../rest/ClarinLicenseRestRepositoryIT.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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 d27037598181..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,6 +43,7 @@ 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; @@ -53,6 +54,7 @@ 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; @@ -510,8 +512,8 @@ public void findAllBitstreamsAttachedToLicense() throws Exception { } /** - * PUT /api/core/items/{uuid}/bundles?licenseID= moves every ORIGINAL bitstream to the license and writes - * the dc.rights metadata of the item; an unknown id (-1) only detaches. + * 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 { @@ -529,10 +531,14 @@ public void updateLicenseForBundleAttachesAndDetachesLicense() throws Exception String tokenAdmin = getAuthToken(admin.getEmail(), password); String itemUri = "/api/core/items/" + item.getID(); - // attach the license to every bitstream of the ORIGINAL bundle and to the item metadata + // 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()) @@ -562,10 +568,16 @@ public void updateLicenseForBundleAttachesAndDetachesLicense() throws Exception } /** - * The license PUT needs ADD on the item: 403 for a plain user, 401 for an anonymous call. + * 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())))