1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
9 getAllNotificationsSettings,
11 prepareNotificationsTest,
15 } from '@shared/extra-utils'
16 import { UserNotification, UserNotificationSettingValue } from '@shared/models'
18 const expect = chai.expect
20 describe('Test notifications API', function () {
21 let server: ServerInfo
22 let userNotifications: UserNotification[] = []
24 let emails: object[] = []
26 before(async function () {
29 const res = await prepareNotificationsTest(1)
31 userToken = res.userAccessToken
32 userNotifications = res.userNotifications
33 server = res.servers[0]
35 await server.subscriptionsCommand.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
37 for (let i = 0; i < 10; i++) {
38 await uploadRandomVideo(server, false)
41 await waitJobs([ server ])
44 describe('Mark as read', function () {
46 it('Should mark as read some notifications', async function () {
47 const { data } = await server.notificationsCommand.list({ token: userToken, start: 2, count: 3 })
48 const ids = data.map(n => n.id)
50 await server.notificationsCommand.markAsRead({ token: userToken, ids })
53 it('Should have the notifications marked as read', async function () {
54 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10 })
56 expect(data[0].read).to.be.false
57 expect(data[1].read).to.be.false
58 expect(data[2].read).to.be.true
59 expect(data[3].read).to.be.true
60 expect(data[4].read).to.be.true
61 expect(data[5].read).to.be.false
64 it('Should only list read notifications', async function () {
65 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: false })
67 for (const notification of data) {
68 expect(notification.read).to.be.true
72 it('Should only list unread notifications', async function () {
73 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
75 for (const notification of data) {
76 expect(notification.read).to.be.false
80 it('Should mark as read all notifications', async function () {
81 await server.notificationsCommand.markAsReadAll({ token: userToken })
83 const body = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
85 expect(body.total).to.equal(0)
86 expect(body.data).to.have.lengthOf(0)
90 describe('Notification settings', function () {
91 let baseParams: CheckerBaseParams
97 socketNotifications: userNotifications,
102 it('Should not have notifications', async function () {
105 await server.notificationsCommand.updateMySettings({
107 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
111 const info = await server.usersCommand.getMyInfo({ token: userToken })
112 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
115 const { name, uuid } = await uploadRandomVideo(server)
117 const check = { web: true, mail: true }
118 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
121 it('Should only have web notifications', async function () {
124 await server.notificationsCommand.updateMySettings({
126 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
130 const info = await server.usersCommand.getMyInfo({ token: userToken })
131 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
134 const { name, uuid } = await uploadRandomVideo(server)
137 const check = { mail: true, web: false }
138 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
142 const check = { mail: false, web: true }
143 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
147 it('Should only have mail notifications', async function () {
150 await server.notificationsCommand.updateMySettings({
152 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
156 const info = await server.usersCommand.getMyInfo({ token: userToken })
157 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
160 const { name, uuid } = await uploadRandomVideo(server)
163 const check = { mail: false, web: true }
164 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
168 const check = { mail: true, web: false }
169 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
173 it('Should have email and web notifications', async function () {
176 await server.notificationsCommand.updateMySettings({
179 ...getAllNotificationsSettings(),
180 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
185 const info = await server.usersCommand.getMyInfo({ token: userToken })
186 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
187 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
191 const { name, uuid } = await uploadRandomVideo(server)
193 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
197 after(async function () {
198 MockSmtpServer.Instance.kill()
200 await cleanupTests([ server ])