@@ -190,28 +190,43 @@ func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBran
190190func TestAPIUpdateBranch (t * testing.T ) {
191191 onGiteaRun (t , func (t * testing.T , _ * url.URL ) {
192192 t .Run ("UpdateBranchWithEmptyRepo" , func (t * testing.T ) {
193- testAPIUpdateBranch (t , "user10" , "repo6" , "master" , "test" , http .StatusNotFound )
193+ testAPIUpdateBranch (t , "user10" , "user10" , " repo6" , "master" , "test" , http .StatusNotFound )
194194 })
195195 t .Run ("UpdateBranchWithSameBranchNames" , func (t * testing.T ) {
196- resp := testAPIUpdateBranch (t , "user2" , "repo1" , "master" , "master" , http .StatusUnprocessableEntity )
196+ resp := testAPIUpdateBranch (t , "user2" , "user2" , " repo1" , "master" , "master" , http .StatusUnprocessableEntity )
197197 assert .Contains (t , resp .Body .String (), "Cannot rename a branch using the same name or rename to a branch that already exists." )
198198 })
199199 t .Run ("UpdateBranchThatAlreadyExists" , func (t * testing.T ) {
200- resp := testAPIUpdateBranch (t , "user2" , "repo1" , "master" , "branch2" , http .StatusUnprocessableEntity )
200+ resp := testAPIUpdateBranch (t , "user2" , "user2" , " repo1" , "master" , "branch2" , http .StatusUnprocessableEntity )
201201 assert .Contains (t , resp .Body .String (), "Cannot rename a branch using the same name or rename to a branch that already exists." )
202202 })
203203 t .Run ("UpdateBranchWithNonExistentBranch" , func (t * testing.T ) {
204- resp := testAPIUpdateBranch (t , "user2" , "repo1" , "i-dont-exist" , "new-branch-name" , http .StatusNotFound )
204+ resp := testAPIUpdateBranch (t , "user2" , "user2" , " repo1" , "i-dont-exist" , "new-branch-name" , http .StatusNotFound )
205205 assert .Contains (t , resp .Body .String (), "Branch doesn't exist." )
206206 })
207+ t .Run ("UpdateBranchWithNonAdminDoer" , func (t * testing.T ) {
208+ // don't allow default branch renaming
209+ resp := testAPIUpdateBranch (t , "user4" , "user2" , "repo1" , "master" , "new-branch-name" , http .StatusForbidden )
210+ assert .Contains (t , resp .Body .String (), "User must be a repo or site admin to rename default or protected branches." )
211+
212+ // don't allow protected branch renaming
213+ token := getUserToken (t , "user2" , auth_model .AccessTokenScopeWriteRepository )
214+ req := NewRequestWithJSON (t , "POST" , "/api/v1/repos/user2/repo1/branches" , & api.CreateBranchRepoOption {
215+ BranchName : "protected-branch" ,
216+ }).AddTokenAuth (token )
217+ MakeRequest (t , req , http .StatusCreated )
218+ testAPICreateBranchProtection (t , "protected-branch" , 1 , http .StatusCreated )
219+ resp = testAPIUpdateBranch (t , "user4" , "user2" , "repo1" , "protected-branch" , "new-branch-name" , http .StatusForbidden )
220+ assert .Contains (t , resp .Body .String (), "User must be a repo or site admin to rename default or protected branches." )
221+ })
207222 t .Run ("RenameBranchNormalScenario" , func (t * testing.T ) {
208- testAPIUpdateBranch (t , "user2" , "repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
223+ testAPIUpdateBranch (t , "user2" , "user2" , " repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
209224 })
210225 })
211226}
212227
213- func testAPIUpdateBranch (t * testing.T , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest.ResponseRecorder {
214- token := getUserToken (t , ownerName , auth_model .AccessTokenScopeWriteRepository )
228+ func testAPIUpdateBranch (t * testing.T , doerName , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest.ResponseRecorder {
229+ token := getUserToken (t , doerName , auth_model .AccessTokenScopeWriteRepository )
215230 req := NewRequestWithJSON (t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api.UpdateBranchRepoOption {
216231 Name : to ,
217232 }).AddTokenAuth (token )
0 commit comments