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_compare_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "code.gitea.io/gitea/models/db"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. repo_service "code.gitea.io/gitea/services/repository"
  15. "code.gitea.io/gitea/tests"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestPullCompare(t *testing.T) {
  19. defer tests.PrepareTestEnv(t)()
  20. session := loginUser(t, "user2")
  21. req := NewRequest(t, "GET", "/user2/repo1/pulls")
  22. resp := session.MakeRequest(t, req, http.StatusOK)
  23. htmlDoc := NewHTMLParser(t, resp.Body)
  24. link, exists := htmlDoc.doc.Find(".new-pr-button").Attr("href")
  25. assert.True(t, exists, "The template has changed")
  26. req = NewRequest(t, "GET", link)
  27. resp = session.MakeRequest(t, req, http.StatusOK)
  28. assert.EqualValues(t, http.StatusOK, resp.Code)
  29. // test the edit button in the PR diff view
  30. req = NewRequest(t, "GET", "/user2/repo1/pulls/3/files")
  31. resp = session.MakeRequest(t, req, http.StatusOK)
  32. doc := NewHTMLParser(t, resp.Body)
  33. editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  34. assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
  35. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  36. defer tests.PrepareTestEnv(t)()
  37. session := loginUser(t, "user1")
  38. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  39. testCreateBranch(t, session, "user1", "repo1", "branch/master", "master1", http.StatusSeeOther)
  40. testEditFile(t, session, "user1", "repo1", "master1", "README.md", "Hello, World (Edited)\n")
  41. testPullCreate(t, session, "user1", "repo1", false, "master", "master1", "This is a pull title")
  42. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
  43. issueIndex := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueIndex{GroupID: repo1.ID}, unittest.OrderBy("group_id ASC"))
  44. prFilesURL := fmt.Sprintf("/user2/repo1/pulls/%d/files", issueIndex.MaxIndex)
  45. req = NewRequest(t, "GET", prFilesURL)
  46. resp = session.MakeRequest(t, req, http.StatusOK)
  47. doc := NewHTMLParser(t, resp.Body)
  48. editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  49. assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
  50. repoForked := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
  51. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  52. // delete the head repository and revisit the PR diff view
  53. err := repo_service.DeleteRepositoryDirectly(db.DefaultContext, user2, repoForked.ID)
  54. assert.NoError(t, err)
  55. req = NewRequest(t, "GET", prFilesURL)
  56. resp = session.MakeRequest(t, req, http.StatusOK)
  57. doc = NewHTMLParser(t, resp.Body)
  58. editButtonCount = doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
  59. assert.EqualValues(t, editButtonCount, 0, "Expected not to find a button to edit a file in the PR diff view because head repository has been deleted")
  60. })
  61. }