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.

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. "code.gitea.io/gitea/modules/test"
  14. "github.com/PuerkitoBio/goquery"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func getIssuesSelection(t testing.TB, htmlDoc *HTMLDoc) *goquery.Selection {
  18. issueList := htmlDoc.doc.Find(".issue.list")
  19. assert.EqualValues(t, 1, issueList.Length())
  20. return issueList.Find("li").Find(".title")
  21. }
  22. func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
  23. href, exists := issueSelection.Attr("href")
  24. assert.True(t, exists)
  25. indexStr := href[strings.LastIndexByte(href, '/')+1:]
  26. index, err := strconv.Atoi(indexStr)
  27. assert.NoError(t, err, "Invalid issue href: %s", href)
  28. return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
  29. }
  30. func assertMatch(t testing.TB, issue *models.Issue, keyword string) {
  31. matches := strings.Contains(strings.ToLower(issue.Title), keyword) ||
  32. strings.Contains(strings.ToLower(issue.Content), keyword)
  33. for _, comment := range issue.Comments {
  34. matches = matches || strings.Contains(
  35. strings.ToLower(comment.Content),
  36. keyword,
  37. )
  38. }
  39. assert.True(t, matches)
  40. }
  41. func TestNoLoginViewIssues(t *testing.T) {
  42. prepareTestEnv(t)
  43. req := NewRequest(t, "GET", "/user2/repo1/issues")
  44. MakeRequest(t, req, http.StatusOK)
  45. }
  46. func TestViewIssuesSortByType(t *testing.T) {
  47. prepareTestEnv(t)
  48. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  49. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  50. session := loginUser(t, user.Name)
  51. req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
  52. resp := session.MakeRequest(t, req, http.StatusOK)
  53. htmlDoc := NewHTMLParser(t, resp.Body)
  54. issuesSelection := getIssuesSelection(t, htmlDoc)
  55. expectedNumIssues := models.GetCount(t,
  56. &models.Issue{RepoID: repo.ID, PosterID: user.ID},
  57. models.Cond("is_closed=?", false),
  58. models.Cond("is_pull=?", false),
  59. )
  60. if expectedNumIssues > setting.UI.IssuePagingNum {
  61. expectedNumIssues = setting.UI.IssuePagingNum
  62. }
  63. assert.EqualValues(t, expectedNumIssues, issuesSelection.Length())
  64. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  65. issue := getIssue(t, repo.ID, selection)
  66. assert.EqualValues(t, user.ID, issue.PosterID)
  67. })
  68. }
  69. func TestViewIssuesKeyword(t *testing.T) {
  70. prepareTestEnv(t)
  71. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  72. const keyword = "first"
  73. req := NewRequestf(t, "GET", "%s/issues?q=%s", repo.RelLink(), keyword)
  74. resp := MakeRequest(t, req, http.StatusOK)
  75. htmlDoc := NewHTMLParser(t, resp.Body)
  76. issuesSelection := getIssuesSelection(t, htmlDoc)
  77. assert.EqualValues(t, 1, issuesSelection.Length())
  78. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  79. issue := getIssue(t, repo.ID, selection)
  80. assert.False(t, issue.IsClosed)
  81. assert.False(t, issue.IsPull)
  82. assertMatch(t, issue, keyword)
  83. })
  84. }
  85. func TestNoLoginViewIssue(t *testing.T) {
  86. prepareTestEnv(t)
  87. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  88. MakeRequest(t, req, http.StatusOK)
  89. }
  90. func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content string) string {
  91. req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
  92. resp := session.MakeRequest(t, req, http.StatusOK)
  93. htmlDoc := NewHTMLParser(t, resp.Body)
  94. link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
  95. assert.True(t, exists, "The template has changed")
  96. req = NewRequestWithValues(t, "POST", link, map[string]string{
  97. "_csrf": htmlDoc.GetCSRF(),
  98. "title": title,
  99. "content": content,
  100. })
  101. resp = session.MakeRequest(t, req, http.StatusFound)
  102. issueURL := test.RedirectURL(resp)
  103. req = NewRequest(t, "GET", issueURL)
  104. resp = session.MakeRequest(t, req, http.StatusOK)
  105. htmlDoc = NewHTMLParser(t, resp.Body)
  106. val := htmlDoc.doc.Find("#issue-title").Text()
  107. assert.Equal(t, title, val)
  108. val = htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  109. assert.Equal(t, content, val)
  110. return issueURL
  111. }
  112. func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) {
  113. req := NewRequest(t, "GET", issueURL)
  114. resp := session.MakeRequest(t, req, http.StatusOK)
  115. htmlDoc := NewHTMLParser(t, resp.Body)
  116. link, exists := htmlDoc.doc.Find("#comment-form").Attr("action")
  117. assert.True(t, exists, "The template has changed")
  118. commentCount := htmlDoc.doc.Find(".comment-list .comments .comment .render-content").Length()
  119. req = NewRequestWithValues(t, "POST", link, map[string]string{
  120. "_csrf": htmlDoc.GetCSRF(),
  121. "content": content,
  122. "status": status,
  123. })
  124. resp = session.MakeRequest(t, req, http.StatusFound)
  125. req = NewRequest(t, "GET", test.RedirectURL(resp))
  126. resp = session.MakeRequest(t, req, http.StatusOK)
  127. htmlDoc = NewHTMLParser(t, resp.Body)
  128. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").Eq(commentCount).Text()
  129. assert.Equal(t, content, val)
  130. }
  131. func TestNewIssue(t *testing.T) {
  132. prepareTestEnv(t)
  133. session := loginUser(t, "user2")
  134. testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  135. }
  136. func TestIssueCommentClose(t *testing.T) {
  137. prepareTestEnv(t)
  138. session := loginUser(t, "user2")
  139. issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  140. testIssueAddComment(t, session, issueURL, "Test comment 1", "")
  141. testIssueAddComment(t, session, issueURL, "Test comment 2", "")
  142. testIssueAddComment(t, session, issueURL, "Test comment 3", "close")
  143. // Validate that issue content has not been updated
  144. req := NewRequest(t, "GET", issueURL)
  145. resp := session.MakeRequest(t, req, http.StatusOK)
  146. htmlDoc := NewHTMLParser(t, resp.Body)
  147. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  148. assert.Equal(t, "Description", val)
  149. }