#9668: Ensure proper handling of non-subpath URLs in link tokens

(cherry picked from commit 6eb3271fa3)
This commit is contained in:
Kim Shepherd
2024-07-10 16:04:34 +02:00
committed by github-actions[bot]
parent 083f7b45e0
commit 6f1bc3bb6a
2 changed files with 21 additions and 4 deletions

View File

@@ -297,7 +297,8 @@ public class RequestItemRepository
final String base = configurationService.getProperty("dspace.ui.url"); final String base = configurationService.getProperty("dspace.ui.url");
URIBuilder uriBuilder = new URIBuilder(base); URIBuilder uriBuilder = new URIBuilder(base);
// Add request-a-copy/token to the existing path (without breaking /sub/dir dspace URLs) // Add request-a-copy/token to the existing path (without breaking /sub/dir dspace URLs)
URI uri = uriBuilder.setPath(StringUtils.stripEnd(uriBuilder.getPath(), "") URI uri = uriBuilder.setPath(
(uriBuilder.getPath() == null ? "" : StringUtils.stripEnd(uriBuilder.getPath(), ""))
+ "/request-a-copy/" + token).build(); + "/request-a-copy/" + token).build();
return uri.toURL().toExternalForm(); return uri.toURL().toExternalForm();
} }

View File

@@ -606,10 +606,10 @@ public class RequestItemRepositoryIT
} }
/** /**
* Test that generated links include the correct base URL, even if it has extra URL segments * Test that generated links include the correct base URL, where the UI URL has a subpath like /subdir
*/ */
@Test @Test
public void testGetLinkTokenEmail() throws MalformedURLException, URISyntaxException { public void testGetLinkTokenEmailWithSubPath() throws MalformedURLException, URISyntaxException {
RequestItemRepository instance = applicationContext.getBean( RequestItemRepository instance = applicationContext.getBean(
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME, RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
RequestItemRepository.class); RequestItemRepository.class);
@@ -623,4 +623,20 @@ public class RequestItemRepositoryIT
assertEquals(expectedUrl, generatedLink); assertEquals(expectedUrl, generatedLink);
configurationService.reloadConfig(); configurationService.reloadConfig();
} }
/**
* Test that generated links include the correct base URL, with NO subpath elements
*/
@Test
public void testGetLinkTokenEmailWithoutSubPath() throws MalformedURLException, URISyntaxException {
RequestItemRepository instance = applicationContext.getBean(
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
RequestItemRepository.class);
String currentDspaceUrl = configurationService.getProperty("dspace.ui.url");
String expectedUrl = currentDspaceUrl + "/request-a-copy/token";
String generatedLink = instance.getLinkTokenEmail("token");
// The URLs should match
assertEquals(expectedUrl, generatedLink);
configurationService.reloadConfig();
}
} }