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.

103 lines
3.9 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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestCreateOrUpdateIssueNotifications(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  12. assert.NoError(t, CreateOrUpdateIssueNotifications(issue.ID, 0, 2))
  13. // User 9 is inactive, thus notifications for user 1 and 4 are created
  14. notf := AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification)
  15. assert.Equal(t, NotificationStatusUnread, notf.Status)
  16. CheckConsistencyFor(t, &Issue{ID: issue.ID})
  17. notf = AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification)
  18. assert.Equal(t, NotificationStatusUnread, notf.Status)
  19. }
  20. func TestNotificationsForUser(t *testing.T) {
  21. assert.NoError(t, PrepareTestDatabase())
  22. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  23. statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
  24. notfs, err := NotificationsForUser(user, statuses, 1, 10)
  25. assert.NoError(t, err)
  26. if assert.Len(t, notfs, 3) {
  27. assert.EqualValues(t, 5, notfs[0].ID)
  28. assert.EqualValues(t, user.ID, notfs[0].UserID)
  29. assert.EqualValues(t, 4, notfs[1].ID)
  30. assert.EqualValues(t, user.ID, notfs[1].UserID)
  31. assert.EqualValues(t, 2, notfs[2].ID)
  32. assert.EqualValues(t, user.ID, notfs[2].UserID)
  33. }
  34. }
  35. func TestNotification_GetRepo(t *testing.T) {
  36. assert.NoError(t, PrepareTestDatabase())
  37. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  38. repo, err := notf.GetRepo()
  39. assert.NoError(t, err)
  40. assert.Equal(t, repo, notf.Repository)
  41. assert.EqualValues(t, notf.RepoID, repo.ID)
  42. }
  43. func TestNotification_GetIssue(t *testing.T) {
  44. assert.NoError(t, PrepareTestDatabase())
  45. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  46. issue, err := notf.GetIssue()
  47. assert.NoError(t, err)
  48. assert.Equal(t, issue, notf.Issue)
  49. assert.EqualValues(t, notf.IssueID, issue.ID)
  50. }
  51. func TestGetNotificationCount(t *testing.T) {
  52. assert.NoError(t, PrepareTestDatabase())
  53. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  54. cnt, err := GetNotificationCount(user, NotificationStatusRead)
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, 0, cnt)
  57. cnt, err = GetNotificationCount(user, NotificationStatusUnread)
  58. assert.NoError(t, err)
  59. assert.EqualValues(t, 1, cnt)
  60. }
  61. func TestSetNotificationStatus(t *testing.T) {
  62. assert.NoError(t, PrepareTestDatabase())
  63. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  64. notf := AssertExistsAndLoadBean(t,
  65. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  66. assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
  67. AssertExistsAndLoadBean(t,
  68. &Notification{ID: notf.ID, Status: NotificationStatusPinned})
  69. assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
  70. assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
  71. }
  72. func TestUpdateNotificationStatuses(t *testing.T) {
  73. assert.NoError(t, PrepareTestDatabase())
  74. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  75. notfUnread := AssertExistsAndLoadBean(t,
  76. &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification)
  77. notfRead := AssertExistsAndLoadBean(t,
  78. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  79. notfPinned := AssertExistsAndLoadBean(t,
  80. &Notification{UserID: user.ID, Status: NotificationStatusPinned}).(*Notification)
  81. assert.NoError(t, UpdateNotificationStatuses(user, NotificationStatusUnread, NotificationStatusRead))
  82. AssertExistsAndLoadBean(t,
  83. &Notification{ID: notfUnread.ID, Status: NotificationStatusRead})
  84. AssertExistsAndLoadBean(t,
  85. &Notification{ID: notfRead.ID, Status: NotificationStatusRead})
  86. AssertExistsAndLoadBean(t,
  87. &Notification{ID: notfPinned.ID, Status: NotificationStatusPinned})
  88. }