]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/notifications/user-notifications.ts
Introduce channels command
[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 { buildUUID } from '@server/helpers/uuid'
6 import {
7 CheckerBaseParams,
8 checkMyVideoImportIsFinished,
9 checkNewActorFollow,
10 checkNewVideoFromSubscription,
11 checkVideoIsPublished,
12 cleanupTests,
13 getLastNotification,
14 ImportsCommand,
15 MockSmtpServer,
16 prepareNotificationsTest,
17 ServerInfo,
18 updateMyUser,
19 updateVideo,
20 uploadRandomVideoOnServers,
21 wait,
22 waitJobs
23 } from '@shared/extra-utils'
24 import { UserNotification, UserNotificationType, VideoPrivacy } from '@shared/models'
25
26 const expect = chai.expect
27
28 describe('Test user notifications', function () {
29 let servers: ServerInfo[] = []
30 let userAccessToken: string
31 let userNotifications: UserNotification[] = []
32 let adminNotifications: UserNotification[] = []
33 let adminNotificationsServer2: UserNotification[] = []
34 let emails: object[] = []
35 let channelId: number
36
37 before(async function () {
38 this.timeout(120000)
39
40 const res = await prepareNotificationsTest(3)
41 emails = res.emails
42 userAccessToken = res.userAccessToken
43 servers = res.servers
44 userNotifications = res.userNotifications
45 adminNotifications = res.adminNotifications
46 adminNotificationsServer2 = res.adminNotificationsServer2
47 channelId = res.channelId
48 })
49
50 describe('New video from my subscription notification', function () {
51 let baseParams: CheckerBaseParams
52
53 before(() => {
54 baseParams = {
55 server: servers[0],
56 emails,
57 socketNotifications: userNotifications,
58 token: userAccessToken
59 }
60 })
61
62 it('Should not send notifications if the user does not follow the video publisher', async function () {
63 this.timeout(50000)
64
65 await uploadRandomVideoOnServers(servers, 1)
66
67 const notification = await getLastNotification(servers[0].url, userAccessToken)
68 expect(notification).to.be.undefined
69
70 expect(emails).to.have.lengthOf(0)
71 expect(userNotifications).to.have.lengthOf(0)
72 })
73
74 it('Should send a new video notification if the user follows the local video publisher', async function () {
75 this.timeout(15000)
76
77 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
78 await waitJobs(servers)
79
80 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1)
81 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
82 })
83
84 it('Should send a new video notification from a remote account', async function () {
85 this.timeout(150000) // Server 2 has transcoding enabled
86
87 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[1].port })
88 await waitJobs(servers)
89
90 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2)
91 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
92 })
93
94 it('Should send a new video notification on a scheduled publication', async function () {
95 this.timeout(50000)
96
97 // In 2 seconds
98 const updateAt = new Date(new Date().getTime() + 2000)
99
100 const data = {
101 privacy: VideoPrivacy.PRIVATE,
102 scheduleUpdate: {
103 updateAt: updateAt.toISOString(),
104 privacy: VideoPrivacy.PUBLIC
105 }
106 }
107 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
108
109 await wait(6000)
110 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
111 })
112
113 it('Should send a new video notification on a remote scheduled publication', async function () {
114 this.timeout(100000)
115
116 // In 2 seconds
117 const updateAt = new Date(new Date().getTime() + 2000)
118
119 const data = {
120 privacy: VideoPrivacy.PRIVATE,
121 scheduleUpdate: {
122 updateAt: updateAt.toISOString(),
123 privacy: VideoPrivacy.PUBLIC
124 }
125 }
126 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
127 await waitJobs(servers)
128
129 await wait(6000)
130 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
131 })
132
133 it('Should not send a notification before the video is published', async function () {
134 this.timeout(50000)
135
136 const updateAt = new Date(new Date().getTime() + 1000000)
137
138 const data = {
139 privacy: VideoPrivacy.PRIVATE,
140 scheduleUpdate: {
141 updateAt: updateAt.toISOString(),
142 privacy: VideoPrivacy.PUBLIC
143 }
144 }
145 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
146
147 await wait(6000)
148 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
149 })
150
151 it('Should send a new video notification when a video becomes public', async function () {
152 this.timeout(50000)
153
154 const data = { privacy: VideoPrivacy.PRIVATE }
155 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
156
157 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
158
159 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
160
161 await waitJobs(servers)
162 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
163 })
164
165 it('Should send a new video notification when a remote video becomes public', async function () {
166 this.timeout(50000)
167
168 const data = { privacy: VideoPrivacy.PRIVATE }
169 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
170
171 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
172
173 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
174
175 await waitJobs(servers)
176 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
177 })
178
179 it('Should not send a new video notification when a video becomes unlisted', async function () {
180 this.timeout(50000)
181
182 const data = { privacy: VideoPrivacy.PRIVATE }
183 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data)
184
185 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
186
187 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
188 })
189
190 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
191 this.timeout(50000)
192
193 const data = { privacy: VideoPrivacy.PRIVATE }
194 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
195
196 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
197
198 await waitJobs(servers)
199 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
200 })
201
202 it('Should send a new video notification after a video import', async function () {
203 this.timeout(100000)
204
205 const name = 'video import ' + buildUUID()
206
207 const attributes = {
208 name,
209 channelId,
210 privacy: VideoPrivacy.PUBLIC,
211 targetUrl: ImportsCommand.getGoodVideoUrl()
212 }
213 const { video } = await servers[0].importsCommand.importVideo({ attributes })
214
215 await waitJobs(servers)
216
217 await checkNewVideoFromSubscription(baseParams, name, video.uuid, 'presence')
218 })
219 })
220
221 describe('My video is published', function () {
222 let baseParams: CheckerBaseParams
223
224 before(() => {
225 baseParams = {
226 server: servers[1],
227 emails,
228 socketNotifications: adminNotificationsServer2,
229 token: servers[1].accessToken
230 }
231 })
232
233 it('Should not send a notification if transcoding is not enabled', async function () {
234 this.timeout(50000)
235
236 const { name, uuid } = await uploadRandomVideoOnServers(servers, 1)
237 await waitJobs(servers)
238
239 await checkVideoIsPublished(baseParams, name, uuid, 'absence')
240 })
241
242 it('Should not send a notification if the wait transcoding is false', async function () {
243 this.timeout(50000)
244
245 await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false })
246 await waitJobs(servers)
247
248 const notification = await getLastNotification(servers[0].url, userAccessToken)
249 if (notification) {
250 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
251 }
252 })
253
254 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
255 this.timeout(50000)
256
257 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
258 await waitJobs(servers)
259
260 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
261 })
262
263 it('Should send a notification with a transcoded video', async function () {
264 this.timeout(50000)
265
266 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true })
267 await waitJobs(servers)
268
269 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
270 })
271
272 it('Should send a notification when an imported video is transcoded', async function () {
273 this.timeout(50000)
274
275 const name = 'video import ' + buildUUID()
276
277 const attributes = {
278 name,
279 channelId,
280 privacy: VideoPrivacy.PUBLIC,
281 targetUrl: ImportsCommand.getGoodVideoUrl(),
282 waitTranscoding: true
283 }
284 const { video } = await servers[1].importsCommand.importVideo({ attributes })
285
286 await waitJobs(servers)
287 await checkVideoIsPublished(baseParams, name, video.uuid, 'presence')
288 })
289
290 it('Should send a notification when the scheduled update has been proceeded', async function () {
291 this.timeout(70000)
292
293 // In 2 seconds
294 const updateAt = new Date(new Date().getTime() + 2000)
295
296 const data = {
297 privacy: VideoPrivacy.PRIVATE,
298 scheduleUpdate: {
299 updateAt: updateAt.toISOString(),
300 privacy: VideoPrivacy.PUBLIC
301 }
302 }
303 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
304
305 await wait(6000)
306 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
307 })
308
309 it('Should not send a notification before the video is published', async function () {
310 this.timeout(50000)
311
312 const updateAt = new Date(new Date().getTime() + 1000000)
313
314 const data = {
315 privacy: VideoPrivacy.PRIVATE,
316 scheduleUpdate: {
317 updateAt: updateAt.toISOString(),
318 privacy: VideoPrivacy.PUBLIC
319 }
320 }
321 const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data)
322
323 await wait(6000)
324 await checkVideoIsPublished(baseParams, name, uuid, 'absence')
325 })
326 })
327
328 describe('My video is imported', function () {
329 let baseParams: CheckerBaseParams
330
331 before(() => {
332 baseParams = {
333 server: servers[0],
334 emails,
335 socketNotifications: adminNotifications,
336 token: servers[0].accessToken
337 }
338 })
339
340 it('Should send a notification when the video import failed', async function () {
341 this.timeout(70000)
342
343 const name = 'video import ' + buildUUID()
344
345 const attributes = {
346 name,
347 channelId,
348 privacy: VideoPrivacy.PRIVATE,
349 targetUrl: ImportsCommand.getBadVideoUrl()
350 }
351 const { video } = await servers[0].importsCommand.importVideo({ attributes })
352
353 await waitJobs(servers)
354 await checkMyVideoImportIsFinished(baseParams, name, video.uuid, ImportsCommand.getBadVideoUrl(), false, 'presence')
355 })
356
357 it('Should send a notification when the video import succeeded', async function () {
358 this.timeout(70000)
359
360 const name = 'video import ' + buildUUID()
361
362 const attributes = {
363 name,
364 channelId,
365 privacy: VideoPrivacy.PRIVATE,
366 targetUrl: ImportsCommand.getGoodVideoUrl()
367 }
368 const { video } = await servers[0].importsCommand.importVideo({ attributes })
369
370 await waitJobs(servers)
371 await checkMyVideoImportIsFinished(baseParams, name, video.uuid, ImportsCommand.getGoodVideoUrl(), true, 'presence')
372 })
373 })
374
375 describe('New actor follow', function () {
376 let baseParams: CheckerBaseParams
377 const myChannelName = 'super channel name'
378 const myUserName = 'super user name'
379
380 before(async () => {
381 baseParams = {
382 server: servers[0],
383 emails,
384 socketNotifications: userNotifications,
385 token: userAccessToken
386 }
387
388 await updateMyUser({
389 url: servers[0].url,
390 accessToken: servers[0].accessToken,
391 displayName: 'super root name'
392 })
393
394 await updateMyUser({
395 url: servers[0].url,
396 accessToken: userAccessToken,
397 displayName: myUserName
398 })
399
400 await updateMyUser({
401 url: servers[1].url,
402 accessToken: servers[1].accessToken,
403 displayName: 'super root 2 name'
404 })
405
406 await servers[0].channelsCommand.update({
407 token: userAccessToken,
408 channelName: 'user_1_channel',
409 attributes: { displayName: myChannelName }
410 })
411 })
412
413 it('Should notify when a local channel is following one of our channel', async function () {
414 this.timeout(50000)
415
416 await servers[0].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
417 await waitJobs(servers)
418
419 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
420
421 await servers[0].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
422 })
423
424 it('Should notify when a remote channel is following one of our channel', async function () {
425 this.timeout(50000)
426
427 await servers[1].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
428 await waitJobs(servers)
429
430 await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
431
432 await servers[1].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
433 })
434
435 // PeerTube does not support accout -> account follows
436 // it('Should notify when a local account is following one of our channel', async function () {
437 // this.timeout(50000)
438 //
439 // await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:' + servers[0].port)
440 //
441 // await waitJobs(servers)
442 //
443 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
444 // })
445
446 // it('Should notify when a remote account is following one of our channel', async function () {
447 // this.timeout(50000)
448 //
449 // await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:' + servers[0].port)
450 //
451 // await waitJobs(servers)
452 //
453 // await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
454 // })
455 })
456
457 after(async function () {
458 MockSmtpServer.Instance.kill()
459
460 await cleanupTests(servers)
461 })
462 })