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.

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