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 12KB

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