Split notification tests
[oweals/peertube.git] / server / tests / api / notifications / notifications-api.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
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'
9 import {
10   CheckerBaseParams,
11   checkNewVideoFromSubscription,
12   getAllNotificationsSettings,
13   getUserNotifications,
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'
20
21 const expect = chai.expect
22
23 describe('Test notifications API', function () {
24   let server: ServerInfo
25   let userNotifications: UserNotification[] = []
26   let userAccessToken: string
27   let emails: object[] = []
28
29   before(async function () {
30     this.timeout(120000)
31
32     const res = await prepareNotificationsTest(1)
33     emails = res.emails
34     userAccessToken = res.userAccessToken
35     userNotifications = res.userNotifications
36     server = res.servers[0]
37
38     await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port)
39
40     for (let i = 0; i < 10; i++) {
41       await uploadRandomVideo(server, false)
42     }
43
44     await waitJobs([ server ])
45   })
46
47   describe('Mark as read', function () {
48
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)
52
53       await markAsReadNotifications(server.url, userAccessToken, ids)
54     })
55
56     it('Should have the notifications marked as read', async function () {
57       const res = await getUserNotifications(server.url, userAccessToken, 0, 10)
58
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
66     })
67
68     it('Should only list read notifications', async function () {
69       const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false)
70
71       const notifications = res.body.data as UserNotification[]
72       for (const notification of notifications) {
73         expect(notification.read).to.be.true
74       }
75     })
76
77     it('Should only list unread notifications', async function () {
78       const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
79
80       const notifications = res.body.data as UserNotification[]
81       for (const notification of notifications) {
82         expect(notification.read).to.be.false
83       }
84     })
85
86     it('Should mark as read all notifications', async function () {
87       await markAsReadAllNotifications(server.url, userAccessToken)
88
89       const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true)
90
91       expect(res.body.total).to.equal(0)
92       expect(res.body.data).to.have.lengthOf(0)
93     })
94   })
95
96   describe('Notification settings', function () {
97     let baseParams: CheckerBaseParams
98
99     before(() => {
100       baseParams = {
101         server: server,
102         emails,
103         socketNotifications: userNotifications,
104         token: userAccessToken
105       }
106     })
107
108     it('Should not have notifications', async function () {
109       this.timeout(20000)
110
111       await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
112         newVideoFromSubscription: UserNotificationSettingValue.NONE
113       }))
114
115       {
116         const res = await getMyUserInformation(server.url, userAccessToken)
117         const info = res.body as User
118         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
119       }
120
121       const { name, uuid } = await uploadRandomVideo(server)
122
123       const check = { web: true, mail: true }
124       await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
125     })
126
127     it('Should only have web notifications', async function () {
128       this.timeout(20000)
129
130       await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
131         newVideoFromSubscription: UserNotificationSettingValue.WEB
132       }))
133
134       {
135         const res = await getMyUserInformation(server.url, userAccessToken)
136         const info = res.body as User
137         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
138       }
139
140       const { name, uuid } = await uploadRandomVideo(server)
141
142       {
143         const check = { mail: true, web: false }
144         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
145       }
146
147       {
148         const check = { mail: false, web: true }
149         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
150       }
151     })
152
153     it('Should only have mail notifications', async function () {
154       this.timeout(20000)
155
156       await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
157         newVideoFromSubscription: UserNotificationSettingValue.EMAIL
158       }))
159
160       {
161         const res = await getMyUserInformation(server.url, userAccessToken)
162         const info = res.body as User
163         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
164       }
165
166       const { name, uuid } = await uploadRandomVideo(server)
167
168       {
169         const check = { mail: false, web: true }
170         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
171       }
172
173       {
174         const check = { mail: true, web: false }
175         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
176       }
177     })
178
179     it('Should have email and web notifications', async function () {
180       this.timeout(20000)
181
182       await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), {
183         newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
184       }))
185
186       {
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
191         )
192       }
193
194       const { name, uuid } = await uploadRandomVideo(server)
195
196       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
197     })
198   })
199
200   after(async function () {
201     MockSmtpServer.Instance.kill()
202
203     await cleanupTests([ server ])
204   })
205 })