1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { v4 as uuidv4 } from 'uuid'
11 uploadRandomVideoOnServers,
13 } from '../../../../shared/extra-utils'
14 import { ServerInfo } from '../../../../shared/extra-utils/index'
15 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
16 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
19 checkMyVideoImportIsFinished,
21 checkNewVideoFromSubscription,
22 checkVideoIsPublished,
24 prepareNotificationsTest
25 } from '../../../../shared/extra-utils/users/user-notifications'
26 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
27 import { getBadVideoUrl, getGoodVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
28 import { UserNotification, UserNotificationType } from '../../../../shared/models/users'
29 import { VideoPrivacy } from '../../../../shared/models/videos'
31 const expect = chai.expect
33 describe('Test user notifications', function () {
34 let servers: ServerInfo[] = []
35 let userAccessToken: string
36 let userNotifications: UserNotification[] = []
37 let adminNotifications: UserNotification[] = []
38 let adminNotificationsServer2: UserNotification[] = []
39 let emails: object[] = []
42 before(async function () {
45 const res = await prepareNotificationsTest(3)
47 userAccessToken = res.userAccessToken
49 userNotifications = res.userNotifications
50 adminNotifications = res.adminNotifications
51 adminNotificationsServer2 = res.adminNotificationsServer2
52 channelId = res.channelId
55 describe('New video from my subscription notification', function () {
56 let baseParams: CheckerBaseParams
62 socketNotifications: userNotifications,
63 token: userAccessToken
67 it('Should not send notifications if the user does not follow the video publisher', async function () {
70 await uploadRandomVideoOnServers(servers, 1)
72 const notification = await getLastNotification(servers[0].url, userAccessToken)
73 expect(notification).to.be.undefined
75 expect(emails).to.have.lengthOf(0)
76 expect(userNotifications).to.have.lengthOf(0)
79 it('Should send a new video notification if the user follows the local video publisher', async function () {
82 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port)
83 await waitJobs(servers)
85 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1)
86 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
89 it('Should send a new video notification from a remote account', async function () {
90 this.timeout(150000) // Server 2 has transcoding enabled
92 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[1].port)
93 await waitJobs(servers)
95 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2)
96 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
99 it('Should send a new video notification on a scheduled publication', async function () {
103 const updateAt = new Date(new Date().getTime() + 2000)
106 privacy: VideoPrivacy.PRIVATE,
108 updateAt: updateAt.toISOString(),
109 privacy: VideoPrivacy.PUBLIC
112 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
115 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
118 it('Should send a new video notification on a remote scheduled publication', async function () {
122 const updateAt = new Date(new Date().getTime() + 2000)
125 privacy: VideoPrivacy.PRIVATE,
127 updateAt: updateAt.toISOString(),
128 privacy: VideoPrivacy.PUBLIC
131 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
132 await waitJobs(servers)
135 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
138 it('Should not send a notification before the video is published', async function () {
141 const updateAt = new Date(new Date().getTime() + 1000000)
144 privacy: VideoPrivacy.PRIVATE,
146 updateAt: updateAt.toISOString(),
147 privacy: VideoPrivacy.PUBLIC
150 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
153 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
156 it('Should send a new video notification when a video becomes public', async function () {
159 const data = { privacy: VideoPrivacy.PRIVATE }
160 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
162 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
164 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
166 await waitJobs(servers)
167 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
170 it('Should send a new video notification when a remote video becomes public', async function () {
173 const data = { privacy: VideoPrivacy.PRIVATE }
174 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
176 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
178 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
180 await waitJobs(servers)
181 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
184 it('Should not send a new video notification when a video becomes unlisted', async function () {
187 const data = { privacy: VideoPrivacy.PRIVATE }
188 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
190 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
192 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
195 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
198 const data = { privacy: VideoPrivacy.PRIVATE }
199 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
201 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
203 await waitJobs(servers)
204 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
207 it('Should send a new video notification after a video import', async function () {
210 const name = 'video import ' + uuidv4()
215 privacy: VideoPrivacy.PUBLIC,
216 targetUrl: getGoodVideoUrl()
218 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
219 const uuid = res.body.video.uuid
221 await waitJobs(servers)
223 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
227 describe('My video is published', function () {
228 let baseParams: CheckerBaseParams
234 socketNotifications: adminNotificationsServer2,
235 token: servers[1].accessToken
239 it('Should not send a notification if transcoding is not enabled', async function () {
242 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1)
243 await waitJobs(servers)
245 await checkVideoIsPublished(baseParams, name, uuid, 'absence')
248 it('Should not send a notification if the wait transcoding is false', async function () {
251 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
252 await waitJobs(servers)
254 const notification = await getLastNotification(servers[0].url, userAccessToken)
256 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
260 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
263 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
264 await waitJobs(servers)
266 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
269 it('Should send a notification with a transcoded video', async function () {
272 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
273 await waitJobs(servers)
275 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
278 it('Should send a notification when an imported video is transcoded', async function () {
281 const name = 'video import ' + uuidv4()
286 privacy: VideoPrivacy.PUBLIC,
287 targetUrl: getGoodVideoUrl(),
288 waitTranscoding: true
290 const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
291 const uuid = res.body.video.uuid
293 await waitJobs(servers)
294 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
297 it('Should send a notification when the scheduled update has been proceeded', async function () {
301 const updateAt = new Date(new Date().getTime() + 2000)
304 privacy: VideoPrivacy.PRIVATE,
306 updateAt: updateAt.toISOString(),
307 privacy: VideoPrivacy.PUBLIC
310 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
313 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
316 it('Should not send a notification before the video is published', async function () {
319 const updateAt = new Date(new Date().getTime() + 1000000)
322 privacy: VideoPrivacy.PRIVATE,
324 updateAt: updateAt.toISOString(),
325 privacy: VideoPrivacy.PUBLIC
328 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
331 await checkVideoIsPublished(baseParams, name, uuid, 'absence')
335 describe('My video is imported', function () {
336 let baseParams: CheckerBaseParams
342 socketNotifications: adminNotifications,
343 token: servers[0].accessToken
347 it('Should send a notification when the video import failed', async function () {
350 const name = 'video import ' + uuidv4()
355 privacy: VideoPrivacy.PRIVATE,
356 targetUrl: getBadVideoUrl()
358 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
359 const uuid = res.body.video.uuid
361 await waitJobs(servers)
362 await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence')
365 it('Should send a notification when the video import succeeded', async function () {
368 const name = 'video import ' + uuidv4()
373 privacy: VideoPrivacy.PRIVATE,
374 targetUrl: getGoodVideoUrl()
376 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
377 const uuid = res.body.video.uuid
379 await waitJobs(servers)
380 await checkMyVideoImportIsFinished(baseParams, name, uuid, getGoodVideoUrl(), true, 'presence')
384 describe('New actor follow', function () {
385 let baseParams: CheckerBaseParams
386 const myChannelName = 'super channel name'
387 const myUserName = 'super user name'
393 socketNotifications: userNotifications,
394 token: userAccessToken
399 accessToken: servers[0].accessToken,
400 displayName: 'super root name'
405 accessToken: userAccessToken,
406 displayName: myUserName
411 accessToken: servers[1].accessToken,
412 displayName: 'super root 2 name'
415 await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName })
418 it('Should notify when a local channel is following one of our channel', async function () {
421 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
422 await waitJobs(servers)
424 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
426 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
429 it('Should notify when a remote channel is following one of our channel', async function () {
432 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
433 await waitJobs(servers)
435 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
437 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
440 // PeerTube does not support accout -> account follows
441 // it('Should notify when a local account is following one of our channel', async function () {
442 // this.timeout(50000)
444 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:' + servers[0].port)
446 // await waitJobs(servers)
448 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
451 // it('Should notify when a remote account is following one of our channel', async function () {
452 // this.timeout(50000)
454 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:' + servers[0].port)
456 // await waitJobs(servers)
458 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
462 after(async function () {
463 MockSmtpServer.Instance.kill()
465 await cleanupTests(servers)