Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / videos / multiple-servers.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { join } from 'path'
6 import * as request from 'supertest'
7 import { VideoPrivacy } from '../../../../shared/models/videos'
8 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
9 import {
10   addVideoChannel,
11   checkTmpIsEmpty,
12   checkVideoFilesWereRemoved,
13   cleanupTests,
14   completeVideoCheck,
15   createUser,
16   dateIsValid,
17   doubleFollow,
18   flushAndRunServer,
19   flushAndRunMultipleServers,
20   getLocalVideos,
21   getVideo,
22   getVideoChannelsList,
23   getVideosList,
24   rateVideo,
25   removeVideo,
26   ServerInfo,
27   setAccessTokensToServers,
28   testImage,
29   updateVideo,
30   uploadVideo,
31   userLogin,
32   viewVideo,
33   wait,
34   webtorrentAdd
35 } from '../../../../shared/extra-utils'
36 import {
37   addVideoCommentReply,
38   addVideoCommentThread,
39   deleteVideoComment,
40   getVideoCommentThreads,
41   getVideoThreadComments
42 } from '../../../../shared/extra-utils/videos/video-comments'
43 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
44
45 const expect = chai.expect
46
47 describe('Test multiple servers', function () {
48   let servers: ServerInfo[] = []
49   const toRemove = []
50   let videoUUID = ''
51   let videoChannelId: number
52
53   before(async function () {
54     this.timeout(120000)
55
56     servers = await flushAndRunMultipleServers(3)
57
58     // Get the access tokens
59     await setAccessTokensToServers(servers)
60
61     {
62       const videoChannel = {
63         name: 'super_channel_name',
64         displayName: 'my channel',
65         description: 'super channel'
66       }
67       await addVideoChannel(servers[ 0 ].url, servers[ 0 ].accessToken, videoChannel)
68       const channelRes = await getVideoChannelsList(servers[ 0 ].url, 0, 1)
69       videoChannelId = channelRes.body.data[ 0 ].id
70     }
71
72     // Server 1 and server 2 follow each other
73     await doubleFollow(servers[0], servers[1])
74     // Server 1 and server 3 follow each other
75     await doubleFollow(servers[0], servers[2])
76     // Server 2 and server 3 follow each other
77     await doubleFollow(servers[1], servers[2])
78   })
79
80   it('Should not have videos for all servers', async function () {
81     for (const server of servers) {
82       const res = await getVideosList(server.url)
83       const videos = res.body.data
84       expect(videos).to.be.an('array')
85       expect(videos.length).to.equal(0)
86     }
87   })
88
89   describe('Should upload the video and propagate on each server', function () {
90     it('Should upload the video on server 1 and propagate on each server', async function () {
91       this.timeout(25000)
92
93       const videoAttributes = {
94         name: 'my super name for server 1',
95         category: 5,
96         licence: 4,
97         language: 'ja',
98         nsfw: true,
99         description: 'my super description for server 1',
100         support: 'my super support text for server 1',
101         originallyPublishedAt: '2019-02-10T13:38:14.449Z',
102         tags: [ 'tag1p1', 'tag2p1' ],
103         channelId: videoChannelId,
104         fixture: 'video_short1.webm'
105       }
106       await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
107
108       await waitJobs(servers)
109
110       // All servers should have this video
111       let publishedAt: string = null
112       for (const server of servers) {
113         const isLocal = server.port === servers[0].port
114         const checkAttributes = {
115           name: 'my super name for server 1',
116           category: 5,
117           licence: 4,
118           language: 'ja',
119           nsfw: true,
120           description: 'my super description for server 1',
121           support: 'my super support text for server 1',
122           originallyPublishedAt: '2019-02-10T13:38:14.449Z',
123           account: {
124             name: 'root',
125             host: 'localhost:' + servers[0].port
126           },
127           isLocal,
128           publishedAt,
129           duration: 10,
130           tags: [ 'tag1p1', 'tag2p1' ],
131           privacy: VideoPrivacy.PUBLIC,
132           commentsEnabled: true,
133           downloadEnabled: true,
134           channel: {
135             displayName: 'my channel',
136             name: 'super_channel_name',
137             description: 'super channel',
138             isLocal
139           },
140           fixture: 'video_short1.webm',
141           files: [
142             {
143               resolution: 720,
144               size: 572456
145             }
146           ]
147         }
148
149         const res = await getVideosList(server.url)
150         const videos = res.body.data
151         expect(videos).to.be.an('array')
152         expect(videos.length).to.equal(1)
153         const video = videos[0]
154
155         await completeVideoCheck(server.url, video, checkAttributes)
156         publishedAt = video.publishedAt
157       }
158     })
159
160     it('Should upload the video on server 2 and propagate on each server', async function () {
161       this.timeout(50000)
162
163       const user = {
164         username: 'user1',
165         password: 'super_password'
166       }
167       await createUser({ url: servers[ 1 ].url, accessToken: servers[ 1 ].accessToken, username: user.username, password: user.password })
168       const userAccessToken = await userLogin(servers[1], user)
169
170       const videoAttributes = {
171         name: 'my super name for server 2',
172         category: 4,
173         licence: 3,
174         language: 'de',
175         nsfw: true,
176         description: 'my super description for server 2',
177         support: 'my super support text for server 2',
178         tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
179         fixture: 'video_short2.webm',
180         thumbnailfile: 'thumbnail.jpg',
181         previewfile: 'preview.jpg'
182       }
183       await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
184
185       // Transcoding
186       await waitJobs(servers)
187
188       // All servers should have this video
189       for (const server of servers) {
190         const isLocal = server.url === 'http://localhost:' + servers[1].port
191         const checkAttributes = {
192           name: 'my super name for server 2',
193           category: 4,
194           licence: 3,
195           language: 'de',
196           nsfw: true,
197           description: 'my super description for server 2',
198           support: 'my super support text for server 2',
199           account: {
200             name: 'user1',
201             host: 'localhost:' + servers[1].port
202           },
203           isLocal,
204           commentsEnabled: true,
205           downloadEnabled: true,
206           duration: 5,
207           tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
208           privacy: VideoPrivacy.PUBLIC,
209           channel: {
210             displayName: 'Main user1 channel',
211             name: 'user1_channel',
212             description: 'super channel',
213             isLocal
214           },
215           fixture: 'video_short2.webm',
216           files: [
217             {
218               resolution: 240,
219               size: 189000
220             },
221             {
222               resolution: 360,
223               size: 278000
224             },
225             {
226               resolution: 480,
227               size: 384000
228             },
229             {
230               resolution: 720,
231               size: 706000
232             }
233           ],
234           thumbnailfile: 'thumbnail',
235           previewfile: 'preview'
236         }
237
238         const res = await getVideosList(server.url)
239         const videos = res.body.data
240         expect(videos).to.be.an('array')
241         expect(videos.length).to.equal(2)
242         const video = videos[1]
243
244         await completeVideoCheck(server.url, video, checkAttributes)
245       }
246     })
247
248     it('Should upload two videos on server 3 and propagate on each server', async function () {
249       this.timeout(45000)
250
251       const videoAttributes1 = {
252         name: 'my super name for server 3',
253         category: 6,
254         licence: 5,
255         language: 'de',
256         nsfw: true,
257         description: 'my super description for server 3',
258         support: 'my super support text for server 3',
259         tags: [ 'tag1p3' ],
260         fixture: 'video_short3.webm'
261       }
262       await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
263
264       const videoAttributes2 = {
265         name: 'my super name for server 3-2',
266         category: 7,
267         licence: 6,
268         language: 'ko',
269         nsfw: false,
270         description: 'my super description for server 3-2',
271         support: 'my super support text for server 3-2',
272         tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
273         fixture: 'video_short.webm'
274       }
275       await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
276
277       await waitJobs(servers)
278
279       // All servers should have this video
280       for (const server of servers) {
281         const isLocal = server.url === 'http://localhost:' + servers[2].port
282         const res = await getVideosList(server.url)
283
284         const videos = res.body.data
285         expect(videos).to.be.an('array')
286         expect(videos.length).to.equal(4)
287
288         // We not sure about the order of the two last uploads
289         let video1 = null
290         let video2 = null
291         if (videos[2].name === 'my super name for server 3') {
292           video1 = videos[2]
293           video2 = videos[3]
294         } else {
295           video1 = videos[3]
296           video2 = videos[2]
297         }
298
299         const checkAttributesVideo1 = {
300           name: 'my super name for server 3',
301           category: 6,
302           licence: 5,
303           language: 'de',
304           nsfw: true,
305           description: 'my super description for server 3',
306           support: 'my super support text for server 3',
307           account: {
308             name: 'root',
309             host: 'localhost:' + servers[2].port
310           },
311           isLocal,
312           duration: 5,
313           commentsEnabled: true,
314           downloadEnabled: true,
315           tags: [ 'tag1p3' ],
316           privacy: VideoPrivacy.PUBLIC,
317           channel: {
318             displayName: 'Main root channel',
319             name: 'root_channel',
320             description: '',
321             isLocal
322           },
323           fixture: 'video_short3.webm',
324           files: [
325             {
326               resolution: 720,
327               size: 292677
328             }
329           ]
330         }
331         await completeVideoCheck(server.url, video1, checkAttributesVideo1)
332
333         const checkAttributesVideo2 = {
334           name: 'my super name for server 3-2',
335           category: 7,
336           licence: 6,
337           language: 'ko',
338           nsfw: false,
339           description: 'my super description for server 3-2',
340           support: 'my super support text for server 3-2',
341           account: {
342             name: 'root',
343             host: 'localhost:' + servers[2].port
344           },
345           commentsEnabled: true,
346           downloadEnabled: true,
347           isLocal,
348           duration: 5,
349           tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
350           privacy: VideoPrivacy.PUBLIC,
351           channel: {
352             displayName: 'Main root channel',
353             name: 'root_channel',
354             description: '',
355             isLocal
356           },
357           fixture: 'video_short.webm',
358           files: [
359             {
360               resolution: 720,
361               size: 218910
362             }
363           ]
364         }
365         await completeVideoCheck(server.url, video2, checkAttributesVideo2)
366       }
367     })
368   })
369
370   describe('It should list local videos', function () {
371     it('Should list only local videos on server 1', async function () {
372       const { body } = await getLocalVideos(servers[0].url)
373
374       expect(body.total).to.equal(1)
375       expect(body.data).to.be.an('array')
376       expect(body.data.length).to.equal(1)
377       expect(body.data[0].name).to.equal('my super name for server 1')
378     })
379
380     it('Should list only local videos on server 2', async function () {
381       const { body } = await getLocalVideos(servers[1].url)
382
383       expect(body.total).to.equal(1)
384       expect(body.data).to.be.an('array')
385       expect(body.data.length).to.equal(1)
386       expect(body.data[0].name).to.equal('my super name for server 2')
387     })
388
389     it('Should list only local videos on server 3', async function () {
390       const { body } = await getLocalVideos(servers[2].url)
391
392       expect(body.total).to.equal(2)
393       expect(body.data).to.be.an('array')
394       expect(body.data.length).to.equal(2)
395       expect(body.data[0].name).to.equal('my super name for server 3')
396       expect(body.data[1].name).to.equal('my super name for server 3-2')
397     })
398   })
399
400   describe('Should seed the uploaded video', function () {
401     it('Should add the file 1 by asking server 3', async function () {
402       this.timeout(10000)
403
404       const res = await getVideosList(servers[2].url)
405
406       const video = res.body.data[0]
407       toRemove.push(res.body.data[2])
408       toRemove.push(res.body.data[3])
409
410       const res2 = await getVideo(servers[2].url, video.id)
411       const videoDetails = res2.body
412
413       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
414       expect(torrent.files).to.be.an('array')
415       expect(torrent.files.length).to.equal(1)
416       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
417     })
418
419     it('Should add the file 2 by asking server 1', async function () {
420       this.timeout(10000)
421
422       const res = await getVideosList(servers[0].url)
423
424       const video = res.body.data[1]
425       const res2 = await getVideo(servers[0].url, video.id)
426       const videoDetails = res2.body
427
428       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
429       expect(torrent.files).to.be.an('array')
430       expect(torrent.files.length).to.equal(1)
431       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
432     })
433
434     it('Should add the file 3 by asking server 2', async function () {
435       this.timeout(10000)
436
437       const res = await getVideosList(servers[1].url)
438
439       const video = res.body.data[2]
440       const res2 = await getVideo(servers[1].url, video.id)
441       const videoDetails = res2.body
442
443       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
444       expect(torrent.files).to.be.an('array')
445       expect(torrent.files.length).to.equal(1)
446       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
447     })
448
449     it('Should add the file 3-2 by asking server 1', async function () {
450       this.timeout(10000)
451
452       const res = await getVideosList(servers[0].url)
453
454       const video = res.body.data[3]
455       const res2 = await getVideo(servers[0].url, video.id)
456       const videoDetails = res2.body
457
458       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
459       expect(torrent.files).to.be.an('array')
460       expect(torrent.files.length).to.equal(1)
461       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
462     })
463
464     it('Should add the file 2 in 360p by asking server 1', async function () {
465       this.timeout(10000)
466
467       const res = await getVideosList(servers[0].url)
468
469       const video = res.body.data.find(v => v.name === 'my super name for server 2')
470       const res2 = await getVideo(servers[0].url, video.id)
471       const videoDetails = res2.body
472
473       const file = videoDetails.files.find(f => f.resolution.id === 360)
474       expect(file).not.to.be.undefined
475
476       const torrent = await webtorrentAdd(file.magnetUri)
477       expect(torrent.files).to.be.an('array')
478       expect(torrent.files.length).to.equal(1)
479       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
480     })
481   })
482
483   describe('Should update video views, likes and dislikes', function () {
484     let localVideosServer3 = []
485     let remoteVideosServer1 = []
486     let remoteVideosServer2 = []
487     let remoteVideosServer3 = []
488
489     before(async function () {
490       const res1 = await getVideosList(servers[0].url)
491       remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
492
493       const res2 = await getVideosList(servers[1].url)
494       remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
495
496       const res3 = await getVideosList(servers[2].url)
497       localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid)
498       remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
499     })
500
501     it('Should view multiple videos on owned servers', async function () {
502       this.timeout(30000)
503
504       await viewVideo(servers[2].url, localVideosServer3[0])
505       await wait(1000)
506
507       await viewVideo(servers[2].url, localVideosServer3[0])
508       await viewVideo(servers[2].url, localVideosServer3[1])
509
510       await wait(1000)
511
512       await viewVideo(servers[2].url, localVideosServer3[0])
513       await viewVideo(servers[2].url, localVideosServer3[0])
514
515       await waitJobs(servers)
516
517       // Wait the repeatable job
518       await wait(6000)
519
520       await waitJobs(servers)
521
522       for (const server of servers) {
523         const res = await getVideosList(server.url)
524
525         const videos = res.body.data
526         const video0 = videos.find(v => v.uuid === localVideosServer3[0])
527         const video1 = videos.find(v => v.uuid === localVideosServer3[1])
528
529         expect(video0.views).to.equal(3)
530         expect(video1.views).to.equal(1)
531       }
532     })
533
534     it('Should view multiple videos on each servers', async function () {
535       this.timeout(45000)
536
537       const tasks: Promise<any>[] = []
538       tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
539       tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
540       tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
541       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
542       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
543       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
544       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
545       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
546       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
547       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
548
549       await Promise.all(tasks)
550
551       await waitJobs(servers)
552
553       // Wait the repeatable job
554       await wait(16000)
555
556       await waitJobs(servers)
557
558       let baseVideos = null
559
560       for (const server of servers) {
561         const res = await getVideosList(server.url)
562
563         const videos = res.body.data
564
565         // Initialize base videos for future comparisons
566         if (baseVideos === null) {
567           baseVideos = videos
568           continue
569         }
570
571         for (const baseVideo of baseVideos) {
572           const sameVideo = videos.find(video => video.name === baseVideo.name)
573           expect(baseVideo.views).to.equal(sameVideo.views)
574         }
575       }
576     })
577
578     it('Should like and dislikes videos on different services', async function () {
579       this.timeout(20000)
580
581       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
582       await wait(500)
583       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike')
584       await wait(500)
585       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
586       await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like')
587       await wait(500)
588       await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike')
589       await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike')
590       await wait(500)
591       await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like')
592
593       await waitJobs(servers)
594
595       let baseVideos = null
596       for (const server of servers) {
597         const res = await getVideosList(server.url)
598
599         const videos = res.body.data
600
601         // Initialize base videos for future comparisons
602         if (baseVideos === null) {
603           baseVideos = videos
604           continue
605         }
606
607         for (const baseVideo of baseVideos) {
608           const sameVideo = videos.find(video => video.name === baseVideo.name)
609           expect(baseVideo.likes).to.equal(sameVideo.likes)
610           expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
611         }
612       }
613     })
614   })
615
616   describe('Should manipulate these videos', function () {
617     it('Should update the video 3 by asking server 3', async function () {
618       this.timeout(10000)
619
620       const attributes = {
621         name: 'my super video updated',
622         category: 10,
623         licence: 7,
624         language: 'fr',
625         nsfw: true,
626         description: 'my super description updated',
627         support: 'my super support text updated',
628         tags: [ 'tag_up_1', 'tag_up_2' ],
629         thumbnailfile: 'thumbnail.jpg',
630         originallyPublishedAt: '2019-02-11T13:38:14.449Z',
631         previewfile: 'preview.jpg'
632       }
633
634       await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
635
636       await waitJobs(servers)
637     })
638
639     it('Should have the video 3 updated on each server', async function () {
640       this.timeout(10000)
641
642       for (const server of servers) {
643         const res = await getVideosList(server.url)
644
645         const videos = res.body.data
646         const videoUpdated = videos.find(video => video.name === 'my super video updated')
647         expect(!!videoUpdated).to.be.true
648
649         const isLocal = server.url === 'http://localhost:' + servers[2].port
650         const checkAttributes = {
651           name: 'my super video updated',
652           category: 10,
653           licence: 7,
654           language: 'fr',
655           nsfw: true,
656           description: 'my super description updated',
657           support: 'my super support text updated',
658           originallyPublishedAt: '2019-02-11T13:38:14.449Z',
659           account: {
660             name: 'root',
661             host: 'localhost:' + servers[2].port
662           },
663           isLocal,
664           duration: 5,
665           commentsEnabled: true,
666           downloadEnabled: true,
667           tags: [ 'tag_up_1', 'tag_up_2' ],
668           privacy: VideoPrivacy.PUBLIC,
669           channel: {
670             displayName: 'Main root channel',
671             name: 'root_channel',
672             description: '',
673             isLocal
674           },
675           fixture: 'video_short3.webm',
676           files: [
677             {
678               resolution: 720,
679               size: 292677
680             }
681           ],
682           thumbnailfile: 'thumbnail',
683           previewfile: 'preview'
684         }
685         await completeVideoCheck(server.url, videoUpdated, checkAttributes)
686       }
687     })
688
689     it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
690       this.timeout(10000)
691
692       await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
693       await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
694
695       await waitJobs(servers)
696     })
697
698     it('Should not have files of videos 3 and 3-2 on each server', async function () {
699       for (const server of servers) {
700         await checkVideoFilesWereRemoved(toRemove[0].uuid, server.serverNumber)
701         await checkVideoFilesWereRemoved(toRemove[1].uuid, server.serverNumber)
702       }
703     })
704
705     it('Should have videos 1 and 3 on each server', async function () {
706       for (const server of servers) {
707         const res = await getVideosList(server.url)
708
709         const videos = res.body.data
710         expect(videos).to.be.an('array')
711         expect(videos.length).to.equal(2)
712         expect(videos[0].name).not.to.equal(videos[1].name)
713         expect(videos[0].name).not.to.equal(toRemove[0].name)
714         expect(videos[1].name).not.to.equal(toRemove[0].name)
715         expect(videos[0].name).not.to.equal(toRemove[1].name)
716         expect(videos[1].name).not.to.equal(toRemove[1].name)
717
718         videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
719       }
720     })
721
722     it('Should get the same video by UUID on each server', async function () {
723       let baseVideo = null
724       for (const server of servers) {
725         const res = await getVideo(server.url, videoUUID)
726
727         const video = res.body
728
729         if (baseVideo === null) {
730           baseVideo = video
731           continue
732         }
733
734         expect(baseVideo.name).to.equal(video.name)
735         expect(baseVideo.uuid).to.equal(video.uuid)
736         expect(baseVideo.category.id).to.equal(video.category.id)
737         expect(baseVideo.language.id).to.equal(video.language.id)
738         expect(baseVideo.licence.id).to.equal(video.licence.id)
739         expect(baseVideo.nsfw).to.equal(video.nsfw)
740         expect(baseVideo.account.name).to.equal(video.account.name)
741         expect(baseVideo.account.displayName).to.equal(video.account.displayName)
742         expect(baseVideo.account.url).to.equal(video.account.url)
743         expect(baseVideo.account.host).to.equal(video.account.host)
744         expect(baseVideo.tags).to.deep.equal(video.tags)
745       }
746     })
747
748     it('Should get the preview from each server', async function () {
749       for (const server of servers) {
750         const res = await getVideo(server.url, videoUUID)
751         const video = res.body
752
753         await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
754       }
755     })
756   })
757
758   describe('Should comment these videos', function () {
759     let childOfFirstChild: VideoCommentThreadTree
760
761     it('Should add comment (threads and replies)', async function () {
762       this.timeout(25000)
763
764       {
765         const text = 'my super first comment'
766         await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
767       }
768
769       {
770         const text = 'my super second comment'
771         await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
772       }
773
774       await waitJobs(servers)
775
776       {
777         const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
778         const threadId = res.body.data.find(c => c.text === 'my super first comment').id
779
780         const text = 'my super answer to thread 1'
781         await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
782       }
783
784       await waitJobs(servers)
785
786       {
787         const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
788         const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
789
790         const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
791         const childCommentId = res2.body.children[0].comment.id
792
793         const text3 = 'my second answer to thread 1'
794         await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
795
796         const text2 = 'my super answer to answer of thread 1'
797         await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
798       }
799
800       await waitJobs(servers)
801     })
802
803     it('Should have these threads', async function () {
804       for (const server of servers) {
805         const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
806
807         expect(res.body.total).to.equal(2)
808         expect(res.body.data).to.be.an('array')
809         expect(res.body.data).to.have.lengthOf(2)
810
811         {
812           const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
813           expect(comment).to.not.be.undefined
814           expect(comment.inReplyToCommentId).to.be.null
815           expect(comment.account.name).to.equal('root')
816           expect(comment.account.host).to.equal('localhost:' + servers[0].port)
817           expect(comment.totalReplies).to.equal(3)
818           expect(dateIsValid(comment.createdAt as string)).to.be.true
819           expect(dateIsValid(comment.updatedAt as string)).to.be.true
820         }
821
822         {
823           const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
824           expect(comment).to.not.be.undefined
825           expect(comment.inReplyToCommentId).to.be.null
826           expect(comment.account.name).to.equal('root')
827           expect(comment.account.host).to.equal('localhost:' + servers[2].port)
828           expect(comment.totalReplies).to.equal(0)
829           expect(dateIsValid(comment.createdAt as string)).to.be.true
830           expect(dateIsValid(comment.updatedAt as string)).to.be.true
831         }
832       }
833     })
834
835     it('Should have these comments', async function () {
836       for (const server of servers) {
837         const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
838         const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
839
840         const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
841
842         const tree: VideoCommentThreadTree = res2.body
843         expect(tree.comment.text).equal('my super first comment')
844         expect(tree.comment.account.name).equal('root')
845         expect(tree.comment.account.host).equal('localhost:' + servers[0].port)
846         expect(tree.children).to.have.lengthOf(2)
847
848         const firstChild = tree.children[0]
849         expect(firstChild.comment.text).to.equal('my super answer to thread 1')
850         expect(firstChild.comment.account.name).equal('root')
851         expect(firstChild.comment.account.host).equal('localhost:' + servers[1].port)
852         expect(firstChild.children).to.have.lengthOf(1)
853
854         childOfFirstChild = firstChild.children[0]
855         expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
856         expect(childOfFirstChild.comment.account.name).equal('root')
857         expect(childOfFirstChild.comment.account.host).equal('localhost:' + servers[2].port)
858         expect(childOfFirstChild.children).to.have.lengthOf(0)
859
860         const secondChild = tree.children[1]
861         expect(secondChild.comment.text).to.equal('my second answer to thread 1')
862         expect(secondChild.comment.account.name).equal('root')
863         expect(secondChild.comment.account.host).equal('localhost:' + servers[2].port)
864         expect(secondChild.children).to.have.lengthOf(0)
865       }
866     })
867
868     it('Should delete a reply', async function () {
869       this.timeout(10000)
870
871       await deleteVideoComment(servers[2].url, servers[2].accessToken, videoUUID, childOfFirstChild.comment.id)
872
873       await waitJobs(servers)
874     })
875
876     it('Should have this comment marked as deleted', async function () {
877       for (const server of servers) {
878         const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
879         const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
880
881         const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
882
883         const tree: VideoCommentThreadTree = res2.body
884         expect(tree.comment.text).equal('my super first comment')
885
886         const firstChild = tree.children[0]
887         expect(firstChild.comment.text).to.equal('my super answer to thread 1')
888         expect(firstChild.children).to.have.lengthOf(1)
889
890         const deletedComment = firstChild.children[0].comment
891         expect(deletedComment.isDeleted).to.be.true
892         expect(deletedComment.deletedAt).to.not.be.null
893         expect(deletedComment.account).to.be.null
894         expect(deletedComment.text).to.equal('')
895
896         const secondChild = tree.children[1]
897         expect(secondChild.comment.text).to.equal('my second answer to thread 1')
898       }
899     })
900
901     it('Should delete the thread comments', async function () {
902       this.timeout(10000)
903
904       const res = await getVideoCommentThreads(servers[ 0 ].url, videoUUID, 0, 5)
905       const threadId = res.body.data.find(c => c.text === 'my super first comment').id
906       await deleteVideoComment(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, threadId)
907
908       await waitJobs(servers)
909     })
910
911     it('Should have the threads marked as deleted on other servers too', async function () {
912       for (const server of servers) {
913         const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
914
915         expect(res.body.total).to.equal(2)
916         expect(res.body.data).to.be.an('array')
917         expect(res.body.data).to.have.lengthOf(2)
918
919         {
920           const comment: VideoComment = res.body.data[0]
921           expect(comment).to.not.be.undefined
922           expect(comment.inReplyToCommentId).to.be.null
923           expect(comment.account.name).to.equal('root')
924           expect(comment.account.host).to.equal('localhost:' + servers[2].port)
925           expect(comment.totalReplies).to.equal(0)
926           expect(dateIsValid(comment.createdAt as string)).to.be.true
927           expect(dateIsValid(comment.updatedAt as string)).to.be.true
928         }
929
930         {
931           const deletedComment: VideoComment = res.body.data[1]
932           expect(deletedComment).to.not.be.undefined
933           expect(deletedComment.isDeleted).to.be.true
934           expect(deletedComment.deletedAt).to.not.be.null
935           expect(deletedComment.text).to.equal('')
936           expect(deletedComment.inReplyToCommentId).to.be.null
937           expect(deletedComment.account).to.be.null
938           expect(deletedComment.totalReplies).to.equal(3)
939           expect(dateIsValid(deletedComment.createdAt as string)).to.be.true
940           expect(dateIsValid(deletedComment.updatedAt as string)).to.be.true
941           expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true
942         }
943       }
944     })
945
946     it('Should delete a remote thread by the origin server', async function () {
947       this.timeout(5000)
948
949       const res = await getVideoCommentThreads(servers[ 0 ].url, videoUUID, 0, 5)
950       const threadId = res.body.data.find(c => c.text === 'my super second comment').id
951       await deleteVideoComment(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, threadId)
952
953       await waitJobs(servers)
954     })
955
956     it('Should have the threads marked as deleted on other servers too', async function () {
957       for (const server of servers) {
958         const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
959
960         expect(res.body.total).to.equal(2)
961         expect(res.body.data).to.have.lengthOf(2)
962
963         {
964           const comment: VideoComment = res.body.data[0]
965           expect(comment.text).to.equal('')
966           expect(comment.isDeleted).to.be.true
967           expect(comment.createdAt).to.not.be.null
968           expect(comment.deletedAt).to.not.be.null
969           expect(comment.account).to.be.null
970           expect(comment.totalReplies).to.equal(0)
971         }
972
973         {
974           const comment: VideoComment = res.body.data[1]
975           expect(comment.text).to.equal('')
976           expect(comment.isDeleted).to.be.true
977           expect(comment.createdAt).to.not.be.null
978           expect(comment.deletedAt).to.not.be.null
979           expect(comment.account).to.be.null
980           expect(comment.totalReplies).to.equal(3)
981         }
982       }
983     })
984
985     it('Should disable comments and download', async function () {
986       this.timeout(20000)
987
988       const attributes = {
989         commentsEnabled: false,
990         downloadEnabled: false
991       }
992
993       await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes)
994
995       await waitJobs(servers)
996
997       for (const server of servers) {
998         const res = await getVideo(server.url, videoUUID)
999         expect(res.body.commentsEnabled).to.be.false
1000         expect(res.body.downloadEnabled).to.be.false
1001
1002         const text = 'my super forbidden comment'
1003         await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, 409)
1004       }
1005     })
1006   })
1007
1008   describe('With minimum parameters', function () {
1009     it('Should upload and propagate the video', async function () {
1010       this.timeout(60000)
1011
1012       const path = '/api/v1/videos/upload'
1013
1014       const req = request(servers[1].url)
1015         .post(path)
1016         .set('Accept', 'application/json')
1017         .set('Authorization', 'Bearer ' + servers[1].accessToken)
1018         .field('name', 'minimum parameters')
1019         .field('privacy', '1')
1020         .field('channelId', '1')
1021
1022       const filePath = join(__dirname, '..', '..', 'fixtures', 'video_short.webm')
1023
1024       await req.attach('videofile', filePath)
1025         .expect(200)
1026
1027       await waitJobs(servers)
1028
1029       for (const server of servers) {
1030         const res = await getVideosList(server.url)
1031         const video = res.body.data.find(v => v.name === 'minimum parameters')
1032
1033         const isLocal = server.url === 'http://localhost:' + servers[1].port
1034         const checkAttributes = {
1035           name: 'minimum parameters',
1036           category: null,
1037           licence: null,
1038           language: null,
1039           nsfw: false,
1040           description: null,
1041           support: null,
1042           account: {
1043             name: 'root',
1044             host: 'localhost:' + servers[1].port
1045           },
1046           isLocal,
1047           duration: 5,
1048           commentsEnabled: true,
1049           downloadEnabled: true,
1050           tags: [ ],
1051           privacy: VideoPrivacy.PUBLIC,
1052           channel: {
1053             displayName: 'Main root channel',
1054             name: 'root_channel',
1055             description: '',
1056             isLocal
1057           },
1058           fixture: 'video_short.webm',
1059           files: [
1060             {
1061               resolution: 720,
1062               size: 72000
1063             },
1064             {
1065               resolution: 480,
1066               size: 45000
1067             },
1068             {
1069               resolution: 360,
1070               size: 34600
1071             },
1072             {
1073               resolution: 240,
1074               size: 24770
1075             }
1076           ]
1077         }
1078         await completeVideoCheck(server.url, video, checkAttributes)
1079       }
1080     })
1081   })
1082
1083   describe('TMP directory', function () {
1084     it('Should have an empty tmp directory', async function () {
1085       for (const server of servers) {
1086         await checkTmpIsEmpty(server)
1087       }
1088     })
1089   })
1090
1091   after(async function () {
1092     await cleanupTests(servers)
1093   })
1094 })