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.

editor_test.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/http/httptest"
  8. "path"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCreateFile(t *testing.T) {
  13. prepareTestEnv(t)
  14. session := loginUser(t, "user2")
  15. // Request editor page
  16. req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
  17. resp := session.MakeRequest(t, req, http.StatusOK)
  18. doc := NewHTMLParser(t, resp.Body)
  19. lastCommit := doc.GetInputValueByName("last_commit")
  20. assert.NotEmpty(t, lastCommit)
  21. // Save new file to master branch
  22. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  23. "_csrf": doc.GetCSRF(),
  24. "last_commit": lastCommit,
  25. "tree_path": "test.txt",
  26. "content": "Content",
  27. "commit_choice": "direct",
  28. })
  29. resp = session.MakeRequest(t, req, http.StatusFound)
  30. }
  31. func TestCreateFileOnProtectedBranch(t *testing.T) {
  32. prepareTestEnv(t)
  33. session := loginUser(t, "user2")
  34. csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
  35. // Change master branch to protected
  36. req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/master", map[string]string{
  37. "_csrf": csrf,
  38. "protected": "on",
  39. })
  40. resp := session.MakeRequest(t, req, http.StatusFound)
  41. // Check if master branch has been locked successfully
  42. flashCookie := session.GetCookie("macaron_flash")
  43. assert.NotNil(t, flashCookie)
  44. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527master%2527%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
  45. // Request editor page
  46. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  47. resp = session.MakeRequest(t, req, http.StatusOK)
  48. doc := NewHTMLParser(t, resp.Body)
  49. lastCommit := doc.GetInputValueByName("last_commit")
  50. assert.NotEmpty(t, lastCommit)
  51. // Save new file to master branch
  52. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  53. "_csrf": doc.GetCSRF(),
  54. "last_commit": lastCommit,
  55. "tree_path": "test.txt",
  56. "content": "Content",
  57. "commit_choice": "direct",
  58. })
  59. resp = session.MakeRequest(t, req, http.StatusOK)
  60. // Check body for error message
  61. assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch 'master'.")
  62. // remove the protected branch
  63. csrf = GetCSRF(t, session, "/user2/repo1/settings/branches")
  64. // Change master branch to protected
  65. req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/master", map[string]string{
  66. "_csrf": csrf,
  67. "protected": "off",
  68. })
  69. resp = session.MakeRequest(t, req, http.StatusFound)
  70. // Check if master branch has been locked successfully
  71. flashCookie = session.GetCookie("macaron_flash")
  72. assert.NotNil(t, flashCookie)
  73. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527master%2527%2Bhas%2Bbeen%2Bdisabled.", flashCookie.Value)
  74. }
  75. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder {
  76. // Get to the 'edit this file' page
  77. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  78. resp := session.MakeRequest(t, req, http.StatusOK)
  79. htmlDoc := NewHTMLParser(t, resp.Body)
  80. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  81. assert.NotEmpty(t, lastCommit)
  82. // Submit the edits
  83. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  84. map[string]string{
  85. "_csrf": htmlDoc.GetCSRF(),
  86. "last_commit": lastCommit,
  87. "tree_path": filePath,
  88. "content": newContent,
  89. "commit_choice": "direct",
  90. },
  91. )
  92. resp = session.MakeRequest(t, req, http.StatusFound)
  93. // Verify the change
  94. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
  95. resp = session.MakeRequest(t, req, http.StatusOK)
  96. assert.EqualValues(t, newContent, resp.Body.String())
  97. return resp
  98. }
  99. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder {
  100. // Get to the 'edit this file' page
  101. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  102. resp := session.MakeRequest(t, req, http.StatusOK)
  103. htmlDoc := NewHTMLParser(t, resp.Body)
  104. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  105. assert.NotEmpty(t, lastCommit)
  106. // Submit the edits
  107. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  108. map[string]string{
  109. "_csrf": htmlDoc.GetCSRF(),
  110. "last_commit": lastCommit,
  111. "tree_path": filePath,
  112. "content": newContent,
  113. "commit_choice": "commit-to-new-branch",
  114. "new_branch_name": targetBranch,
  115. },
  116. )
  117. resp = session.MakeRequest(t, req, http.StatusFound)
  118. // Verify the change
  119. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))
  120. resp = session.MakeRequest(t, req, http.StatusOK)
  121. assert.EqualValues(t, newContent, resp.Body.String())
  122. return resp
  123. }
  124. func TestEditFile(t *testing.T) {
  125. prepareTestEnv(t)
  126. session := loginUser(t, "user2")
  127. testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  128. }
  129. func TestEditFileToNewBranch(t *testing.T) {
  130. prepareTestEnv(t)
  131. session := loginUser(t, "user2")
  132. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
  133. }