Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

api_branch_test.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 TestAPIGetBranch(t *testing.T) {
  70. for _, test := range []struct {
  71. BranchName string
  72. Exists bool
  73. }{
  74. {"master", true},
  75. {"master/doesnotexist", false},
  76. {"feature/1", true},
  77. {"feature/1/doesnotexist", false},
  78. } {
  79. testAPIGetBranch(t, test.BranchName, test.Exists)
  80. }
  81. }
  82. func TestAPIBranchProtection(t *testing.T) {
  83. defer prepareTestEnv(t)()
  84. // Branch protection only on branch that exist
  85. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusNotFound)
  86. // Get branch protection on branch that exist but not branch protection
  87. testAPIGetBranchProtection(t, "master", http.StatusNotFound)
  88. testAPICreateBranchProtection(t, "master", http.StatusCreated)
  89. // Can only create once
  90. testAPICreateBranchProtection(t, "master", http.StatusForbidden)
  91. testAPIGetBranchProtection(t, "master", http.StatusOK)
  92. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  93. EnablePush: true,
  94. }, http.StatusOK)
  95. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  96. }