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

issue_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(htmlDoc *HTMLDoc) *goquery.Selection {
  17. return htmlDoc.doc.Find(".issue.list").Find("li").Find(".title")
  18. }
  19. func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
  20. href, exists := issueSelection.Attr("href")
  21. assert.True(t, exists)
  22. indexStr := href[strings.LastIndexByte(href, '/')+1:]
  23. index, err := strconv.Atoi(indexStr)
  24. assert.NoError(t, err, "Invalid issue href: %s", href)
  25. return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
  26. }
  27. func TestNoLoginViewIssues(t *testing.T) {
  28. prepareTestEnv(t)
  29. req := NewRequest(t, "GET", "/user2/repo1/issues")
  30. MakeRequest(t, req, http.StatusOK)
  31. }
  32. func TestNoLoginViewIssuesSortByType(t *testing.T) {
  33. prepareTestEnv(t)
  34. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  35. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  36. repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  37. session := loginUser(t, user.Name)
  38. req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
  39. resp := session.MakeRequest(t, req, http.StatusOK)
  40. htmlDoc := NewHTMLParser(t, resp.Body)
  41. issuesSelection := getIssuesSelection(htmlDoc)
  42. expectedNumIssues := models.GetCount(t,
  43. &models.Issue{RepoID: repo.ID, PosterID: user.ID},
  44. models.Cond("is_closed=?", false),
  45. models.Cond("is_pull=?", false),
  46. )
  47. if expectedNumIssues > setting.UI.IssuePagingNum {
  48. expectedNumIssues = setting.UI.IssuePagingNum
  49. }
  50. assert.EqualValues(t, expectedNumIssues, issuesSelection.Length())
  51. issuesSelection.Each(func(_ int, selection *goquery.Selection) {
  52. issue := getIssue(t, repo.ID, selection)
  53. assert.EqualValues(t, user.ID, issue.PosterID)
  54. })
  55. }
  56. func TestNoLoginViewIssue(t *testing.T) {
  57. prepareTestEnv(t)
  58. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  59. MakeRequest(t, req, http.StatusOK)
  60. }
  61. func testNewIssue(t *testing.T, session *TestSession, user, repo, title string) {
  62. req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
  63. resp := session.MakeRequest(t, req, http.StatusOK)
  64. htmlDoc := NewHTMLParser(t, resp.Body)
  65. link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
  66. assert.True(t, exists, "The template has changed")
  67. req = NewRequestWithValues(t, "POST", link, map[string]string{
  68. "_csrf": htmlDoc.GetCSRF(),
  69. "title": title,
  70. })
  71. resp = session.MakeRequest(t, req, http.StatusFound)
  72. req = NewRequest(t, "GET", RedirectURL(t, resp))
  73. resp = session.MakeRequest(t, req, http.StatusOK)
  74. }
  75. func TestNewIssue(t *testing.T) {
  76. prepareTestEnv(t)
  77. session := loginUser(t, "user2")
  78. testNewIssue(t, session, "user2", "repo1", "Title")
  79. }