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.

pull_create_test.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 testPullCreate(t *testing.T, session *TestSession, user, repo, branch, title string) *httptest.ResponseRecorder {
  14. req := NewRequest(t, "GET", path.Join(user, repo))
  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("button.ui.green.tiny.compact.button").Parent().Attr("href")
  19. assert.True(t, exists, "The template has changed")
  20. if branch != "master" {
  21. link = strings.Replace(link, ":master", ":"+branch, 1)
  22. }
  23. req = NewRequest(t, "GET", link)
  24. resp = session.MakeRequest(t, req, http.StatusOK)
  25. // Submit the form for creating the pull
  26. htmlDoc = NewHTMLParser(t, resp.Body)
  27. link, exists = htmlDoc.doc.Find("form.ui.form").Attr("action")
  28. assert.True(t, exists, "The template has changed")
  29. req = NewRequestWithValues(t, "POST", link, map[string]string{
  30. "_csrf": htmlDoc.GetCSRF(),
  31. "title": title,
  32. })
  33. resp = session.MakeRequest(t, req, http.StatusFound)
  34. return resp
  35. }
  36. func TestPullCreate(t *testing.T) {
  37. prepareTestEnv(t)
  38. session := loginUser(t, "user1")
  39. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  40. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  41. resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")
  42. // check the redirected URL
  43. url := resp.HeaderMap.Get("Location")
  44. assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)
  45. // check .diff can be accessed and matches performed change
  46. req := NewRequest(t, "GET", url+".diff")
  47. resp = session.MakeRequest(t, req, http.StatusOK)
  48. assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body)
  49. assert.Regexp(t, "^diff", resp.Body)
  50. assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one
  51. // check .patch can be accessed and matches performed change
  52. req = NewRequest(t, "GET", url+".patch")
  53. resp = session.MakeRequest(t, req, http.StatusOK)
  54. assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body)
  55. assert.Regexp(t, "diff", resp.Body)
  56. assert.Regexp(t, `Subject: \[PATCH\] Update 'README.md'`, resp.Body)
  57. assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one
  58. }
  59. func TestPullCreate_TitleEscape(t *testing.T) {
  60. prepareTestEnv(t)
  61. session := loginUser(t, "user1")
  62. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  63. testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
  64. resp := testPullCreate(t, session, "user1", "repo1", "master", "<i>XSS PR</i>")
  65. // check the redirected URL
  66. url := resp.HeaderMap.Get("Location")
  67. assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)
  68. // Edit title
  69. req := NewRequest(t, "GET", url)
  70. resp = session.MakeRequest(t, req, http.StatusOK)
  71. htmlDoc := NewHTMLParser(t, resp.Body)
  72. editTestTitleURL, exists := htmlDoc.doc.Find("#save-edit-title").First().Attr("data-update-url")
  73. assert.True(t, exists, "The template has changed")
  74. req = NewRequestWithValues(t, "POST", editTestTitleURL, map[string]string{
  75. "_csrf": htmlDoc.GetCSRF(),
  76. "title": "<u>XSS PR</u>",
  77. })
  78. session.MakeRequest(t, req, http.StatusOK)
  79. req = NewRequest(t, "GET", url)
  80. resp = session.MakeRequest(t, req, http.StatusOK)
  81. htmlDoc = NewHTMLParser(t, resp.Body)
  82. titleHTML, err := htmlDoc.doc.Find(".comments .event .text b").First().Html()
  83. assert.NoError(t, err)
  84. assert.Equal(t, "&lt;i&gt;XSS PR&lt;/i&gt;", titleHTML)
  85. titleHTML, err = htmlDoc.doc.Find(".comments .event .text b").Next().Html()
  86. assert.NoError(t, err)
  87. assert.Equal(t, "&lt;u&gt;XSS PR&lt;/u&gt;", titleHTML)
  88. }