1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
6 checkMyVideoImportIsFinished,
8 checkNewVideoFromSubscription,
10 checkVideoStudioEditionIsFinished,
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, VideoStudioTask } from '@shared/models'
19 import { cleanupTests, findExternalSavedVideo, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
21 describe('Test user notifications', function () {
22 let servers: PeerTubeServer[] = []
23 let userAccessToken: string
25 let userNotifications: UserNotification[] = []
26 let adminNotifications: UserNotification[] = []
27 let adminNotificationsServer2: UserNotification[] = []
28 let emails: object[] = []
32 before(async function () {
35 const res = await prepareNotificationsTest(3)
37 userAccessToken = res.userAccessToken
39 userNotifications = res.userNotifications
40 adminNotifications = res.adminNotifications
41 adminNotificationsServer2 = res.adminNotificationsServer2
42 channelId = res.channelId
45 describe('New video from my subscription notification', function () {
46 let baseParams: CheckerBaseParams
52 socketNotifications: userNotifications,
53 token: userAccessToken
57 it('Should not send notifications if the user does not follow the video publisher', async function () {
60 await uploadRandomVideoOnServers(servers, 1)
62 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
63 expect(notification).to.be.undefined
65 expect(emails).to.have.lengthOf(0)
66 expect(userNotifications).to.have.lengthOf(0)
69 it('Should send a new video notification if the user follows the local video publisher', async function () {
72 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
73 await waitJobs(servers)
75 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
76 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
79 it('Should send a new video notification from a remote account', async function () {
80 this.timeout(150000) // Server 2 has transcoding enabled
82 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[1].host })
83 await waitJobs(servers)
85 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2)
86 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
89 it('Should send a new video notification on a scheduled publication', async function () {
93 const updateAt = new Date(new Date().getTime() + 2000)
96 privacy: VideoPrivacy.PRIVATE,
98 updateAt: updateAt.toISOString(),
99 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
102 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
105 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
108 it('Should send a new video notification on a remote scheduled publication', async function () {
112 const updateAt = new Date(new Date().getTime() + 2000)
115 privacy: VideoPrivacy.PRIVATE,
117 updateAt: updateAt.toISOString(),
118 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
121 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
122 await waitJobs(servers)
125 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
128 it('Should not send a notification before the video is published', async function () {
131 const updateAt = new Date(new Date().getTime() + 1000000)
134 privacy: VideoPrivacy.PRIVATE,
136 updateAt: updateAt.toISOString(),
137 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
140 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
143 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
146 it('Should send a new video notification when a video becomes public', async function () {
149 const data = { privacy: VideoPrivacy.PRIVATE }
150 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
152 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
154 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
156 await waitJobs(servers)
157 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
160 it('Should send a new video notification when a remote video becomes public', async function () {
163 const data = { privacy: VideoPrivacy.PRIVATE }
164 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
166 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
168 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.PUBLIC } })
170 await waitJobs(servers)
171 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
174 it('Should not send a new video notification when a video becomes unlisted', async function () {
177 const data = { privacy: VideoPrivacy.PRIVATE }
178 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 1, data)
180 await servers[0].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
182 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
185 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
188 const data = { privacy: VideoPrivacy.PRIVATE }
189 const { name, uuid, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
191 await servers[1].videos.update({ id: uuid, attributes: { privacy: VideoPrivacy.UNLISTED } })
193 await waitJobs(servers)
194 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
197 it('Should send a new video notification after a video import', async function () {
200 const name = 'video import ' + buildUUID()
205 privacy: VideoPrivacy.PUBLIC,
206 targetUrl: FIXTURE_URLS.goodVideo
208 const { video } = await servers[0].imports.importVideo({ attributes })
210 await waitJobs(servers)
212 await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
216 describe('My video is published', function () {
217 let baseParams: CheckerBaseParams
223 socketNotifications: adminNotificationsServer2,
224 token: servers[1].accessToken
228 it('Should not send a notification if transcoding is not enabled', async function () {
231 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 1)
232 await waitJobs(servers)
234 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
237 it('Should not send a notification if the wait transcoding is false', async function () {
238 this.timeout(100_000)
240 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
241 await waitJobs(servers)
243 const notification = await servers[0].notifications.getLatest({ token: userAccessToken })
245 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
249 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
250 this.timeout(100_000)
252 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
253 await waitJobs(servers)
255 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
258 it('Should send a notification with a transcoded video', async function () {
259 this.timeout(100_000)
261 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
262 await waitJobs(servers)
264 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
267 it('Should send a notification when an imported video is transcoded', async function () {
270 const name = 'video import ' + buildUUID()
275 privacy: VideoPrivacy.PUBLIC,
276 targetUrl: FIXTURE_URLS.goodVideo,
277 waitTranscoding: true
279 const { video } = await servers[1].imports.importVideo({ attributes })
281 await waitJobs(servers)
282 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID: video.shortUUID, checkType: 'presence' })
285 it('Should send a notification when the scheduled update has been proceeded', async function () {
289 const updateAt = new Date(new Date().getTime() + 2000)
292 privacy: VideoPrivacy.PRIVATE,
294 updateAt: updateAt.toISOString(),
295 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
298 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
301 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
304 it('Should not send a notification before the video is published', async function () {
307 const updateAt = new Date(new Date().getTime() + 1000000)
310 privacy: VideoPrivacy.PRIVATE,
312 updateAt: updateAt.toISOString(),
313 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
316 const { name, shortUUID } = await uploadRandomVideoOnServers(servers, 2, data)
319 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'absence' })
323 describe('My live replay is published', function () {
325 let baseParams: CheckerBaseParams
331 socketNotifications: adminNotificationsServer2,
332 token: servers[1].accessToken
336 it('Should send a notification is a live replay of a non permanent live is published', async function () {
339 const { shortUUID } = await servers[1].live.create({
341 name: 'non permanent live',
342 privacy: VideoPrivacy.PUBLIC,
343 channelId: servers[1].store.channel.id,
345 replaySettings: { privacy: VideoPrivacy.PUBLIC },
350 const ffmpegCommand = await servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID })
352 await waitJobs(servers)
353 await servers[1].live.waitUntilPublished({ videoId: shortUUID })
355 await stopFfmpeg(ffmpegCommand)
356 await servers[1].live.waitUntilReplacedByReplay({ videoId: shortUUID })
358 await waitJobs(servers)
359 await checkVideoIsPublished({ ...baseParams, videoName: 'non permanent live', shortUUID, checkType: 'presence' })
362 it('Should send a notification is a live replay of a permanent live is published', async function () {
365 const { shortUUID } = await servers[1].live.create({
367 name: 'permanent live',
368 privacy: VideoPrivacy.PUBLIC,
369 channelId: servers[1].store.channel.id,
371 replaySettings: { privacy: VideoPrivacy.PUBLIC },
376 const ffmpegCommand = await servers[1].live.sendRTMPStreamInVideo({ videoId: shortUUID })
378 await waitJobs(servers)
379 await servers[1].live.waitUntilPublished({ videoId: shortUUID })
381 const liveDetails = await servers[1].videos.get({ id: shortUUID })
383 await stopFfmpeg(ffmpegCommand)
385 await servers[1].live.waitUntilWaiting({ videoId: shortUUID })
386 await waitJobs(servers)
388 const video = await findExternalSavedVideo(servers[1], liveDetails)
389 expect(video).to.exist
391 await checkVideoIsPublished({ ...baseParams, videoName: video.name, shortUUID: video.shortUUID, checkType: 'presence' })
395 describe('Video studio', function () {
396 let baseParams: CheckerBaseParams
402 socketNotifications: adminNotificationsServer2,
403 token: servers[1].accessToken
407 it('Should send a notification after studio edition', async function () {
410 const { name, shortUUID, id } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
412 await waitJobs(servers)
413 await checkVideoIsPublished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
415 const tasks: VideoStudioTask[] = [
424 await servers[1].videoStudio.createEditionTasks({ videoId: id, tasks })
425 await waitJobs(servers)
427 await checkVideoStudioEditionIsFinished({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
431 describe('My video is imported', function () {
432 let baseParams: CheckerBaseParams
438 socketNotifications: adminNotifications,
439 token: servers[0].accessToken
443 it('Should send a notification when the video import failed', async function () {
446 const name = 'video import ' + buildUUID()
451 privacy: VideoPrivacy.PRIVATE,
452 targetUrl: FIXTURE_URLS.badVideo
454 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
456 await waitJobs(servers)
458 const url = FIXTURE_URLS.badVideo
459 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: false, checkType: 'presence' })
462 it('Should send a notification when the video import succeeded', async function () {
465 const name = 'video import ' + buildUUID()
470 privacy: VideoPrivacy.PRIVATE,
471 targetUrl: FIXTURE_URLS.goodVideo
473 const { video: { shortUUID } } = await servers[0].imports.importVideo({ attributes })
475 await waitJobs(servers)
477 const url = FIXTURE_URLS.goodVideo
478 await checkMyVideoImportIsFinished({ ...baseParams, videoName: name, shortUUID, url, success: true, checkType: 'presence' })
482 describe('New actor follow', function () {
483 let baseParams: CheckerBaseParams
484 const myChannelName = 'super channel name'
485 const myUserName = 'super user name'
491 socketNotifications: userNotifications,
492 token: userAccessToken
495 await servers[0].users.updateMe({ displayName: 'super root name' })
497 await servers[0].users.updateMe({
498 token: userAccessToken,
499 displayName: myUserName
502 await servers[1].users.updateMe({ displayName: 'super root 2 name' })
504 await servers[0].channels.update({
505 token: userAccessToken,
506 channelName: 'user_1_channel',
507 attributes: { displayName: myChannelName }
511 it('Should notify when a local channel is following one of our channel', async function () {
514 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
515 await waitJobs(servers)
517 await checkNewActorFollow({
519 followType: 'channel',
520 followerName: 'root',
521 followerDisplayName: 'super root name',
522 followingDisplayName: myChannelName,
523 checkType: 'presence'
526 await servers[0].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
529 it('Should notify when a remote channel is following one of our channel', async function () {
532 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
533 await waitJobs(servers)
535 await checkNewActorFollow({
537 followType: 'channel',
538 followerName: 'root',
539 followerDisplayName: 'super root 2 name',
540 followingDisplayName: myChannelName,
541 checkType: 'presence'
544 await servers[1].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
547 // PeerTube does not support account -> account follows
548 // it('Should notify when a local account is following one of our channel', async function () {
549 // this.timeout(50000)
551 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@' + servers[0].host)
553 // await waitJobs(servers)
555 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
558 // it('Should notify when a remote account is following one of our channel', async function () {
559 // this.timeout(50000)
561 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@' + servers[0].host)
563 // await waitJobs(servers)
565 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
569 after(async function () {
570 MockSmtpServer.Instance.kill()
572 await cleanupTests(servers)