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.3KB

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