1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai 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 const expect = chai.expect
16 describe('Test notifications API', function () {
17 let server: PeerTubeServer
18 let userNotifications: UserNotification[] = []
20 let emails: object[] = []
22 before(async function () {
25 const res = await prepareNotificationsTest(1)
27 userToken = res.userAccessToken
28 userNotifications = res.userNotifications
29 server = res.servers[0]
31 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
33 for (let i = 0; i < 10; i++) {
34 await server.videos.randomUpload({ wait: false })
37 await waitJobs([ server ])
40 describe('Notification list & count', function () {
42 it('Should correctly list notifications', async function () {
43 const { data, total } = await server.notifications.list({ token: userToken, start: 0, count: 2 })
45 expect(data).to.have.lengthOf(2)
46 expect(total).to.equal(10)
50 describe('Mark as read', function () {
52 it('Should mark as read some notifications', async function () {
53 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
54 const ids = data.map(n => n.id)
56 await server.notifications.markAsRead({ token: userToken, ids })
59 it('Should have the notifications marked as read', async function () {
60 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
62 expect(data[0].read).to.be.false
63 expect(data[1].read).to.be.false
64 expect(data[2].read).to.be.true
65 expect(data[3].read).to.be.true
66 expect(data[4].read).to.be.true
67 expect(data[5].read).to.be.false
70 it('Should only list read notifications', async function () {
71 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
73 for (const notification of data) {
74 expect(notification.read).to.be.true
78 it('Should only list unread notifications', async function () {
79 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
81 for (const notification of data) {
82 expect(notification.read).to.be.false
86 it('Should mark as read all notifications', async function () {
87 await server.notifications.markAsReadAll({ token: userToken })
89 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
91 expect(body.total).to.equal(0)
92 expect(body.data).to.have.lengthOf(0)
96 describe('Notification settings', function () {
97 let baseParams: CheckerBaseParams
103 socketNotifications: userNotifications,
108 it('Should not have notifications', async function () {
111 await server.notifications.updateMySettings({
113 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
117 const info = await server.users.getMyInfo({ token: userToken })
118 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
121 const { name, shortUUID } = await server.videos.randomUpload()
123 const check = { web: true, mail: true }
124 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
127 it('Should only have web notifications', async function () {
130 await server.notifications.updateMySettings({
132 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
136 const info = await server.users.getMyInfo({ token: userToken })
137 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
140 const { name, shortUUID } = await server.videos.randomUpload()
143 const check = { mail: true, web: false }
144 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
148 const check = { mail: false, web: true }
149 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
153 it('Should only have mail notifications', async function () {
156 await server.notifications.updateMySettings({
158 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
162 const info = await server.users.getMyInfo({ token: userToken })
163 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
166 const { name, shortUUID } = await server.videos.randomUpload()
169 const check = { mail: false, web: true }
170 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
174 const check = { mail: true, web: false }
175 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
179 it('Should have email and web notifications', async function () {
182 await server.notifications.updateMySettings({
185 ...getAllNotificationsSettings(),
186 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
191 const info = await server.users.getMyInfo({ token: userToken })
192 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
193 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
197 const { name, shortUUID } = await server.videos.randomUpload()
199 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
203 after(async function () {
204 MockSmtpServer.Instance.kill()
206 await cleanupTests([ server ])