@@ -237,7 +237,40 @@ func TestAPIRenameBranch(t *testing.T) {
237237 MakeRequest (t , req , http .StatusCreated )
238238
239239 resp := testAPIRenameBranch (t , "user2" , "user2" , "repo1" , from , "new-branch-name" , http .StatusForbidden )
240- assert .Contains (t , resp .Body .String (), "Branch is protected by glob-based protection rules." )
240+ assert .Contains (t , resp .Body .String (), "Failed to rename branch due to branch protection rules." )
241+ })
242+ t .Run ("RenameBranchToMatchProtectionRulesWithAllowedUser" , func (t * testing.T ) {
243+ // allow an admin (the owner in this case) to rename a regular branch to one that matches a branch protection rule
244+ repoName := "repo1"
245+ ownerName := "user2"
246+ from := "regular-branch-1"
247+ ctx := NewAPITestContext (t , ownerName , repoName , auth_model .AccessTokenScopeWriteRepository )
248+ testAPICreateBranch (t , ctx .Session , ownerName , repoName , "" , from , http .StatusCreated )
249+
250+ // NOTE: The protected/** branch protection rule was created in a previous test, with push enabled.
251+ testAPIRenameBranch (t , ownerName , ownerName , repoName , from , "protected/2" , http .StatusNoContent )
252+ })
253+ t .Run ("RenameBranchToMatchProtectionRulesWithUnauthorizedUser" , func (t * testing.T ) {
254+ // don't allow renaming a regular branch to a protected branch if the doer is not in the push whitelist
255+ repoName := "repo1"
256+ ownerName := "user2"
257+ pushWhitelist := []string {ownerName }
258+ token := getUserToken (t , "user2" , auth_model .AccessTokenScopeWriteRepository )
259+ req := NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/branch_protections" , ownerName , repoName ),
260+ & api.BranchProtection {
261+ RuleName : "owner-protected/**" ,
262+ PushWhitelistUsernames : pushWhitelist ,
263+ }).AddTokenAuth (token )
264+ MakeRequest (t , req , http .StatusCreated )
265+
266+ from := "regular-branch-2"
267+ ctx := NewAPITestContext (t , ownerName , repoName , auth_model .AccessTokenScopeWriteRepository )
268+ testAPICreateBranch (t , ctx .Session , ownerName , repoName , "" , from , http .StatusCreated )
269+
270+ unprivilegedUser := "user40"
271+ resp := testAPIRenameBranch (t , unprivilegedUser , ownerName , repoName , from , "owner-protected/1" , http .StatusForbidden )
272+
273+ assert .Contains (t , resp .Body .String (), "Failed to rename branch due to branch protection rules." )
241274 })
242275 t .Run ("RenameBranchNormalScenario" , func (t * testing.T ) {
243276 testAPIRenameBranch (t , "user2" , "user2" , "repo1" , "branch2" , "new-branch-name" , http .StatusNoContent )
0 commit comments