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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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?action=protected_branch", map[string]string{
  36. "_csrf": csrf,
  37. "branchName": "master",
  38. "canPush": "true",
  39. })
  40. resp := session.MakeRequest(t, req, http.StatusOK)
  41. // Check if master branch has been locked successfully
  42. flashCookie := session.GetCookie("macaron_flash")
  43. assert.NotNil(t, flashCookie)
  44. assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")
  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, string(resp.Body), "Can not commit to protected branch 'master'.")
  62. }
  63. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
  64. newContent := "Hello, World (Edited)\n"
  65. // Get to the 'edit this file' page
  66. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  67. resp := session.MakeRequest(t, req, http.StatusOK)
  68. htmlDoc := NewHTMLParser(t, resp.Body)
  69. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  70. assert.NotEmpty(t, lastCommit)
  71. // Submit the edits
  72. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  73. map[string]string{
  74. "_csrf": htmlDoc.GetCSRF(),
  75. "last_commit": lastCommit,
  76. "tree_path": filePath,
  77. "content": newContent,
  78. "commit_choice": "direct",
  79. },
  80. )
  81. resp = session.MakeRequest(t, req, http.StatusFound)
  82. // Verify the change
  83. req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath))
  84. resp = session.MakeRequest(t, req, http.StatusOK)
  85. assert.EqualValues(t, newContent, string(resp.Body))
  86. return resp
  87. }
  88. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath string) *TestResponse {
  89. newContent := "Hello, World (Edited)\n"
  90. // Get to the 'edit this file' page
  91. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  92. resp := session.MakeRequest(t, req, http.StatusOK)
  93. htmlDoc := NewHTMLParser(t, resp.Body)
  94. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  95. assert.NotEmpty(t, lastCommit)
  96. // Submit the edits
  97. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  98. map[string]string{
  99. "_csrf": htmlDoc.GetCSRF(),
  100. "last_commit": lastCommit,
  101. "tree_path": filePath,
  102. "content": newContent,
  103. "commit_choice": "commit-to-new-branch",
  104. "new_branch_name": targetBranch,
  105. },
  106. )
  107. resp = session.MakeRequest(t, req, http.StatusFound)
  108. // Verify the change
  109. req = NewRequest(t, "GET", path.Join(user, repo, "raw", targetBranch, filePath))
  110. resp = session.MakeRequest(t, req, http.StatusOK)
  111. assert.EqualValues(t, newContent, string(resp.Body))
  112. return resp
  113. }
  114. func TestEditFile(t *testing.T) {
  115. prepareTestEnv(t)
  116. session := loginUser(t, "user2")
  117. testEditFile(t, session, "user2", "repo1", "master", "README.md")
  118. }
  119. func TestEditFileToNewBranch(t *testing.T) {
  120. prepareTestEnv(t)
  121. session := loginUser(t, "user2")
  122. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md")
  123. }