diff --git a/javatests/com/google/gerrit/acceptance/rest/project/CreateBranchIT.java b/javatests/com/google/gerrit/acceptance/rest/project/CreateBranchIT.java index 15418505cd..311c264c58 100644 --- a/javatests/com/google/gerrit/acceptance/rest/project/CreateBranchIT.java +++ b/javatests/com/google/gerrit/acceptance/rest/project/CreateBranchIT.java @@ -45,6 +45,7 @@ import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.server.events.RefReceivedEvent; import com.google.gerrit.server.git.validators.RefOperationValidationListener; import com.google.gerrit.server.git.validators.ValidationMessage; +import com.google.gerrit.server.util.MagicBranch; import com.google.gerrit.server.validators.ValidationException; import com.google.inject.Inject; import java.io.IOException; @@ -140,6 +141,20 @@ public class CreateBranchIT extends AbstractDaemonTest { } } + @Test + public void conflictingBranchAlreadyExists_Conflict() throws Exception { + assertCreateSucceeds(testBranch); + BranchNameKey testBranch2 = BranchNameKey.create(project, testBranch.branch() + "/foo/bar"); + assertCreateFails( + testBranch2, + ResourceConflictException.class, + "Cannot create branch \"" + + testBranch2.branch() + + "\" since it conflicts with branch \"" + + testBranch.branch() + + "\""); + } + @Test public void createBranchByProjectOwner() throws Exception { grantOwner(); @@ -313,6 +328,22 @@ public class CreateBranchIT extends AbstractDaemonTest { "invalid revision \"invalid\trevision\""); } + @Test + public void cannotCreateBranchInMagicBranchNamespace() throws Exception { + assertCreateFails( + BranchNameKey.create(project, MagicBranch.NEW_CHANGE + "foo"), + BadRequestException.class, + "not allowed to create branches under \"" + MagicBranch.NEW_CHANGE + "\""); + } + + @Test + public void cannotCreateBranchWithInvalidName() throws Exception { + assertCreateFails( + BranchNameKey.create(project, RefNames.REFS_HEADS), + BadRequestException.class, + "invalid branch name \"" + RefNames.REFS_HEADS + "\""); + } + @Test public void createBranchLeadingSlashesAreRemoved() throws Exception { BranchNameKey expectedNameKey = BranchNameKey.create(project, "test"); @@ -334,6 +365,17 @@ public class CreateBranchIT extends AbstractDaemonTest { .isEqualTo(expectedNameKey.branch()); } + @Test + public void branchNameInInputMustMatchBranchNameInUrl() throws Exception { + BranchInput branchInput = new BranchInput(); + branchInput.ref = "foo"; + BadRequestException ex = + assertThrows( + BadRequestException.class, + () -> gApi.projects().name(project.get()).branch("bar").create(branchInput)); + assertThat(ex).hasMessageThat().isEqualTo("ref must match URL"); + } + private void blockCreateReference() throws Exception { projectOperations .project(project)