-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent duplicate and invalid titles #6326
base: main
Are you sure you want to change the base?
Changes from 9 commits
4d9c5fc
eeff580
75d4aa0
849dadc
667867d
5d6a7d4
d463f2c
b843df8
9bd8055
2e960a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
|
||
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; | ||
|
||
import java.io.File; | ||
|
@@ -23,6 +25,7 @@ | |
|
||
import org.apache.commons.lang3.SystemUtils; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.kitodo.ExecutionPermission; | ||
|
@@ -32,7 +35,9 @@ | |
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; | ||
import org.kitodo.production.helper.TempProcess; | ||
import org.kitodo.production.services.ServiceManager; | ||
|
@@ -48,6 +53,7 @@ public class CreateProcessFormIT { | |
private static final ProcessService processService = ServiceManager.getProcessService(); | ||
|
||
private static final String firstProcess = "First process"; | ||
private Process createdProcess; | ||
|
||
/** | ||
* Is running before the class runs. | ||
|
@@ -75,65 +81,138 @@ public static void cleanDatabase() throws Exception { | |
MockDatabase.cleanDatabase(); | ||
} | ||
|
||
@Test | ||
public void shouldCreateNewProcess() throws Exception { | ||
CreateProcessForm underTest = new CreateProcessForm(); | ||
underTest.getProcessDataTab().setDocType("Monograph"); | ||
Process newProcess = new Process(); | ||
Workpiece newWorkPiece = new Workpiece(); | ||
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece); | ||
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess))); | ||
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1)); | ||
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1)); | ||
underTest.getMainProcess().setTitle("title"); | ||
@AfterEach | ||
public void cleanUpAfterEach() throws Exception { | ||
if (createdProcess != null && createdProcess.getId() != null) { | ||
processService.remove(createdProcess.getId()); | ||
fileService.delete(URI.create(createdProcess.getId().toString())); | ||
} | ||
createdProcess = null; | ||
setScriptPermissions(false); | ||
} | ||
|
||
// Helper to create and initialize a CreateProcessForm with common properties | ||
private CreateProcessForm setupCreateProcessForm(String docType) throws Exception { | ||
CreateProcessForm form = new CreateProcessForm(); | ||
form.getProcessDataTab().setDocType(docType); | ||
|
||
Process process = new Process(); | ||
Workpiece workPiece = new Workpiece(); | ||
TempProcess tempProcess = new TempProcess(process, workPiece); | ||
form.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess))); | ||
|
||
form.getMainProcess().setProject(ServiceManager.getProjectService().getById(1)); | ||
form.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1)); | ||
|
||
form.updateRulesetAndDocType(tempProcess.getProcess().getRuleset()); | ||
return form; | ||
} | ||
|
||
// Helper to manage script permissions | ||
private void setScriptPermissions(boolean enable) throws Exception { | ||
File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META)); | ||
if (!SystemUtils.IS_OS_WINDOWS) { | ||
ExecutionPermission.setExecutePermission(script); | ||
if (enable) { | ||
ExecutionPermission.setExecutePermission(script); | ||
} else { | ||
ExecutionPermission.setNoExecutePermission(script); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldCreateNewProcess() throws Exception { | ||
CreateProcessForm underTest = setupCreateProcessForm("Monograph"); | ||
underTest.getMainProcess().setTitle("title"); | ||
setScriptPermissions(true); | ||
long before = processService.count(); | ||
underTest.createNewProcess(); | ||
if (!SystemUtils.IS_OS_WINDOWS) { | ||
ExecutionPermission.setNoExecutePermission(script); | ||
} | ||
setScriptPermissions(false); | ||
|
||
long after = processService.count(); | ||
assertEquals(before + 1, after, "No process was created!"); | ||
|
||
// clean up database, index and file system | ||
Integer processId = newProcess.getId(); | ||
processService.remove(processId); | ||
fileService.delete(URI.create(processId.toString())); | ||
createdProcess = underTest.getMainProcess(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The determination of the created process could be moved before the first assertion is executed so you have anytime the right process information before cleaning up. If adjusted here it should be adjusted on the other places too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, good idea. I did that. |
||
} | ||
|
||
/** | ||
* tests creation of processes without workflow. | ||
*/ | ||
@Test | ||
public void shouldCreateNewProcessWithoutWorkflow() throws Exception { | ||
CreateProcessForm underTest = new CreateProcessForm(); | ||
underTest.getProcessDataTab().setDocType("MultiVolumeWork"); | ||
Process newProcess = new Process(); | ||
Workpiece newWorkPiece = new Workpiece(); | ||
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece); | ||
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess))); | ||
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1)); | ||
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1)); | ||
CreateProcessForm underTest = setupCreateProcessForm("MultiVolumeWork"); | ||
underTest.getMainProcess().setTitle("title"); | ||
|
||
File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META)); | ||
ExecutionPermission.setExecutePermission(script); | ||
setScriptPermissions(true); | ||
long before = processService.count(); | ||
underTest.createNewProcess(); | ||
ExecutionPermission.setNoExecutePermission(script); | ||
setScriptPermissions(false); | ||
|
||
long after = processService.count(); | ||
assertEquals(before + 1, after, "No process was created!"); | ||
assertTrue(underTest.getMainProcess().getTasks().isEmpty(), "Process should not have tasks"); | ||
assertNull(underTest.getMainProcess().getSortHelperStatus(), "Process should not have sortHelperStatus"); | ||
|
||
assertTrue(newProcess.getTasks().isEmpty(), "Process should not have tasks"); | ||
assertNull(newProcess.getSortHelperStatus(), "process should not have sortHelperStatus"); | ||
createdProcess = underTest.getMainProcess(); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionForInvalidTitle() throws Exception { | ||
// Attempt to create a process with an invalid title | ||
CreateProcessForm underTest = setupCreateProcessForm("Monograph"); | ||
underTest.getMainProcess().setTitle("title with whitespaces"); | ||
long before = processService.count(); | ||
assertThrows(ProcessGenerationException.class, underTest::createProcessHierarchy, | ||
"Expected a ProcessGenerationException to be thrown for an invalid title, but it was not."); | ||
long after = processService.count(); | ||
// Ensure no process was created | ||
assertEquals(before, after, "A process with an invalid title was created!"); | ||
} | ||
|
||
// clean up database, index and file system | ||
Integer processId = newProcess.getId(); | ||
processService.remove(processId); | ||
fileService.delete(URI.create(processId.toString())); | ||
|
||
@Test | ||
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(); | ||
underTest.createProcessHierarchy(); | ||
setScriptPermissions(false); | ||
|
||
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); | ||
// Assert that the user 2 is NOT associated with project 2 | ||
assertFalse(ServiceManager.getProjectService() | ||
.getById(projectId) | ||
.getUsers() | ||
.contains(userTwo), "User should not have access to projectOne"); | ||
} | ||
|
||
// 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."); | ||
long afterDuplicate = processService.count(); | ||
assertEquals(beforeDuplicate, afterDuplicate, "A duplicate process with the same title was created!"); | ||
|
||
createdProcess = underTest.getMainProcess(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the other
kitodo_config.properties
files with this entry be adjusted too?