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.

195 lines
6.3 KiB

  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. token := getTokenForLoggedInUser(t, session)
  137. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/my-noo-repo/branches?token="+token, &api.CreateBranchRepoOption{
  138. BranchName: test.NewBranch,
  139. OldBranchName: test.OldBranch,
  140. })
  141. resp := session.MakeRequest(t, req, test.ExpectedHTTPStatus)
  142. var branch api.Branch
  143. DecodeJSON(t, resp, &branch)
  144. if test.ExpectedHTTPStatus == http.StatusCreated {
  145. assert.EqualValues(t, test.NewBranch, branch.Name)
  146. }
  147. }
  148. }
  149. func TestAPIBranchProtection(t *testing.T) {
  150. defer prepareTestEnv(t)()
  151. // Branch protection only on branch that exist
  152. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusNotFound)
  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. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  165. // Test branch deletion
  166. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  167. testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
  168. }