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

notification_test.go 2.8KB

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