You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_branch_test.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "net/url"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/tests"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
  14. token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository)
  15. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  16. resp := MakeRequest(t, req, NoExpectedStatus)
  17. if !exists {
  18. assert.EqualValues(t, http.StatusNotFound, resp.Code)
  19. return
  20. }
  21. assert.EqualValues(t, http.StatusOK, resp.Code)
  22. var branch api.Branch
  23. DecodeJSON(t, resp, &branch)
  24. assert.EqualValues(t, branchName, branch.Name)
  25. assert.True(t, branch.UserCanPush)
  26. assert.True(t, branch.UserCanMerge)
  27. }
  28. func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) *api.BranchProtection {
  29. token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository)
  30. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  31. resp := MakeRequest(t, req, expectedHTTPStatus)
  32. if resp.Code == http.StatusOK {
  33. var branchProtection api.BranchProtection
  34. DecodeJSON(t, resp, &branchProtection)
  35. assert.EqualValues(t, branchName, branchProtection.RuleName)
  36. return &branchProtection
  37. }
  38. return nil
  39. }
  40. func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  41. token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
  42. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections?token="+token, &api.BranchProtection{
  43. RuleName: branchName,
  44. })
  45. resp := MakeRequest(t, req, expectedHTTPStatus)
  46. if resp.Code == http.StatusCreated {
  47. var branchProtection api.BranchProtection
  48. DecodeJSON(t, resp, &branchProtection)
  49. assert.EqualValues(t, branchName, branchProtection.RuleName)
  50. }
  51. }
  52. func testAPIEditBranchProtection(t *testing.T, branchName string, body *api.BranchProtection, expectedHTTPStatus int) {
  53. token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
  54. req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/branch_protections/"+branchName+"?token="+token, body)
  55. resp := MakeRequest(t, req, expectedHTTPStatus)
  56. if resp.Code == http.StatusOK {
  57. var branchProtection api.BranchProtection
  58. DecodeJSON(t, resp, &branchProtection)
  59. assert.EqualValues(t, branchName, branchProtection.RuleName)
  60. }
  61. }
  62. func testAPIDeleteBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  63. token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
  64. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  65. MakeRequest(t, req, expectedHTTPStatus)
  66. }
  67. func testAPIDeleteBranch(t *testing.T, branchName string, expectedHTTPStatus int) {
  68. token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
  69. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  70. MakeRequest(t, req, expectedHTTPStatus)
  71. }
  72. func TestAPIGetBranch(t *testing.T) {
  73. defer tests.PrepareTestEnv(t)()
  74. for _, test := range []struct {
  75. BranchName string
  76. Exists bool
  77. }{
  78. {"master", true},
  79. {"master/doesnotexist", false},
  80. {"feature/1", true},
  81. {"feature/1/doesnotexist", false},
  82. } {
  83. testAPIGetBranch(t, test.BranchName, test.Exists)
  84. }
  85. }
  86. func TestAPICreateBranch(t *testing.T) {
  87. onGiteaRun(t, testAPICreateBranches)
  88. }
  89. func testAPICreateBranches(t *testing.T, giteaURL *url.URL) {
  90. username := "user2"
  91. ctx := NewAPITestContext(t, username, "my-noo-repo", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
  92. giteaURL.Path = ctx.GitPath()
  93. t.Run("CreateRepo", doAPICreateRepository(ctx, false))
  94. testCases := []struct {
  95. OldBranch string
  96. NewBranch string
  97. ExpectedHTTPStatus int
  98. }{
  99. // Creating branch from default branch
  100. {
  101. OldBranch: "",
  102. NewBranch: "new_branch_from_default_branch",
  103. ExpectedHTTPStatus: http.StatusCreated,
  104. },
  105. // Creating branch from master
  106. {
  107. OldBranch: "master",
  108. NewBranch: "new_branch_from_master_1",
  109. ExpectedHTTPStatus: http.StatusCreated,
  110. },
  111. // Trying to create from master but already exists
  112. {
  113. OldBranch: "master",
  114. NewBranch: "new_branch_from_master_1",
  115. ExpectedHTTPStatus: http.StatusConflict,
  116. },
  117. // Trying to create from other branch (not default branch)
  118. {
  119. OldBranch: "new_branch_from_master_1",
  120. NewBranch: "branch_2",
  121. ExpectedHTTPStatus: http.StatusCreated,
  122. },
  123. // Trying to create from a branch which does not exist
  124. {
  125. OldBranch: "does_not_exist",
  126. NewBranch: "new_branch_from_non_existent",
  127. ExpectedHTTPStatus: http.StatusNotFound,
  128. },
  129. }
  130. for _, test := range testCases {
  131. session := ctx.Session
  132. testAPICreateBranch(t, session, "user2", "my-noo-repo", test.OldBranch, test.NewBranch, test.ExpectedHTTPStatus)
  133. }
  134. }
  135. func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBranch, newBranch string, status int) bool {
  136. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  137. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/"+user+"/"+repo+"/branches?token="+token, &api.CreateBranchRepoOption{
  138. BranchName: newBranch,
  139. OldBranchName: oldBranch,
  140. })
  141. resp := MakeRequest(t, req, status)
  142. var branch api.Branch
  143. DecodeJSON(t, resp, &branch)
  144. if status == http.StatusCreated {
  145. assert.EqualValues(t, newBranch, branch.Name)
  146. }
  147. return resp.Result().StatusCode == status
  148. }
  149. func TestAPIBranchProtection(t *testing.T) {
  150. defer tests.PrepareTestEnv(t)()
  151. // Branch protection on branch that not exist
  152. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusCreated)
  153. // Get branch protection on branch that exist but not branch protection
  154. testAPIGetBranchProtection(t, "master", http.StatusNotFound)
  155. testAPICreateBranchProtection(t, "master", http.StatusCreated)
  156. // Can only create once
  157. testAPICreateBranchProtection(t, "master", http.StatusForbidden)
  158. // Can't delete a protected branch
  159. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  160. testAPIGetBranchProtection(t, "master", http.StatusOK)
  161. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  162. EnablePush: true,
  163. }, http.StatusOK)
  164. // enable status checks, require the "test1" check to pass
  165. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  166. EnableStatusCheck: true,
  167. StatusCheckContexts: []string{"test1"},
  168. }, http.StatusOK)
  169. bp := testAPIGetBranchProtection(t, "master", http.StatusOK)
  170. assert.Equal(t, true, bp.EnableStatusCheck)
  171. assert.Equal(t, []string{"test1"}, bp.StatusCheckContexts)
  172. // disable status checks, clear the list of required checks
  173. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  174. EnableStatusCheck: false,
  175. StatusCheckContexts: []string{},
  176. }, http.StatusOK)
  177. bp = testAPIGetBranchProtection(t, "master", http.StatusOK)
  178. assert.Equal(t, false, bp.EnableStatusCheck)
  179. assert.Equal(t, []string{}, bp.StatusCheckContexts)
  180. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  181. // Test branch deletion
  182. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  183. testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
  184. }