Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

api_issue_reaction_test.go 5.5KB

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