@ -187,26 +187,26 @@ func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBran
return resp . Result ( ) . StatusCode == status
}
func TestAPI Updat eBranch( t * testing . T ) {
func TestAPI Renam eBranch( t * testing . T ) {
onGiteaRun ( t , func ( t * testing . T , _ * url . URL ) {
t . Run ( " Updat eBranchWithEmptyRepo", func ( t * testing . T ) {
testAPI Updat eBranch( t , "user10" , "user10" , "repo6" , "master" , "test" , http . StatusNotFound )
t . Run ( " Renam eBranchWithEmptyRepo", func ( t * testing . T ) {
testAPI Renam eBranch( t , "user10" , "user10" , "repo6" , "master" , "test" , http . StatusNotFound )
} )
t . Run ( " Updat eBranchWithSameBranchNames", func ( t * testing . T ) {
resp := testAPI Updat eBranch( t , "user2" , "user2" , "repo1" , "master" , "master" , http . StatusUnprocessableEntity )
t . Run ( " Renam eBranchWithSameBranchNames", func ( t * testing . T ) {
resp := testAPI Renam eBranch( t , "user2" , "user2" , "repo1" , "master" , "master" , http . StatusUnprocessableEntity )
assert . Contains ( t , resp . Body . String ( ) , "Cannot rename a branch using the same name or rename to a branch that already exists." )
} )
t . Run ( " Updat eBranchThatAlreadyExists", func ( t * testing . T ) {
resp := testAPI Updat eBranch( t , "user2" , "user2" , "repo1" , "master" , "branch2" , http . StatusUnprocessableEntity )
t . Run ( " Renam eBranchThatAlreadyExists", func ( t * testing . T ) {
resp := testAPI Renam eBranch( t , "user2" , "user2" , "repo1" , "master" , "branch2" , http . StatusUnprocessableEntity )
assert . Contains ( t , resp . Body . String ( ) , "Cannot rename a branch using the same name or rename to a branch that already exists." )
} )
t . Run ( " Updat eBranchWithNonExistentBranch", func ( t * testing . T ) {
resp := testAPI Updat eBranch( t , "user2" , "user2" , "repo1" , "i-dont-exist" , "new-branch-name" , http . StatusNotFound )
t . Run ( " Renam eBranchWithNonExistentBranch", func ( t * testing . T ) {
resp := testAPI Renam eBranch( t , "user2" , "user2" , "repo1" , "i-dont-exist" , "new-branch-name" , http . StatusNotFound )
assert . Contains ( t , resp . Body . String ( ) , "Branch doesn't exist." )
} )
t . Run ( " Updat eBranchWithNonAdminDoer", func ( t * testing . T ) {
t . Run ( " Renam eBranchWithNonAdminDoer", func ( t * testing . T ) {
// don't allow default branch renaming
resp := testAPI Updat eBranch( t , "user40" , "user2" , "repo1" , "master" , "new-branch-name" , http . StatusForbidden )
resp := testAPI Renam eBranch( t , "user40" , "user2" , "repo1" , "master" , "new-branch-name" , http . StatusForbidden )
assert . Contains ( t , resp . Body . String ( ) , "User must be a repo or site admin to rename default or protected branches." )
// don't allow protected branch renaming
@ -216,10 +216,10 @@ func TestAPIUpdateBranch(t *testing.T) {
} ) . AddTokenAuth ( token )
MakeRequest ( t , req , http . StatusCreated )
testAPICreateBranchProtection ( t , "protected-branch" , 1 , http . StatusCreated )
resp = testAPI Updat eBranch( t , "user40" , "user2" , "repo1" , "protected-branch" , "new-branch-name" , http . StatusForbidden )
resp = testAPI Renam eBranch( t , "user40" , "user2" , "repo1" , "protected-branch" , "new-branch-name" , http . StatusForbidden )
assert . Contains ( t , resp . Body . String ( ) , "User must be a repo or site admin to rename default or protected branches." )
} )
t . Run ( " Updat eBranchWithGlobedBasedProtectionRulesAndAdminAccess", func ( t * testing . T ) {
t . Run ( " Renam eBranchWithGlobedBasedProtectionRulesAndAdminAccess", func ( t * testing . T ) {
// don't allow branch that falls under glob-based protection rules to be renamed
token := getUserToken ( t , "user2" , auth_model . AccessTokenScopeWriteRepository )
req := NewRequestWithJSON ( t , "POST" , "/api/v1/repos/user2/repo1/branch_protections" , & api . BranchProtection {
@ -234,18 +234,18 @@ func TestAPIUpdateBranch(t *testing.T) {
} ) . AddTokenAuth ( token )
MakeRequest ( t , req , http . StatusCreated )
resp := testAPI Updat eBranch( t , "user2" , "user2" , "repo1" , from , "new-branch-name" , http . StatusForbidden )
resp := testAPI Renam eBranch( t , "user2" , "user2" , "repo1" , from , "new-branch-name" , http . StatusForbidden )
assert . Contains ( t , resp . Body . String ( ) , "Branch is protected by glob-based protection rules." )
} )
t . Run ( " Updat eBranchNormalScenario", func ( t * testing . T ) {
testAPI Updat eBranch( t , "user2" , "user2" , "repo1" , "branch2" , "new-branch-name" , http . StatusNoContent )
t . Run ( " Renam eBranchNormalScenario", func ( t * testing . T ) {
testAPI Renam eBranch( t , "user2" , "user2" , "repo1" , "branch2" , "new-branch-name" , http . StatusNoContent )
} )
} )
}
func testAPI Updat eBranch( t * testing . T , doerName , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest . ResponseRecorder {
func testAPI Renam eBranch( t * testing . T , doerName , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest . ResponseRecorder {
token := getUserToken ( t , doerName , auth_model . AccessTokenScopeWriteRepository )
req := NewRequestWithJSON ( t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api . Updat eBranchRepoOption{
req := NewRequestWithJSON ( t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api . Renam eBranchRepoOption{
Name : to ,
} ) . AddTokenAuth ( token )
return MakeRequest ( t , req , expectedHTTPStatus )