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.

165 lines
5.6 KiB

  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. "net/http/httptest"
  8. "net/url"
  9. "path"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestCreateFile(t *testing.T) {
  14. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  15. session := loginUser(t, "user2")
  16. // Request editor page
  17. req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
  18. resp := session.MakeRequest(t, req, http.StatusOK)
  19. doc := NewHTMLParser(t, resp.Body)
  20. lastCommit := doc.GetInputValueByName("last_commit")
  21. assert.NotEmpty(t, lastCommit)
  22. // Save new file to master branch
  23. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  24. "_csrf": doc.GetCSRF(),
  25. "last_commit": lastCommit,
  26. "tree_path": "test.txt",
  27. "content": "Content",
  28. "commit_choice": "direct",
  29. })
  30. session.MakeRequest(t, req, http.StatusFound)
  31. })
  32. }
  33. func TestCreateFileOnProtectedBranch(t *testing.T) {
  34. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  35. session := loginUser(t, "user2")
  36. csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
  37. // Change master branch to protected
  38. req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/master", map[string]string{
  39. "_csrf": csrf,
  40. "protected": "on",
  41. })
  42. session.MakeRequest(t, req, http.StatusFound)
  43. // Check if master branch has been locked successfully
  44. flashCookie := session.GetCookie("macaron_flash")
  45. assert.NotNil(t, flashCookie)
  46. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527master%2527%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
  47. // Request editor page
  48. req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
  49. resp := session.MakeRequest(t, req, http.StatusOK)
  50. doc := NewHTMLParser(t, resp.Body)
  51. lastCommit := doc.GetInputValueByName("last_commit")
  52. assert.NotEmpty(t, lastCommit)
  53. // Save new file to master branch
  54. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  55. "_csrf": doc.GetCSRF(),
  56. "last_commit": lastCommit,
  57. "tree_path": "test.txt",
  58. "content": "Content",
  59. "commit_choice": "direct",
  60. })
  61. resp = session.MakeRequest(t, req, http.StatusOK)
  62. // Check body for error message
  63. assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch 'master'.")
  64. // remove the protected branch
  65. csrf = GetCSRF(t, session, "/user2/repo1/settings/branches")
  66. // Change master branch to protected
  67. req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/master", map[string]string{
  68. "_csrf": csrf,
  69. "protected": "off",
  70. })
  71. resp = session.MakeRequest(t, req, http.StatusFound)
  72. // Check if master branch has been locked successfully
  73. flashCookie = session.GetCookie("macaron_flash")
  74. assert.NotNil(t, flashCookie)
  75. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527master%2527%2Bhas%2Bbeen%2Bdisabled.", flashCookie.Value)
  76. })
  77. }
  78. func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder {
  79. // Get to the 'edit this file' page
  80. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  81. resp := session.MakeRequest(t, req, http.StatusOK)
  82. htmlDoc := NewHTMLParser(t, resp.Body)
  83. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  84. assert.NotEmpty(t, lastCommit)
  85. // Submit the edits
  86. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
  87. map[string]string{
  88. "_csrf": htmlDoc.GetCSRF(),
  89. "last_commit": lastCommit,
  90. "tree_path": filePath,
  91. "content": newContent,
  92. "commit_choice": "direct",
  93. },
  94. )
  95. resp = session.MakeRequest(t, req, http.StatusFound)
  96. // Verify the change
  97. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
  98. resp = session.MakeRequest(t, req, http.StatusOK)
  99. assert.EqualValues(t, newContent, resp.Body.String())
  100. return resp
  101. }
  102. func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder {
  103. // Get to the 'edit this file' page
  104. req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
  105. resp := session.MakeRequest(t, req, http.StatusOK)
  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, http.StatusFound)
  121. // Verify the change
  122. req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))
  123. resp = session.MakeRequest(t, req, http.StatusOK)
  124. assert.EqualValues(t, newContent, resp.Body.String())
  125. return resp
  126. }
  127. func TestEditFile(t *testing.T) {
  128. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  129. session := loginUser(t, "user2")
  130. testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  131. })
  132. }
  133. func TestEditFileToNewBranch(t *testing.T) {
  134. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  135. session := loginUser(t, "user2")
  136. testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
  137. })
  138. }