gitea源码

pull_compare_test.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unittest"
  12. "code.gitea.io/gitea/modules/test"
  13. repo_service "code.gitea.io/gitea/services/repository"
  14. "code.gitea.io/gitea/tests"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestPullCompare(t *testing.T) {
  18. defer tests.PrepareTestEnv(t)()
  19. t.Run("PullsNewRedirect", func(t *testing.T) {
  20. req := NewRequest(t, "GET", "/user2/repo1/pulls/new/foo")
  21. resp := MakeRequest(t, req, http.StatusSeeOther)
  22. redirect := test.RedirectURL(resp)
  23. assert.Equal(t, "/user2/repo1/compare/master...foo?expand=1", redirect)
  24. req = NewRequest(t, "GET", "/user13/repo11/pulls/new/foo")
  25. resp = MakeRequest(t, req, http.StatusSeeOther)
  26. redirect = test.RedirectURL(resp)
  27. assert.Equal(t, "/user12/repo10/compare/master...user13:foo?expand=1", redirect)
  28. })
  29. t.Run("ButtonsExist", func(t *testing.T) {
  30. session := loginUser(t, "user2")
  31. // test the "New PR" button
  32. req := NewRequest(t, "GET", "/user2/repo1/pulls")
  33. resp := session.MakeRequest(t, req, http.StatusOK)
  34. htmlDoc := NewHTMLParser(t, resp.Body)
  35. link, exists := htmlDoc.doc.Find(".new-pr-button").Attr("href")
  36. assert.True(t, exists, "The template has changed")
  37. req = NewRequest(t, "GET", link)
  38. resp = session.MakeRequest(t, req, http.StatusOK)
  39. assert.Equal(t, http.StatusOK, resp.Code)
  40. // test the edit button in the PR diff view
  41. req = NewRequest(t, "GET", "/user2/repo1/pulls/3/files")
  42. resp = session.MakeRequest(t, req, http.StatusOK)
  43. doc := NewHTMLParser(t, resp.Body)
  44. editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  45. assert.Positive(t, editButtonCount, "Expected to find a button to edit a file in the PR diff view but there were none")
  46. })
  47. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  48. defer tests.PrepareTestEnv(t)()
  49. session := loginUser(t, "user1")
  50. testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
  51. testCreateBranch(t, session, "user1", "repo1", "branch/master", "master1", http.StatusSeeOther)
  52. testEditFile(t, session, "user1", "repo1", "master1", "README.md", "Hello, World (Edited)\n")
  53. testPullCreate(t, session, "user1", "repo1", false, "master", "master1", "This is a pull title")
  54. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
  55. issueIndex := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueIndex{GroupID: repo1.ID}, unittest.OrderBy("group_id ASC"))
  56. prFilesURL := fmt.Sprintf("/user2/repo1/pulls/%d/files", issueIndex.MaxIndex)
  57. req := NewRequest(t, "GET", prFilesURL)
  58. resp := session.MakeRequest(t, req, http.StatusOK)
  59. doc := NewHTMLParser(t, resp.Body)
  60. editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  61. assert.Positive(t, editButtonCount, "Expected to find a button to edit a file in the PR diff view but there were none")
  62. repoForked := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
  63. // delete the head repository and revisit the PR diff view
  64. err := repo_service.DeleteRepositoryDirectly(t.Context(), repoForked.ID)
  65. assert.NoError(t, err)
  66. req = NewRequest(t, "GET", prFilesURL)
  67. resp = session.MakeRequest(t, req, http.StatusOK)
  68. doc = NewHTMLParser(t, resp.Body)
  69. editButtonCount = doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  70. assert.Equal(t, 0, editButtonCount, "Expected not to find a button to edit a file in the PR diff view because head repository has been deleted")
  71. })
  72. }
  73. func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) {
  74. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  75. // repo3 is private
  76. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
  77. assert.True(t, repo3.IsPrivate)
  78. // user4 forks repo3
  79. user4Session := loginUser(t, "user4")
  80. forkedRepoName := "user4-forked-repo3"
  81. testRepoFork(t, user4Session, repo3.OwnerName, repo3.Name, "user4", forkedRepoName, "")
  82. forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user4", Name: forkedRepoName})
  83. assert.True(t, forkedRepo.IsPrivate)
  84. // user4 creates a new branch and a PR
  85. testEditFileToNewBranch(t, user4Session, "user4", forkedRepoName, "master", "user4/update-readme", "README.md", "Hello, World\n(Edited by user4)\n")
  86. resp := testPullCreateDirectly(t, user4Session, repo3.OwnerName, repo3.Name, "master", "user4", forkedRepoName, "user4/update-readme", "PR for user4 forked repo3")
  87. prURL := test.RedirectURL(resp)
  88. // user2 (admin of repo3) goes to the PR files page
  89. user2Session := loginUser(t, "user2")
  90. resp = user2Session.MakeRequest(t, NewRequest(t, "GET", prURL+"/files"), http.StatusOK)
  91. htmlDoc := NewHTMLParser(t, resp.Body)
  92. nodes := htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
  93. if assert.Equal(t, 1, nodes.Length()) {
  94. // there is only "View File" button, no "Edit File" button
  95. assert.Equal(t, "View File", nodes.First().Text())
  96. viewFileLink, exists := nodes.First().Attr("href")
  97. if assert.True(t, exists) {
  98. user2Session.MakeRequest(t, NewRequest(t, "GET", viewFileLink), http.StatusOK)
  99. }
  100. }
  101. // user4 goes to the PR page and enable "Allow maintainers to edit"
  102. resp = user4Session.MakeRequest(t, NewRequest(t, "GET", prURL), http.StatusOK)
  103. htmlDoc = NewHTMLParser(t, resp.Body)
  104. dataURL, exists := htmlDoc.doc.Find("#allow-edits-from-maintainers").Attr("data-url")
  105. assert.True(t, exists)
  106. req := NewRequestWithValues(t, "POST", dataURL+"/set_allow_maintainer_edit", map[string]string{
  107. "_csrf": htmlDoc.GetCSRF(),
  108. "allow_maintainer_edit": "true",
  109. })
  110. user4Session.MakeRequest(t, req, http.StatusOK)
  111. // user2 (admin of repo3) goes to the PR files page again
  112. resp = user2Session.MakeRequest(t, NewRequest(t, "GET", prURL+"/files"), http.StatusOK)
  113. htmlDoc = NewHTMLParser(t, resp.Body)
  114. nodes = htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
  115. if assert.Equal(t, 2, nodes.Length()) {
  116. // there are "View File" button and "Edit File" button
  117. assert.Equal(t, "View File", nodes.First().Text())
  118. viewFileLink, exists := nodes.First().Attr("href")
  119. if assert.True(t, exists) {
  120. user2Session.MakeRequest(t, NewRequest(t, "GET", viewFileLink), http.StatusOK)
  121. }
  122. assert.Equal(t, "Edit File", nodes.Last().Text())
  123. editFileLink, exists := nodes.Last().Attr("href")
  124. if assert.True(t, exists) {
  125. // edit the file
  126. resp := user2Session.MakeRequest(t, NewRequest(t, "GET", editFileLink), http.StatusOK)
  127. htmlDoc := NewHTMLParser(t, resp.Body)
  128. lastCommit := htmlDoc.GetInputValueByName("last_commit")
  129. assert.NotEmpty(t, lastCommit)
  130. req := NewRequestWithValues(t, "POST", editFileLink, map[string]string{
  131. "_csrf": htmlDoc.GetCSRF(),
  132. "last_commit": lastCommit,
  133. "tree_path": "README.md",
  134. "content": "File is edited by the maintainer user2",
  135. "commit_summary": "user2 updated the file",
  136. "commit_choice": "direct",
  137. })
  138. resp = user2Session.MakeRequest(t, req, http.StatusOK)
  139. assert.NotEmpty(t, test.RedirectURL(resp))
  140. }
  141. }
  142. })
  143. }