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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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", repoOwner.Name, repo.Name, issue.Index).
  68. AddTokenAuth(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",
  84. repoOwner.Name, repo.Name, issue.Index)
  85. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  86. "body": commentBody,
  87. }).AddTokenAuth(token)
  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. t.Run("BlockedByRepoOwner", func(t *testing.T) {
  94. defer tests.PrintCurrentTest(t)()
  95. user34 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 34})
  96. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
  97. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  98. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", repo.OwnerName, repo.Name, issue.Index), map[string]string{
  99. "body": commentBody,
  100. }).AddTokenAuth(getUserToken(t, user34.Name, auth_model.AccessTokenScopeWriteRepository))
  101. MakeRequest(t, req, http.StatusForbidden)
  102. })
  103. t.Run("BlockedByIssuePoster", func(t *testing.T) {
  104. defer tests.PrintCurrentTest(t)()
  105. user34 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 34})
  106. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 13})
  107. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  108. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", repo.OwnerName, repo.Name, issue.Index), map[string]string{
  109. "body": commentBody,
  110. }).AddTokenAuth(getUserToken(t, user34.Name, auth_model.AccessTokenScopeWriteRepository))
  111. MakeRequest(t, req, http.StatusForbidden)
  112. })
  113. }
  114. func TestAPIGetComment(t *testing.T) {
  115. defer tests.PrepareTestEnv(t)()
  116. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
  117. assert.NoError(t, comment.LoadIssue(db.DefaultContext))
  118. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: comment.Issue.RepoID})
  119. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  120. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeReadIssue)
  121. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  122. MakeRequest(t, req, http.StatusOK)
  123. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID).
  124. AddTokenAuth(token)
  125. resp := MakeRequest(t, req, http.StatusOK)
  126. var apiComment api.Comment
  127. DecodeJSON(t, resp, &apiComment)
  128. assert.NoError(t, comment.LoadPoster(db.DefaultContext))
  129. expect := convert.ToAPIComment(db.DefaultContext, repo, comment)
  130. assert.Equal(t, expect.ID, apiComment.ID)
  131. assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
  132. assert.Equal(t, expect.Body, apiComment.Body)
  133. assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
  134. }
  135. func TestAPIGetSystemUserComment(t *testing.T) {
  136. defer tests.PrepareTestEnv(t)()
  137. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
  138. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  139. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  140. for _, systemUser := range []*user_model.User{
  141. user_model.NewGhostUser(),
  142. user_model.NewActionsUser(),
  143. } {
  144. body := fmt.Sprintf("Hello %s", systemUser.Name)
  145. comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
  146. Type: issues_model.CommentTypeComment,
  147. Doer: systemUser,
  148. Repo: repo,
  149. Issue: issue,
  150. Content: body,
  151. })
  152. assert.NoError(t, err)
  153. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  154. resp := MakeRequest(t, req, http.StatusOK)
  155. var apiComment api.Comment
  156. DecodeJSON(t, resp, &apiComment)
  157. if assert.NotNil(t, apiComment.Poster) {
  158. if assert.Equal(t, systemUser.ID, apiComment.Poster.ID) {
  159. assert.NoError(t, comment.LoadPoster(db.DefaultContext))
  160. assert.Equal(t, systemUser.Name, apiComment.Poster.UserName)
  161. }
  162. }
  163. assert.Equal(t, body, apiComment.Body)
  164. }
  165. }
  166. func TestAPIEditComment(t *testing.T) {
  167. defer tests.PrepareTestEnv(t)()
  168. const newCommentBody = "This is the new comment body"
  169. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 8},
  170. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  171. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  172. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  173. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  174. t.Run("UnrelatedCommentID", func(t *testing.T) {
  175. // Using the ID of a comment that does not belong to the repository must fail
  176. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
  177. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  178. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  179. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d",
  180. repoOwner.Name, repo.Name, comment.ID)
  181. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  182. "body": newCommentBody,
  183. }).AddTokenAuth(token)
  184. MakeRequest(t, req, http.StatusNotFound)
  185. })
  186. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  187. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d",
  188. repoOwner.Name, repo.Name, comment.ID)
  189. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  190. "body": newCommentBody,
  191. }).AddTokenAuth(token)
  192. resp := MakeRequest(t, req, http.StatusOK)
  193. var updatedComment api.Comment
  194. DecodeJSON(t, resp, &updatedComment)
  195. assert.EqualValues(t, comment.ID, updatedComment.ID)
  196. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  197. unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  198. }
  199. func TestAPIDeleteComment(t *testing.T) {
  200. defer tests.PrepareTestEnv(t)()
  201. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 8},
  202. unittest.Cond("type = ?", issues_model.CommentTypeComment))
  203. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
  204. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  205. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  206. t.Run("UnrelatedCommentID", func(t *testing.T) {
  207. // Using the ID of a comment that does not belong to the repository must fail
  208. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
  209. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  210. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  211. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID).
  212. AddTokenAuth(token)
  213. MakeRequest(t, req, http.StatusNotFound)
  214. })
  215. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  216. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID).
  217. AddTokenAuth(token)
  218. MakeRequest(t, req, http.StatusNoContent)
  219. unittest.AssertNotExistsBean(t, &issues_model.Comment{ID: comment.ID})
  220. }
  221. func TestAPIListIssueTimeline(t *testing.T) {
  222. defer tests.PrepareTestEnv(t)()
  223. // load comment
  224. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
  225. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  226. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  227. // make request
  228. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/timeline", repoOwner.Name, repo.Name, issue.Index)
  229. resp := MakeRequest(t, req, http.StatusOK)
  230. // check if lens of list returned by API and
  231. // lists extracted directly from DB are the same
  232. var comments []*api.TimelineComment
  233. DecodeJSON(t, resp, &comments)
  234. expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID})
  235. assert.Len(t, comments, expectedCount)
  236. }