@@ -5,6 +5,7 @@ package integration
55
66import (
77 "net/http"
8+ "net/http/httptest"
89 "net/url"
910 "testing"
1011
@@ -186,6 +187,37 @@ func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBran
186187 return resp .Result ().StatusCode == status
187188}
188189
190+ func TestAPIUpdateBranch (t * testing.T ) {
191+ onGiteaRun (t , func (t * testing.T , _ * url.URL ) {
192+ t .Run ("UpdateBranchWithEmptyRepo" , func (t * testing.T ) {
193+ testAPIUpdateBranch (t , "user10" , "repo6" , "master" , "test" , http .StatusNotFound )
194+ })
195+ t .Run ("UpdateBranchWithSameBranchNames" , func (t * testing.T ) {
196+ resp := testAPIUpdateBranch (t , "user2" , "repo1" , "master" , "master" , http .StatusUnprocessableEntity )
197+ assert .Contains (t , resp .Body .String (), "Cannot rename a branch using the same name or rename to a branch that already exists." )
198+ })
199+ t .Run ("UpdateBranchThatAlreadyExists" , func (t * testing.T ) {
200+ resp := testAPIUpdateBranch (t , "user2" , "repo1" , "master" , "branch2" , http .StatusUnprocessableEntity )
201+ assert .Contains (t , resp .Body .String (), "Cannot rename a branch using the same name or rename to a branch that already exists." )
202+ })
203+ t .Run ("UpdateBranchWithNonExistentBranch" , func (t * testing.T ) {
204+ resp := testAPIUpdateBranch (t , "user2" , "repo1" , "i-dont-exist" , "new-branch-name" , http .StatusNotFound )
205+ assert .Contains (t , resp .Body .String (), "Branch doesn't exist." )
206+ })
207+ t .Run ("RenameBranchNormalScenario" , func (t * testing.T ) {
208+ testAPIUpdateBranch (t , "user2" , "repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
209+ })
210+ })
211+ }
212+
213+ func testAPIUpdateBranch (t * testing.T , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest.ResponseRecorder {
214+ token := getUserToken (t , ownerName , auth_model .AccessTokenScopeWriteRepository )
215+ req := NewRequestWithJSON (t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api.UpdateBranchRepoOption {
216+ Name : to ,
217+ }).AddTokenAuth (token )
218+ return MakeRequest (t , req , expectedHTTPStatus )
219+ }
220+
189221func TestAPIBranchProtection (t * testing.T ) {
190222 defer tests .PrepareTestEnv (t )()
191223
0 commit comments