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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "path"
  9. "testing"
  10. gitea_context "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/json"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestCreateFile(t *testing.T) {
  15. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  16. session := loginUser(t, "user2")
  17. // Request editor page
  18. req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
  19. resp := session.MakeRequest(t, req, http.StatusOK)
  20. doc := NewHTMLParser(t, resp.Body)
  21. lastCommit := doc.GetInputValueByName("last_commit")
  22. assert.NotEmpty(t, lastCommit)
  23. // Save new file to master branch
  24. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  25. "_csrf": doc.GetCSRF(),
  26. "last_commit": lastCommit,
  27. "tree_path": "test.txt",
  28. "content": "Content",
  29. "commit_choice": "direct",
  30. })
  31. session.MakeRequest(t, req, http.StatusSeeOther)
  32. })
  33. }
  34. func TestCreateFileOnProtectedBranch(t *testing.T) {
  35. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  36. session := loginUser(t, "user2")
  37. csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
  38. // Change master branch to protected
  39. req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
  40. "_csrf": csrf,
  41. "rule_name": "master",
  42. "enable_push": "true",
  43. })
  44. session.MakeRequest(t, req, http.StatusSeeOther)
  45. // Check if master branch has been locked successfully
  46. flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
  47. assert.NotNil(t, flashCookie)
  48. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522master%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
  49. // Request editor page
  50. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  51. resp := session.MakeRequest(t, req, http.StatusOK)
  52. doc := NewHTMLParser(t, resp.Body)
  53. lastCommit := doc.GetInputValueByName("last_commit")
  54. assert.NotEmpty(t, lastCommit)
  55. // Save new file to master branch
  56. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  57. "_csrf": doc.GetCSRF(),
  58. "last_commit": lastCommit,
  59. "tree_path": "test.txt",
  60. "content": "Content",
  61. "commit_choice": "direct",
  62. })
  63. resp = session.MakeRequest(t, req, http.StatusOK)
  64. // Check body for error message
  65. assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch "master".")
  66. // remove the protected branch
  67. csrf = GetCSRF(t, session, "/user2/repo1/settings/branches")
  68. // Change master branch to protected
  69. req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{
  70. "_csrf": csrf,
  71. })
  72. resp = session.MakeRequest(t, req, http.StatusOK)
  73. res := make(map[string]string)
  74. assert.NoError(t, json.NewDecoder(resp.Body).Decode(&res))
  75. assert.EqualValues(t, "/user2/repo1/settings/branches", res["redirect"])
  76. // Check if master branch has been locked successfully
  77. flashCookie = session.GetCookie(gitea_context.CookieNameFlash)
  78. assert.NotNil(t, flashCookie)
  79. assert.EqualValues(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value)
  80. })
  81. }
  82. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder {
  83. // Get to the 'edit this file' page
  84. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  85. resp := session.MakeRequest(t, req, http.StatusOK)
  86. htmlDoc := NewHTMLParser(t, resp.Body)
  87. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  88. assert.NotEmpty(t, lastCommit)
  89. // Submit the edits
  90. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  91. map[string]string{
  92. "_csrf": htmlDoc.GetCSRF(),
  93. "last_commit": lastCommit,
  94. "tree_path": filePath,
  95. "content": newContent,
  96. "commit_choice": "direct",
  97. },
  98. )
  99. session.MakeRequest(t, req, http.StatusSeeOther)
  100. // Verify the change
  101. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
  102. resp = session.MakeRequest(t, req, http.StatusOK)
  103. assert.EqualValues(t, newContent, resp.Body.String())
  104. return resp
  105. }
  106. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder {
  107. // Get to the 'edit this file' page
  108. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  109. resp := session.MakeRequest(t, req, http.StatusOK)
  110. htmlDoc := NewHTMLParser(t, resp.Body)
  111. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  112. assert.NotEmpty(t, lastCommit)
  113. // Submit the edits
  114. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  115. map[string]string{
  116. "_csrf": htmlDoc.GetCSRF(),
  117. "last_commit": lastCommit,
  118. "tree_path": filePath,
  119. "content": newContent,
  120. "commit_choice": "commit-to-new-branch",
  121. "new_branch_name": targetBranch,
  122. },
  123. )
  124. session.MakeRequest(t, req, http.StatusSeeOther)
  125. // Verify the change
  126. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))
  127. resp = session.MakeRequest(t, req, http.StatusOK)
  128. assert.EqualValues(t, newContent, resp.Body.String())
  129. return resp
  130. }
  131. func TestEditFile(t *testing.T) {
  132. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  133. session := loginUser(t, "user2")
  134. testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  135. })
  136. }
  137. func TestEditFileToNewBranch(t *testing.T) {
  138. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  139. session := loginUser(t, "user2")
  140. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
  141. })
  142. }