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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
  13. session := loginUser(t, "user2")
  14. token := getTokenForLoggedInUser(t, session)
  15. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  16. resp := session.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) {
  29. session := loginUser(t, "user2")
  30. token := getTokenForLoggedInUser(t, session)
  31. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  32. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  33. if resp.Code == 200 {
  34. var branchProtection api.BranchProtection
  35. DecodeJSON(t, resp, &branchProtection)
  36. assert.EqualValues(t, branchName, branchProtection.BranchName)
  37. }
  38. }
  39. func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  40. session := loginUser(t, "user2")
  41. token := getTokenForLoggedInUser(t, session)
  42. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections?token="+token, &api.BranchProtection{
  43. BranchName: branchName,
  44. })
  45. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  46. if resp.Code == 201 {
  47. var branchProtection api.BranchProtection
  48. DecodeJSON(t, resp, &branchProtection)
  49. assert.EqualValues(t, branchName, branchProtection.BranchName)
  50. }
  51. }
  52. func testAPIEditBranchProtection(t *testing.T, branchName string, body *api.BranchProtection, expectedHTTPStatus int) {
  53. session := loginUser(t, "user2")
  54. token := getTokenForLoggedInUser(t, session)
  55. req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/branch_protections/"+branchName+"?token="+token, body)
  56. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  57. if resp.Code == 200 {
  58. var branchProtection api.BranchProtection
  59. DecodeJSON(t, resp, &branchProtection)
  60. assert.EqualValues(t, branchName, branchProtection.BranchName)
  61. }
  62. }
  63. func testAPIDeleteBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  64. session := loginUser(t, "user2")
  65. token := getTokenForLoggedInUser(t, session)
  66. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  67. session.MakeRequest(t, req, expectedHTTPStatus)
  68. }
  69. func testAPIDeleteBranch(t *testing.T, branchName string, expectedHTTPStatus int) {
  70. session := loginUser(t, "user2")
  71. token := getTokenForLoggedInUser(t, session)
  72. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  73. session.MakeRequest(t, req, expectedHTTPStatus)
  74. }
  75. func TestAPIGetBranch(t *testing.T) {
  76. defer prepareTestEnv(t)()
  77. for _, test := range []struct {
  78. BranchName string
  79. Exists bool
  80. }{
  81. {"master", true},
  82. {"master/doesnotexist", false},
  83. {"feature/1", true},
  84. {"feature/1/doesnotexist", false},
  85. } {
  86. testAPIGetBranch(t, test.BranchName, test.Exists)
  87. }
  88. }
  89. func TestAPICreateBranch(t *testing.T) {
  90. onGiteaRun(t, testAPICreateBranches)
  91. }
  92. func testAPICreateBranches(t *testing.T, giteaURL *url.URL) {
  93. username := "user2"
  94. ctx := NewAPITestContext(t, username, "my-noo-repo")
  95. giteaURL.Path = ctx.GitPath()
  96. t.Run("CreateRepo", doAPICreateRepository(ctx, false))
  97. tests := []struct {
  98. OldBranch string
  99. NewBranch string
  100. ExpectedHTTPStatus int
  101. }{
  102. // Creating branch from default branch
  103. {
  104. OldBranch: "",
  105. NewBranch: "new_branch_from_default_branch",
  106. ExpectedHTTPStatus: http.StatusCreated,
  107. },
  108. // Creating branch from master
  109. {
  110. OldBranch: "master",
  111. NewBranch: "new_branch_from_master_1",
  112. ExpectedHTTPStatus: http.StatusCreated,
  113. },
  114. // Trying to create from master but already exists
  115. {
  116. OldBranch: "master",
  117. NewBranch: "new_branch_from_master_1",
  118. ExpectedHTTPStatus: http.StatusConflict,
  119. },
  120. // Trying to create from other branch (not default branch)
  121. {
  122. OldBranch: "new_branch_from_master_1",
  123. NewBranch: "branch_2",
  124. ExpectedHTTPStatus: http.StatusCreated,
  125. },
  126. // Trying to create from a branch which does not exist
  127. {
  128. OldBranch: "does_not_exist",
  129. NewBranch: "new_branch_from_non_existent",
  130. ExpectedHTTPStatus: http.StatusNotFound,
  131. },
  132. }
  133. for _, test := range tests {
  134. defer resetFixtures(t)
  135. session := ctx.Session
  136. testAPICreateBranch(t, session, "user2", "my-noo-repo", test.OldBranch, test.NewBranch, test.ExpectedHTTPStatus)
  137. }
  138. }
  139. func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBranch, newBranch string, status int) bool {
  140. token := getTokenForLoggedInUser(t, session)
  141. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/"+user+"/"+repo+"/branches?token="+token, &api.CreateBranchRepoOption{
  142. BranchName: newBranch,
  143. OldBranchName: oldBranch,
  144. })
  145. resp := session.MakeRequest(t, req, status)
  146. var branch api.Branch
  147. DecodeJSON(t, resp, &branch)
  148. if status == http.StatusCreated {
  149. assert.EqualValues(t, newBranch, branch.Name)
  150. }
  151. return resp.Result().StatusCode == status
  152. }
  153. func TestAPIBranchProtection(t *testing.T) {
  154. defer prepareTestEnv(t)()
  155. // Branch protection only on branch that exist
  156. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusNotFound)
  157. // Get branch protection on branch that exist but not branch protection
  158. testAPIGetBranchProtection(t, "master", http.StatusNotFound)
  159. testAPICreateBranchProtection(t, "master", http.StatusCreated)
  160. // Can only create once
  161. testAPICreateBranchProtection(t, "master", http.StatusForbidden)
  162. // Can't delete a protected branch
  163. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  164. testAPIGetBranchProtection(t, "master", http.StatusOK)
  165. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  166. EnablePush: true,
  167. }, http.StatusOK)
  168. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  169. // Test branch deletion
  170. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  171. testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
  172. }