2017-12-06 04:57:01 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-31 17:20:39 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
package issue
|
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"
|
|
|
|
)
|
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
func addReaction(t *testing.T, doer *user_model.User, issue *issues_model.Issue, comment *issues_model.Comment, 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
|
2024-03-04 16:16:03 +08:00
|
|
|
if comment == nil {
|
|
|
|
reaction, err = CreateIssueReaction(db.DefaultContext, doer, issue, content)
|
2017-12-06 04:57:01 +08:00
|
|
|
} else {
|
2024-03-04 16:16:03 +08:00
|
|
|
reaction, err = CreateCommentReaction(db.DefaultContext, doer, comment, 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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 16:16:03 +08:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 16:16:03 +08:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
reaction, err := CreateIssueReaction(db.DefaultContext, user1, issue, "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
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 16:16:03 +08:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
err := issues_model.DeleteIssueReaction(db.DefaultContext, user1.ID, issue.ID, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
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
|
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-14 10:59:53 +08:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-08-16 10:22:25 +08:00
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
2021-11-24 17:49:20 +08:00
|
|
|
ghost := user_model.NewGhostUser()
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2022-08-16 10:22:25 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
|
|
|
addReaction(t, user2, issue, nil, "heart")
|
|
|
|
addReaction(t, org3, issue, nil, "heart")
|
|
|
|
addReaction(t, org3, issue, nil, "+1")
|
|
|
|
addReaction(t, user4, issue, nil, "+1")
|
|
|
|
addReaction(t, user4, issue, nil, "heart")
|
|
|
|
addReaction(t, ghost, issue, nil, "-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{
|
2024-03-04 16:16:03 +08:00
|
|
|
IssueID: issue.ID,
|
2022-03-31 17:20:39 +08:00
|
|
|
})
|
|
|
|
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())
|
2023-08-11 05:20:01 +08:00
|
|
|
assert.Equal(t, user1.Name+", "+user2.Name, 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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 16:16:03 +08:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, nil, comment, "heart")
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: comment.IssueID, CommentID: comment.ID})
|
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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-14 10:59:53 +08:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-08-16 10:22:25 +08:00
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, nil, comment, "heart")
|
|
|
|
addReaction(t, user2, nil, comment, "heart")
|
|
|
|
addReaction(t, org3, nil, comment, "heart")
|
|
|
|
addReaction(t, user4, nil, comment, "+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{
|
2024-03-04 16:16:03 +08:00
|
|
|
IssueID: comment.IssueID,
|
|
|
|
CommentID: comment.ID,
|
2022-03-31 17:20:39 +08:00
|
|
|
})
|
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
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 16:16:03 +08:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
addReaction(t, user1, nil, comment, "heart")
|
|
|
|
assert.NoError(t, issues_model.DeleteCommentReaction(db.DefaultContext, user1.ID, comment.IssueID, comment.ID, "heart"))
|
2017-12-06 04:57:01 +08:00
|
|
|
|
2024-03-04 16:16:03 +08:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: comment.IssueID, CommentID: comment.ID})
|
2017-12-06 04:57:01 +08:00
|
|
|
}
|