2017-12-05 23:57:01 +03: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.
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 14:49:59 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-12-05 23:57:01 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
func addReaction(t *testing.T, doer *user_model.User, issue *Issue, comment *Comment, content string) {
|
2017-12-05 23:57:01 +03:00
|
|
|
var reaction *Reaction
|
|
|
|
var err error
|
|
|
|
if comment == nil {
|
|
|
|
reaction, err = CreateIssueReaction(doer, issue, content)
|
|
|
|
} else {
|
|
|
|
reaction, err = CreateCommentReaction(doer, issue, comment, content)
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, reaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddReaction(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart")
|
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
|
2017-12-05 23:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddDuplicateReaction(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart")
|
|
|
|
|
|
|
|
reaction, err := CreateReaction(&ReactionOptions{
|
|
|
|
Doer: user1,
|
|
|
|
Issue: issue1,
|
|
|
|
Type: "heart",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2019-12-31 11:21:21 +03:00
|
|
|
assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction)
|
2019-12-31 11:21:21 +03:00
|
|
|
assert.Equal(t, existingR.ID, reaction.ID)
|
2017-12-05 23:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueDeleteReaction(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart")
|
|
|
|
|
|
|
|
err := DeleteIssueReaction(user1, issue1, "heart")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
|
2017-12-05 23:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueReactionCount(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
setting.UI.ReactionMaxUserNum = 2
|
|
|
|
|
2021-11-24 12:49:20 +03: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-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2019-12-08 01:04:19 +03:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
|
|
|
addReaction(t, user2, issue, nil, "heart")
|
|
|
|
addReaction(t, user3, issue, nil, "heart")
|
|
|
|
addReaction(t, user3, issue, nil, "+1")
|
|
|
|
addReaction(t, user4, issue, nil, "+1")
|
|
|
|
addReaction(t, user4, issue, nil, "heart")
|
|
|
|
addReaction(t, ghost, issue, nil, "-1")
|
|
|
|
|
2021-12-10 04:27:50 +03:00
|
|
|
err := issue.loadReactions(db.DefaultContext)
|
2017-12-05 23:57:01 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-12-08 01:04:19 +03:00
|
|
|
assert.Len(t, issue.Reactions, 7)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2019-12-08 01:04:19 +03:00
|
|
|
reactions := issue.Reactions.GroupByType()
|
2017-12-05 23:57:01 +03:00
|
|
|
assert.Len(t, reactions["heart"], 4)
|
|
|
|
assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
|
2019-01-24 17:12:17 +03:00
|
|
|
assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), reactions["heart"].GetFirstUsers())
|
2017-12-05 23:57:01 +03: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 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart")
|
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
|
2017-12-05 23:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentDeleteReaction(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03: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-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2021-12-10 04:27:50 +03:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue1.RepoID}).(*repo_model.Repository)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart")
|
|
|
|
addReaction(t, user2, issue1, comment1, "heart")
|
|
|
|
addReaction(t, user3, issue1, comment1, "heart")
|
|
|
|
addReaction(t, user4, issue1, comment1, "+1")
|
|
|
|
|
2020-01-15 14:14:07 +03:00
|
|
|
err := comment1.LoadReactions(repo1)
|
2017-12-05 23:57:01 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-31 11:21:21 +03:00
|
|
|
assert.Len(t, comment1.Reactions, 4)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
reactions := comment1.Reactions.GroupByType()
|
2019-12-31 11:21:21 +03:00
|
|
|
assert.Len(t, reactions["heart"], 3)
|
2017-12-05 23:57:01 +03:00
|
|
|
assert.Len(t, reactions["+1"], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentReactionCount(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
|
2017-12-05 23:57:01 +03:00
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart")
|
2019-12-31 11:21:21 +03:00
|
|
|
assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart"))
|
2017-12-05 23:57:01 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
|
2017-12-05 23:57:01 +03:00
|
|
|
}
|