1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
6 checkNewVideoFromSubscription,
7 getAllNotificationsSettings,
9 prepareNotificationsTest
10 } from '@server/tests/shared'
11 import { UserNotification, UserNotificationSettingValue } from '@shared/models'
12 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
14 describe('Test notifications API', function () {
15 let server: PeerTubeServer
16 let userNotifications: UserNotification[] = []
18 let emails: object[] = []
20 before(async function () {
23 const res = await prepareNotificationsTest(1)
25 userToken = res.userAccessToken
26 userNotifications = res.userNotifications
27 server = res.servers[0]
29 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@' + server.host })
31 for (let i = 0; i < 10; i++) {
32 await server.videos.randomUpload({ wait: false })
35 await waitJobs([ server ])
38 describe('Notification list & count', function () {
40 it('Should correctly list notifications', async function () {
41 const { data, total } = await server.notifications.list({ token: userToken, start: 0, count: 2 })
43 expect(data).to.have.lengthOf(2)
44 expect(total).to.equal(10)
48 describe('Mark as read', function () {
50 it('Should mark as read some notifications', async function () {
51 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
52 const ids = data.map(n => n.id)
54 await server.notifications.markAsRead({ token: userToken, ids })
57 it('Should have the notifications marked as read', async function () {
58 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
60 expect(data[0].read).to.be.false
61 expect(data[1].read).to.be.false
62 expect(data[2].read).to.be.true
63 expect(data[3].read).to.be.true
64 expect(data[4].read).to.be.true
65 expect(data[5].read).to.be.false
68 it('Should only list read notifications', async function () {
69 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
71 for (const notification of data) {
72 expect(notification.read).to.be.true
76 it('Should only list unread notifications', async function () {
77 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
79 for (const notification of data) {
80 expect(notification.read).to.be.false
84 it('Should mark as read all notifications', async function () {
85 await server.notifications.markAsReadAll({ token: userToken })
87 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
89 expect(body.total).to.equal(0)
90 expect(body.data).to.have.lengthOf(0)
94 describe('Notification settings', function () {
95 let baseParams: CheckerBaseParams
101 socketNotifications: userNotifications,
106 it('Should not have notifications', async function () {
109 await server.notifications.updateMySettings({
111 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
115 const info = await server.users.getMyInfo({ token: userToken })
116 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
119 const { name, shortUUID } = await server.videos.randomUpload()
121 const check = { web: true, mail: true }
122 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
125 it('Should only have web notifications', async function () {
128 await server.notifications.updateMySettings({
130 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
134 const info = await server.users.getMyInfo({ token: userToken })
135 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
138 const { name, shortUUID } = await server.videos.randomUpload()
141 const check = { mail: true, web: false }
142 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
146 const check = { mail: false, web: true }
147 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
151 it('Should only have mail notifications', async function () {
154 await server.notifications.updateMySettings({
156 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
160 const info = await server.users.getMyInfo({ token: userToken })
161 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
164 const { name, shortUUID } = await server.videos.randomUpload()
167 const check = { mail: false, web: true }
168 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
172 const check = { mail: true, web: false }
173 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
177 it('Should have email and web notifications', async function () {
180 await server.notifications.updateMySettings({
183 ...getAllNotificationsSettings(),
184 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
189 const info = await server.users.getMyInfo({ token: userToken })
190 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
191 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
195 const { name, shortUUID } = await server.videos.randomUpload()
197 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
201 after(async function () {
202 MockSmtpServer.Instance.kill()
204 await cleanupTests([ server ])