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.

139 lines
4.2 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. "path"
  9. "strings"
  10. "testing"
  11. "code.gitea.io/gitea/modules/test"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/unknwon/i18n"
  14. )
  15. func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
  16. var csrf string
  17. if expectedStatus == http.StatusNotFound {
  18. csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
  19. } else {
  20. csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefSubURL))
  21. }
  22. req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefSubURL), map[string]string{
  23. "_csrf": csrf,
  24. "new_branch_name": newBranchName,
  25. })
  26. resp := session.MakeRequest(t, req, expectedStatus)
  27. if expectedStatus != http.StatusFound {
  28. return ""
  29. }
  30. return test.RedirectURL(resp)
  31. }
  32. func TestCreateBranch(t *testing.T) {
  33. onGiteaRun(t, testCreateBranches)
  34. }
  35. func testCreateBranches(t *testing.T, giteaURL *url.URL) {
  36. tests := []struct {
  37. OldRefSubURL string
  38. NewBranch string
  39. CreateRelease string
  40. FlashMessage string
  41. ExpectedStatus int
  42. }{
  43. {
  44. OldRefSubURL: "branch/master",
  45. NewBranch: "feature/test1",
  46. ExpectedStatus: http.StatusFound,
  47. FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"),
  48. },
  49. {
  50. OldRefSubURL: "branch/master",
  51. NewBranch: "",
  52. ExpectedStatus: http.StatusFound,
  53. FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"),
  54. },
  55. {
  56. OldRefSubURL: "branch/master",
  57. NewBranch: "feature=test1",
  58. ExpectedStatus: http.StatusFound,
  59. FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature=test1"),
  60. },
  61. {
  62. OldRefSubURL: "branch/master",
  63. NewBranch: strings.Repeat("b", 101),
  64. ExpectedStatus: http.StatusFound,
  65. FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"),
  66. },
  67. {
  68. OldRefSubURL: "branch/master",
  69. NewBranch: "master",
  70. ExpectedStatus: http.StatusFound,
  71. FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"),
  72. },
  73. {
  74. OldRefSubURL: "branch/master",
  75. NewBranch: "master/test",
  76. ExpectedStatus: http.StatusFound,
  77. FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"),
  78. },
  79. {
  80. OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
  81. NewBranch: "feature/test2",
  82. ExpectedStatus: http.StatusNotFound,
  83. },
  84. {
  85. OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  86. NewBranch: "feature/test3",
  87. ExpectedStatus: http.StatusFound,
  88. FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"),
  89. },
  90. {
  91. OldRefSubURL: "branch/master",
  92. NewBranch: "v1.0.0",
  93. CreateRelease: "v1.0.0",
  94. ExpectedStatus: http.StatusFound,
  95. FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"),
  96. },
  97. {
  98. OldRefSubURL: "tag/v1.0.0",
  99. NewBranch: "feature/test4",
  100. CreateRelease: "v1.0.0",
  101. ExpectedStatus: http.StatusFound,
  102. FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"),
  103. },
  104. }
  105. for _, test := range tests {
  106. prepareTestEnv(t)
  107. session := loginUser(t, "user2")
  108. if test.CreateRelease != "" {
  109. createNewRelease(t, session, "/user2/repo1", test.CreateRelease, test.CreateRelease, false, false)
  110. }
  111. redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldRefSubURL, test.NewBranch, test.ExpectedStatus)
  112. if test.ExpectedStatus == http.StatusFound {
  113. req := NewRequest(t, "GET", redirectURL)
  114. resp := session.MakeRequest(t, req, http.StatusOK)
  115. htmlDoc := NewHTMLParser(t, resp.Body)
  116. assert.Equal(t,
  117. test.FlashMessage,
  118. strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
  119. )
  120. }
  121. }
  122. }
  123. func TestCreateBranchInvalidCSRF(t *testing.T) {
  124. prepareTestEnv(t)
  125. session := loginUser(t, "user2")
  126. req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", map[string]string{
  127. "_csrf": "fake_csrf",
  128. "new_branch_name": "test",
  129. })
  130. session.MakeRequest(t, req, http.StatusBadRequest)
  131. }