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

editor_test.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 string) *TestResponse {
  75. newContent := "Hello, World (Edited)\n"
  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, filePath))
  95. resp = session.MakeRequest(t, req, http.StatusOK)
  96. assert.EqualValues(t, newContent, string(resp.Body))
  97. return resp
  98. }
  99. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath string) *TestResponse {
  100. newContent := "Hello, World (Edited)\n"
  101. // Get to the 'edit this file' page
  102. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  103. resp := session.MakeRequest(t, req, http.StatusOK)
  104. htmlDoc := NewHTMLParser(t, resp.Body)
  105. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  106. assert.NotEmpty(t, lastCommit)
  107. // Submit the edits
  108. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  109. map[string]string{
  110. "_csrf": htmlDoc.GetCSRF(),
  111. "last_commit": lastCommit,
  112. "tree_path": filePath,
  113. "content": newContent,
  114. "commit_choice": "commit-to-new-branch",
  115. "new_branch_name": targetBranch,
  116. },
  117. )
  118. resp = session.MakeRequest(t, req, http.StatusFound)
  119. // Verify the change
  120. req = NewRequest(t, "GET", path.Join(user, repo, "raw", targetBranch, filePath))
  121. resp = session.MakeRequest(t, req, http.StatusOK)
  122. assert.EqualValues(t, newContent, string(resp.Body))
  123. return resp
  124. }
  125. func TestEditFile(t *testing.T) {
  126. prepareTestEnv(t)
  127. session := loginUser(t, "user2")
  128. testEditFile(t, session, "user2", "repo1", "master", "README.md")
  129. }
  130. func TestEditFileToNewBranch(t *testing.T) {
  131. prepareTestEnv(t)
  132. session := loginUser(t, "user2")
  133. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md")
  134. }