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.

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