Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

issue_test.go 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. "fmt"
  7. "net/http"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/references"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/test"
  16. "github.com/PuerkitoBio/goquery"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func getIssuesSelection(t testing.TB, htmlDoc *HTMLDoc) *goquery.Selection {
  20. issueList := htmlDoc.doc.Find(".issue.list")
  21. assert.EqualValues(t, 1, issueList.Length())
  22. return issueList.Find("li").Find(".title")
  23. }
  24. func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
  25. href, exists := issueSelection.Attr("href")
  26. assert.True(t, exists)
  27. indexStr := href[strings.LastIndexByte(href, '/')+1:]
  28. index, err := strconv.Atoi(indexStr)
  29. assert.NoError(t, err, "Invalid issue href: %s", href)
  30. return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
  31. }
  32. func assertMatch(t testing.TB, issue *models.Issue, keyword string) {
  33. matches := strings.Contains(strings.ToLower(issue.Title), keyword) ||
  34. strings.Contains(strings.ToLower(issue.Content), keyword)
  35. for _, comment := range issue.Comments {
  36. matches = matches || strings.Contains(
  37. strings.ToLower(comment.Content),
  38. keyword,
  39. )
  40. }
  41. assert.True(t, matches)
  42. }
  43. func TestNoLoginViewIssues(t *testing.T) {
  44. prepareTestEnv(t)
  45. req := NewRequest(t, "GET", "/user2/repo1/issues")
  46. MakeRequest(t, req, http.StatusOK)
  47. }
  48. func TestViewIssuesSortByType(t *testing.T) {
  49. prepareTestEnv(t)
  50. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  51. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  52. session := loginUser(t, user.Name)
  53. req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
  54. resp := session.MakeRequest(t, req, http.StatusOK)
  55. htmlDoc := NewHTMLParser(t, resp.Body)
  56. issuesSelection := getIssuesSelection(t, htmlDoc)
  57. expectedNumIssues := models.GetCount(t,
  58. &models.Issue{RepoID: repo.ID, PosterID: user.ID},
  59. models.Cond("is_closed=?", false),
  60. models.Cond("is_pull=?", false),
  61. )
  62. if expectedNumIssues > setting.UI.IssuePagingNum {
  63. expectedNumIssues = setting.UI.IssuePagingNum
  64. }
  65. assert.EqualValues(t, expectedNumIssues, issuesSelection.Length())
  66. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  67. issue := getIssue(t, repo.ID, selection)
  68. assert.EqualValues(t, user.ID, issue.PosterID)
  69. })
  70. }
  71. func TestViewIssuesKeyword(t *testing.T) {
  72. prepareTestEnv(t)
  73. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  74. const keyword = "first"
  75. req := NewRequestf(t, "GET", "%s/issues?q=%s", repo.RelLink(), keyword)
  76. resp := MakeRequest(t, req, http.StatusOK)
  77. htmlDoc := NewHTMLParser(t, resp.Body)
  78. issuesSelection := getIssuesSelection(t, htmlDoc)
  79. assert.EqualValues(t, 1, issuesSelection.Length())
  80. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  81. issue := getIssue(t, repo.ID, selection)
  82. assert.False(t, issue.IsClosed)
  83. assert.False(t, issue.IsPull)
  84. assertMatch(t, issue, keyword)
  85. })
  86. }
  87. func TestNoLoginViewIssue(t *testing.T) {
  88. prepareTestEnv(t)
  89. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  90. MakeRequest(t, req, http.StatusOK)
  91. }
  92. func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content string) string {
  93. req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
  94. resp := session.MakeRequest(t, req, http.StatusOK)
  95. htmlDoc := NewHTMLParser(t, resp.Body)
  96. link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
  97. assert.True(t, exists, "The template has changed")
  98. req = NewRequestWithValues(t, "POST", link, map[string]string{
  99. "_csrf": htmlDoc.GetCSRF(),
  100. "title": title,
  101. "content": content,
  102. })
  103. resp = session.MakeRequest(t, req, http.StatusFound)
  104. issueURL := test.RedirectURL(resp)
  105. req = NewRequest(t, "GET", issueURL)
  106. resp = session.MakeRequest(t, req, http.StatusOK)
  107. htmlDoc = NewHTMLParser(t, resp.Body)
  108. val := htmlDoc.doc.Find("#issue-title").Text()
  109. assert.Equal(t, title, val)
  110. val = htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  111. assert.Equal(t, content, val)
  112. return issueURL
  113. }
  114. func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) int64 {
  115. req := NewRequest(t, "GET", issueURL)
  116. resp := session.MakeRequest(t, req, http.StatusOK)
  117. htmlDoc := NewHTMLParser(t, resp.Body)
  118. link, exists := htmlDoc.doc.Find("#comment-form").Attr("action")
  119. assert.True(t, exists, "The template has changed")
  120. commentCount := htmlDoc.doc.Find(".comment-list .comments .comment .render-content").Length()
  121. req = NewRequestWithValues(t, "POST", link, map[string]string{
  122. "_csrf": htmlDoc.GetCSRF(),
  123. "content": content,
  124. "status": status,
  125. })
  126. resp = session.MakeRequest(t, req, http.StatusFound)
  127. req = NewRequest(t, "GET", test.RedirectURL(resp))
  128. resp = session.MakeRequest(t, req, http.StatusOK)
  129. htmlDoc = NewHTMLParser(t, resp.Body)
  130. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").Eq(commentCount).Text()
  131. assert.Equal(t, content, val)
  132. idAttr, has := htmlDoc.doc.Find(".comment-list .comments .comment").Eq(commentCount).Attr("id")
  133. idStr := idAttr[strings.LastIndexByte(idAttr, '-')+1:]
  134. assert.True(t, has)
  135. id, err := strconv.Atoi(idStr)
  136. assert.NoError(t, err)
  137. return int64(id)
  138. }
  139. func TestNewIssue(t *testing.T) {
  140. prepareTestEnv(t)
  141. session := loginUser(t, "user2")
  142. testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  143. }
  144. func TestIssueCommentClose(t *testing.T) {
  145. prepareTestEnv(t)
  146. session := loginUser(t, "user2")
  147. issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
  148. testIssueAddComment(t, session, issueURL, "Test comment 1", "")
  149. testIssueAddComment(t, session, issueURL, "Test comment 2", "")
  150. testIssueAddComment(t, session, issueURL, "Test comment 3", "close")
  151. // Validate that issue content has not been updated
  152. req := NewRequest(t, "GET", issueURL)
  153. resp := session.MakeRequest(t, req, http.StatusOK)
  154. htmlDoc := NewHTMLParser(t, resp.Body)
  155. val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
  156. assert.Equal(t, "Description", val)
  157. }
  158. func TestIssueCrossReference(t *testing.T) {
  159. prepareTestEnv(t)
  160. // Issue that will be referenced
  161. _, issueBase := testIssueWithBean(t, "user2", 1, "Title", "Description")
  162. // Ref from issue title
  163. issueRefURL, issueRef := testIssueWithBean(t, "user2", 1, fmt.Sprintf("Title ref #%d", issueBase.Index), "Description")
  164. models.AssertExistsAndLoadBean(t, &models.Comment{
  165. IssueID: issueBase.ID,
  166. RefRepoID: 1,
  167. RefIssueID: issueRef.ID,
  168. RefCommentID: 0,
  169. RefIsPull: false,
  170. RefAction: references.XRefActionNone})
  171. // Edit title, neuter ref
  172. testIssueChangeInfo(t, "user2", issueRefURL, "title", "Title no ref")
  173. models.AssertExistsAndLoadBean(t, &models.Comment{
  174. IssueID: issueBase.ID,
  175. RefRepoID: 1,
  176. RefIssueID: issueRef.ID,
  177. RefCommentID: 0,
  178. RefIsPull: false,
  179. RefAction: references.XRefActionNeutered})
  180. // Ref from issue content
  181. issueRefURL, issueRef = testIssueWithBean(t, "user2", 1, "TitleXRef", fmt.Sprintf("Description ref #%d", issueBase.Index))
  182. models.AssertExistsAndLoadBean(t, &models.Comment{
  183. IssueID: issueBase.ID,
  184. RefRepoID: 1,
  185. RefIssueID: issueRef.ID,
  186. RefCommentID: 0,
  187. RefIsPull: false,
  188. RefAction: references.XRefActionNone})
  189. // Edit content, neuter ref
  190. testIssueChangeInfo(t, "user2", issueRefURL, "content", "Description no ref")
  191. models.AssertExistsAndLoadBean(t, &models.Comment{
  192. IssueID: issueBase.ID,
  193. RefRepoID: 1,
  194. RefIssueID: issueRef.ID,
  195. RefCommentID: 0,
  196. RefIsPull: false,
  197. RefAction: references.XRefActionNeutered})
  198. // Ref from a comment
  199. session := loginUser(t, "user2")
  200. commentID := testIssueAddComment(t, session, issueRefURL, fmt.Sprintf("Adding ref from comment #%d", issueBase.Index), "")
  201. comment := &models.Comment{
  202. IssueID: issueBase.ID,
  203. RefRepoID: 1,
  204. RefIssueID: issueRef.ID,
  205. RefCommentID: commentID,
  206. RefIsPull: false,
  207. RefAction: references.XRefActionNone}
  208. models.AssertExistsAndLoadBean(t, comment)
  209. // Ref from a different repository
  210. issueRefURL, issueRef = testIssueWithBean(t, "user12", 10, "TitleXRef", fmt.Sprintf("Description ref user2/repo1#%d", issueBase.Index))
  211. models.AssertExistsAndLoadBean(t, &models.Comment{
  212. IssueID: issueBase.ID,
  213. RefRepoID: 10,
  214. RefIssueID: issueRef.ID,
  215. RefCommentID: 0,
  216. RefIsPull: false,
  217. RefAction: references.XRefActionNone})
  218. }
  219. func testIssueWithBean(t *testing.T, user string, repoID int64, title, content string) (string, *models.Issue) {
  220. session := loginUser(t, user)
  221. issueURL := testNewIssue(t, session, user, fmt.Sprintf("repo%d", repoID), title, content)
  222. indexStr := issueURL[strings.LastIndexByte(issueURL, '/')+1:]
  223. index, err := strconv.Atoi(indexStr)
  224. assert.NoError(t, err, "Invalid issue href: %s", issueURL)
  225. issue := &models.Issue{RepoID: repoID, Index: int64(index)}
  226. models.AssertExistsAndLoadBean(t, issue)
  227. return issueURL, issue
  228. }
  229. func testIssueChangeInfo(t *testing.T, user, issueURL, info string, value string) {
  230. session := loginUser(t, user)
  231. req := NewRequest(t, "GET", issueURL)
  232. resp := session.MakeRequest(t, req, http.StatusOK)
  233. htmlDoc := NewHTMLParser(t, resp.Body)
  234. req = NewRequestWithValues(t, "POST", path.Join(issueURL, info), map[string]string{
  235. "_csrf": htmlDoc.GetCSRF(),
  236. info: value,
  237. })
  238. _ = session.MakeRequest(t, req, http.StatusOK)
  239. }