]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/notifications/user-notifications.ts
Fix live constraints tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / user-notifications.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
cef534ed 2
cef534ed 3import 'mocha'
8eb07b01 4import * as chai from 'chai'
cef534ed
C
5import {
6 CheckerBaseParams,
f7cc67b4
C
7 checkMyVideoImportIsFinished,
8 checkNewActorFollow,
cef534ed 9 checkNewVideoFromSubscription,
f7cc67b4 10 checkVideoIsPublished,
26e3e98f 11 checkVideoStudioEditionIsFinished,
59bbcced 12 FIXTURE_URLS,
2c27e704 13 MockSmtpServer,
59bbcced 14 prepareNotificationsTest,
c55e3d72
C
15 uploadRandomVideoOnServers
16} from '@server/tests/shared'
0628157f
C
17import { wait } from '@shared/core-utils'
18import { buildUUID } from '@shared/extra-utils'
26e3e98f
C
19import { UserNotification, UserNotificationType, VideoPrivacy, VideoStudioTask } from '@shared/models'
20import { cleanupTests, findExternalSavedVideo, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
cef534ed
C
21
22const expect = chai.expect
23
8eb07b01 24describe('Test user notifications', function () {
254d3579 25 let servers: PeerTubeServer[] = []
cef534ed 26 let userAccessToken: string
1808a1f8 27
8eb07b01
C
28 let userNotifications: UserNotification[] = []
29 let adminNotifications: UserNotification[] = []
30 let adminNotificationsServer2: UserNotification[] = []
31 let emails: object[] = []
1808a1f8 32
dc133480
C
33 let channelId: number
34
cef534ed
C
35 before(async function () {
36 this.timeout(120000)
37
8eb07b01
C
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
cef534ed
C
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 () {
59fd824c 61 this.timeout(50000)
f7effe8d 62
8eb07b01 63 await uploadRandomVideoOnServers(servers, 1)
cef534ed 64
5d3c5f27 65 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
cef534ed
C
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 () {
89ada4e2 73 this.timeout(15000)
2f1548fd 74
89d241a7 75 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
2f1548fd 76 await waitJobs(servers)
cef534ed 77
29837f88
C
78 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
79 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
80 })
81
82 it('Should send a new video notification from a remote account', async function () {
83befebe 83 this.timeout(150000) // Server 2 has transcoding enabled
cef534ed 84
89d241a7 85 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
2f1548fd 86 await waitJobs(servers)
cef534ed 87
29837f88
C
88 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2)
89 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
90 })
91
92 it('Should send a new video notification on a scheduled publication', async function () {
59fd824c 93 this.timeout(50000)
cef534ed 94
cef534ed 95 // In 2 seconds
a1587156 96 const updateAt = new Date(new Date().getTime() + 2000)
cef534ed
C
97
98 const data = {
99 privacy: VideoPrivacy.PRIVATE,
100 scheduleUpdate: {
101 updateAt: updateAt.toISOString(),
d23dd9fb 102 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
103 }
104 }
29837f88 105 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed
C
106
107 await wait(6000)
29837f88 108 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
109 })
110
111 it('Should send a new video notification on a remote scheduled publication', async function () {
8ace6805 112 this.timeout(100000)
cef534ed 113
cef534ed 114 // In 2 seconds
a1587156 115 const updateAt = new Date(new Date().getTime() + 2000)
cef534ed
C
116
117 const data = {
118 privacy: VideoPrivacy.PRIVATE,
119 scheduleUpdate: {
120 updateAt: updateAt.toISOString(),
d23dd9fb 121 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
122 }
123 }
29837f88 124 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
e8d246d5 125 await waitJobs(servers)
cef534ed
C
126
127 await wait(6000)
29837f88 128 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
129 })
130
131 it('Should not send a notification before the video is published', async function () {
21d68e68 132 this.timeout(150000)
cef534ed 133
a1587156 134 const updateAt = new Date(new Date().getTime() + 1000000)
cef534ed
C
135
136 const data = {
137 privacy: VideoPrivacy.PRIVATE,
138 scheduleUpdate: {
139 updateAt: updateAt.toISOString(),
d23dd9fb 140 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
cef534ed
C
141 }
142 }
29837f88 143 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed
C
144
145 await wait(6000)
29837f88 146 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
147 })
148
149 it('Should send a new video notification when a video becomes public', async function () {
59fd824c 150 this.timeout(50000)
cef534ed 151
cef534ed 152 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 153 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed 154
29837f88 155 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed 156
89d241a7 157 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
cef534ed 158
9f39c546 159 await waitJobs(servers)
29837f88 160 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
161 })
162
163 it('Should send a new video notification when a remote video becomes public', async function () {
59fd824c 164 this.timeout(50000)
cef534ed 165
cef534ed 166 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 167 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
cef534ed 168
29837f88 169 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed 170
89d241a7 171 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
cef534ed
C
172
173 await waitJobs(servers)
29837f88 174 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
cef534ed
C
175 })
176
177 it('Should not send a new video notification when a video becomes unlisted', async function () {
59fd824c 178 this.timeout(50000)
cef534ed 179
cef534ed 180 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 181 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
cef534ed 182
89d241a7 183 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
cef534ed 184
29837f88 185 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
186 })
187
188 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
59fd824c 189 this.timeout(50000)
cef534ed 190
cef534ed 191 const data = { privacy: VideoPrivacy.PRIVATE }
29837f88 192 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
cef534ed 193
89d241a7 194 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
cef534ed
C
195
196 await waitJobs(servers)
29837f88 197 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
cef534ed
C
198 })
199
200 it('Should send a new video notification after a video import', async function () {
7ccddd7b 201 this.timeout(100000)
cef534ed 202
d4a8e7a6 203 const name = 'video import ' + buildUUID()
cef534ed
C
204
205 const attributes = {
dc133480 206 name,
cef534ed
C
207 channelId,
208 privacy: VideoPrivacy.PUBLIC,
59bbcced 209 targetUrl: FIXTURE_URLS.goodVideo
cef534ed 210 }
89d241a7 211 const { video } = await servers[0].imports.importVideo({ attributes })
cef534ed
C
212
213 await waitJobs(servers)
214
29837f88 215 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
cef534ed
C
216 })
217 })
218
dc133480
C
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 () {
59fd824c 232 this.timeout(50000)
f7effe8d 233
29837f88 234 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
dc133480
C
235 await waitJobs(servers)
236
29837f88 237 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
dc133480
C
238 })
239
240 it('Should not send a notification if the wait transcoding is false', async function () {
5d3c5f27 241 this.timeout(100_000)
dc133480 242
8eb07b01 243 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
dc133480
C
244 await waitJobs(servers)
245
5d3c5f27 246 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
dc133480
C
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
29837f88 255 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
dc133480
C
256 await waitJobs(servers)
257
29837f88 258 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480
C
259 })
260
261 it('Should send a notification with a transcoded video', async function () {
262 this.timeout(50000)
263
29837f88 264 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
dc133480
C
265 await waitJobs(servers)
266
29837f88 267 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480
C
268 })
269
270 it('Should send a notification when an imported video is transcoded', async function () {
33675a47 271 this.timeout(120000)
dc133480 272
d4a8e7a6 273 const name = 'video import ' + buildUUID()
dc133480
C
274
275 const attributes = {
276 name,
277 channelId,
278 privacy: VideoPrivacy.PUBLIC,
59bbcced 279 targetUrl: FIXTURE_URLS.goodVideo,
dc133480
C
280 waitTranscoding: true
281 }
89d241a7 282 const { video } = await servers[1].imports.importVideo({ attributes })
dc133480
C
283
284 await waitJobs(servers)
29837f88 285 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
dc133480
C
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
a1587156 292 const updateAt = new Date(new Date().getTime() + 2000)
dc133480
C
293
294 const data = {
295 privacy: VideoPrivacy.PRIVATE,
296 scheduleUpdate: {
297 updateAt: updateAt.toISOString(),
d23dd9fb 298 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
dc133480
C
299 }
300 }
29837f88 301 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
dc133480
C
302
303 await wait(6000)
29837f88 304 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
dc133480 305 })
f7effe8d
JM
306
307 it('Should not send a notification before the video is published', async function () {
94d721ef 308 this.timeout(50000)
f7effe8d 309
a1587156 310 const updateAt = new Date(new Date().getTime() + 1000000)
f7effe8d
JM
311
312 const data = {
313 privacy: VideoPrivacy.PRIVATE,
314 scheduleUpdate: {
315 updateAt: updateAt.toISOString(),
d23dd9fb 316 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
f7effe8d
JM
317 }
318 }
29837f88 319 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
f7effe8d
JM
320
321 await wait(6000)
29837f88 322 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
f7effe8d 323 })
dc133480
C
324 })
325
26e3e98f
C
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
92e66e04 396 describe('Video studio', function () {
1808a1f8
C
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
92e66e04 408 it('Should send a notification after studio edition', async function () {
1808a1f8
C
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
92e66e04 416 const tasks: VideoStudioTask[] = [
1808a1f8
C
417 {
418 name: 'cut',
419 options: {
420 start: 0,
421 end: 1
422 }
423 }
424 ]
92e66e04 425 await servers[1].videoStudio.createEditionTasks({ videoId: id, tasks })
1808a1f8
C
426 await waitJobs(servers)
427
92e66e04 428 await checkVideoStudioEditionIsFinished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
1808a1f8
C
429 })
430 })
431
dc133480
C
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
d4a8e7a6 447 const name = 'video import ' + buildUUID()
dc133480
C
448
449 const attributes = {
450 name,
451 channelId,
452 privacy: VideoPrivacy.PRIVATE,
59bbcced 453 targetUrl: FIXTURE_URLS.badVideo
dc133480 454 }
29837f88 455 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
dc133480
C
456
457 await waitJobs(servers)
29837f88
C
458
459 const url = FIXTURE_URLS.badVideo
460 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: false, checkType: 'presence' })
dc133480
C
461 })
462
463 it('Should send a notification when the video import succeeded', async function () {
464 this.timeout(70000)
465
d4a8e7a6 466 const name = 'video import ' + buildUUID()
dc133480
C
467
468 const attributes = {
469 name,
470 channelId,
471 privacy: VideoPrivacy.PRIVATE,
59bbcced 472 targetUrl: FIXTURE_URLS.goodVideo
dc133480 473 }
29837f88 474 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
dc133480
C
475
476 await waitJobs(servers)
29837f88
C
477
478 const url = FIXTURE_URLS.goodVideo
479 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: true, checkType: 'presence' })
cef534ed
C
480 })
481 })
482
f7cc67b4
C
483 describe('New actor follow', function () {
484 let baseParams: CheckerBaseParams
a1587156
C
485 const myChannelName = 'super channel name'
486 const myUserName = 'super user name'
f7cc67b4
C
487
488 before(async () => {
489 baseParams = {
490 server: servers[0],
491 emails,
492 socketNotifications: userNotifications,
493 token: userAccessToken
494 }
495
89d241a7 496 await servers[0].users.updateMe({ displayName: 'super root name' })
f7cc67b4 497
89d241a7 498 await servers[0].users.updateMe({
7926c5f9 499 token: userAccessToken,
f7cc67b4
C
500 displayName: myUserName
501 })
502
89d241a7 503 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
f7cc67b4 504
89d241a7 505 await servers[0].channels.update({
a5461888
C
506 token: userAccessToken,
507 channelName: 'user_1_channel',
508 attributes: { displayName: myChannelName }
509 })
f7cc67b4
C
510 })
511
512 it('Should notify when a local channel is following one of our channel', async function () {
59fd824c 513 this.timeout(50000)
f7cc67b4 514
89d241a7 515 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
516 await waitJobs(servers)
517
29837f88
C
518 await checkNewActorFollow({
519 ...baseParams,
520 followType: 'channel',
521 followerName: 'root',
522 followerDisplayName: 'super root name',
523 followingDisplayName: myChannelName,
524 checkType: 'presence'
525 })
f7cc67b4 526
89d241a7 527 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
528 })
529
530 it('Should notify when a remote channel is following one of our channel', async function () {
59fd824c 531 this.timeout(50000)
f7cc67b4 532
89d241a7 533 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
534 await waitJobs(servers)
535
29837f88
C
536 await checkNewActorFollow({
537 ...baseParams,
538 followType: 'channel',
539 followerName: 'root',
540 followerDisplayName: 'super root 2 name',
541 followingDisplayName: myChannelName,
542 checkType: 'presence'
543 })
f7cc67b4 544
89d241a7 545 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
f7cc67b4
C
546 })
547
0dd57e4d
C
548 // PeerTube does not support accout -> account follows
549 // it('Should notify when a local account is following one of our channel', async function () {
59fd824c 550 // this.timeout(50000)
0dd57e4d
C
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 // })
f7cc67b4 558
6ed2e4ea 559 // it('Should notify when a remote account is following one of our channel', async function () {
59fd824c 560 // this.timeout(50000)
6ed2e4ea
C
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 // })
f7cc67b4
C
568 })
569
7c3b7976 570 after(async function () {
89ada4e2
C
571 MockSmtpServer.Instance.kill()
572
7c3b7976 573 await cleanupTests(servers)
cef534ed
C
574 })
575})