notifications-api.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import * as chai from 'chai'
  4. import { addUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
  5. import { cleanupTests, getMyUserInformation, immutableAssign, uploadRandomVideo, waitJobs } from '../../../../shared/extra-utils'
  6. import { ServerInfo } from '../../../../shared/extra-utils/index'
  7. import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
  8. import {
  9. CheckerBaseParams,
  10. checkNewVideoFromSubscription,
  11. getAllNotificationsSettings,
  12. getUserNotifications,
  13. markAsReadAllNotifications,
  14. markAsReadNotifications,
  15. prepareNotificationsTest,
  16. updateMyNotificationSettings
  17. } from '../../../../shared/extra-utils/users/user-notifications'
  18. import { User, UserNotification, UserNotificationSettingValue } from '../../../../shared/models/users'
  19. const expect = chai.expect
  20. describe('Test notifications API', function () {
  21. let server: ServerInfo
  22. let userNotifications: UserNotification[] = []
  23. let userAccessToken: string
  24. let emails: object[] = []
  25. before(async function () {
  26. this.timeout(120000)
  27. const res = await prepareNotificationsTest(1)
  28. emails = res.emails
  29. userAccessToken = res.userAccessToken
  30. userNotifications = res.userNotifications
  31. server = res.servers[0]
  32. await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port)
  33. for (let i = 0; i < 10; i++) {
  34. await uploadRandomVideo(server, false)
  35. }
  36. await waitJobs([ server ])
  37. })
  38. describe('Mark as read', function () {
  39. it('Should mark as read some notifications', async function () {
  40. const res = await getUserNotifications(server.url, userAccessToken, 2, 3)
  41. const ids = res.body.data.map(n => n.id)
  42. await markAsReadNotifications(server.url, userAccessToken, ids)
  43. })
  44. it('Should have the notifications marked as read', async function () {
  45. const res = await getUserNotifications(server.url, userAccessToken, 0, 10)
  46. const notifications = res.body.data as UserNotification[]
  47. expect(notifications[0].read).to.be.false
  48. expect(notifications[1].read).to.be.false
  49. expect(notifications[2].read).to.be.true
  50. expect(notifications[3].read).to.be.true
  51. expect(notifications[4].read).to.be.true
  52. expect(notifications[5].read).to.be.false
  53. })
  54. it('Should only list read notifications', async function () {
  55. const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false)
  56. const notifications = res.body.data as UserNotification[]
  57. for (const notification of notifications) {
  58. expect(notification.read).to.be.true
  59. }
  60. })
  61. it('Should only list unread notifications', async function () {
  62. const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
  63. const notifications = res.body.data as UserNotification[]
  64. for (const notification of notifications) {
  65. expect(notification.read).to.be.false
  66. }
  67. })
  68. it('Should mark as read all notifications', async function () {
  69. await markAsReadAllNotifications(server.url, userAccessToken)
  70. const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
  71. expect(res.body.total).to.equal(0)
  72. expect(res.body.data).to.have.lengthOf(0)
  73. })
  74. })
  75. describe('Notification settings', function () {
  76. let baseParams: CheckerBaseParams
  77. before(() => {
  78. baseParams = {
  79. server: server,
  80. emails,
  81. socketNotifications: userNotifications,
  82. token: userAccessToken
  83. }
  84. })
  85. it('Should not have notifications', async function () {
  86. this.timeout(20000)
  87. await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
  88. newVideoFromSubscription: UserNotificationSettingValue.NONE
  89. }))
  90. {
  91. const res = await getMyUserInformation(server.url, userAccessToken)
  92. const info = res.body as User
  93. expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
  94. }
  95. const { name, uuid } = await uploadRandomVideo(server)
  96. const check = { web: true, mail: true }
  97. await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
  98. })
  99. it('Should only have web notifications', async function () {
  100. this.timeout(20000)
  101. await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
  102. newVideoFromSubscription: UserNotificationSettingValue.WEB
  103. }))
  104. {
  105. const res = await getMyUserInformation(server.url, userAccessToken)
  106. const info = res.body as User
  107. expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
  108. }
  109. const { name, uuid } = await uploadRandomVideo(server)
  110. {
  111. const check = { mail: true, web: false }
  112. await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
  113. }
  114. {
  115. const check = { mail: false, web: true }
  116. await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
  117. }
  118. })
  119. it('Should only have mail notifications', async function () {
  120. this.timeout(20000)
  121. await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
  122. newVideoFromSubscription: UserNotificationSettingValue.EMAIL
  123. }))
  124. {
  125. const res = await getMyUserInformation(server.url, userAccessToken)
  126. const info = res.body as User
  127. expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
  128. }
  129. const { name, uuid } = await uploadRandomVideo(server)
  130. {
  131. const check = { mail: false, web: true }
  132. await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
  133. }
  134. {
  135. const check = { mail: true, web: false }
  136. await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
  137. }
  138. })
  139. it('Should have email and web notifications', async function () {
  140. this.timeout(20000)
  141. await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
  142. newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
  143. }))
  144. {
  145. const res = await getMyUserInformation(server.url, userAccessToken)
  146. const info = res.body as User
  147. expect(info.notificationSettings.newVideoFromSubscription).to.equal(
  148. UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
  149. )
  150. }
  151. const { name, uuid } = await uploadRandomVideo(server)
  152. await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
  153. })
  154. })
  155. after(async function () {
  156. MockSmtpServer.Instance.kill()
  157. await cleanupTests([ server ])
  158. })
  159. })