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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "testing"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
  12. session := loginUser(t, "user2")
  13. token := getTokenForLoggedInUser(t, session)
  14. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  15. resp := session.MakeRequest(t, req, NoExpectedStatus)
  16. if !exists {
  17. assert.EqualValues(t, http.StatusNotFound, resp.Code)
  18. return
  19. }
  20. assert.EqualValues(t, http.StatusOK, resp.Code)
  21. var branch api.Branch
  22. DecodeJSON(t, resp, &branch)
  23. assert.EqualValues(t, branchName, branch.Name)
  24. assert.True(t, branch.UserCanPush)
  25. assert.True(t, branch.UserCanMerge)
  26. }
  27. func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  28. session := loginUser(t, "user2")
  29. token := getTokenForLoggedInUser(t, session)
  30. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  31. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  32. if resp.Code == 200 {
  33. var branchProtection api.BranchProtection
  34. DecodeJSON(t, resp, &branchProtection)
  35. assert.EqualValues(t, branchName, branchProtection.BranchName)
  36. }
  37. }
  38. func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  39. session := loginUser(t, "user2")
  40. token := getTokenForLoggedInUser(t, session)
  41. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections?token="+token, &api.BranchProtection{
  42. BranchName: branchName,
  43. })
  44. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  45. if resp.Code == 201 {
  46. var branchProtection api.BranchProtection
  47. DecodeJSON(t, resp, &branchProtection)
  48. assert.EqualValues(t, branchName, branchProtection.BranchName)
  49. }
  50. }
  51. func testAPIEditBranchProtection(t *testing.T, branchName string, body *api.BranchProtection, expectedHTTPStatus int) {
  52. session := loginUser(t, "user2")
  53. token := getTokenForLoggedInUser(t, session)
  54. req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/branch_protections/"+branchName+"?token="+token, body)
  55. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  56. if resp.Code == 200 {
  57. var branchProtection api.BranchProtection
  58. DecodeJSON(t, resp, &branchProtection)
  59. assert.EqualValues(t, branchName, branchProtection.BranchName)
  60. }
  61. }
  62. func testAPIDeleteBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  63. session := loginUser(t, "user2")
  64. token := getTokenForLoggedInUser(t, session)
  65. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  66. session.MakeRequest(t, req, expectedHTTPStatus)
  67. }
  68. func testAPIDeleteBranch(t *testing.T, branchName string, expectedHTTPStatus int) {
  69. session := loginUser(t, "user2")
  70. token := getTokenForLoggedInUser(t, session)
  71. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
  72. session.MakeRequest(t, req, expectedHTTPStatus)
  73. }
  74. func TestAPIGetBranch(t *testing.T) {
  75. defer prepareTestEnv(t)()
  76. for _, test := range []struct {
  77. BranchName string
  78. Exists bool
  79. }{
  80. {"master", true},
  81. {"master/doesnotexist", false},
  82. {"feature/1", true},
  83. {"feature/1/doesnotexist", false},
  84. } {
  85. testAPIGetBranch(t, test.BranchName, test.Exists)
  86. }
  87. }
  88. func TestAPIBranchProtection(t *testing.T) {
  89. defer prepareTestEnv(t)()
  90. // Branch protection only on branch that exist
  91. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusNotFound)
  92. // Get branch protection on branch that exist but not branch protection
  93. testAPIGetBranchProtection(t, "master", http.StatusNotFound)
  94. testAPICreateBranchProtection(t, "master", http.StatusCreated)
  95. // Can only create once
  96. testAPICreateBranchProtection(t, "master", http.StatusForbidden)
  97. // Can't delete a protected branch
  98. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  99. testAPIGetBranchProtection(t, "master", http.StatusOK)
  100. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  101. EnablePush: true,
  102. }, http.StatusOK)
  103. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  104. // Test branch deletion
  105. testAPIDeleteBranch(t, "master", http.StatusForbidden)
  106. testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
  107. }