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.
|
|
|
|
|
2022-09-03 04:18:23 +09:00
|
|
|
package integration
|
2019-12-08 07:04:19 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-04-08 18:11:15 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 18:37:59 +09:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 17:53:21 +09:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-10-17 13:23:08 +09:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-12-08 07:04:19 +09:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-03 04:18:23 +09:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-12-08 07:04:19 +09:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIIssuesReactions(t *testing.T) {
|
2022-09-03 04:18:23 +09:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2022-04-08 18:11:15 +09:00
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 11:22:25 +09:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 07:04:19 +09:00
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 07:04:19 +09:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, issue.Index, token)
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Try to add not allowed reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Delete not allowed reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "zzz",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Add allowed reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "rocket",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
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)
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Add existing reaction
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 07:04:19 +09:00
|
|
|
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{
|
2021-03-28 01:45:26 +09:00
|
|
|
User: convert.ToUser(user2, user2),
|
2019-12-08 07:04:19 +09:00
|
|
|
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) {
|
2022-09-03 04:18:23 +09:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
|
2019-12-08 07:04:19 +09:00
|
|
|
_ = comment.LoadIssue()
|
|
|
|
issue := comment.Issue
|
2022-04-08 18:11:15 +09:00
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 11:22:25 +09:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 07:04:19 +09:00
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 07:04:19 +09:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, comment.ID, token)
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Try to add not allowed reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Delete none existing reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "eyes",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Add allowed reaction
|
2019-12-08 07:04:19 +09:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "+1",
|
|
|
|
})
|
2021-11-18 10:33:06 +09:00
|
|
|
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)
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Add existing reaction
|
2021-11-18 10:33:06 +09:00
|
|
|
session.MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 07:04:19 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 07:04:19 +09:00
|
|
|
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{
|
2021-03-28 01:45:26 +09:00
|
|
|
User: convert.ToUser(user2, user2),
|
2019-12-08 07:04:19 +09:00
|
|
|
Reaction: "laugh",
|
|
|
|
Created: time.Unix(1573248004, 0),
|
|
|
|
}
|
2019-12-31 17:21:21 +09:00
|
|
|
expectResponse[1] = api.Reaction{
|
2021-03-28 01:45:26 +09:00
|
|
|
User: convert.ToUser(user1, user1),
|
2019-12-08 07:04:19 +09:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|