@ -5,6 +5,7 @@ package integration
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
@ -186,6 +187,37 @@ func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBran
return resp . Result ( ) . StatusCode == status
}
func TestAPIUpdateBranch ( t * testing . T ) {
onGiteaRun ( t , func ( t * testing . T , _ * url . URL ) {
t . Run ( "UpdateBranchWithEmptyRepo" , func ( t * testing . T ) {
testAPIUpdateBranch ( t , "user10" , "repo6" , "master" , "test" , http . StatusNotFound )
} )
t . Run ( "UpdateBranchWithSameBranchNames" , func ( t * testing . T ) {
resp := testAPIUpdateBranch ( t , "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 ( "UpdateBranchThatAlreadyExists" , func ( t * testing . T ) {
resp := testAPIUpdateBranch ( t , "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 ( "UpdateBranchWithNonExistentBranch" , func ( t * testing . T ) {
resp := testAPIUpdateBranch ( t , "user2" , "repo1" , "i-dont-exist" , "new-branch-name" , http . StatusNotFound )
assert . Contains ( t , resp . Body . String ( ) , "Branch doesn't exist." )
} )
t . Run ( "RenameBranchNormalScenario" , func ( t * testing . T ) {
testAPIUpdateBranch ( t , "user2" , "repo1" , "branch2" , "new-branch-name" , http . StatusNoContent )
} )
} )
}
func testAPIUpdateBranch ( t * testing . T , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest . ResponseRecorder {
token := getUserToken ( t , ownerName , auth_model . AccessTokenScopeWriteRepository )
req := NewRequestWithJSON ( t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api . UpdateBranchRepoOption {
Name : to ,
} ) . AddTokenAuth ( token )
return MakeRequest ( t , req , expectedHTTPStatus )
}
func TestAPIBranchProtection ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )