1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
9 getAllNotificationsSettings,
12 prepareNotificationsTest,
16 } from '@shared/extra-utils'
17 import { User, UserNotification, UserNotificationSettingValue } from '@shared/models'
19 const expect = chai.expect
21 describe('Test notifications API', function () {
22 let server: ServerInfo
23 let userNotifications: UserNotification[] = []
25 let emails: object[] = []
27 before(async function () {
30 const res = await prepareNotificationsTest(1)
32 userToken = res.userAccessToken
33 userNotifications = res.userNotifications
34 server = res.servers[0]
36 await server.subscriptionsCommand.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
38 for (let i = 0; i < 10; i++) {
39 await uploadRandomVideo(server, false)
42 await waitJobs([ server ])
45 describe('Mark as read', function () {
47 it('Should mark as read some notifications', async function () {
48 const { data } = await server.notificationsCommand.list({ token: userToken, start: 2, count: 3 })
49 const ids = data.map(n => n.id)
51 await server.notificationsCommand.markAsRead({ token: userToken, ids })
54 it('Should have the notifications marked as read', async function () {
55 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10 })
57 expect(data[0].read).to.be.false
58 expect(data[1].read).to.be.false
59 expect(data[2].read).to.be.true
60 expect(data[3].read).to.be.true
61 expect(data[4].read).to.be.true
62 expect(data[5].read).to.be.false
65 it('Should only list read notifications', async function () {
66 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: false })
68 for (const notification of data) {
69 expect(notification.read).to.be.true
73 it('Should only list unread notifications', async function () {
74 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
76 for (const notification of data) {
77 expect(notification.read).to.be.false
81 it('Should mark as read all notifications', async function () {
82 await server.notificationsCommand.markAsReadAll({ token: userToken })
84 const body = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
86 expect(body.total).to.equal(0)
87 expect(body.data).to.have.lengthOf(0)
91 describe('Notification settings', function () {
92 let baseParams: CheckerBaseParams
98 socketNotifications: userNotifications,
103 it('Should not have notifications', async function () {
106 await server.notificationsCommand.updateMySettings({
108 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
112 const res = await getMyUserInformation(server.url, userToken)
113 const info = res.body as User
114 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
117 const { name, uuid } = await uploadRandomVideo(server)
119 const check = { web: true, mail: true }
120 await checkNewVideoFromSubscription({ ...baseParams, ...check }, name, uuid, 'absence')
123 it('Should only have web notifications', async function () {
126 await server.notificationsCommand.updateMySettings({
128 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
132 const res = await getMyUserInformation(server.url, userToken)
133 const info = res.body as User
134 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
137 const { name, uuid } = await uploadRandomVideo(server)
140 const check = { mail: true, web: false }
141 await checkNewVideoFromSubscription({ ...baseParams, ...check }, name, uuid, 'absence')
145 const check = { mail: false, web: true }
146 await checkNewVideoFromSubscription({ ...baseParams, ...check }, name, uuid, 'presence')
150 it('Should only have mail notifications', async function () {
153 await server.notificationsCommand.updateMySettings({
155 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
159 const res = await getMyUserInformation(server.url, userToken)
160 const info = res.body as User
161 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
164 const { name, uuid } = await uploadRandomVideo(server)
167 const check = { mail: false, web: true }
168 await checkNewVideoFromSubscription({ ...baseParams, ...check }, name, uuid, 'absence')
172 const check = { mail: true, web: false }
173 await checkNewVideoFromSubscription({ ...baseParams, ...check }, name, uuid, 'presence')
177 it('Should have email and web notifications', async function () {
180 await server.notificationsCommand.updateMySettings({
183 ...getAllNotificationsSettings(),
184 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
189 const res = await getMyUserInformation(server.url, userToken)
190 const info = res.body as User
191 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
192 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
196 const { name, uuid } = await uploadRandomVideo(server)
198 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
202 after(async function () {
203 MockSmtpServer.Instance.kill()
205 await cleanupTests([ server ])