1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 checkNewVideoFromSubscription,
9 getAllNotificationsSettings,
13 prepareNotificationsTest,
17 } from '@shared/extra-utils'
18 import { User, UserNotification, UserNotificationSettingValue } from '@shared/models'
20 const expect = chai.expect
22 describe('Test notifications API', function () {
23 let server: ServerInfo
24 let userNotifications: UserNotification[] = []
26 let emails: object[] = []
28 before(async function () {
31 const res = await prepareNotificationsTest(1)
33 userToken = res.userAccessToken
34 userNotifications = res.userNotifications
35 server = res.servers[0]
37 await server.subscriptionsCommand.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
39 for (let i = 0; i < 10; i++) {
40 await uploadRandomVideo(server, false)
43 await waitJobs([ server ])
46 describe('Mark as read', function () {
48 it('Should mark as read some notifications', async function () {
49 const { data } = await server.notificationsCommand.list({ token: userToken, start: 2, count: 3 })
50 const ids = data.map(n => n.id)
52 await server.notificationsCommand.markAsRead({ token: userToken, ids })
55 it('Should have the notifications marked as read', async function () {
56 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10 })
58 expect(data[0].read).to.be.false
59 expect(data[1].read).to.be.false
60 expect(data[2].read).to.be.true
61 expect(data[3].read).to.be.true
62 expect(data[4].read).to.be.true
63 expect(data[5].read).to.be.false
66 it('Should only list read notifications', async function () {
67 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: false })
69 for (const notification of data) {
70 expect(notification.read).to.be.true
74 it('Should only list unread notifications', async function () {
75 const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
77 for (const notification of data) {
78 expect(notification.read).to.be.false
82 it('Should mark as read all notifications', async function () {
83 await server.notificationsCommand.markAsReadAll({ token: userToken })
85 const body = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
87 expect(body.total).to.equal(0)
88 expect(body.data).to.have.lengthOf(0)
92 describe('Notification settings', function () {
93 let baseParams: CheckerBaseParams
99 socketNotifications: userNotifications,
104 it('Should not have notifications', async function () {
107 await server.notificationsCommand.updateMySettings({
109 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
113 const res = await getMyUserInformation(server.url, userToken)
114 const info = res.body as User
115 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
118 const { name, uuid } = await uploadRandomVideo(server)
120 const check = { web: true, mail: true }
121 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
124 it('Should only have web notifications', async function () {
127 await server.notificationsCommand.updateMySettings({
129 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
133 const res = await getMyUserInformation(server.url, userToken)
134 const info = res.body as User
135 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
138 const { name, uuid } = await uploadRandomVideo(server)
141 const check = { mail: true, web: false }
142 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
146 const check = { mail: false, web: true }
147 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
151 it('Should only have mail notifications', async function () {
154 await server.notificationsCommand.updateMySettings({
156 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
160 const res = await getMyUserInformation(server.url, userToken)
161 const info = res.body as User
162 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
165 const { name, uuid } = await uploadRandomVideo(server)
168 const check = { mail: false, web: true }
169 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
173 const check = { mail: true, web: false }
174 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
178 it('Should have email and web notifications', async function () {
181 await server.notificationsCommand.updateMySettings({
184 ...getAllNotificationsSettings(),
185 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
190 const res = await getMyUserInformation(server.url, userToken)
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 ])