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.

153 lines
4.8 KiB

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