2017-12-05 13:57:01 -07:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-31 03:20:39 -06:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
package issue
|
2017-12-05 13:57:01 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 03:37:59 -06:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-12-05 13:57:01 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
func addReaction(t *testing.T, doer *user_model.User, issue *issues_model.Issue, comment *issues_model.Comment, content string) {
|
2022-06-13 03:37:59 -06:00
|
|
|
var reaction *issues_model.Reaction
|
2017-12-05 13:57:01 -07:00
|
|
|
var err error
|
2024-03-04 01:16:03 -07:00
|
|
|
if comment == nil {
|
|
|
|
reaction, err = CreateIssueReaction(db.DefaultContext, doer, issue, content)
|
2017-12-05 13:57:01 -07:00
|
|
|
} else {
|
2024-03-04 01:16:03 -07:00
|
|
|
reaction, err = CreateCommentReaction(db.DefaultContext, doer, comment, content)
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, reaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddReaction(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 01:16:03 -07:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueAddDuplicateReaction(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 01:16:03 -07:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
reaction, err := CreateIssueReaction(db.DefaultContext, user1, issue, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.Equal(t, issues_model.ErrReactionAlreadyExist{Reaction: "heart"}, err)
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
2019-12-31 01:21:21 -07:00
|
|
|
assert.Equal(t, existingR.ID, reaction.ID)
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueDeleteReaction(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 01:16:03 -07:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
addReaction(t, user1, issue, nil, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
err := issues_model.DeleteIssueReaction(db.DefaultContext, user1.ID, issue.ID, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue.ID})
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueReactionCount(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
|
|
|
setting.UI.ReactionMaxUserNum = 2
|
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 20:59:53 -06:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-08-15 20:22:25 -06:00
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
2021-11-24 02:49:20 -07:00
|
|
|
ghost := user_model.NewGhostUser()
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2022-08-15 20:22:25 -06:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07: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-07 15:04:19 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
|
2024-03-04 01:16:03 -07:00
|
|
|
IssueID: issue.ID,
|
2022-03-31 03:20:39 -06:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, reactionsList, 7)
|
|
|
|
_, err = reactionsList.LoadUsers(db.DefaultContext, repo)
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-31 03:20:39 -06:00
|
|
|
reactions := reactionsList.GroupByType()
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.Len(t, reactions["heart"], 4)
|
|
|
|
assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
|
2023-08-10 15:20:01 -06:00
|
|
|
assert.Equal(t, user1.Name+", "+user2.Name, reactions["heart"].GetFirstUsers())
|
2017-12-05 13:57:01 -07: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 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 01:16:03 -07:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
addReaction(t, user1, nil, comment, "heart")
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: comment.IssueID, CommentID: comment.ID})
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentDeleteReaction(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 20:59:53 -06:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-08-15 20:22:25 -06:00
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07: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-05 13:57:01 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
|
2024-03-04 01:16:03 -07:00
|
|
|
IssueID: comment.IssueID,
|
|
|
|
CommentID: comment.ID,
|
2022-03-31 03:20:39 -06:00
|
|
|
})
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.NoError(t, err)
|
2022-03-31 03:20:39 -06:00
|
|
|
assert.Len(t, reactionsList, 4)
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-03-31 03:20:39 -06:00
|
|
|
reactions := reactionsList.GroupByType()
|
2019-12-31 01:21:21 -07:00
|
|
|
assert.Len(t, reactions["heart"], 3)
|
2017-12-05 13:57:01 -07:00
|
|
|
assert.Len(t, reactions["+1"], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssueCommentReactionCount(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2024-03-04 01:16:03 -07:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
addReaction(t, user1, nil, comment, "heart")
|
|
|
|
assert.NoError(t, issues_model.DeleteCommentReaction(db.DefaultContext, user1.ID, comment.IssueID, comment.ID, "heart"))
|
2017-12-05 13:57:01 -07:00
|
|
|
|
2024-03-04 01:16:03 -07:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: comment.IssueID, CommentID: comment.ID})
|
2017-12-05 13:57:01 -07:00
|
|
|
}
|