1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
9 getAllNotificationsSettings,
13 markAsReadAllNotifications,
14 markAsReadNotifications,
16 prepareNotificationsTest,
18 updateMyNotificationSettings,
21 } from '@shared/extra-utils'
22 import { User, UserNotification, UserNotificationSettingValue } from '@shared/models'
24 const expect = chai.expect
26 describe('Test notifications API', function () {
27 let server: ServerInfo
28 let userNotifications: UserNotification[] = []
29 let userAccessToken: string
30 let emails: object[] = []
32 before(async function () {
35 const res = await prepareNotificationsTest(1)
37 userAccessToken = res.userAccessToken
38 userNotifications = res.userNotifications
39 server = res.servers[0]
41 await server.subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + server.port })
43 for (let i = 0; i < 10; i++) {
44 await uploadRandomVideo(server, false)
47 await waitJobs([ server ])
50 describe('Mark as read', function () {
52 it('Should mark as read some notifications', async function () {
53 const res = await getUserNotifications(server.url, userAccessToken, 2, 3)
54 const ids = res.body.data.map(n => n.id)
56 await markAsReadNotifications(server.url, userAccessToken, ids)
59 it('Should have the notifications marked as read', async function () {
60 const res = await getUserNotifications(server.url, userAccessToken, 0, 10)
62 const notifications = res.body.data as UserNotification[]
63 expect(notifications[0].read).to.be.false
64 expect(notifications[1].read).to.be.false
65 expect(notifications[2].read).to.be.true
66 expect(notifications[3].read).to.be.true
67 expect(notifications[4].read).to.be.true
68 expect(notifications[5].read).to.be.false
71 it('Should only list read notifications', async function () {
72 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false)
74 const notifications = res.body.data as UserNotification[]
75 for (const notification of notifications) {
76 expect(notification.read).to.be.true
80 it('Should only list unread notifications', async function () {
81 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
83 const notifications = res.body.data as UserNotification[]
84 for (const notification of notifications) {
85 expect(notification.read).to.be.false
89 it('Should mark as read all notifications', async function () {
90 await markAsReadAllNotifications(server.url, userAccessToken)
92 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
94 expect(res.body.total).to.equal(0)
95 expect(res.body.data).to.have.lengthOf(0)
99 describe('Notification settings', function () {
100 let baseParams: CheckerBaseParams
106 socketNotifications: userNotifications,
107 token: userAccessToken
111 it('Should not have notifications', async function () {
114 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
115 newVideoFromSubscription: UserNotificationSettingValue.NONE
119 const res = await getMyUserInformation(server.url, userAccessToken)
120 const info = res.body as User
121 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
124 const { name, uuid } = await uploadRandomVideo(server)
126 const check = { web: true, mail: true }
127 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
130 it('Should only have web notifications', async function () {
133 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
134 newVideoFromSubscription: UserNotificationSettingValue.WEB
138 const res = await getMyUserInformation(server.url, userAccessToken)
139 const info = res.body as User
140 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
143 const { name, uuid } = await uploadRandomVideo(server)
146 const check = { mail: true, web: false }
147 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
151 const check = { mail: false, web: true }
152 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
156 it('Should only have mail notifications', async function () {
159 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
160 newVideoFromSubscription: UserNotificationSettingValue.EMAIL
164 const res = await getMyUserInformation(server.url, userAccessToken)
165 const info = res.body as User
166 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
169 const { name, uuid } = await uploadRandomVideo(server)
172 const check = { mail: false, web: true }
173 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
177 const check = { mail: true, web: false }
178 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
182 it('Should have email and web notifications', async function () {
185 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
186 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
190 const res = await getMyUserInformation(server.url, userAccessToken)
191 const info = res.body as User
192 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
193 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
197 const { name, uuid } = await uploadRandomVideo(server)
199 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
203 after(async function () {
204 MockSmtpServer.Instance.kill()
206 await cleanupTests([ server ])