You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_issue_reaction_test.go 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAPIAllowedReactions(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. type allowed []string
  18. a := new(allowed)
  19. req := NewRequest(t, "GET", "/api/v1/settings/allowed_reactions")
  20. resp := MakeRequest(t, req, http.StatusOK)
  21. DecodeJSON(t, resp, &a)
  22. assert.Len(t, *a, len(setting.UI.Reactions))
  23. assert.ElementsMatch(t, setting.UI.Reactions, *a)
  24. }
  25. func TestAPIIssuesReactions(t *testing.T) {
  26. defer prepareTestEnv(t)()
  27. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue)
  28. _ = issue.LoadRepo()
  29. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
  30. session := loginUser(t, owner.Name)
  31. token := getTokenForLoggedInUser(t, session)
  32. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  33. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
  34. owner.Name, issue.Repo.Name, issue.Index, token)
  35. //Try to add not allowed reaction
  36. req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  37. Reaction: "wrong",
  38. })
  39. resp := session.MakeRequest(t, req, http.StatusForbidden)
  40. //Delete not allowed reaction
  41. req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
  42. Reaction: "zzz",
  43. })
  44. resp = session.MakeRequest(t, req, http.StatusOK)
  45. //Add allowed reaction
  46. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  47. Reaction: "rocket",
  48. })
  49. resp = session.MakeRequest(t, req, http.StatusCreated)
  50. var apiNewReaction api.Reaction
  51. DecodeJSON(t, resp, &apiNewReaction)
  52. //Add existing reaction
  53. resp = session.MakeRequest(t, req, http.StatusForbidden)
  54. //Get end result of reaction list of issue #1
  55. req = NewRequestf(t, "GET", urlStr)
  56. resp = session.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: user2.APIFormat(),
  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 prepareTestEnv(t)()
  75. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment)
  76. _ = comment.LoadIssue()
  77. issue := comment.Issue
  78. _ = issue.LoadRepo()
  79. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
  80. session := loginUser(t, owner.Name)
  81. token := getTokenForLoggedInUser(t, session)
  82. user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  83. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  84. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
  85. owner.Name, issue.Repo.Name, comment.ID, token)
  86. //Try to add not allowed reaction
  87. req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  88. Reaction: "wrong",
  89. })
  90. resp := session.MakeRequest(t, req, http.StatusForbidden)
  91. //Delete none existing reaction
  92. req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
  93. Reaction: "eyes",
  94. })
  95. resp = session.MakeRequest(t, req, http.StatusOK)
  96. //Add allowed reaction
  97. req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
  98. Reaction: "+1",
  99. })
  100. resp = session.MakeRequest(t, req, http.StatusCreated)
  101. var apiNewReaction api.Reaction
  102. DecodeJSON(t, resp, &apiNewReaction)
  103. //Add existing reaction
  104. resp = session.MakeRequest(t, req, http.StatusForbidden)
  105. //Get end result of reaction list of issue #1
  106. req = NewRequestf(t, "GET", urlStr)
  107. resp = session.MakeRequest(t, req, http.StatusOK)
  108. var apiReactions []*api.Reaction
  109. DecodeJSON(t, resp, &apiReactions)
  110. expectResponse := make(map[int]api.Reaction)
  111. expectResponse[0] = api.Reaction{
  112. User: user2.APIFormat(),
  113. Reaction: "laugh",
  114. Created: time.Unix(1573248004, 0),
  115. }
  116. expectResponse[1] = api.Reaction{
  117. User: user1.APIFormat(),
  118. Reaction: "laugh",
  119. Created: time.Unix(1573248005, 0),
  120. }
  121. expectResponse[2] = apiNewReaction
  122. assert.Len(t, apiReactions, 3)
  123. for i, r := range apiReactions {
  124. assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
  125. assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
  126. assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
  127. }
  128. }