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

editor_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  30. resp = session.MakeRequest(t, req)
  31. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  32. }
  33. func TestCreateFileOnProtectedBranch(t *testing.T) {
  34. prepareTestEnv(t)
  35. session := loginUser(t, "user2")
  36. // Open repository branch settings
  37. req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
  38. resp := session.MakeRequest(t, req)
  39. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  40. doc := NewHTMLParser(t, resp.Body)
  41. // Change master branch to protected
  42. req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
  43. "_csrf": doc.GetCSRF(),
  44. "branchName": "master",
  45. "canPush": "true",
  46. })
  47. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  48. resp = session.MakeRequest(t, req)
  49. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  50. // Check if master branch has been locked successfully
  51. flashCookie := session.GetCookie("macaron_flash")
  52. assert.NotNil(t, flashCookie)
  53. assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")
  54. // Request editor page
  55. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  56. resp = session.MakeRequest(t, req)
  57. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  58. doc = NewHTMLParser(t, resp.Body)
  59. lastCommit := doc.GetInputValueByName("last_commit")
  60. assert.NotEmpty(t, lastCommit)
  61. // Save new file to master branch
  62. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  63. "_csrf": doc.GetCSRF(),
  64. "last_commit": lastCommit,
  65. "tree_path": "test.txt",
  66. "content": "Content",
  67. "commit_choice": "direct",
  68. })
  69. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  70. resp = session.MakeRequest(t, req)
  71. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  72. // Check body for error message
  73. assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
  74. }
  75. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
  76. newContent := "Hello, World (Edited)\n"
  77. // Get to the 'edit this file' page
  78. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  79. resp := session.MakeRequest(t, req)
  80. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  81. htmlDoc := NewHTMLParser(t, resp.Body)
  82. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  83. assert.NotEmpty(t, lastCommit)
  84. // Submit the edits
  85. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  86. map[string]string{
  87. "_csrf": htmlDoc.GetCSRF(),
  88. "last_commit": lastCommit,
  89. "tree_path": filePath,
  90. "content": newContent,
  91. "commit_choice": "direct",
  92. },
  93. )
  94. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  95. resp = session.MakeRequest(t, req)
  96. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  97. // Verify the change
  98. req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath))
  99. resp = session.MakeRequest(t, req)
  100. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  101. assert.EqualValues(t, newContent, string(resp.Body))
  102. return resp
  103. }
  104. func TestEditFile(t *testing.T) {
  105. prepareTestEnv(t)
  106. session := loginUser(t, "user2")
  107. testEditFile(t, session, "user2", "repo1", "master", "README.md")
  108. }