From 2c3ce88cf7847724ab60833d041490215dea5778 Mon Sep 17 00:00:00 2001 From: BartChris Date: Wed, 11 Dec 2024 18:03:01 +0100 Subject: [PATCH] Add tests for duplicate titles in projects the user does not belong to --- .../copyprocess/CreateProcessFormIT.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Kitodo/src/test/java/org/kitodo/production/forms/copyprocess/CreateProcessFormIT.java b/Kitodo/src/test/java/org/kitodo/production/forms/copyprocess/CreateProcessFormIT.java index 47a73108f80..5eb88d0c1af 100644 --- a/Kitodo/src/test/java/org/kitodo/production/forms/copyprocess/CreateProcessFormIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/forms/copyprocess/CreateProcessFormIT.java @@ -13,6 +13,7 @@ import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -34,6 +35,7 @@ import org.kitodo.config.ConfigCore; import org.kitodo.config.enums.ParameterCore; import org.kitodo.data.database.beans.Process; +import org.kitodo.data.database.beans.Project; import org.kitodo.data.database.beans.User; import org.kitodo.exceptions.ProcessGenerationException; import org.kitodo.production.forms.createprocess.CreateProcessForm; @@ -163,11 +165,22 @@ public void shouldThrowExceptionForInvalidTitle() throws Exception { assertEquals(before, after, "A process with an invalid title was created!"); } + @Test - public void shouldNotAllowDuplicateTitles() throws Exception { + public void shouldNotAllowDuplicateProcessTitles() throws Exception { + assertDuplicateTitleNotAllowed(1, false); + } + + @Test + public void shouldNotAllowProcessTitlesInProjectsTheUserDoesNotBelongTo() throws Exception { + assertDuplicateTitleNotAllowed(2, true); + } + + private void assertDuplicateTitleNotAllowed(int projectId, boolean switchUserContext) throws Exception { // First process creation CreateProcessForm underTest = setupCreateProcessForm("Monograph"); underTest.getMainProcess().setTitle("title"); + underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(projectId)); setScriptPermissions(true); long before = processService.count(); @@ -176,9 +189,18 @@ public void shouldNotAllowDuplicateTitles() throws Exception { long after = processService.count(); assertEquals(before + 1, after, "First process creation failed. No process was created!"); + + // Switch user context to check with a user which does not has access to project 2 + if (switchUserContext) { + User userTwo = ServiceManager.getUserService().getById(2); + SecurityTestUtils.addUserDataToSecurityContext(userTwo, 1); + } + // Second process creation with duplicate title CreateProcessForm underTestTwo = setupCreateProcessForm("Monograph"); underTestTwo.getMainProcess().setTitle("title"); + underTestTwo.getMainProcess().setProject(ServiceManager.getProjectService().getById(projectId)); + long beforeDuplicate = processService.count(); assertThrows(ProcessGenerationException.class, underTestTwo::createProcessHierarchy, "Expected a ProcessGenerationException to be thrown for duplicate title, but it was not.");