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.

129 lines
4.4 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. "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. defer prepareTestEnv(t)()
  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. 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. }