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('Notification list & count', function () {
43 it('Should correctly list notifications', async function () {
44 const { data, total } = await server.notifications.list({ token: userToken, start: 0, count: 2 })
46 expect(data).to.have.lengthOf(2)
47 expect(total).to.equal(10)
51 describe('Mark as read', function () {
53 it('Should mark as read some notifications', async function () {
54 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
55 const ids = data.map(n => n.id)
57 await server.notifications.markAsRead({ token: userToken, ids })
60 it('Should have the notifications marked as read', async function () {
61 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
63 expect(data[0].read).to.be.false
64 expect(data[1].read).to.be.false
65 expect(data[2].read).to.be.true
66 expect(data[3].read).to.be.true
67 expect(data[4].read).to.be.true
68 expect(data[5].read).to.be.false
71 it('Should only list read notifications', async function () {
72 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
74 for (const notification of data) {
75 expect(notification.read).to.be.true
79 it('Should only list unread notifications', async function () {
80 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
82 for (const notification of data) {
83 expect(notification.read).to.be.false
87 it('Should mark as read all notifications', async function () {
88 await server.notifications.markAsReadAll({ token: userToken })
90 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
92 expect(body.total).to.equal(0)
93 expect(body.data).to.have.lengthOf(0)
97 describe('Notification settings', function () {
98 let baseParams: CheckerBaseParams
104 socketNotifications: userNotifications,
109 it('Should not have notifications', async function () {
112 await server.notifications.updateMySettings({
114 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
118 const info = await server.users.getMyInfo({ token: userToken })
119 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
122 const { name, shortUUID } = await server.videos.randomUpload()
124 const check = { web: true, mail: true }
125 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
128 it('Should only have web notifications', async function () {
131 await server.notifications.updateMySettings({
133 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
137 const info = await server.users.getMyInfo({ token: userToken })
138 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
141 const { name, shortUUID } = await server.videos.randomUpload()
144 const check = { mail: true, web: false }
145 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
149 const check = { mail: false, web: true }
150 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
154 it('Should only have mail notifications', async function () {
157 await server.notifications.updateMySettings({
159 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
163 const info = await server.users.getMyInfo({ token: userToken })
164 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
167 const { name, shortUUID } = await server.videos.randomUpload()
170 const check = { mail: false, web: true }
171 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
175 const check = { mail: true, web: false }
176 await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
180 it('Should have email and web notifications', async function () {
183 await server.notifications.updateMySettings({
186 ...getAllNotificationsSettings(),
187 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
192 const info = await server.users.getMyInfo({ token: userToken })
193 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
194 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
198 const { name, shortUUID } = await server.videos.randomUpload()
200 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
204 after(async function () {
205 MockSmtpServer.Instance.kill()
207 await cleanupTests([ server ])