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 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "bytes"
  7. "net/http"
  8. "net/url"
  9. "path"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestCreateFile(t *testing.T) {
  14. prepareTestEnv(t)
  15. session := loginUser(t, "user2", "password")
  16. // Request editor page
  17. req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
  18. resp := session.MakeRequest(t, req)
  19. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  20. doc, err := NewHtmlParser(resp.Body)
  21. assert.NoError(t, err)
  22. lastCommit := doc.GetInputValueByName("last_commit")
  23. assert.NotEmpty(t, lastCommit)
  24. // Save new file to master branch
  25. req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/",
  26. bytes.NewBufferString(url.Values{
  27. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  28. "last_commit": []string{lastCommit},
  29. "tree_path": []string{"test.txt"},
  30. "content": []string{"Content"},
  31. "commit_choice": []string{"direct"},
  32. }.Encode()),
  33. )
  34. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  35. resp = session.MakeRequest(t, req)
  36. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  37. }
  38. func TestCreateFileOnProtectedBranch(t *testing.T) {
  39. prepareTestEnv(t)
  40. session := loginUser(t, "user2", "password")
  41. // Open repository branch settings
  42. req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
  43. resp := session.MakeRequest(t, req)
  44. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  45. doc, err := NewHtmlParser(resp.Body)
  46. assert.NoError(t, err)
  47. // Change master branch to protected
  48. req = NewRequestBody(t, "POST", "/user2/repo1/settings/branches?action=protected_branch",
  49. bytes.NewBufferString(url.Values{
  50. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  51. "branchName": []string{"master"},
  52. "canPush": []string{"true"},
  53. }.Encode()),
  54. )
  55. assert.NoError(t, err)
  56. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  57. resp = session.MakeRequest(t, req)
  58. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  59. // Check if master branch has been locked successfully
  60. flashCookie := session.GetCookie("macaron_flash")
  61. assert.NotNil(t, flashCookie)
  62. assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")
  63. // Request editor page
  64. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  65. resp = session.MakeRequest(t, req)
  66. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  67. doc, err = NewHtmlParser(resp.Body)
  68. assert.NoError(t, err)
  69. lastCommit := doc.GetInputValueByName("last_commit")
  70. assert.NotEmpty(t, lastCommit)
  71. // Save new file to master branch
  72. req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/",
  73. bytes.NewBufferString(url.Values{
  74. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  75. "last_commit": []string{lastCommit},
  76. "tree_path": []string{"test.txt"},
  77. "content": []string{"Content"},
  78. "commit_choice": []string{"direct"},
  79. }.Encode()),
  80. )
  81. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  82. resp = session.MakeRequest(t, req)
  83. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  84. // Check body for error message
  85. assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
  86. }
  87. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
  88. newContent := "Hello, World (Edited)\n"
  89. // Get to the 'edit this file' page
  90. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  91. resp := session.MakeRequest(t, req)
  92. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  93. htmlDoc, err := NewHtmlParser(resp.Body)
  94. assert.NoError(t, err)
  95. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  96. assert.NotEmpty(t, lastCommit)
  97. // Submit the edits
  98. req = NewRequestBody(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  99. bytes.NewBufferString(url.Values{
  100. "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")},
  101. "last_commit": []string{lastCommit},
  102. "tree_path": []string{filePath},
  103. "content": []string{newContent},
  104. "commit_choice": []string{"direct"},
  105. }.Encode()),
  106. )
  107. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  108. resp = session.MakeRequest(t, req)
  109. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  110. // Verify the change
  111. req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath))
  112. resp = session.MakeRequest(t, req)
  113. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  114. assert.EqualValues(t, newContent, string(resp.Body))
  115. return resp
  116. }
  117. func TestEditFile(t *testing.T) {
  118. prepareTestEnv(t)
  119. session := loginUser(t, "user2", "password")
  120. testEditFile(t, session, "user2", "repo1", "master", "README.md")
  121. }