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.

106 lines
3.4 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. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCreateFile(t *testing.T) {
  13. prepareTestEnv(t)
  14. session := loginUser(t, "user2", "password")
  15. // Request editor page
  16. req, err := http.NewRequest("GET", "/user2/repo1/_new/master/", nil)
  17. assert.NoError(t, err)
  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, err = http.NewRequest("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. assert.NoError(t, err)
  35. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  36. resp = session.MakeRequest(t, req)
  37. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  38. }
  39. func TestCreateFileOnProtectedBranch(t *testing.T) {
  40. prepareTestEnv(t)
  41. session := loginUser(t, "user2", "password")
  42. // Open repository branch settings
  43. req, err := http.NewRequest("GET", "/user2/repo1/settings/branches", nil)
  44. assert.NoError(t, err)
  45. resp := session.MakeRequest(t, req)
  46. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  47. doc, err := NewHtmlParser(resp.Body)
  48. assert.NoError(t, err)
  49. // Change master branch to protected
  50. req, err = http.NewRequest("POST", "/user2/repo1/settings/branches?action=protected_branch",
  51. bytes.NewBufferString(url.Values{
  52. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  53. "branchName": []string{"master"},
  54. "canPush": []string{"true"},
  55. }.Encode()),
  56. )
  57. assert.NoError(t, err)
  58. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  59. resp = session.MakeRequest(t, req)
  60. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  61. // Check if master branch has been locked successfully
  62. flashCookie := session.GetCookie("macaron_flash")
  63. assert.NotNil(t, flashCookie)
  64. assert.EqualValues(t, flashCookie.Value, "success%3Dmaster%2BLocked%2Bsuccessfully")
  65. // Request editor page
  66. req, err = http.NewRequest("GET", "/user2/repo1/_new/master/", nil)
  67. assert.NoError(t, err)
  68. resp = session.MakeRequest(t, req)
  69. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  70. doc, err = NewHtmlParser(resp.Body)
  71. assert.NoError(t, err)
  72. lastCommit := doc.GetInputValueByName("last_commit")
  73. assert.NotEmpty(t, lastCommit)
  74. // Save new file to master branch
  75. req, err = http.NewRequest("POST", "/user2/repo1/_new/master/",
  76. bytes.NewBufferString(url.Values{
  77. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  78. "last_commit": []string{lastCommit},
  79. "tree_path": []string{"test.txt"},
  80. "content": []string{"Content"},
  81. "commit_choice": []string{"direct"},
  82. }.Encode()),
  83. )
  84. assert.NoError(t, err)
  85. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  86. resp = session.MakeRequest(t, req)
  87. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  88. // Check body for error message
  89. assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
  90. }