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