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