summaryrefslogtreecommitdiffstats
path: root/integrations
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2019-12-07 23:04:19 +0100
committertechknowlogick <techknowlogick@gitea.io>2019-12-07 17:04:19 -0500
commit37e10d4543c1e516e1a721d72c0054fefceb9499 (patch)
treef824b014a135d3bcf243d5bb3e87cab3de225c90 /integrations
parentee7df7ba8c5e6a4b32b0c4048d2b535d8df3cbe9 (diff)
downloadgitea-37e10d4543c1e516e1a721d72c0054fefceb9499.tar.gz
gitea-37e10d4543c1e516e1a721d72c0054fefceb9499.zip
[API] Add Reactions (#9220)
* reject reactions wich ar not allowed * dont duble check CreateReaction now throw ErrForbiddenIssueReaction * add /repos/{owner}/{repo}/issues/comments/{id}/reactions endpoint * add Find Functions * fix some swagger stuff + add issue reaction endpoints + GET ReactionList now use FindReactions... * explicite Issue Only Reaction for FindReactionsOptions with "-1" commentID * load issue; load user ... * return error again * swagger def canged after LINT * check if user has ben loaded * add Tests * better way of comparing results * add suggestion * use different issue for test (dont interfear with integration test) * test dont compare Location on timeCompare * TEST: add forbidden dubble add * add comments in code to explain * add settings.UI.ReactionsMap so if !setting.UI.ReactionsMap[opts.Type] works
Diffstat (limited to 'integrations')
-rw-r--r--integrations/api_issue_reaction_test.go145
1 files changed, 145 insertions, 0 deletions
diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go
new file mode 100644
index 0000000000..f3fcf3946f
--- /dev/null
+++ b/integrations/api_issue_reaction_test.go
@@ -0,0 +1,145 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package integrations
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+ "time"
+
+ "code.gitea.io/gitea/models"
+ api "code.gitea.io/gitea/modules/structs"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestAPIIssuesReactions(t *testing.T) {
+ defer prepareTestEnv(t)()
+
+ issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue)
+ _ = issue.LoadRepo()
+ owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
+
+ session := loginUser(t, owner.Name)
+ token := getTokenForLoggedInUser(t, session)
+
+ user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
+ user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+ urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
+ owner.Name, issue.Repo.Name, issue.Index, token)
+
+ //Try to add not allowed reaction
+ req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
+ Reaction: "wrong",
+ })
+ resp := session.MakeRequest(t, req, http.StatusForbidden)
+
+ //Delete not allowed reaction
+ req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
+ Reaction: "zzz",
+ })
+ resp = session.MakeRequest(t, req, http.StatusOK)
+
+ //Add allowed reaction
+ req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
+ Reaction: "rocket",
+ })
+ resp = session.MakeRequest(t, req, http.StatusCreated)
+ var apiNewReaction api.ReactionResponse
+ DecodeJSON(t, resp, &apiNewReaction)
+
+ //Add existing reaction
+ resp = session.MakeRequest(t, req, http.StatusForbidden)
+
+ //Get end result of reaction list of issue #1
+ req = NewRequestf(t, "GET", urlStr)
+ resp = session.MakeRequest(t, req, http.StatusOK)
+ var apiReactions []*api.ReactionResponse
+ DecodeJSON(t, resp, &apiReactions)
+ expectResponse := make(map[int]api.ReactionResponse)
+ expectResponse[0] = api.ReactionResponse{
+ User: user1.APIFormat(),
+ Reaction: "zzz",
+ Created: time.Unix(1573248002, 0),
+ }
+ expectResponse[1] = api.ReactionResponse{
+ User: user2.APIFormat(),
+ Reaction: "eyes",
+ Created: time.Unix(1573248003, 0),
+ }
+ expectResponse[2] = apiNewReaction
+ assert.Len(t, apiReactions, 3)
+ for i, r := range apiReactions {
+ assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
+ assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
+ assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
+ }
+}
+
+func TestAPICommentReactions(t *testing.T) {
+ defer prepareTestEnv(t)()
+
+ comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment)
+ _ = comment.LoadIssue()
+ issue := comment.Issue
+ _ = issue.LoadRepo()
+ owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
+
+ session := loginUser(t, owner.Name)
+ token := getTokenForLoggedInUser(t, session)
+
+ user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
+ user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+ urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
+ owner.Name, issue.Repo.Name, comment.ID, token)
+
+ //Try to add not allowed reaction
+ req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
+ Reaction: "wrong",
+ })
+ resp := session.MakeRequest(t, req, http.StatusForbidden)
+
+ //Delete none existing reaction
+ req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
+ Reaction: "eyes",
+ })
+ resp = session.MakeRequest(t, req, http.StatusOK)
+
+ //Add allowed reaction
+ req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
+ Reaction: "+1",
+ })
+ resp = session.MakeRequest(t, req, http.StatusCreated)
+ var apiNewReaction api.ReactionResponse
+ DecodeJSON(t, resp, &apiNewReaction)
+
+ //Add existing reaction
+ resp = session.MakeRequest(t, req, http.StatusForbidden)
+
+ //Get end result of reaction list of issue #1
+ req = NewRequestf(t, "GET", urlStr)
+ resp = session.MakeRequest(t, req, http.StatusOK)
+ var apiReactions []*api.ReactionResponse
+ DecodeJSON(t, resp, &apiReactions)
+ expectResponse := make(map[int]api.ReactionResponse)
+ expectResponse[0] = api.ReactionResponse{
+ User: user2.APIFormat(),
+ Reaction: "laugh",
+ Created: time.Unix(1573248004, 0),
+ }
+ expectResponse[1] = api.ReactionResponse{
+ User: user1.APIFormat(),
+ Reaction: "laugh",
+ Created: time.Unix(1573248005, 0),
+ }
+ expectResponse[2] = apiNewReaction
+ assert.Len(t, apiReactions, 3)
+ for i, r := range apiReactions {
+ assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
+ assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
+ assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
+ }
+}