2017-12-05 23:57:01 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-03-31 12:20:39 +03:00
2022-06-13 12:37:59 +03:00
package issues_test
2017-12-05 23:57:01 +03:00
import (
"testing"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2022-06-13 12:37:59 +03:00
issues_model "code.gitea.io/gitea/models/issues"
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"
)
2022-03-31 12:20:39 +03:00
func addReaction ( t * testing . T , doerID , issueID , commentID int64 , content string ) {
2022-06-13 12:37:59 +03:00
var reaction * issues_model . Reaction
2017-12-05 23:57:01 +03:00
var err error
2022-03-31 12:20:39 +03:00
if commentID == 0 {
2023-09-25 16:17:37 +03:00
reaction , err = issues_model . CreateIssueReaction ( db . DefaultContext , doerID , issueID , content )
2017-12-05 23:57:01 +03:00
} else {
2023-09-25 16:17:37 +03:00
reaction , err = issues_model . CreateCommentReaction ( db . DefaultContext , doerID , issueID , commentID , content )
2017-12-05 23:57:01 +03:00
}
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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , 0 , "heart" )
2017-12-05 23:57:01 +03:00
2022-06-13 12:37:59 +03:00
unittest . AssertExistsAndLoadBean ( t , & issues_model . Reaction { Type : "heart" , UserID : user1 . ID , IssueID : issue1ID } )
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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , 0 , "heart" )
2017-12-05 23:57:01 +03:00
2023-09-25 16:17:37 +03:00
reaction , err := issues_model . CreateReaction ( db . DefaultContext , & issues_model . ReactionOptions {
2022-03-31 12:20:39 +03:00
DoerID : user1 . ID ,
IssueID : issue1ID ,
Type : "heart" ,
2017-12-05 23:57:01 +03:00
} )
assert . Error ( t , err )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . ErrReactionAlreadyExist { Reaction : "heart" } , err )
2017-12-05 23:57:01 +03:00
2022-08-16 05:22:25 +03:00
existingR := unittest . AssertExistsAndLoadBean ( t , & issues_model . Reaction { Type : "heart" , UserID : user1 . ID , IssueID : issue1ID } )
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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , 0 , "heart" )
2017-12-05 23:57:01 +03:00
2023-09-25 16:17:37 +03:00
err := issues_model . DeleteIssueReaction ( db . DefaultContext , user1 . ID , issue1ID , "heart" )
2017-12-05 23:57:01 +03:00
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
unittest . AssertNotExistsBean ( t , & issues_model . Reaction { Type : "heart" , UserID : user1 . ID , IssueID : issue1ID } )
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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
user2 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } )
2022-08-16 05:22:25 +03:00
user4 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 4 } )
2021-11-24 12:49:20 +03:00
ghost := user_model . NewGhostUser ( )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issueID int64 = 2
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issueID , 0 , "heart" )
addReaction ( t , user2 . ID , issueID , 0 , "heart" )
2023-09-14 05:59:53 +03:00
addReaction ( t , org3 . ID , issueID , 0 , "heart" )
addReaction ( t , org3 . ID , issueID , 0 , "+1" )
2022-03-31 12:20:39 +03:00
addReaction ( t , user4 . ID , issueID , 0 , "+1" )
addReaction ( t , user4 . ID , issueID , 0 , "heart" )
addReaction ( t , ghost . ID , issueID , 0 , "-1" )
2019-12-08 01:04:19 +03:00
2022-06-13 12:37:59 +03:00
reactionsList , _ , err := issues_model . FindReactions ( db . DefaultContext , issues_model . FindReactionsOptions {
2022-03-31 12:20:39 +03:00
IssueID : issueID ,
} )
assert . NoError ( t , err )
assert . Len ( t , reactionsList , 7 )
_ , err = reactionsList . LoadUsers ( db . DefaultContext , repo )
2017-12-05 23:57:01 +03:00
assert . NoError ( t , err )
2022-03-31 12:20:39 +03:00
reactions := reactionsList . GroupByType ( )
2017-12-05 23:57:01 +03:00
assert . Len ( t , reactions [ "heart" ] , 4 )
assert . Equal ( t , 2 , reactions [ "heart" ] . GetMoreUserCount ( ) )
2023-08-11 00:20:01 +03:00
assert . Equal ( t , user1 . Name + ", " + user2 . Name , 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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
var comment1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , comment1ID , "heart" )
2017-12-05 23:57:01 +03:00
2022-06-13 12:37:59 +03:00
unittest . AssertExistsAndLoadBean ( t , & issues_model . Reaction { Type : "heart" , UserID : user1 . ID , IssueID : issue1ID , CommentID : comment1ID } )
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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
user2 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } )
2022-08-16 05:22:25 +03:00
user4 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 4 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
var comment1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , comment1ID , "heart" )
addReaction ( t , user2 . ID , issue1ID , comment1ID , "heart" )
2023-09-14 05:59:53 +03:00
addReaction ( t , org3 . ID , issue1ID , comment1ID , "heart" )
2022-03-31 12:20:39 +03:00
addReaction ( t , user4 . ID , issue1ID , comment1ID , "+1" )
2017-12-05 23:57:01 +03:00
2022-06-13 12:37:59 +03:00
reactionsList , _ , err := issues_model . FindReactions ( db . DefaultContext , issues_model . FindReactionsOptions {
2022-03-31 12:20:39 +03:00
IssueID : issue1ID ,
CommentID : comment1ID ,
} )
2017-12-05 23:57:01 +03:00
assert . NoError ( t , err )
2022-03-31 12:20:39 +03:00
assert . Len ( t , reactionsList , 4 )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
reactions := reactionsList . 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
2022-08-16 05:22:25 +03:00
user1 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
var issue1ID int64 = 1
var comment1ID int64 = 1
2017-12-05 23:57:01 +03:00
2022-03-31 12:20:39 +03:00
addReaction ( t , user1 . ID , issue1ID , comment1ID , "heart" )
2023-09-25 16:17:37 +03:00
assert . NoError ( t , issues_model . DeleteCommentReaction ( db . DefaultContext , user1 . ID , issue1ID , comment1ID , "heart" ) )
2017-12-05 23:57:01 +03:00
2022-06-13 12:37:59 +03:00
unittest . AssertNotExistsBean ( t , & issues_model . Reaction { Type : "heart" , UserID : user1 . ID , IssueID : issue1ID , CommentID : comment1ID } )
2017-12-05 23:57:01 +03:00
}