1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
9 getAllNotificationsSettings,
12 prepareNotificationsTest,
14 } from '@shared/extra-utils'
15 import { UserNotification, UserNotificationSettingValue } from '@shared/models'
17 const expect = chai.expect
19 describe('Test notifications API', function () {
20 let server: PeerTubeServer
21 let userNotifications: UserNotification[] = []
23 let emails: object[] = []
25 before(async function () {
28 const res = await prepareNotificationsTest(1)
30 userToken = res.userAccessToken
31 userNotifications = res.userNotifications
32 server = res.servers[0]
34 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
36 for (let i = 0; i < 10; i++) {
37 await server.videos.randomUpload({ wait: false })
40 await waitJobs([ server ])
43 describe('Mark as read', function () {
45 it('Should mark as read some notifications', async function () {
46 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
47 const ids = data.map(n => n.id)
49 await server.notifications.markAsRead({ token: userToken, ids })
52 it('Should have the notifications marked as read', async function () {
53 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
55 expect(data[0].read).to.be.false
56 expect(data[1].read).to.be.false
57 expect(data[2].read).to.be.true
58 expect(data[3].read).to.be.true
59 expect(data[4].read).to.be.true
60 expect(data[5].read).to.be.false
63 it('Should only list read notifications', async function () {
64 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
66 for (const notification of data) {
67 expect(notification.read).to.be.true
71 it('Should only list unread notifications', async function () {
72 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
74 for (const notification of data) {
75 expect(notification.read).to.be.false
79 it('Should mark as read all notifications', async function () {
80 await server.notifications.markAsReadAll({ token: userToken })
82 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
84 expect(body.total).to.equal(0)
85 expect(body.data).to.have.lengthOf(0)
89 describe('Notification settings', function () {
90 let baseParams: CheckerBaseParams
96 socketNotifications: userNotifications,
101 it('Should not have notifications', async function () {
104 await server.notifications.updateMySettings({
106 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
110 const info = await server.users.getMyInfo({ token: userToken })
111 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
114 const { name, shortUUID } = await server.videos.randomUpload()
116 const check = { web: true, mail: true }
117 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
120 it('Should only have web notifications', async function () {
123 await server.notifications.updateMySettings({
125 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
129 const info = await server.users.getMyInfo({ token: userToken })
130 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
133 const { name, shortUUID } = await server.videos.randomUpload()
136 const check = { mail: true, web: false }
137 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
141 const check = { mail: false, web: true }
142 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
146 it('Should only have mail notifications', async function () {
149 await server.notifications.updateMySettings({
151 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
155 const info = await server.users.getMyInfo({ token: userToken })
156 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
159 const { name, shortUUID } = await server.videos.randomUpload()
162 const check = { mail: false, web: true }
163 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
167 const check = { mail: true, web: false }
168 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
172 it('Should have email and web notifications', async function () {
175 await server.notifications.updateMySettings({
178 ...getAllNotificationsSettings(),
179 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
184 const info = await server.users.getMyInfo({ token: userToken })
185 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
186 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
190 const { name, shortUUID } = await server.videos.randomUpload()
192 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
196 after(async function () {
197 MockSmtpServer.Instance.kill()
199 await cleanupTests([ server ])