Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_branch_test.go 4.1KB

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