Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

pull_merge_test.go 3.1KB

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