Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

issue_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "strconv"
  9. "strings"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/PuerkitoBio/goquery"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func getIssuesSelection(t testing.TB, htmlDoc *HTMLDoc) *goquery.Selection {
  17. issueList := htmlDoc.doc.Find(".issue.list")
  18. assert.EqualValues(t, 1, issueList.Length())
  19. return issueList.Find("li").Find(".title")
  20. }
  21. func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
  22. href, exists := issueSelection.Attr("href")
  23. assert.True(t, exists)
  24. indexStr := href[strings.LastIndexByte(href, '/')+1:]
  25. index, err := strconv.Atoi(indexStr)
  26. assert.NoError(t, err, "Invalid issue href: %s", href)
  27. return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
  28. }
  29. func assertMatch(t testing.TB, issue *models.Issue, keyword string) {
  30. matches := strings.Contains(strings.ToLower(issue.Title), keyword) ||
  31. strings.Contains(strings.ToLower(issue.Content), keyword)
  32. for _, comment := range issue.Comments {
  33. matches = matches || strings.Contains(
  34. strings.ToLower(comment.Content),
  35. keyword,
  36. )
  37. }
  38. assert.True(t, matches)
  39. }
  40. func TestNoLoginViewIssues(t *testing.T) {
  41. prepareTestEnv(t)
  42. req := NewRequest(t, "GET", "/user2/repo1/issues")
  43. MakeRequest(t, req, http.StatusOK)
  44. }
  45. func TestViewIssuesSortByType(t *testing.T) {
  46. prepareTestEnv(t)
  47. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  48. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  49. session := loginUser(t, user.Name)
  50. req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
  51. resp := session.MakeRequest(t, req, http.StatusOK)
  52. htmlDoc := NewHTMLParser(t, resp.Body)
  53. issuesSelection := getIssuesSelection(t, htmlDoc)
  54. expectedNumIssues := models.GetCount(t,
  55. &models.Issue{RepoID: repo.ID, PosterID: user.ID},
  56. models.Cond("is_closed=?", false),
  57. models.Cond("is_pull=?", false),
  58. )
  59. if expectedNumIssues > setting.UI.IssuePagingNum {
  60. expectedNumIssues = setting.UI.IssuePagingNum
  61. }
  62. assert.EqualValues(t, expectedNumIssues, issuesSelection.Length())
  63. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  64. issue := getIssue(t, repo.ID, selection)
  65. assert.EqualValues(t, user.ID, issue.PosterID)
  66. })
  67. }
  68. func TestViewIssuesKeyword(t *testing.T) {
  69. prepareTestEnv(t)
  70. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  71. const keyword = "first"
  72. req := NewRequestf(t, "GET", "%s/issues?q=%s", repo.RelLink(), keyword)
  73. resp := MakeRequest(t, req, http.StatusOK)
  74. htmlDoc := NewHTMLParser(t, resp.Body)
  75. issuesSelection := getIssuesSelection(t, htmlDoc)
  76. assert.EqualValues(t, 1, issuesSelection.Length())
  77. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  78. issue := getIssue(t, repo.ID, selection)
  79. assert.False(t, issue.IsClosed)
  80. assert.False(t, issue.IsPull)
  81. assertMatch(t, issue, keyword)
  82. })
  83. }
  84. func TestNoLoginViewIssue(t *testing.T) {
  85. prepareTestEnv(t)
  86. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  87. MakeRequest(t, req, http.StatusOK)
  88. }
  89. func testNewIssue(t *testing.T, session *TestSession, user, repo, title string) {
  90. req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
  91. resp := session.MakeRequest(t, req, http.StatusOK)
  92. htmlDoc := NewHTMLParser(t, resp.Body)
  93. link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
  94. assert.True(t, exists, "The template has changed")
  95. req = NewRequestWithValues(t, "POST", link, map[string]string{
  96. "_csrf": htmlDoc.GetCSRF(),
  97. "title": title,
  98. })
  99. resp = session.MakeRequest(t, req, http.StatusFound)
  100. req = NewRequest(t, "GET", RedirectURL(t, resp))
  101. resp = session.MakeRequest(t, req, http.StatusOK)
  102. }
  103. func TestNewIssue(t *testing.T) {
  104. prepareTestEnv(t)
  105. session := loginUser(t, "user2")
  106. testNewIssue(t, session, "user2", "repo1", "Title")
  107. }