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