You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.2 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, content string) {
  11. var reaction *Reaction
  12. var err error
  13. if comment == nil {
  14. reaction, err = CreateIssueReaction(doer, issue, content)
  15. } else {
  16. reaction, err = CreateCommentReaction(doer, issue, comment, content)
  17. }
  18. assert.NoError(t, err)
  19. assert.NotNil(t, reaction)
  20. }
  21. func TestIssueAddReaction(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  24. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  25. addReaction(t, user1, issue1, nil, "heart")
  26. AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
  27. }
  28. func TestIssueAddDuplicateReaction(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  31. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  32. addReaction(t, user1, issue1, nil, "heart")
  33. reaction, err := CreateReaction(&ReactionOptions{
  34. Doer: user1,
  35. Issue: issue1,
  36. Type: "heart",
  37. })
  38. assert.Error(t, err)
  39. assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err)
  40. existingR := AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction)
  41. assert.Equal(t, existingR.ID, reaction.ID)
  42. }
  43. func TestIssueDeleteReaction(t *testing.T) {
  44. assert.NoError(t, PrepareTestDatabase())
  45. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  46. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  47. addReaction(t, user1, issue1, nil, "heart")
  48. err := DeleteIssueReaction(user1, issue1, "heart")
  49. assert.NoError(t, err)
  50. AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
  51. }
  52. func TestIssueReactionCount(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. setting.UI.ReactionMaxUserNum = 2
  55. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  56. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  57. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  58. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  59. ghost := NewGhostUser()
  60. issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
  61. addReaction(t, user1, issue, nil, "heart")
  62. addReaction(t, user2, issue, nil, "heart")
  63. addReaction(t, user3, issue, nil, "heart")
  64. addReaction(t, user3, issue, nil, "+1")
  65. addReaction(t, user4, issue, nil, "+1")
  66. addReaction(t, user4, issue, nil, "heart")
  67. addReaction(t, ghost, issue, nil, "-1")
  68. err := issue.loadReactions(x)
  69. assert.NoError(t, err)
  70. assert.Len(t, issue.Reactions, 7)
  71. reactions := issue.Reactions.GroupByType()
  72. assert.Len(t, reactions["heart"], 4)
  73. assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
  74. assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), reactions["heart"].GetFirstUsers())
  75. assert.True(t, reactions["heart"].HasUser(1))
  76. assert.False(t, reactions["heart"].HasUser(5))
  77. assert.False(t, reactions["heart"].HasUser(0))
  78. assert.Len(t, reactions["+1"], 2)
  79. assert.Equal(t, 0, reactions["+1"].GetMoreUserCount())
  80. assert.Len(t, reactions["-1"], 1)
  81. }
  82. func TestIssueCommentAddReaction(t *testing.T) {
  83. assert.NoError(t, PrepareTestDatabase())
  84. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  85. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  86. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  87. addReaction(t, user1, issue1, comment1, "heart")
  88. AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
  89. }
  90. func TestIssueCommentDeleteReaction(t *testing.T) {
  91. assert.NoError(t, PrepareTestDatabase())
  92. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  93. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  94. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  95. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  96. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  97. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository)
  98. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  99. addReaction(t, user1, issue1, comment1, "heart")
  100. addReaction(t, user2, issue1, comment1, "heart")
  101. addReaction(t, user3, issue1, comment1, "heart")
  102. addReaction(t, user4, issue1, comment1, "+1")
  103. err := comment1.LoadReactions(repo1)
  104. assert.NoError(t, err)
  105. assert.Len(t, comment1.Reactions, 4)
  106. reactions := comment1.Reactions.GroupByType()
  107. assert.Len(t, reactions["heart"], 3)
  108. assert.Len(t, reactions["+1"], 1)
  109. }
  110. func TestIssueCommentReactionCount(t *testing.T) {
  111. assert.NoError(t, PrepareTestDatabase())
  112. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  113. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  114. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  115. addReaction(t, user1, issue1, comment1, "heart")
  116. assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart"))
  117. AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
  118. }