gitea源码

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