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.

175 lines
5.8 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. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIListIssues(t *testing.T) {
  15. defer prepareTestEnv(t)()
  16. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  17. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  18. session := loginUser(t, owner.Name)
  19. token := getTokenForLoggedInUser(t, session)
  20. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all&token=%s",
  21. owner.Name, repo.Name, token)
  22. resp := session.MakeRequest(t, req, http.StatusOK)
  23. var apiIssues []*api.Issue
  24. DecodeJSON(t, resp, &apiIssues)
  25. assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
  26. for _, apiIssue := range apiIssues {
  27. models.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID})
  28. }
  29. }
  30. func TestAPICreateIssue(t *testing.T) {
  31. defer prepareTestEnv(t)()
  32. const body, title = "apiTestBody", "apiTestTitle"
  33. repoBefore := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  34. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User)
  35. session := loginUser(t, owner.Name)
  36. token := getTokenForLoggedInUser(t, session)
  37. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repoBefore.Name, token)
  38. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
  39. Body: body,
  40. Title: title,
  41. Assignee: owner.Name,
  42. })
  43. resp := session.MakeRequest(t, req, http.StatusCreated)
  44. var apiIssue api.Issue
  45. DecodeJSON(t, resp, &apiIssue)
  46. assert.Equal(t, apiIssue.Body, body)
  47. assert.Equal(t, apiIssue.Title, title)
  48. models.AssertExistsAndLoadBean(t, &models.Issue{
  49. RepoID: repoBefore.ID,
  50. AssigneeID: owner.ID,
  51. Content: body,
  52. Title: title,
  53. })
  54. repoAfter := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  55. assert.Equal(t, repoBefore.NumIssues+1, repoAfter.NumIssues)
  56. assert.Equal(t, repoBefore.NumClosedIssues, repoAfter.NumClosedIssues)
  57. }
  58. func TestAPIEditIssue(t *testing.T) {
  59. defer prepareTestEnv(t)()
  60. issueBefore := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)
  61. repoBefore := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository)
  62. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User)
  63. assert.NoError(t, issueBefore.LoadAttributes())
  64. assert.Equal(t, int64(1019307200), int64(issueBefore.DeadlineUnix))
  65. assert.Equal(t, api.StateOpen, issueBefore.State())
  66. session := loginUser(t, owner.Name)
  67. token := getTokenForLoggedInUser(t, session)
  68. // update values of issue
  69. issueState := "closed"
  70. removeDeadline := true
  71. milestone := int64(4)
  72. body := "new content!"
  73. title := "new title from api set"
  74. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d?token=%s", owner.Name, repoBefore.Name, issueBefore.Index, token)
  75. req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{
  76. State: &issueState,
  77. RemoveDeadline: &removeDeadline,
  78. Milestone: &milestone,
  79. Body: &body,
  80. Title: title,
  81. // ToDo change more
  82. })
  83. resp := session.MakeRequest(t, req, http.StatusCreated)
  84. var apiIssue api.Issue
  85. DecodeJSON(t, resp, &apiIssue)
  86. issueAfter := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)
  87. repoAfter := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository)
  88. // check deleted user
  89. assert.Equal(t, int64(500), issueAfter.PosterID)
  90. assert.NoError(t, issueAfter.LoadAttributes())
  91. assert.Equal(t, int64(-1), issueAfter.PosterID)
  92. assert.Equal(t, int64(-1), issueBefore.PosterID)
  93. assert.Equal(t, int64(-1), apiIssue.Poster.ID)
  94. // check repo change
  95. assert.Equal(t, repoBefore.NumClosedIssues+1, repoAfter.NumClosedIssues)
  96. // API response
  97. assert.Equal(t, api.StateClosed, apiIssue.State)
  98. assert.Equal(t, milestone, apiIssue.Milestone.ID)
  99. assert.Equal(t, body, apiIssue.Body)
  100. assert.True(t, apiIssue.Deadline == nil)
  101. assert.Equal(t, title, apiIssue.Title)
  102. // in database
  103. assert.Equal(t, api.StateClosed, issueAfter.State())
  104. assert.Equal(t, milestone, issueAfter.MilestoneID)
  105. assert.Equal(t, int64(0), int64(issueAfter.DeadlineUnix))
  106. assert.Equal(t, body, issueAfter.Content)
  107. assert.Equal(t, title, issueAfter.Title)
  108. }
  109. func TestAPISearchIssue(t *testing.T) {
  110. defer prepareTestEnv(t)()
  111. session := loginUser(t, "user2")
  112. token := getTokenForLoggedInUser(t, session)
  113. link, _ := url.Parse("/api/v1/repos/issues/search")
  114. req := NewRequest(t, "GET", link.String())
  115. resp := session.MakeRequest(t, req, http.StatusOK)
  116. var apiIssues []*api.Issue
  117. DecodeJSON(t, resp, &apiIssues)
  118. assert.Len(t, apiIssues, 9)
  119. query := url.Values{}
  120. query.Add("token", token)
  121. link.RawQuery = query.Encode()
  122. req = NewRequest(t, "GET", link.String())
  123. resp = session.MakeRequest(t, req, http.StatusOK)
  124. DecodeJSON(t, resp, &apiIssues)
  125. assert.Len(t, apiIssues, 9)
  126. query.Add("state", "closed")
  127. link.RawQuery = query.Encode()
  128. req = NewRequest(t, "GET", link.String())
  129. resp = session.MakeRequest(t, req, http.StatusOK)
  130. DecodeJSON(t, resp, &apiIssues)
  131. assert.Len(t, apiIssues, 2)
  132. query.Set("state", "all")
  133. link.RawQuery = query.Encode()
  134. req = NewRequest(t, "GET", link.String())
  135. resp = session.MakeRequest(t, req, http.StatusOK)
  136. DecodeJSON(t, resp, &apiIssues)
  137. assert.Len(t, apiIssues, 10) //there are more but 10 is page item limit
  138. query.Add("page", "2")
  139. link.RawQuery = query.Encode()
  140. req = NewRequest(t, "GET", link.String())
  141. resp = session.MakeRequest(t, req, http.StatusOK)
  142. DecodeJSON(t, resp, &apiIssues)
  143. assert.Len(t, apiIssues, 1)
  144. }