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_issue_reaction_test.go 4.6KB

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