Skip to content

Commit

Permalink
Add tests for duplicate titles in projects the user does not belong to
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Dec 11, 2024
1 parent d463f2c commit 2c3ce88
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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.");
Expand Down

0 comments on commit 2c3ce88

Please sign in to comment.