2017-12-06 04:57:01 +08:00
|
|
|
// Copyright 2017 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-03-31 17:20:39 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
package issues_test
|
2017-12-06 04:57:01 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-12-06 04:57:01 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
func addReaction(t *testing.T, doerID, issueID, commentID int64, content string) {
|
2022-06-13 17:37:59 +08:00
|
|
|
var reaction *issues_model.Reaction
|
2017-12-06 04:57:01 +08:00
|
|
|
var err error
|
2022-03-31 17:20:39 +08:00
|
|
|
if commentID == 0 {
|
2022-06-13 17:37:59 +08:00
|
|
|
reaction, err = issues_model.CreateIssueReaction(doerID, issueID, content)
|
2017-12-06 04:57:01 +08:00
|
|
|
} else {
|
2022-06-13 17:37:59 +08:00
|
|
|
reaction, err = issues_model.CreateCommentReaction(doerID, issueID, commentID, content)
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, reaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddReaction(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID})
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddDuplicateReaction(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
reaction, err := issues_model.CreateReaction(&issues_model.ReactionOptions{
|
2022-03-31 17:20:39 +08:00
|
|
|
DoerID: user1.ID,
|
|
|
|
IssueID: issue1ID,
|
|
|
|
Type: "heart",
|
2017-12-06 04:57:01 +08:00
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-06-13 17:37:59 +08:00
|
|
|
assert.Equal(t, issues_model.ErrReactionAlreadyExist{Reaction: "heart"}, err)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID}).(*issues_model.Reaction)
|
2019-12-31 16:21:21 +08:00
|
|
|
assert.Equal(t, existingR.ID, reaction.ID)
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueDeleteReaction(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
err := issues_model.DeleteIssueReaction(user1.ID, issue1ID, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID})
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueReactionCount(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
|
|
|
setting.UI.ReactionMaxUserNum = 2
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
|
|
|
ghost := user_model.NewGhostUser()
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issueID int64 = 2
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issueID, 0, "heart")
|
|
|
|
addReaction(t, user2.ID, issueID, 0, "heart")
|
|
|
|
addReaction(t, user3.ID, issueID, 0, "heart")
|
|
|
|
addReaction(t, user3.ID, issueID, 0, "+1")
|
|
|
|
addReaction(t, user4.ID, issueID, 0, "+1")
|
|
|
|
addReaction(t, user4.ID, issueID, 0, "heart")
|
|
|
|
addReaction(t, ghost.ID, issueID, 0, "-1")
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
|
2022-03-31 17:20:39 +08:00
|
|
|
IssueID: issueID,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reactionsList, 7)
|
|
|
|
_, err = reactionsList.LoadUsers(db.DefaultContext, repo)
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
reactions := reactionsList.GroupByType()
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.Len(t, reactions["heart"], 4)
|
|
|
|
assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
|
2019-01-24 22:12:17 +08:00
|
|
|
assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), reactions["heart"].GetFirstUsers())
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.True(t, reactions["heart"].HasUser(1))
|
|
|
|
assert.False(t, reactions["heart"].HasUser(5))
|
|
|
|
assert.False(t, reactions["heart"].HasUser(0))
|
|
|
|
assert.Len(t, reactions["+1"], 2)
|
|
|
|
assert.Equal(t, 0, reactions["+1"].GetMoreUserCount())
|
|
|
|
assert.Len(t, reactions["-1"], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentAddReaction(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
|
|
|
var comment1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID})
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentDeleteReaction(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
|
|
|
var comment1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
|
|
|
|
addReaction(t, user2.ID, issue1ID, comment1ID, "heart")
|
|
|
|
addReaction(t, user3.ID, issue1ID, comment1ID, "heart")
|
|
|
|
addReaction(t, user4.ID, issue1ID, comment1ID, "+1")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
|
2022-03-31 17:20:39 +08:00
|
|
|
IssueID: issue1ID,
|
|
|
|
CommentID: comment1ID,
|
|
|
|
})
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.NoError(t, err)
|
2022-03-31 17:20:39 +08:00
|
|
|
assert.Len(t, reactionsList, 4)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
reactions := reactionsList.GroupByType()
|
2019-12-31 16:21:21 +08:00
|
|
|
assert.Len(t, reactions["heart"], 3)
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.Len(t, reactions["+1"], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentReactionCount(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
var issue1ID int64 = 1
|
|
|
|
var comment1ID int64 = 1
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-03-31 17:20:39 +08:00
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
|
2022-06-13 17:37:59 +08:00
|
|
|
assert.NoError(t, issues_model.DeleteCommentReaction(user1.ID, issue1ID, comment1ID, "heart"))
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID})
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|