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