1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { addUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
6 import { cleanupTests, getMyUserInformation, immutableAssign, uploadRandomVideo, waitJobs } from '../../../../shared/extra-utils'
7 import { ServerInfo } from '../../../../shared/extra-utils/index'
8 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
11 checkNewVideoFromSubscription,
12 getAllNotificationsSettings,
14 markAsReadAllNotifications,
15 markAsReadNotifications,
16 prepareNotificationsTest,
17 updateMyNotificationSettings
18 } from '../../../../shared/extra-utils/users/user-notifications'
19 import { User, UserNotification, UserNotificationSettingValue } from '../../../../shared/models/users'
21 const expect = chai.expect
23 describe('Test notifications API', function () {
24 let server: ServerInfo
25 let userNotifications: UserNotification[] = []
26 let userAccessToken: string
27 let emails: object[] = []
29 before(async function () {
32 const res = await prepareNotificationsTest(1)
34 userAccessToken = res.userAccessToken
35 userNotifications = res.userNotifications
36 server = res.servers[0]
38 await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port)
40 for (let i = 0; i < 10; i++) {
41 await uploadRandomVideo(server, false)
44 await waitJobs([ server ])
47 describe('Mark as read', function () {
49 it('Should mark as read some notifications', async function () {
50 const res = await getUserNotifications(server.url, userAccessToken, 2, 3)
51 const ids = res.body.data.map(n => n.id)
53 await markAsReadNotifications(server.url, userAccessToken, ids)
56 it('Should have the notifications marked as read', async function () {
57 const res = await getUserNotifications(server.url, userAccessToken, 0, 10)
59 const notifications = res.body.data as UserNotification[]
60 expect(notifications[0].read).to.be.false
61 expect(notifications[1].read).to.be.false
62 expect(notifications[2].read).to.be.true
63 expect(notifications[3].read).to.be.true
64 expect(notifications[4].read).to.be.true
65 expect(notifications[5].read).to.be.false
68 it('Should only list read notifications', async function () {
69 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false)
71 const notifications = res.body.data as UserNotification[]
72 for (const notification of notifications) {
73 expect(notification.read).to.be.true
77 it('Should only list unread notifications', async function () {
78 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
80 const notifications = res.body.data as UserNotification[]
81 for (const notification of notifications) {
82 expect(notification.read).to.be.false
86 it('Should mark as read all notifications', async function () {
87 await markAsReadAllNotifications(server.url, userAccessToken)
89 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
91 expect(res.body.total).to.equal(0)
92 expect(res.body.data).to.have.lengthOf(0)
96 describe('Notification settings', function () {
97 let baseParams: CheckerBaseParams
103 socketNotifications: userNotifications,
104 token: userAccessToken
108 it('Should not have notifications', async function () {
111 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
112 newVideoFromSubscription: UserNotificationSettingValue.NONE
116 const res = await getMyUserInformation(server.url, userAccessToken)
117 const info = res.body as User
118 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
121 const { name, uuid } = await uploadRandomVideo(server)
123 const check = { web: true, mail: true }
124 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
127 it('Should only have web notifications', async function () {
130 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
131 newVideoFromSubscription: UserNotificationSettingValue.WEB
135 const res = await getMyUserInformation(server.url, userAccessToken)
136 const info = res.body as User
137 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
140 const { name, uuid } = await uploadRandomVideo(server)
143 const check = { mail: true, web: false }
144 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
148 const check = { mail: false, web: true }
149 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
153 it('Should only have mail notifications', async function () {
156 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
157 newVideoFromSubscription: UserNotificationSettingValue.EMAIL
161 const res = await getMyUserInformation(server.url, userAccessToken)
162 const info = res.body as User
163 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
166 const { name, uuid } = await uploadRandomVideo(server)
169 const check = { mail: false, web: true }
170 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
174 const check = { mail: true, web: false }
175 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
179 it('Should have email and web notifications', async function () {
182 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
183 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
187 const res = await getMyUserInformation(server.url, userAccessToken)
188 const info = res.body as User
189 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
190 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
194 const { name, uuid } = await uploadRandomVideo(server)
196 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
200 after(async function () {
201 MockSmtpServer.Instance.kill()
203 await cleanupTests([ server ])