Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

notification_test.go 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, 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, 1) {
  27. assert.EqualValues(t, 2, notfs[0].ID)
  28. assert.EqualValues(t, user.ID, notfs[0].UserID)
  29. }
  30. }
  31. func TestNotification_GetRepo(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  34. repo, err := notf.GetRepo()
  35. assert.NoError(t, err)
  36. assert.Equal(t, repo, notf.Repository)
  37. assert.EqualValues(t, notf.RepoID, repo.ID)
  38. }
  39. func TestNotification_GetIssue(t *testing.T) {
  40. assert.NoError(t, PrepareTestDatabase())
  41. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  42. issue, err := notf.GetIssue()
  43. assert.NoError(t, err)
  44. assert.Equal(t, issue, notf.Issue)
  45. assert.EqualValues(t, notf.IssueID, issue.ID)
  46. }
  47. func TestGetNotificationCount(t *testing.T) {
  48. assert.NoError(t, PrepareTestDatabase())
  49. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  50. cnt, err := GetNotificationCount(user, NotificationStatusUnread)
  51. assert.NoError(t, err)
  52. assert.EqualValues(t, 0, cnt)
  53. cnt, err = GetNotificationCount(user, NotificationStatusRead)
  54. assert.NoError(t, err)
  55. assert.EqualValues(t, 1, cnt)
  56. }
  57. func TestSetNotificationStatus(t *testing.T) {
  58. assert.NoError(t, PrepareTestDatabase())
  59. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  60. notf := AssertExistsAndLoadBean(t,
  61. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  62. assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
  63. AssertExistsAndLoadBean(t,
  64. &Notification{ID: notf.ID, Status: NotificationStatusPinned})
  65. assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
  66. assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
  67. }