選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

api_issue_reaction_test.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. "time"
  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 TestAPIIssuesReactions(t *testing.T) {
  21. defer tests.PrepareTestEnv(t)()
  22. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
  23. _ = issue.LoadRepo(db.DefaultContext)
  24. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
  25. session := loginUser(t, owner.Name)
  26. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  27. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  28. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions", owner.Name, issue.Repo.Name, issue.Index)
  29. // Try to add not allowed reaction
  30. req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  31. Reaction: "wrong",
  32. }).AddTokenAuth(token)
  33. MakeRequest(t, req, http.StatusForbidden)
  34. // Delete not allowed reaction
  35. req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
  36. Reaction: "zzz",
  37. }).AddTokenAuth(token)
  38. MakeRequest(t, req, http.StatusOK)
  39. // Add allowed reaction
  40. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  41. Reaction: "rocket",
  42. }).AddTokenAuth(token)
  43. resp := MakeRequest(t, req, http.StatusCreated)
  44. var apiNewReaction api.Reaction
  45. DecodeJSON(t, resp, &apiNewReaction)
  46. // Add existing reaction
  47. MakeRequest(t, req, http.StatusForbidden)
  48. // Blocked user can't react to comment
  49. user34 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 34})
  50. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  51. Reaction: "rocket",
  52. }).AddTokenAuth(getUserToken(t, user34.Name, auth_model.AccessTokenScopeWriteIssue))
  53. MakeRequest(t, req, http.StatusForbidden)
  54. // Get end result of reaction list of issue #1
  55. req = NewRequest(t, "GET", urlStr).
  56. AddTokenAuth(token)
  57. resp = MakeRequest(t, req, http.StatusOK)
  58. var apiReactions []*api.Reaction
  59. DecodeJSON(t, resp, &apiReactions)
  60. expectResponse := make(map[int]api.Reaction)
  61. expectResponse[0] = api.Reaction{
  62. User: convert.ToUser(db.DefaultContext, user2, user2),
  63. Reaction: "eyes",
  64. Created: time.Unix(1573248003, 0),
  65. }
  66. expectResponse[1] = apiNewReaction
  67. assert.Len(t, apiReactions, 2)
  68. for i, r := range apiReactions {
  69. assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
  70. assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
  71. assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
  72. }
  73. }
  74. func TestAPICommentReactions(t *testing.T) {
  75. defer tests.PrepareTestEnv(t)()
  76. comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
  77. _ = comment.LoadIssue(db.DefaultContext)
  78. issue := comment.Issue
  79. _ = issue.LoadRepo(db.DefaultContext)
  80. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
  81. session := loginUser(t, owner.Name)
  82. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  83. user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
  84. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  85. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions", owner.Name, issue.Repo.Name, comment.ID)
  86. // Try to add not allowed reaction
  87. req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  88. Reaction: "wrong",
  89. }).AddTokenAuth(token)
  90. MakeRequest(t, req, http.StatusForbidden)
  91. // Delete none existing reaction
  92. req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
  93. Reaction: "eyes",
  94. }).AddTokenAuth(token)
  95. MakeRequest(t, req, http.StatusOK)
  96. t.Run("UnrelatedCommentID", func(t *testing.T) {
  97. // Using the ID of a comment that does not belong to the repository must fail
  98. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
  99. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  100. token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
  101. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions", repoOwner.Name, repo.Name, comment.ID)
  102. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  103. Reaction: "+1",
  104. }).AddTokenAuth(token)
  105. MakeRequest(t, req, http.StatusNotFound)
  106. req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
  107. Reaction: "+1",
  108. }).AddTokenAuth(token)
  109. MakeRequest(t, req, http.StatusNotFound)
  110. req = NewRequest(t, "GET", urlStr).
  111. AddTokenAuth(token)
  112. MakeRequest(t, req, http.StatusNotFound)
  113. })
  114. // Add allowed reaction
  115. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  116. Reaction: "+1",
  117. }).AddTokenAuth(token)
  118. resp := MakeRequest(t, req, http.StatusCreated)
  119. var apiNewReaction api.Reaction
  120. DecodeJSON(t, resp, &apiNewReaction)
  121. // Add existing reaction
  122. MakeRequest(t, req, http.StatusForbidden)
  123. // Get end result of reaction list of issue #1
  124. req = NewRequest(t, "GET", urlStr).
  125. AddTokenAuth(token)
  126. resp = MakeRequest(t, req, http.StatusOK)
  127. var apiReactions []*api.Reaction
  128. DecodeJSON(t, resp, &apiReactions)
  129. expectResponse := make(map[int]api.Reaction)
  130. expectResponse[0] = api.Reaction{
  131. User: convert.ToUser(db.DefaultContext, user2, user2),
  132. Reaction: "laugh",
  133. Created: time.Unix(1573248004, 0),
  134. }
  135. expectResponse[1] = api.Reaction{
  136. User: convert.ToUser(db.DefaultContext, user1, user1),
  137. Reaction: "laugh",
  138. Created: time.Unix(1573248005, 0),
  139. }
  140. expectResponse[2] = apiNewReaction
  141. assert.Len(t, apiReactions, 3)
  142. for i, r := range apiReactions {
  143. assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
  144. assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
  145. assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
  146. }
  147. }