gitea源码

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