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.

146 lines
5.3 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. "path"
  9. "strings"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/test"
  13. "github.com/Unknwon/i18n"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum string, mergeStyle models.MergeStyle) *httptest.ResponseRecorder {
  17. req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum))
  18. resp := session.MakeRequest(t, req, http.StatusOK)
  19. // Click the little green button to create a pull
  20. htmlDoc := NewHTMLParser(t, resp.Body)
  21. link, exists := htmlDoc.doc.Find(".ui.form." + string(mergeStyle) + "-fields > form").Attr("action")
  22. assert.True(t, exists, "The template has changed")
  23. req = NewRequestWithValues(t, "POST", link, map[string]string{
  24. "_csrf": htmlDoc.GetCSRF(),
  25. "do": string(mergeStyle),
  26. })
  27. resp = session.MakeRequest(t, req, http.StatusFound)
  28. return resp
  29. }
  30. func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum string) *httptest.ResponseRecorder {
  31. req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum))
  32. resp := session.MakeRequest(t, req, http.StatusOK)
  33. // Click the little green button to create a pull
  34. htmlDoc := NewHTMLParser(t, resp.Body)
  35. link, exists := htmlDoc.doc.Find(".comments .merge .delete-button").Attr("data-url")
  36. assert.True(t, exists, "The template has changed")
  37. req = NewRequestWithValues(t, "POST", link, map[string]string{
  38. "_csrf": htmlDoc.GetCSRF(),
  39. })
  40. resp = session.MakeRequest(t, req, http.StatusOK)
  41. return resp
  42. }
  43. func TestPullMerge(t *testing.T) {
  44. prepareTestEnv(t)
  45. session := loginUser(t, "user1")
  46. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  47. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  48. resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")
  49. elem := strings.Split(test.RedirectURL(resp), "/")
  50. assert.EqualValues(t, "pulls", elem[3])
  51. testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleMerge)
  52. }
  53. func TestPullRebase(t *testing.T) {
  54. prepareTestEnv(t)
  55. session := loginUser(t, "user1")
  56. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  57. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  58. resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")
  59. elem := strings.Split(test.RedirectURL(resp), "/")
  60. assert.EqualValues(t, "pulls", elem[3])
  61. testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleRebase)
  62. }
  63. func TestPullSquash(t *testing.T) {
  64. prepareTestEnv(t)
  65. session := loginUser(t, "user1")
  66. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  67. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  68. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited!)\n")
  69. resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")
  70. elem := strings.Split(test.RedirectURL(resp), "/")
  71. assert.EqualValues(t, "pulls", elem[3])
  72. testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleSquash)
  73. }
  74. func TestPullCleanUpAfterMerge(t *testing.T) {
  75. prepareTestEnv(t)
  76. session := loginUser(t, "user1")
  77. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  78. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
  79. resp := testPullCreate(t, session, "user1", "repo1", "feature/test", "This is a pull title")
  80. elem := strings.Split(test.RedirectURL(resp), "/")
  81. assert.EqualValues(t, "pulls", elem[3])
  82. testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleMerge)
  83. // Check PR branch deletion
  84. resp = testPullCleanUp(t, session, elem[1], elem[2], elem[4])
  85. respJSON := struct {
  86. Redirect string
  87. }{}
  88. DecodeJSON(t, resp, &respJSON)
  89. assert.NotEmpty(t, respJSON.Redirect, "Redirected URL is not found")
  90. elem = strings.Split(respJSON.Redirect, "/")
  91. assert.EqualValues(t, "pulls", elem[3])
  92. // Check branch deletion result
  93. req := NewRequest(t, "GET", respJSON.Redirect)
  94. resp = session.MakeRequest(t, req, http.StatusOK)
  95. htmlDoc := NewHTMLParser(t, resp.Body)
  96. resultMsg := htmlDoc.doc.Find(".ui.message>p").Text()
  97. assert.EqualValues(t, "Branch 'user1/feature/test' has been deleted.", resultMsg)
  98. }
  99. func TestCantMergeWorkInProgress(t *testing.T) {
  100. prepareTestEnv(t)
  101. session := loginUser(t, "user1")
  102. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  103. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  104. resp := testPullCreate(t, session, "user1", "repo1", "master", "[wip] This is a pull title")
  105. req := NewRequest(t, "GET", resp.Header().Get("Location"))
  106. resp = session.MakeRequest(t, req, http.StatusOK)
  107. htmlDoc := NewHTMLParser(t, resp.Body)
  108. text := strings.TrimSpace(htmlDoc.doc.Find(".merge.segment > .text.grey").Text())
  109. assert.NotEmpty(t, text, "Can't find WIP text")
  110. // remove <strong /> from lang
  111. expected := i18n.Tr("en", "repo.pulls.cannot_merge_work_in_progress", "[wip]")
  112. replacer := strings.NewReplacer("<strong>", "", "</strong>", "")
  113. assert.Equal(t, replacer.Replace(expected), text, "Unable to find WIP text")
  114. }