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

issue_test.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, content string) 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. "content": content,
  99. })
  100. resp = session.MakeRequest(t, req, http.StatusFound)
  101. issueURL := RedirectURL(t, resp)
  102. req = NewRequest(t, "GET", issueURL)
  103. resp = session.MakeRequest(t, req, http.StatusOK)
  104. htmlDoc = NewHTMLParser(t, resp.Body)
  105. val := htmlDoc.doc.Find("#issue-title").Text()
  106. assert.Equal(t, title, val)
  107. val = htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  108. assert.Equal(t, content, val)
  109. return issueURL
  110. }
  111. func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) {
  112. req := NewRequest(t, "GET", issueURL)
  113. resp := session.MakeRequest(t, req, http.StatusOK)
  114. htmlDoc := NewHTMLParser(t, resp.Body)
  115. link, exists := htmlDoc.doc.Find("#comment-form").Attr("action")
  116. assert.True(t, exists, "The template has changed")
  117. commentCount := htmlDoc.doc.Find(".comment-list .comments .comment .render-content").Length()
  118. req = NewRequestWithValues(t, "POST", link, map[string]string{
  119. "_csrf": htmlDoc.GetCSRF(),
  120. "content": content,
  121. "status": status,
  122. })
  123. resp = session.MakeRequest(t, req, http.StatusFound)
  124. req = NewRequest(t, "GET", RedirectURL(t, resp))
  125. resp = session.MakeRequest(t, req, http.StatusOK)
  126. htmlDoc = NewHTMLParser(t, resp.Body)
  127. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").Eq(commentCount).Text()
  128. assert.Equal(t, content, val)
  129. }
  130. func TestNewIssue(t *testing.T) {
  131. prepareTestEnv(t)
  132. session := loginUser(t, "user2")
  133. testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  134. }
  135. func TestIssueCommentClose(t *testing.T) {
  136. prepareTestEnv(t)
  137. session := loginUser(t, "user2")
  138. issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  139. testIssueAddComment(t, session, issueURL, "Test comment 1", "")
  140. testIssueAddComment(t, session, issueURL, "Test comment 2", "")
  141. testIssueAddComment(t, session, issueURL, "Test comment 3", "close")
  142. // Validate that issue content has not been updated
  143. req := NewRequest(t, "GET", issueURL)
  144. resp := session.MakeRequest(t, req, http.StatusOK)
  145. htmlDoc := NewHTMLParser(t, resp.Body)
  146. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  147. assert.Equal(t, "Description", val)
  148. }