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.

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