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

api_branch_test.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  26. func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  27. session := loginUser(t, "user2")
  28. token := getTokenForLoggedInUser(t, session)
  29. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  30. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  31. if resp.Code == 200 {
  32. var branchProtection api.BranchProtection
  33. DecodeJSON(t, resp, &branchProtection)
  34. assert.EqualValues(t, branchName, branchProtection.BranchName)
  35. }
  36. }
  37. func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  38. session := loginUser(t, "user2")
  39. token := getTokenForLoggedInUser(t, session)
  40. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections?token="+token, &api.BranchProtection{
  41. BranchName: branchName,
  42. })
  43. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  44. if resp.Code == 201 {
  45. var branchProtection api.BranchProtection
  46. DecodeJSON(t, resp, &branchProtection)
  47. assert.EqualValues(t, branchName, branchProtection.BranchName)
  48. }
  49. }
  50. func testAPIEditBranchProtection(t *testing.T, branchName string, body *api.BranchProtection, expectedHTTPStatus int) {
  51. session := loginUser(t, "user2")
  52. token := getTokenForLoggedInUser(t, session)
  53. req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/branch_protections/"+branchName+"?token="+token, body)
  54. resp := session.MakeRequest(t, req, expectedHTTPStatus)
  55. if resp.Code == 200 {
  56. var branchProtection api.BranchProtection
  57. DecodeJSON(t, resp, &branchProtection)
  58. assert.EqualValues(t, branchName, branchProtection.BranchName)
  59. }
  60. }
  61. func testAPIDeleteBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
  62. session := loginUser(t, "user2")
  63. token := getTokenForLoggedInUser(t, session)
  64. req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branch_protections/%s?token=%s", branchName, token)
  65. session.MakeRequest(t, req, expectedHTTPStatus)
  66. }
  67. func TestAPIGetBranch(t *testing.T) {
  68. for _, test := range []struct {
  69. BranchName string
  70. Exists bool
  71. }{
  72. {"master", true},
  73. {"master/doesnotexist", false},
  74. {"feature/1", true},
  75. {"feature/1/doesnotexist", false},
  76. } {
  77. testAPIGetBranch(t, test.BranchName, test.Exists)
  78. }
  79. }
  80. func TestAPIBranchProtection(t *testing.T) {
  81. defer prepareTestEnv(t)()
  82. // Branch protection only on branch that exist
  83. testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusNotFound)
  84. // Get branch protection on branch that exist but not branch protection
  85. testAPIGetBranchProtection(t, "master", http.StatusNotFound)
  86. testAPICreateBranchProtection(t, "master", http.StatusCreated)
  87. // Can only create once
  88. testAPICreateBranchProtection(t, "master", http.StatusForbidden)
  89. testAPIGetBranchProtection(t, "master", http.StatusOK)
  90. testAPIEditBranchProtection(t, "master", &api.BranchProtection{
  91. EnablePush: true,
  92. }, http.StatusOK)
  93. testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)
  94. }