2019-12-08 07:04:19 +09:00
|
|
|
// 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"
|
2020-06-04 18:16:53 +09:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-08 07:04:19 +09:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-06-04 18:16:53 +09:00
|
|
|
func TestAPIAllowedReactions(t *testing.T) {
|
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
|
2020-06-23 03:21:31 +09:00
|
|
|
a := new(api.GeneralUISettings)
|
2020-06-04 18:16:53 +09:00
|
|
|
|
2020-06-23 03:21:31 +09:00
|
|
|
req := NewRequest(t, "GET", "/api/v1/settings/ui")
|
2020-06-04 18:16:53 +09:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
DecodeJSON(t, resp, &a)
|
2020-06-23 03:21:31 +09:00
|
|
|
assert.Len(t, a.AllowedReactions, len(setting.UI.Reactions))
|
|
|
|
assert.ElementsMatch(t, setting.UI.Reactions, a.AllowedReactions)
|
2020-06-04 18:16:53 +09:00
|
|
|
}
|
|
|
|
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
2019-12-31 17:21:21 +09:00
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
2019-12-31 17:21:21 +09:00
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 17:21:21 +09:00
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
2019-12-08 07:04:19 +09:00
|
|
|
User: user2.APIFormat(),
|
|
|
|
Reaction: "eyes",
|
|
|
|
Created: time.Unix(1573248003, 0),
|
|
|
|
}
|
2019-12-18 22:07:36 +09:00
|
|
|
expectResponse[1] = apiNewReaction
|
|
|
|
assert.Len(t, apiReactions, 2)
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
2019-12-31 17:21:21 +09:00
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
2019-12-31 17:21:21 +09:00
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 17:21:21 +09:00
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
2019-12-08 07:04:19 +09:00
|
|
|
User: user2.APIFormat(),
|
|
|
|
Reaction: "laugh",
|
|
|
|
Created: time.Unix(1573248004, 0),
|
|
|
|
}
|
2019-12-31 17:21:21 +09:00
|
|
|
expectResponse[1] = api.Reaction{
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|