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.

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