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.

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