提交 3661cefa 编写于 作者: A A. Jard 提交者: Oleg Nenashev

[JENKINS-58571] Check if name is editable on item before doing rename. (#4122)

* [58571] Check if name is editable on item before doing rename.

* [JENKINS-58571]Form failure when rename item with not editable name.

* [JENKINS-58571] Change to IOException when isNameEditable is false.

* [JENKINS-58571] Validate test harness furure version.

* [JENKINS-58571] Validate test harness 2.55.
上级 ecdc79c4
......@@ -275,6 +275,11 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
*/
@Restricted(NoExternalUse.class)
public @Nonnull FormValidation doCheckNewName(@QueryParameter String newName) {
if (!isNameEditable()) {
return FormValidation.error("Trying to rename an item that does not support this operation.");
}
// TODO: Create an Item.RENAME permission to use here, see JENKINS-18649.
if (!hasPermission(Item.CONFIGURE)) {
if (parent instanceof AccessControlled) {
......@@ -351,6 +356,11 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
* you can use this method.
*/
protected void renameTo(final String newName) throws IOException {
if (!isNameEditable()) {
throw new IOException("Trying to rename an item that does not support this operation.");
}
// always synchronize from bigger objects first
final ItemGroup parent = getParent();
String oldName = this.name;
......
......@@ -5,10 +5,13 @@ package hudson.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Collection;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
/**
* @author kingfai
......@@ -91,4 +94,60 @@ public class AbstractItemTest {
assertEquals(displayName, i.getDisplayNameOrNull());
assertEquals(displayName, i.getDisplayName());
}
private class NameNotEditableItem extends AbstractItem {
protected NameNotEditableItem(ItemGroup parent, String name){
super(parent, name);
}
@Override
public Collection<? extends Job> getAllJobs() {
return null;
}
@Override
public boolean isNameEditable() {
return false; //so far it's the default value, but it's good to be explicit for test.
}
}
@Test
@Issue("JENKINS-58571")
public void renameMethodShouldThrowExceptionWhenNotIsNameEditable() {
//GIVEN
NameNotEditableItem item = new NameNotEditableItem(null,"NameNotEditableItem");
//WHEN
try {
item.renameTo("NewName");
fail("An item with isNameEditable false must throw exception when trying to rename it.");
} catch (IOException e) {
//THEN
assertEquals(e.getMessage(),"Trying to rename an item that does not support this operation.");
assertEquals("NameNotEditableItem",item.getName());
}
}
@Test
@Issue("JENKINS-58571")
public void doConfirmRenameMustThrowFormFailureWhenNotIsNameEditable() throws IOException {
//GIVEN
NameNotEditableItem item = new NameNotEditableItem(null,"NameNotEditableItem");
//WHEN
try {
item.doConfirmRename("MyNewName");
fail("An item with isNameEditable false must throw exception when trying to call doConfirmRename.");
} catch (Failure f) {
//THEN
assertEquals(f.getMessage(),"Trying to rename an item that does not support this operation.");
assertEquals("NameNotEditableItem",item.getName());
}
}
}
......@@ -64,7 +64,7 @@ THE SOFTWARE.
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jenkins-test-harness</artifactId>
<version>2.54</version>
<version>2.55</version>
<scope>test</scope>
<exclusions>
<exclusion>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册