]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/notifications/user-notifications.ts
Fix multiple servers tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / user-notifications.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 {
6 CheckerBaseParams,
7 checkMyVideoImportIsFinished,
8 checkNewActorFollow,
9 checkNewVideoFromSubscription,
10 checkVideoIsPublished,
11 FIXTURE_URLS,
12 MockSmtpServer,
13 prepareNotificationsTest,
14 uploadRandomVideoOnServers
15 } from '@server/tests/shared'
16 import { wait } from '@shared/core-utils'
17 import { buildUUID } from '@shared/extra-utils'
18 import { UserNotification, UserNotificationType, VideoPrivacy } from '@shared/models'
19 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
20
21 const expect = chai.expect
22
23 describe('Test user notifications', function () {
24 let servers: PeerTubeServer[] = []
25 let userAccessToken: string
26 let userNotifications: UserNotification[] = []
27 let adminNotifications: UserNotification[] = []
28 let adminNotificationsServer2: UserNotification[] = []
29 let emails: object[] = []
30 let channelId: number
31
32 before(async function () {
33 this.timeout(120000)
34
35 const res = await prepareNotificationsTest(3)
36 emails = res.emails
37 userAccessToken = res.userAccessToken
38 servers = res.servers
39 userNotifications = res.userNotifications
40 adminNotifications = res.adminNotifications
41 adminNotificationsServer2 = res.adminNotificationsServer2
42 channelId = res.channelId
43 })
44
45 describe('New video from my subscription notification', function () {
46 let baseParams: CheckerBaseParams
47
48 before(() => {
49 baseParams = {
50 server: servers[0],
51 emails,
52 socketNotifications: userNotifications,
53 token: userAccessToken
54 }
55 })
56
57 it('Should not send notifications if the user does not follow the video publisher', async function () {
58 this.timeout(50000)
59
60 await uploadRandomVideoOnServers(servers, 1)
61
62 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
63 expect(notification).to.be.undefined
64
65 expect(emails).to.have.lengthOf(0)
66 expect(userNotifications).to.have.lengthOf(0)
67 })
68
69 it('Should send a new video notification if the user follows the local video publisher', async function () {
70 this.timeout(15000)
71
72 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
73 await waitJobs(servers)
74
75 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
76 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
77 })
78
79 it('Should send a new video notification from a remote account', async function () {
80 this.timeout(150000) // Server 2 has transcoding enabled
81
82 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
83 await waitJobs(servers)
84
85 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2)
86 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
87 })
88
89 it('Should send a new video notification on a scheduled publication', async function () {
90 this.timeout(50000)
91
92 // In 2 seconds
93 const updateAt = new Date(new Date().getTime() + 2000)
94
95 const data = {
96 privacy: VideoPrivacy.PRIVATE,
97 scheduleUpdate: {
98 updateAt: updateAt.toISOString(),
99 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
100 }
101 }
102 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
103
104 await wait(6000)
105 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
106 })
107
108 it('Should send a new video notification on a remote scheduled publication', async function () {
109 this.timeout(100000)
110
111 // In 2 seconds
112 const updateAt = new Date(new Date().getTime() + 2000)
113
114 const data = {
115 privacy: VideoPrivacy.PRIVATE,
116 scheduleUpdate: {
117 updateAt: updateAt.toISOString(),
118 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
119 }
120 }
121 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
122 await waitJobs(servers)
123
124 await wait(6000)
125 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
126 })
127
128 it('Should not send a notification before the video is published', async function () {
129 this.timeout(150000)
130
131 const updateAt = new Date(new Date().getTime() + 1000000)
132
133 const data = {
134 privacy: VideoPrivacy.PRIVATE,
135 scheduleUpdate: {
136 updateAt: updateAt.toISOString(),
137 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
138 }
139 }
140 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
141
142 await wait(6000)
143 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
144 })
145
146 it('Should send a new video notification when a video becomes public', async function () {
147 this.timeout(50000)
148
149 const data = { privacy: VideoPrivacy.PRIVATE }
150 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
151
152 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
153
154 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
155
156 await waitJobs(servers)
157 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
158 })
159
160 it('Should send a new video notification when a remote video becomes public', async function () {
161 this.timeout(50000)
162
163 const data = { privacy: VideoPrivacy.PRIVATE }
164 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
165
166 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
167
168 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
169
170 await waitJobs(servers)
171 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
172 })
173
174 it('Should not send a new video notification when a video becomes unlisted', async function () {
175 this.timeout(50000)
176
177 const data = { privacy: VideoPrivacy.PRIVATE }
178 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
179
180 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
181
182 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
183 })
184
185 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
186 this.timeout(50000)
187
188 const data = { privacy: VideoPrivacy.PRIVATE }
189 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
190
191 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
192
193 await waitJobs(servers)
194 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
195 })
196
197 it('Should send a new video notification after a video import', async function () {
198 this.timeout(100000)
199
200 const name = 'video import ' + buildUUID()
201
202 const attributes = {
203 name,
204 channelId,
205 privacy: VideoPrivacy.PUBLIC,
206 targetUrl: FIXTURE_URLS.goodVideo
207 }
208 const { video } = await servers[0].imports.importVideo({ attributes })
209
210 await waitJobs(servers)
211
212 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
213 })
214 })
215
216 describe('My video is published', function () {
217 let baseParams: CheckerBaseParams
218
219 before(() => {
220 baseParams = {
221 server: servers[1],
222 emails,
223 socketNotifications: adminNotificationsServer2,
224 token: servers[1].accessToken
225 }
226 })
227
228 it('Should not send a notification if transcoding is not enabled', async function () {
229 this.timeout(50000)
230
231 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
232 await waitJobs(servers)
233
234 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
235 })
236
237 it('Should not send a notification if the wait transcoding is false', async function () {
238 this.timeout(100_000)
239
240 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
241 await waitJobs(servers)
242
243 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
244 if (notification) {
245 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
246 }
247 })
248
249 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
250 this.timeout(50000)
251
252 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
253 await waitJobs(servers)
254
255 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
256 })
257
258 it('Should send a notification with a transcoded video', async function () {
259 this.timeout(50000)
260
261 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
262 await waitJobs(servers)
263
264 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
265 })
266
267 it('Should send a notification when an imported video is transcoded', async function () {
268 this.timeout(120000)
269
270 const name = 'video import ' + buildUUID()
271
272 const attributes = {
273 name,
274 channelId,
275 privacy: VideoPrivacy.PUBLIC,
276 targetUrl: FIXTURE_URLS.goodVideo,
277 waitTranscoding: true
278 }
279 const { video } = await servers[1].imports.importVideo({ attributes })
280
281 await waitJobs(servers)
282 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
283 })
284
285 it('Should send a notification when the scheduled update has been proceeded', async function () {
286 this.timeout(70000)
287
288 // In 2 seconds
289 const updateAt = new Date(new Date().getTime() + 2000)
290
291 const data = {
292 privacy: VideoPrivacy.PRIVATE,
293 scheduleUpdate: {
294 updateAt: updateAt.toISOString(),
295 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
296 }
297 }
298 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
299
300 await wait(6000)
301 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
302 })
303
304 it('Should not send a notification before the video is published', async function () {
305 this.timeout(50000)
306
307 const updateAt = new Date(new Date().getTime() + 1000000)
308
309 const data = {
310 privacy: VideoPrivacy.PRIVATE,
311 scheduleUpdate: {
312 updateAt: updateAt.toISOString(),
313 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
314 }
315 }
316 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
317
318 await wait(6000)
319 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
320 })
321 })
322
323 describe('My video is imported', function () {
324 let baseParams: CheckerBaseParams
325
326 before(() => {
327 baseParams = {
328 server: servers[0],
329 emails,
330 socketNotifications: adminNotifications,
331 token: servers[0].accessToken
332 }
333 })
334
335 it('Should send a notification when the video import failed', async function () {
336 this.timeout(70000)
337
338 const name = 'video import ' + buildUUID()
339
340 const attributes = {
341 name,
342 channelId,
343 privacy: VideoPrivacy.PRIVATE,
344 targetUrl: FIXTURE_URLS.badVideo
345 }
346 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
347
348 await waitJobs(servers)
349
350 const url = FIXTURE_URLS.badVideo
351 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: false, checkType: 'presence' })
352 })
353
354 it('Should send a notification when the video import succeeded', async function () {
355 this.timeout(70000)
356
357 const name = 'video import ' + buildUUID()
358
359 const attributes = {
360 name,
361 channelId,
362 privacy: VideoPrivacy.PRIVATE,
363 targetUrl: FIXTURE_URLS.goodVideo
364 }
365 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
366
367 await waitJobs(servers)
368
369 const url = FIXTURE_URLS.goodVideo
370 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: true, checkType: 'presence' })
371 })
372 })
373
374 describe('New actor follow', function () {
375 let baseParams: CheckerBaseParams
376 const myChannelName = 'super channel name'
377 const myUserName = 'super user name'
378
379 before(async () => {
380 baseParams = {
381 server: servers[0],
382 emails,
383 socketNotifications: userNotifications,
384 token: userAccessToken
385 }
386
387 await servers[0].users.updateMe({ displayName: 'super root name' })
388
389 await servers[0].users.updateMe({
390 token: userAccessToken,
391 displayName: myUserName
392 })
393
394 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
395
396 await servers[0].channels.update({
397 token: userAccessToken,
398 channelName: 'user_1_channel',
399 attributes: { displayName: myChannelName }
400 })
401 })
402
403 it('Should notify when a local channel is following one of our channel', async function () {
404 this.timeout(50000)
405
406 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
407 await waitJobs(servers)
408
409 await checkNewActorFollow({
410 ...baseParams,
411 followType: 'channel',
412 followerName: 'root',
413 followerDisplayName: 'super root name',
414 followingDisplayName: myChannelName,
415 checkType: 'presence'
416 })
417
418 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
419 })
420
421 it('Should notify when a remote channel is following one of our channel', async function () {
422 this.timeout(50000)
423
424 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
425 await waitJobs(servers)
426
427 await checkNewActorFollow({
428 ...baseParams,
429 followType: 'channel',
430 followerName: 'root',
431 followerDisplayName: 'super root 2 name',
432 followingDisplayName: myChannelName,
433 checkType: 'presence'
434 })
435
436 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
437 })
438
439 // PeerTube does not support accout -> account follows
440 // it('Should notify when a local account is following one of our channel', async function () {
441 // this.timeout(50000)
442 //
443 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:' + servers[0].port)
444 //
445 // await waitJobs(servers)
446 //
447 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
448 // })
449
450 // it('Should notify when a remote account is following one of our channel', async function () {
451 // this.timeout(50000)
452 //
453 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:' + servers[0].port)
454 //
455 // await waitJobs(servers)
456 //
457 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
458 // })
459 })
460
461 after(async function () {
462 MockSmtpServer.Instance.kill()
463
464 await cleanupTests(servers)
465 })
466 })