1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
8 getAllNotificationsSettings,
10 prepareNotificationsTest
11 } from '@server/tests/shared'
12 import { UserNotification, UserNotificationSettingValue } from '@shared/models'
13 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
15 const expect = chai.expect
17 describe('Test notifications API', function () {
18 let server: PeerTubeServer
19 let userNotifications: UserNotification[] = []
21 let emails: object[] = []
23 before(async function () {
26 const res = await prepareNotificationsTest(1)
28 userToken = res.userAccessToken
29 userNotifications = res.userNotifications
30 server = res.servers[0]
32 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
34 for (let i = 0; i < 10; i++) {
35 await server.videos.randomUpload({ wait: false })
38 await waitJobs([ server ])
41 describe('Mark as read', function () {
43 it('Should mark as read some notifications', async function () {
44 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
45 const ids = data.map(n => n.id)
47 await server.notifications.markAsRead({ token: userToken, ids })
50 it('Should have the notifications marked as read', async function () {
51 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
53 expect(data[0].read).to.be.false
54 expect(data[1].read).to.be.false
55 expect(data[2].read).to.be.true
56 expect(data[3].read).to.be.true
57 expect(data[4].read).to.be.true
58 expect(data[5].read).to.be.false
61 it('Should only list read notifications', async function () {
62 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
64 for (const notification of data) {
65 expect(notification.read).to.be.true
69 it('Should only list unread notifications', async function () {
70 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
72 for (const notification of data) {
73 expect(notification.read).to.be.false
77 it('Should mark as read all notifications', async function () {
78 await server.notifications.markAsReadAll({ token: userToken })
80 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
82 expect(body.total).to.equal(0)
83 expect(body.data).to.have.lengthOf(0)
87 describe('Notification settings', function () {
88 let baseParams: CheckerBaseParams
94 socketNotifications: userNotifications,
99 it('Should not have notifications', async function () {
102 await server.notifications.updateMySettings({
104 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
108 const info = await server.users.getMyInfo({ token: userToken })
109 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
112 const { name, shortUUID } = await server.videos.randomUpload()
114 const check = { web: true, mail: true }
115 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
118 it('Should only have web notifications', async function () {
121 await server.notifications.updateMySettings({
123 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
127 const info = await server.users.getMyInfo({ token: userToken })
128 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
131 const { name, shortUUID } = await server.videos.randomUpload()
134 const check = { mail: true, web: false }
135 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
139 const check = { mail: false, web: true }
140 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
144 it('Should only have mail notifications', async function () {
147 await server.notifications.updateMySettings({
149 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
153 const info = await server.users.getMyInfo({ token: userToken })
154 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
157 const { name, shortUUID } = await server.videos.randomUpload()
160 const check = { mail: false, web: true }
161 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
165 const check = { mail: true, web: false }
166 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
170 it('Should have email and web notifications', async function () {
173 await server.notifications.updateMySettings({
176 ...getAllNotificationsSettings(),
177 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
182 const info = await server.users.getMyInfo({ token: userToken })
183 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
184 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
188 const { name, shortUUID } = await server.videos.randomUpload()
190 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
194 after(async function () {
195 MockSmtpServer.Instance.kill()
197 await cleanupTests([ server ])