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.

api_comment_test.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. auth_model "code.gitea.io/gitea/models/auth"
  10. "code.gitea.io/gitea/models/db"
  11. issues_model "code.gitea.io/gitea/models/issues"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. user_model "code.gitea.io/gitea/models/user"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/services/convert"
  17. "code.gitea.io/gitea/tests"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestAPIListRepoComments(t *testing.T) {
  21. defer tests.PrepareTestEnv(t)()
  22. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
  23. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  24. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  25. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  26. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  27. link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
  28. req := NewRequest(t, "GET", link.String())
  29. resp := MakeRequest(t, req, http.StatusOK)
  30. var apiComments []*api.Comment
  31. DecodeJSON(t, resp, &apiComments)
  32. assert.Len(t, apiComments, 2)
  33. for _, apiComment := range apiComments {
  34. c := &issues_model.Comment{ID: apiComment.ID}
  35. unittest.AssertExistsAndLoadBean(t, c,
  36. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  37. unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: c.IssueID, RepoID: repo.ID})
  38. }
  39. // test before and since filters
  40. query := url.Values{}
  41. before := "2000-01-01T00:00:11+00:00" // unix: 946684811
  42. since := "2000-01-01T00:00:12+00:00" // unix: 946684812
  43. query.Add("before", before)
  44. link.RawQuery = query.Encode()
  45. req = NewRequest(t, "GET", link.String())
  46. resp = MakeRequest(t, req, http.StatusOK)
  47. DecodeJSON(t, resp, &apiComments)
  48. assert.Len(t, apiComments, 1)
  49. assert.EqualValues(t, 2, apiComments[0].ID)
  50. query.Del("before")
  51. query.Add("since", since)
  52. link.RawQuery = query.Encode()
  53. req = NewRequest(t, "GET", link.String())
  54. resp = MakeRequest(t, req, http.StatusOK)
  55. DecodeJSON(t, resp, &apiComments)
  56. assert.Len(t, apiComments, 1)
  57. assert.EqualValues(t, 3, apiComments[0].ID)
  58. }
  59. func TestAPIListIssueComments(t *testing.T) {
  60. defer tests.PrepareTestEnv(t)()
  61. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
  62. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  63. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  64. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  65. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  66. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeReadIssue)
  67. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments?token=%s",
  68. repoOwner.Name, repo.Name, issue.Index, token)
  69. resp := MakeRequest(t, req, http.StatusOK)
  70. var comments []*api.Comment
  71. DecodeJSON(t, resp, &comments)
  72. expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID},
  73. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  74. assert.Len(t, comments, expectedCount)
  75. }
  76. func TestAPICreateComment(t *testing.T) {
  77. defer tests.PrepareTestEnv(t)()
  78. const commentBody = "Comment body"
  79. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
  80. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  81. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  82. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  83. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments?token=%s",
  84. repoOwner.Name, repo.Name, issue.Index, token)
  85. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  86. "body": commentBody,
  87. })
  88. resp := MakeRequest(t, req, http.StatusCreated)
  89. var updatedComment api.Comment
  90. DecodeJSON(t, resp, &updatedComment)
  91. assert.EqualValues(t, commentBody, updatedComment.Body)
  92. unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
  93. }
  94. func TestAPIGetComment(t *testing.T) {
  95. defer tests.PrepareTestEnv(t)()
  96. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
  97. assert.NoError(t, comment.LoadIssue(db.DefaultContext))
  98. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: comment.Issue.RepoID})
  99. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  100. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeReadIssue)
  101. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  102. MakeRequest(t, req, http.StatusOK)
  103. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s", repoOwner.Name, repo.Name, comment.ID, token)
  104. resp := MakeRequest(t, req, http.StatusOK)
  105. var apiComment api.Comment
  106. DecodeJSON(t, resp, &apiComment)
  107. assert.NoError(t, comment.LoadPoster(db.DefaultContext))
  108. expect := convert.ToAPIComment(db.DefaultContext, repo, comment)
  109. assert.Equal(t, expect.ID, apiComment.ID)
  110. assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
  111. assert.Equal(t, expect.Body, apiComment.Body)
  112. assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
  113. }
  114. func TestAPIGetSystemUserComment(t *testing.T) {
  115. defer tests.PrepareTestEnv(t)()
  116. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
  117. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  118. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  119. for _, systemUser := range []*user_model.User{
  120. user_model.NewGhostUser(),
  121. user_model.NewActionsUser(),
  122. } {
  123. body := fmt.Sprintf("Hello %s", systemUser.Name)
  124. comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
  125. Type: issues_model.CommentTypeComment,
  126. Doer: systemUser,
  127. Repo: repo,
  128. Issue: issue,
  129. Content: body,
  130. })
  131. assert.NoError(t, err)
  132. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  133. resp := MakeRequest(t, req, http.StatusOK)
  134. var apiComment api.Comment
  135. DecodeJSON(t, resp, &apiComment)
  136. if assert.NotNil(t, apiComment.Poster) {
  137. if assert.Equal(t, systemUser.ID, apiComment.Poster.ID) {
  138. assert.NoError(t, comment.LoadPoster(db.DefaultContext))
  139. assert.Equal(t, systemUser.Name, apiComment.Poster.UserName)
  140. }
  141. }
  142. assert.Equal(t, body, apiComment.Body)
  143. }
  144. }
  145. func TestAPIEditComment(t *testing.T) {
  146. defer tests.PrepareTestEnv(t)()
  147. const newCommentBody = "This is the new comment body"
  148. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 8},
  149. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  150. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  151. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  152. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  153. t.Run("UnrelatedCommentID", func(t *testing.T) {
  154. // Using the ID of a comment that does not belong to the repository must fail
  155. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
  156. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  157. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  158. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  159. repoOwner.Name, repo.Name, comment.ID, token)
  160. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  161. "body": newCommentBody,
  162. })
  163. MakeRequest(t, req, http.StatusNotFound)
  164. })
  165. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  166. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  167. repoOwner.Name, repo.Name, comment.ID, token)
  168. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  169. "body": newCommentBody,
  170. })
  171. resp := MakeRequest(t, req, http.StatusOK)
  172. var updatedComment api.Comment
  173. DecodeJSON(t, resp, &updatedComment)
  174. assert.EqualValues(t, comment.ID, updatedComment.ID)
  175. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  176. unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  177. }
  178. func TestAPIDeleteComment(t *testing.T) {
  179. defer tests.PrepareTestEnv(t)()
  180. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 8},
  181. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  182. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  183. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  184. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  185. t.Run("UnrelatedCommentID", func(t *testing.T) {
  186. // Using the ID of a comment that does not belong to the repository must fail
  187. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
  188. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  189. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  190. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  191. repoOwner.Name, repo.Name, comment.ID, token)
  192. MakeRequest(t, req, http.StatusNotFound)
  193. })
  194. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  195. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  196. repoOwner.Name, repo.Name, comment.ID, token)
  197. MakeRequest(t, req, http.StatusNoContent)
  198. unittest.AssertNotExistsBean(t, &issues_model.Comment{ID: comment.ID})
  199. }
  200. func TestAPIListIssueTimeline(t *testing.T) {
  201. defer tests.PrepareTestEnv(t)()
  202. // load comment
  203. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
  204. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  205. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  206. // make request
  207. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/timeline",
  208. repoOwner.Name, repo.Name, issue.Index)
  209. resp := MakeRequest(t, req, http.StatusOK)
  210. // check if lens of list returned by API and
  211. // lists extracted directly from DB are the same
  212. var comments []*api.TimelineComment
  213. DecodeJSON(t, resp, &comments)
  214. expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID})
  215. assert.Len(t, comments, expectedCount)
  216. }