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.

122 lines
4.1 KiB

  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. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIListIssues(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  16. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  17. session := loginUser(t, owner.Name)
  18. token := getTokenForLoggedInUser(t, session)
  19. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all&token=%s",
  20. owner.Name, repo.Name, token)
  21. resp := session.MakeRequest(t, req, http.StatusOK)
  22. var apiIssues []*api.Issue
  23. DecodeJSON(t, resp, &apiIssues)
  24. assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
  25. for _, apiIssue := range apiIssues {
  26. models.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID})
  27. }
  28. }
  29. func TestAPICreateIssue(t *testing.T) {
  30. defer prepareTestEnv(t)()
  31. const body, title = "apiTestBody", "apiTestTitle"
  32. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  33. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  34. session := loginUser(t, owner.Name)
  35. token := getTokenForLoggedInUser(t, session)
  36. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repo.Name, token)
  37. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
  38. Body: body,
  39. Title: title,
  40. Assignee: owner.Name,
  41. })
  42. resp := session.MakeRequest(t, req, http.StatusCreated)
  43. var apiIssue api.Issue
  44. DecodeJSON(t, resp, &apiIssue)
  45. assert.Equal(t, apiIssue.Body, body)
  46. assert.Equal(t, apiIssue.Title, title)
  47. models.AssertExistsAndLoadBean(t, &models.Issue{
  48. RepoID: repo.ID,
  49. AssigneeID: owner.ID,
  50. Content: body,
  51. Title: title,
  52. })
  53. }
  54. func TestAPIEditIssue(t *testing.T) {
  55. defer prepareTestEnv(t)()
  56. issueBefore := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)
  57. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository)
  58. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  59. assert.NoError(t, issueBefore.LoadAttributes())
  60. assert.Equal(t, int64(1019307200), int64(issueBefore.DeadlineUnix))
  61. assert.Equal(t, api.StateOpen, issueBefore.State())
  62. session := loginUser(t, owner.Name)
  63. token := getTokenForLoggedInUser(t, session)
  64. // update values of issue
  65. issueState := "closed"
  66. removeDeadline := true
  67. milestone := int64(4)
  68. body := "new content!"
  69. title := "new title from api set"
  70. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d?token=%s", owner.Name, repo.Name, issueBefore.Index, token)
  71. req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{
  72. State: &issueState,
  73. RemoveDeadline: &removeDeadline,
  74. Milestone: &milestone,
  75. Body: &body,
  76. Title: title,
  77. // ToDo change more
  78. })
  79. resp := session.MakeRequest(t, req, http.StatusCreated)
  80. var apiIssue api.Issue
  81. DecodeJSON(t, resp, &apiIssue)
  82. issueAfter := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)
  83. // check deleted user
  84. assert.Equal(t, int64(500), issueAfter.PosterID)
  85. assert.NoError(t, issueAfter.LoadAttributes())
  86. assert.Equal(t, int64(-1), issueAfter.PosterID)
  87. assert.Equal(t, int64(-1), issueBefore.PosterID)
  88. assert.Equal(t, int64(-1), apiIssue.Poster.ID)
  89. // API response
  90. assert.Equal(t, api.StateClosed, apiIssue.State)
  91. assert.Equal(t, milestone, apiIssue.Milestone.ID)
  92. assert.Equal(t, body, apiIssue.Body)
  93. assert.True(t, apiIssue.Deadline == nil)
  94. assert.Equal(t, title, apiIssue.Title)
  95. // in database
  96. assert.Equal(t, api.StateClosed, issueAfter.State())
  97. assert.Equal(t, milestone, issueAfter.MilestoneID)
  98. assert.Equal(t, int64(0), int64(issueAfter.DeadlineUnix))
  99. assert.Equal(t, body, issueAfter.Content)
  100. assert.Equal(t, title, issueAfter.Title)
  101. }