@@ -190,28 +190,61 @@ 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 ("RenameBranchNormalScenario" , func (t * testing.T ) {
208- testAPIUpdateBranch (t , "user2" , "repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
207+ t .Run ("UpdateBranchWithNonAdminDoer" , func (t * testing.T ) {
208+ // don't allow default branch renaming
209+ resp := testAPIUpdateBranch (t , "user40" , "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 , "user40" , "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+ })
222+ t .Run ("UpdateBranchWithGlobedBasedProtectionRulesAndAdminAccess" , func (t * testing.T ) {
223+ // don't allow branch that falls under glob-based protection rules to be renamed
224+ token := getUserToken (t , "user2" , auth_model .AccessTokenScopeWriteRepository )
225+ req := NewRequestWithJSON (t , "POST" , "/api/v1/repos/user2/repo1/branch_protections" , & api.BranchProtection {
226+ RuleName : "protected/**" ,
227+ EnablePush : true ,
228+ }).AddTokenAuth (token )
229+ MakeRequest (t , req , http .StatusCreated )
230+
231+ from := "protected/1"
232+ req = NewRequestWithJSON (t , "POST" , "/api/v1/repos/user2/repo1/branches" , & api.CreateBranchRepoOption {
233+ BranchName : from ,
234+ }).AddTokenAuth (token )
235+ MakeRequest (t , req , http .StatusCreated )
236+
237+ resp := testAPIUpdateBranch (t , "user2" , "user2" , "repo1" , from , "new-branch-name" , http .StatusForbidden )
238+ assert .Contains (t , resp .Body .String (), "Branch is protected by glob-based protection rules." )
239+ })
240+ t .Run ("UpdateBranchNormalScenario" , func (t * testing.T ) {
241+ testAPIUpdateBranch (t , "user2" , "user2" , "repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
209242 })
210243 })
211244}
212245
213- func testAPIUpdateBranch (t * testing.T , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest.ResponseRecorder {
214- token := getUserToken (t , ownerName , auth_model .AccessTokenScopeWriteRepository )
246+ func testAPIUpdateBranch (t * testing.T , doerName , ownerName , repoName , from , to string , expectedHTTPStatus int ) * httptest.ResponseRecorder {
247+ token := getUserToken (t , doerName , auth_model .AccessTokenScopeWriteRepository )
215248 req := NewRequestWithJSON (t , "PATCH" , "api/v1/repos/" + ownerName + "/" + repoName + "/branches/" + from , & api.UpdateBranchRepoOption {
216249 Name : to ,
217250 }).AddTokenAuth (token )
0 commit comments