code.gitea.io/gitea@v1.21.7/models/issues/reaction_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package issues_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	issues_model "code.gitea.io/gitea/models/issues"
    11  	repo_model "code.gitea.io/gitea/models/repo"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	"code.gitea.io/gitea/modules/setting"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func addReaction(t *testing.T, doerID, issueID, commentID int64, content string) {
    20  	var reaction *issues_model.Reaction
    21  	var err error
    22  	if commentID == 0 {
    23  		reaction, err = issues_model.CreateIssueReaction(db.DefaultContext, doerID, issueID, content)
    24  	} else {
    25  		reaction, err = issues_model.CreateCommentReaction(db.DefaultContext, doerID, issueID, commentID, content)
    26  	}
    27  	assert.NoError(t, err)
    28  	assert.NotNil(t, reaction)
    29  }
    30  
    31  func TestIssueAddReaction(t *testing.T) {
    32  	assert.NoError(t, unittest.PrepareTestDatabase())
    33  
    34  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    35  
    36  	var issue1ID int64 = 1
    37  
    38  	addReaction(t, user1.ID, issue1ID, 0, "heart")
    39  
    40  	unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID})
    41  }
    42  
    43  func TestIssueAddDuplicateReaction(t *testing.T) {
    44  	assert.NoError(t, unittest.PrepareTestDatabase())
    45  
    46  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    47  
    48  	var issue1ID int64 = 1
    49  
    50  	addReaction(t, user1.ID, issue1ID, 0, "heart")
    51  
    52  	reaction, err := issues_model.CreateReaction(db.DefaultContext, &issues_model.ReactionOptions{
    53  		DoerID:  user1.ID,
    54  		IssueID: issue1ID,
    55  		Type:    "heart",
    56  	})
    57  	assert.Error(t, err)
    58  	assert.Equal(t, issues_model.ErrReactionAlreadyExist{Reaction: "heart"}, err)
    59  
    60  	existingR := unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID})
    61  	assert.Equal(t, existingR.ID, reaction.ID)
    62  }
    63  
    64  func TestIssueDeleteReaction(t *testing.T) {
    65  	assert.NoError(t, unittest.PrepareTestDatabase())
    66  
    67  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    68  
    69  	var issue1ID int64 = 1
    70  
    71  	addReaction(t, user1.ID, issue1ID, 0, "heart")
    72  
    73  	err := issues_model.DeleteIssueReaction(db.DefaultContext, user1.ID, issue1ID, "heart")
    74  	assert.NoError(t, err)
    75  
    76  	unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID})
    77  }
    78  
    79  func TestIssueReactionCount(t *testing.T) {
    80  	assert.NoError(t, unittest.PrepareTestDatabase())
    81  
    82  	setting.UI.ReactionMaxUserNum = 2
    83  
    84  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    85  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    86  	org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
    87  	user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
    88  	ghost := user_model.NewGhostUser()
    89  
    90  	var issueID int64 = 2
    91  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    92  
    93  	addReaction(t, user1.ID, issueID, 0, "heart")
    94  	addReaction(t, user2.ID, issueID, 0, "heart")
    95  	addReaction(t, org3.ID, issueID, 0, "heart")
    96  	addReaction(t, org3.ID, issueID, 0, "+1")
    97  	addReaction(t, user4.ID, issueID, 0, "+1")
    98  	addReaction(t, user4.ID, issueID, 0, "heart")
    99  	addReaction(t, ghost.ID, issueID, 0, "-1")
   100  
   101  	reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
   102  		IssueID: issueID,
   103  	})
   104  	assert.NoError(t, err)
   105  	assert.Len(t, reactionsList, 7)
   106  	_, err = reactionsList.LoadUsers(db.DefaultContext, repo)
   107  	assert.NoError(t, err)
   108  
   109  	reactions := reactionsList.GroupByType()
   110  	assert.Len(t, reactions["heart"], 4)
   111  	assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
   112  	assert.Equal(t, user1.Name+", "+user2.Name, reactions["heart"].GetFirstUsers())
   113  	assert.True(t, reactions["heart"].HasUser(1))
   114  	assert.False(t, reactions["heart"].HasUser(5))
   115  	assert.False(t, reactions["heart"].HasUser(0))
   116  	assert.Len(t, reactions["+1"], 2)
   117  	assert.Equal(t, 0, reactions["+1"].GetMoreUserCount())
   118  	assert.Len(t, reactions["-1"], 1)
   119  }
   120  
   121  func TestIssueCommentAddReaction(t *testing.T) {
   122  	assert.NoError(t, unittest.PrepareTestDatabase())
   123  
   124  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   125  
   126  	var issue1ID int64 = 1
   127  	var comment1ID int64 = 1
   128  
   129  	addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
   130  
   131  	unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID})
   132  }
   133  
   134  func TestIssueCommentDeleteReaction(t *testing.T) {
   135  	assert.NoError(t, unittest.PrepareTestDatabase())
   136  
   137  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   138  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   139  	org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
   140  	user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
   141  
   142  	var issue1ID int64 = 1
   143  	var comment1ID int64 = 1
   144  
   145  	addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
   146  	addReaction(t, user2.ID, issue1ID, comment1ID, "heart")
   147  	addReaction(t, org3.ID, issue1ID, comment1ID, "heart")
   148  	addReaction(t, user4.ID, issue1ID, comment1ID, "+1")
   149  
   150  	reactionsList, _, err := issues_model.FindReactions(db.DefaultContext, issues_model.FindReactionsOptions{
   151  		IssueID:   issue1ID,
   152  		CommentID: comment1ID,
   153  	})
   154  	assert.NoError(t, err)
   155  	assert.Len(t, reactionsList, 4)
   156  
   157  	reactions := reactionsList.GroupByType()
   158  	assert.Len(t, reactions["heart"], 3)
   159  	assert.Len(t, reactions["+1"], 1)
   160  }
   161  
   162  func TestIssueCommentReactionCount(t *testing.T) {
   163  	assert.NoError(t, unittest.PrepareTestDatabase())
   164  
   165  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   166  
   167  	var issue1ID int64 = 1
   168  	var comment1ID int64 = 1
   169  
   170  	addReaction(t, user1.ID, issue1ID, comment1ID, "heart")
   171  	assert.NoError(t, issues_model.DeleteCommentReaction(db.DefaultContext, user1.ID, issue1ID, comment1ID, "heart"))
   172  
   173  	unittest.AssertNotExistsBean(t, &issues_model.Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID})
   174  }