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.

102 lines
3.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. "path"
  8. "strings"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum string) *TestResponse {
  13. req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum))
  14. resp := session.MakeRequest(t, req)
  15. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  16. // Click the little green button to craete a pull
  17. htmlDoc := NewHTMLParser(t, resp.Body)
  18. link, exists := htmlDoc.doc.Find("form.ui.form>button.ui.green.button").Parent().Attr("action")
  19. assert.True(t, exists, "The template has changed")
  20. req = NewRequestWithValues(t, "POST", link, map[string]string{
  21. "_csrf": htmlDoc.GetCSRF(),
  22. })
  23. resp = session.MakeRequest(t, req)
  24. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  25. return resp
  26. }
  27. func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum string) *TestResponse {
  28. req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum))
  29. resp := session.MakeRequest(t, req)
  30. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  31. // Click the little green button to craete a pull
  32. htmlDoc := NewHTMLParser(t, resp.Body)
  33. link, exists := htmlDoc.doc.Find(".comments .merge .delete-button").Attr("data-url")
  34. assert.True(t, exists, "The template has changed")
  35. req = NewRequestWithValues(t, "POST", link, map[string]string{
  36. "_csrf": htmlDoc.GetCSRF(),
  37. })
  38. resp = session.MakeRequest(t, req)
  39. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  40. return resp
  41. }
  42. func TestPullMerge(t *testing.T) {
  43. prepareTestEnv(t)
  44. session := loginUser(t, "user1")
  45. testRepoFork(t, session)
  46. testEditFile(t, session, "user1", "repo1", "master", "README.md")
  47. resp := testPullCreate(t, session, "user1", "repo1", "master")
  48. redirectedURL := resp.Headers["Location"]
  49. assert.NotEmpty(t, redirectedURL, "Redirected URL is not found")
  50. elem := strings.Split(redirectedURL[0], "/")
  51. assert.EqualValues(t, "pulls", elem[3])
  52. testPullMerge(t, session, elem[1], elem[2], elem[4])
  53. }
  54. func TestPullCleanUpAfterMerge(t *testing.T) {
  55. prepareTestEnv(t)
  56. session := loginUser(t, "user1")
  57. testRepoFork(t, session)
  58. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feature/test", "README.md")
  59. resp := testPullCreate(t, session, "user1", "repo1", "feature/test")
  60. redirectedURL := resp.Headers["Location"]
  61. assert.NotEmpty(t, redirectedURL, "Redirected URL is not found")
  62. elem := strings.Split(redirectedURL[0], "/")
  63. assert.EqualValues(t, "pulls", elem[3])
  64. testPullMerge(t, session, elem[1], elem[2], elem[4])
  65. // Check PR branch deletion
  66. resp = testPullCleanUp(t, session, elem[1], elem[2], elem[4])
  67. respJSON := struct {
  68. Redirect string
  69. }{}
  70. DecodeJSON(t, resp, &respJSON)
  71. assert.NotEmpty(t, respJSON.Redirect, "Redirected URL is not found")
  72. elem = strings.Split(respJSON.Redirect, "/")
  73. assert.EqualValues(t, "pulls", elem[3])
  74. // Check branch deletion result
  75. req := NewRequest(t, "GET", respJSON.Redirect)
  76. resp = session.MakeRequest(t, req)
  77. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  78. htmlDoc := NewHTMLParser(t, resp.Body)
  79. resultMsg := htmlDoc.doc.Find(".ui.message>p").Text()
  80. assert.EqualValues(t, "user1/feature/test has been deleted.", resultMsg)
  81. }