You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor_test.go 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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)
  17. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  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)
  30. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  31. }
  32. func TestCreateFileOnProtectedBranch(t *testing.T) {
  33. prepareTestEnv(t)
  34. session := loginUser(t, "user2")
  35. // Open repository branch settings
  36. req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
  37. resp := session.MakeRequest(t, req)
  38. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  39. doc := NewHTMLParser(t, resp.Body)
  40. // Change master branch to protected
  41. req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
  42. "_csrf": doc.GetCSRF(),
  43. "branchName": "master",
  44. "canPush": "true",
  45. })
  46. resp = session.MakeRequest(t, req)
  47. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  48. // Check if master branch has been locked successfully
  49. flashCookie := session.GetCookie("macaron_flash")
  50. assert.NotNil(t, flashCookie)
  51. assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")
  52. // Request editor page
  53. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  54. resp = session.MakeRequest(t, req)
  55. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  56. doc = NewHTMLParser(t, resp.Body)
  57. lastCommit := doc.GetInputValueByName("last_commit")
  58. assert.NotEmpty(t, lastCommit)
  59. // Save new file to master branch
  60. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  61. "_csrf": doc.GetCSRF(),
  62. "last_commit": lastCommit,
  63. "tree_path": "test.txt",
  64. "content": "Content",
  65. "commit_choice": "direct",
  66. })
  67. resp = session.MakeRequest(t, req)
  68. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  69. // Check body for error message
  70. assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
  71. }
  72. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
  73. newContent := "Hello, World (Edited)\n"
  74. // Get to the 'edit this file' page
  75. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  76. resp := session.MakeRequest(t, req)
  77. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  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)
  92. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  93. // Verify the change
  94. req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath))
  95. resp = session.MakeRequest(t, req)
  96. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  97. assert.EqualValues(t, newContent, string(resp.Body))
  98. return resp
  99. }
  100. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath string) *TestResponse {
  101. newContent := "Hello, World (Edited)\n"
  102. // Get to the 'edit this file' page
  103. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  104. resp := session.MakeRequest(t, req)
  105. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  106. htmlDoc := NewHTMLParser(t, resp.Body)
  107. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  108. assert.NotEmpty(t, lastCommit)
  109. // Submit the edits
  110. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  111. map[string]string{
  112. "_csrf": htmlDoc.GetCSRF(),
  113. "last_commit": lastCommit,
  114. "tree_path": filePath,
  115. "content": newContent,
  116. "commit_choice": "commit-to-new-branch",
  117. "new_branch_name": targetBranch,
  118. },
  119. )
  120. resp = session.MakeRequest(t, req)
  121. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  122. // Verify the change
  123. req = NewRequest(t, "GET", path.Join(user, repo, "raw", targetBranch, filePath))
  124. resp = session.MakeRequest(t, req)
  125. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  126. assert.EqualValues(t, newContent, string(resp.Body))
  127. return resp
  128. }
  129. func TestEditFile(t *testing.T) {
  130. prepareTestEnv(t)
  131. session := loginUser(t, "user2")
  132. testEditFile(t, session, "user2", "repo1", "master", "README.md")
  133. }
  134. func TestEditFileToNewBranch(t *testing.T) {
  135. prepareTestEnv(t)
  136. session := loginUser(t, "user2")
  137. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md")
  138. }