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.

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