Fix tests
[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
10 import {
11   addVideoChannel, checkVideoFilesWereRemoved, completeVideoCheck, createUser, dateIsValid, doubleFollow, flushAndRunMultipleServers,
12   flushTests, getVideo,
13   getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testImage,
14   updateVideo, uploadVideo, userLogin, viewVideo, wait, webtorrentAdd
15 } from '../../utils'
16 import {
17   addVideoCommentReply, addVideoCommentThread, deleteVideoComment, getVideoCommentThreads,
18   getVideoThreadComments
19 } from '../../utils/videos/video-comments'
20
21 const expect = chai.expect
22
23 describe('Test multiple servers', function () {
24   let servers: ServerInfo[] = []
25   const toRemove = []
26   let videoUUID = ''
27   let videoChannelId: number
28
29   before(async function () {
30     this.timeout(120000)
31
32     servers = await flushAndRunMultipleServers(3)
33
34     // Get the access tokens
35     await setAccessTokensToServers(servers)
36
37     const videoChannel = {
38       name: 'my channel',
39       description: 'super channel'
40     }
41     await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
42     const channelRes = await getVideoChannelsList(servers[0].url, 0, 1)
43     videoChannelId = channelRes.body.data[0].id
44
45     // Server 1 and server 2 follow each other
46     await doubleFollow(servers[0], servers[1])
47     // Server 1 and server 3 follow each other
48     await doubleFollow(servers[0], servers[2])
49     // Server 2 and server 3 follow each other
50     await doubleFollow(servers[1], servers[2])
51   })
52
53   it('Should not have videos for all servers', async function () {
54     for (const server of servers) {
55       const res = await getVideosList(server.url)
56       const videos = res.body.data
57       expect(videos).to.be.an('array')
58       expect(videos.length).to.equal(0)
59     }
60   })
61
62   describe('Should upload the video and propagate on each server', function () {
63     it('Should upload the video on server 1 and propagate on each server', async function () {
64       this.timeout(25000)
65
66       const videoAttributes = {
67         name: 'my super name for server 1',
68         category: 5,
69         licence: 4,
70         language: 9,
71         nsfw: true,
72         description: 'my super description for server 1',
73         tags: [ 'tag1p1', 'tag2p1' ],
74         channelId: videoChannelId,
75         fixture: 'video_short1.webm'
76       }
77       await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
78
79       await wait(10000)
80
81       // All servers should have this video
82       for (const server of servers) {
83         const isLocal = server.url === 'http://localhost:9001'
84         const checkAttributes = {
85           name: 'my super name for server 1',
86           category: 5,
87           licence: 4,
88           language: 9,
89           nsfw: true,
90           description: 'my super description for server 1',
91           host: 'localhost:9001',
92           account: 'root',
93           isLocal,
94           duration: 10,
95           tags: [ 'tag1p1', 'tag2p1' ],
96           privacy: VideoPrivacy.PUBLIC,
97           commentsEnabled: true,
98           channel: {
99             name: 'my channel',
100             description: 'super channel',
101             isLocal
102           },
103           fixture: 'video_short1.webm',
104           files: [
105             {
106               resolution: 720,
107               size: 572456
108             }
109           ]
110         }
111
112         const res = await getVideosList(server.url)
113         const videos = res.body.data
114         expect(videos).to.be.an('array')
115         expect(videos.length).to.equal(1)
116         const video = videos[0]
117
118         await completeVideoCheck(server.url, video, checkAttributes)
119       }
120     })
121
122     it('Should upload the video on server 2 and propagate on each server', async function () {
123       this.timeout(50000)
124
125       const user = {
126         username: 'user1',
127         password: 'super_password'
128       }
129       await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
130       const userAccessToken = await userLogin(servers[1], user)
131
132       const videoAttributes = {
133         name: 'my super name for server 2',
134         category: 4,
135         licence: 3,
136         language: 11,
137         nsfw: true,
138         description: 'my super description for server 2',
139         tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
140         fixture: 'video_short2.webm',
141         thumbnailfile: 'thumbnail.jpg',
142         previewfile: 'preview.jpg'
143       }
144       await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
145
146       // Transcoding
147       await wait(30000)
148
149       // All servers should have this video
150       for (const server of servers) {
151         const isLocal = server.url === 'http://localhost:9002'
152         const checkAttributes = {
153           name: 'my super name for server 2',
154           category: 4,
155           licence: 3,
156           language: 11,
157           nsfw: true,
158           description: 'my super description for server 2',
159           host: 'localhost:9002',
160           account: 'user1',
161           isLocal,
162           commentsEnabled: true,
163           duration: 5,
164           tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
165           privacy: VideoPrivacy.PUBLIC,
166           channel: {
167             name: 'Default user1 channel',
168             description: 'super channel',
169             isLocal
170           },
171           fixture: 'video_short2.webm',
172           files: [
173             {
174               resolution: 240,
175               size: 190000
176             },
177             {
178               resolution: 360,
179               size: 280000
180             },
181             {
182               resolution: 480,
183               size: 390000
184             },
185             {
186               resolution: 720,
187               size: 710000
188             }
189           ],
190           thumbnailfile: 'thumbnail',
191           previewfile: 'preview'
192         }
193
194         const res = await getVideosList(server.url)
195         const videos = res.body.data
196         expect(videos).to.be.an('array')
197         expect(videos.length).to.equal(2)
198         const video = videos[1]
199
200         await completeVideoCheck(server.url, video, checkAttributes)
201       }
202     })
203
204     it('Should upload two videos on server 3 and propagate on each server', async function () {
205       this.timeout(45000)
206
207       const videoAttributes1 = {
208         name: 'my super name for server 3',
209         category: 6,
210         licence: 5,
211         language: 11,
212         nsfw: true,
213         description: 'my super description for server 3',
214         tags: [ 'tag1p3' ],
215         fixture: 'video_short3.webm'
216       }
217       await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
218
219       const videoAttributes2 = {
220         name: 'my super name for server 3-2',
221         category: 7,
222         licence: 6,
223         language: 12,
224         nsfw: false,
225         description: 'my super description for server 3-2',
226         tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
227         fixture: 'video_short.webm'
228       }
229       await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
230
231       await wait(10000)
232
233       // All servers should have this video
234       for (const server of servers) {
235         const isLocal = server.url === 'http://localhost:9003'
236         const res = await getVideosList(server.url)
237
238         const videos = res.body.data
239         expect(videos).to.be.an('array')
240         expect(videos.length).to.equal(4)
241
242         // We not sure about the order of the two last uploads
243         let video1 = null
244         let video2 = null
245         if (videos[2].name === 'my super name for server 3') {
246           video1 = videos[2]
247           video2 = videos[3]
248         } else {
249           video1 = videos[3]
250           video2 = videos[2]
251         }
252
253         const checkAttributesVideo1 = {
254           name: 'my super name for server 3',
255           category: 6,
256           licence: 5,
257           language: 11,
258           nsfw: true,
259           description: 'my super description for server 3',
260           host: 'localhost:9003',
261           account: 'root',
262           isLocal,
263           duration: 5,
264           commentsEnabled: true,
265           tags: [ 'tag1p3' ],
266           privacy: VideoPrivacy.PUBLIC,
267           channel: {
268             name: 'Default root channel',
269             description: '',
270             isLocal
271           },
272           fixture: 'video_short3.webm',
273           files: [
274             {
275               resolution: 720,
276               size: 292677
277             }
278           ]
279         }
280         await completeVideoCheck(server.url, video1, checkAttributesVideo1)
281
282         const checkAttributesVideo2 = {
283           name: 'my super name for server 3-2',
284           category: 7,
285           licence: 6,
286           language: 12,
287           nsfw: false,
288           description: 'my super description for server 3-2',
289           host: 'localhost:9003',
290           account: 'root',
291           commentsEnabled: true,
292           isLocal,
293           duration: 5,
294           tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
295           privacy: VideoPrivacy.PUBLIC,
296           channel: {
297             name: 'Default root channel',
298             description: '',
299             isLocal
300           },
301           fixture: 'video_short.webm',
302           files: [
303             {
304               resolution: 720,
305               size: 218910
306             }
307           ]
308         }
309         await completeVideoCheck(server.url, video2, checkAttributesVideo2)
310       }
311     })
312   })
313
314   describe('Should seed the uploaded video', function () {
315     it('Should add the file 1 by asking server 3', async function () {
316       this.timeout(10000)
317
318       const res = await getVideosList(servers[2].url)
319
320       const video = res.body.data[0]
321       toRemove.push(res.body.data[2])
322       toRemove.push(res.body.data[3])
323
324       const res2 = await getVideo(servers[2].url, video.id)
325       const videoDetails = res2.body
326
327       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
328       expect(torrent.files).to.be.an('array')
329       expect(torrent.files.length).to.equal(1)
330       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
331     })
332
333     it('Should add the file 2 by asking server 1', async function () {
334       this.timeout(10000)
335
336       const res = await getVideosList(servers[0].url)
337
338       const video = res.body.data[1]
339       const res2 = await getVideo(servers[0].url, video.id)
340       const videoDetails = res2.body
341
342       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
343       expect(torrent.files).to.be.an('array')
344       expect(torrent.files.length).to.equal(1)
345       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
346     })
347
348     it('Should add the file 3 by asking server 2', async function () {
349       this.timeout(10000)
350
351       const res = await getVideosList(servers[1].url)
352
353       const video = res.body.data[2]
354       const res2 = await getVideo(servers[1].url, video.id)
355       const videoDetails = res2.body
356
357       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
358       expect(torrent.files).to.be.an('array')
359       expect(torrent.files.length).to.equal(1)
360       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
361     })
362
363     it('Should add the file 3-2 by asking server 1', async function () {
364       this.timeout(10000)
365
366       const res = await getVideosList(servers[0].url)
367
368       const video = res.body.data[3]
369       const res2 = await getVideo(servers[0].url, video.id)
370       const videoDetails = res2.body
371
372       const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
373       expect(torrent.files).to.be.an('array')
374       expect(torrent.files.length).to.equal(1)
375       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
376     })
377
378     it('Should add the file 2 in 360p by asking server 1', async function () {
379       this.timeout(10000)
380
381       const res = await getVideosList(servers[0].url)
382
383       const video = res.body.data.find(v => v.name === 'my super name for server 2')
384       const res2 = await getVideo(servers[0].url, video.id)
385       const videoDetails = res2.body
386
387       const file = videoDetails.files.find(f => f.resolution === 360)
388       expect(file).not.to.be.undefined
389
390       const torrent = await webtorrentAdd(file.magnetUri)
391       expect(torrent.files).to.be.an('array')
392       expect(torrent.files.length).to.equal(1)
393       expect(torrent.files[0].path).to.exist.and.to.not.equal('')
394     })
395   })
396
397   describe('Should update video views, likes and dislikes', function () {
398     let localVideosServer3 = []
399     let remoteVideosServer1 = []
400     let remoteVideosServer2 = []
401     let remoteVideosServer3 = []
402
403     before(async function () {
404       const res1 = await getVideosList(servers[0].url)
405       remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
406
407       const res2 = await getVideosList(servers[1].url)
408       remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
409
410       const res3 = await getVideosList(servers[2].url)
411       localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid)
412       remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
413     })
414
415     it('Should view multiple videos on owned servers', async function () {
416       this.timeout(10000)
417
418       const tasks: Promise<any>[] = []
419       tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
420       tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
421       tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
422       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
423
424       await Promise.all(tasks)
425
426       await wait(5000)
427
428       for (const server of servers) {
429         const res = await getVideosList(server.url)
430
431         const videos = res.body.data
432         const video0 = videos.find(v => v.uuid === localVideosServer3[0])
433         const video1 = videos.find(v => v.uuid === localVideosServer3[1])
434
435         expect(video0.views).to.equal(3)
436         expect(video1.views).to.equal(1)
437       }
438     })
439
440     it('Should view multiple videos on each servers', async function () {
441       this.timeout(15000)
442
443       const tasks: Promise<any>[] = []
444       tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
445       tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
446       tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
447       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
448       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
449       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
450       tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
451       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
452       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
453       tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
454
455       await Promise.all(tasks)
456
457       await wait(10000)
458
459       let baseVideos = null
460
461       for (const server of servers) {
462         const res = await getVideosList(server.url)
463
464         const videos = res.body.data
465
466         // Initialize base videos for future comparisons
467         if (baseVideos === null) {
468           baseVideos = videos
469           continue
470         }
471
472         for (const baseVideo of baseVideos) {
473           const sameVideo = videos.find(video => video.name === baseVideo.name)
474           expect(baseVideo.views).to.equal(sameVideo.views)
475         }
476       }
477     })
478
479     it('Should like and dislikes videos on different services', async function () {
480       this.timeout(20000)
481
482       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
483       await wait(200)
484       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike')
485       await wait(200)
486       await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
487       await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like')
488       await wait(200)
489       await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike')
490       await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike')
491       await wait(200)
492       await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like')
493
494       await wait(10000)
495
496       let baseVideos = null
497       for (const server of servers) {
498         const res = await getVideosList(server.url)
499
500         const videos = res.body.data
501
502         // Initialize base videos for future comparisons
503         if (baseVideos === null) {
504           baseVideos = videos
505           continue
506         }
507
508         for (const baseVideo of baseVideos) {
509           const sameVideo = videos.find(video => video.name === baseVideo.name)
510           expect(baseVideo.likes).to.equal(sameVideo.likes)
511           expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
512         }
513       }
514     })
515   })
516
517   describe('Should manipulate these videos', function () {
518     it('Should update the video 3 by asking server 3', async function () {
519       this.timeout(10000)
520
521       const attributes = {
522         name: 'my super video updated',
523         category: 10,
524         licence: 7,
525         language: 13,
526         nsfw: true,
527         description: 'my super description updated',
528         tags: [ 'tag_up_1', 'tag_up_2' ],
529         thumbnailfile: 'thumbnail.jpg',
530         previewfile: 'preview.jpg'
531       }
532
533       await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
534
535       await wait(5000)
536     })
537
538     it('Should have the video 3 updated on each server', async function () {
539       this.timeout(10000)
540
541       for (const server of servers) {
542         const res = await getVideosList(server.url)
543
544         const videos = res.body.data
545         const videoUpdated = videos.find(video => video.name === 'my super video updated')
546         expect(!!videoUpdated).to.be.true
547
548         const isLocal = server.url === 'http://localhost:9003'
549         const checkAttributes = {
550           name: 'my super video updated',
551           category: 10,
552           licence: 7,
553           language: 13,
554           nsfw: true,
555           description: 'my super description updated',
556           host: 'localhost:9003',
557           account: 'root',
558           isLocal,
559           duration: 5,
560           commentsEnabled: true,
561           tags: [ 'tag_up_1', 'tag_up_2' ],
562           privacy: VideoPrivacy.PUBLIC,
563           channel: {
564             name: 'Default root channel',
565             description: '',
566             isLocal
567           },
568           fixture: 'video_short3.webm',
569           files: [
570             {
571               resolution: 720,
572               size: 292677
573             }
574           ],
575           thumbnailfile: 'thumbnail',
576           previewfile: 'preview'
577         }
578         await completeVideoCheck(server.url, videoUpdated, checkAttributes)
579       }
580     })
581
582     it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
583       this.timeout(10000)
584
585       await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
586       await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
587
588       await wait(5000)
589     })
590
591     it('Should not have files of videos 3 and 3-2 on each server', async function () {
592       for (const server of servers) {
593         await checkVideoFilesWereRemoved(toRemove[0].uuid, server.serverNumber)
594         await checkVideoFilesWereRemoved(toRemove[1].uuid, server.serverNumber)
595       }
596     })
597
598     it('Should have videos 1 and 3 on each server', async function () {
599       for (const server of servers) {
600         const res = await getVideosList(server.url)
601
602         const videos = res.body.data
603         expect(videos).to.be.an('array')
604         expect(videos.length).to.equal(2)
605         expect(videos[0].name).not.to.equal(videos[1].name)
606         expect(videos[0].name).not.to.equal(toRemove[0].name)
607         expect(videos[1].name).not.to.equal(toRemove[0].name)
608         expect(videos[0].name).not.to.equal(toRemove[1].name)
609         expect(videos[1].name).not.to.equal(toRemove[1].name)
610
611         videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
612       }
613     })
614
615     it('Should get the same video by UUID on each server', async function () {
616       let baseVideo = null
617       for (const server of servers) {
618         const res = await getVideo(server.url, videoUUID)
619
620         const video = res.body
621
622         if (baseVideo === null) {
623           baseVideo = video
624           continue
625         }
626
627         expect(baseVideo.name).to.equal(video.name)
628         expect(baseVideo.uuid).to.equal(video.uuid)
629         expect(baseVideo.category).to.equal(video.category)
630         expect(baseVideo.language).to.equal(video.language)
631         expect(baseVideo.licence).to.equal(video.licence)
632         expect(baseVideo.category).to.equal(video.category)
633         expect(baseVideo.nsfw).to.equal(video.nsfw)
634         expect(baseVideo.accountName).to.equal(video.accountName)
635         expect(baseVideo.tags).to.deep.equal(video.tags)
636       }
637     })
638
639     it('Should get the preview from each server', async function () {
640       for (const server of servers) {
641         const res = await getVideo(server.url, videoUUID)
642         const video = res.body
643
644         await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
645       }
646     })
647   })
648
649   describe('Should comment these videos', function () {
650     it('Should add comment (threads and replies)', async function () {
651       this.timeout(25000)
652
653       {
654         const text = 'my super first comment'
655         await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
656       }
657
658       {
659         const text = 'my super second comment'
660         await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
661       }
662
663       await wait(5000)
664
665       {
666         const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
667         const threadId = res.body.data.find(c => c.text === 'my super first comment').id
668
669         const text = 'my super answer to thread 1'
670         await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
671       }
672
673       await wait(5000)
674
675       {
676         const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
677         const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
678
679         const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
680         const childCommentId = res2.body.children[0].comment.id
681
682         const text3 = 'my second answer to thread 1'
683         await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
684
685         const text2 = 'my super answer to answer of thread 1'
686         await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
687       }
688
689       await wait(5000)
690     })
691
692     it('Should have these threads', async function () {
693       for (const server of servers) {
694         const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
695
696         expect(res.body.total).to.equal(2)
697         expect(res.body.data).to.be.an('array')
698         expect(res.body.data).to.have.lengthOf(2)
699
700         {
701           const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
702           expect(comment).to.not.be.undefined
703           expect(comment.inReplyToCommentId).to.be.null
704           expect(comment.account.name).to.equal('root')
705           expect(comment.account.host).to.equal('localhost:9001')
706           expect(comment.totalReplies).to.equal(3)
707           expect(dateIsValid(comment.createdAt as string)).to.be.true
708           expect(dateIsValid(comment.updatedAt as string)).to.be.true
709         }
710
711         {
712           const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
713           expect(comment).to.not.be.undefined
714           expect(comment.inReplyToCommentId).to.be.null
715           expect(comment.account.name).to.equal('root')
716           expect(comment.account.host).to.equal('localhost:9003')
717           expect(comment.totalReplies).to.equal(0)
718           expect(dateIsValid(comment.createdAt as string)).to.be.true
719           expect(dateIsValid(comment.updatedAt as string)).to.be.true
720         }
721       }
722     })
723
724     it('Should have these comments', async function () {
725       for (const server of servers) {
726         const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
727         const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
728
729         const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
730
731         const tree: VideoCommentThreadTree = res2.body
732         expect(tree.comment.text).equal('my super first comment')
733         expect(tree.comment.account.name).equal('root')
734         expect(tree.comment.account.host).equal('localhost:9001')
735         expect(tree.children).to.have.lengthOf(2)
736
737         const firstChild = tree.children[0]
738         expect(firstChild.comment.text).to.equal('my super answer to thread 1')
739         expect(firstChild.comment.account.name).equal('root')
740         expect(firstChild.comment.account.host).equal('localhost:9002')
741         expect(firstChild.children).to.have.lengthOf(1)
742
743         const childOfFirstChild = firstChild.children[0]
744         expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
745         expect(childOfFirstChild.comment.account.name).equal('root')
746         expect(childOfFirstChild.comment.account.host).equal('localhost:9003')
747         expect(childOfFirstChild.children).to.have.lengthOf(0)
748
749         const secondChild = tree.children[1]
750         expect(secondChild.comment.text).to.equal('my second answer to thread 1')
751         expect(secondChild.comment.account.name).equal('root')
752         expect(secondChild.comment.account.host).equal('localhost:9003')
753         expect(secondChild.children).to.have.lengthOf(0)
754       }
755     })
756
757     it('Should delete the thread comments', async function () {
758       this.timeout(10000)
759
760       const res1 = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5)
761       const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
762       await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId)
763
764       await wait(5000)
765     })
766
767     it('Should have the thread comments deleted on other servers too', async function () {
768       for (const server of servers) {
769         const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
770
771         expect(res.body.total).to.equal(1)
772         expect(res.body.data).to.be.an('array')
773         expect(res.body.data).to.have.lengthOf(1)
774
775         {
776           const comment: VideoComment = res.body.data[0]
777           expect(comment).to.not.be.undefined
778           expect(comment.inReplyToCommentId).to.be.null
779           expect(comment.account.name).to.equal('root')
780           expect(comment.account.host).to.equal('localhost:9003')
781           expect(comment.totalReplies).to.equal(0)
782           expect(dateIsValid(comment.createdAt as string)).to.be.true
783           expect(dateIsValid(comment.updatedAt as string)).to.be.true
784         }
785       }
786     })
787
788     it('Should disable comments', async function () {
789       this.timeout(20000)
790
791       const attributes = {
792         commentsEnabled: false
793       }
794
795       await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes)
796
797       await wait(5000)
798
799       for (const server of servers) {
800         const res = await getVideo(server.url, videoUUID)
801         expect(res.body.commentsEnabled).to.be.false
802
803         const text = 'my super forbidden comment'
804         await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, 409)
805       }
806     })
807   })
808
809   describe('With minimum parameters', function () {
810     it('Should upload and propagate the video', async function () {
811       this.timeout(50000)
812
813       const path = '/api/v1/videos/upload'
814
815       const req = request(servers[1].url)
816         .post(path)
817         .set('Accept', 'application/json')
818         .set('Authorization', 'Bearer ' + servers[1].accessToken)
819         .field('name', 'minimum parameters')
820         .field('privacy', '1')
821         .field('nsfw', 'false')
822         .field('channelId', '1')
823         .field('commentsEnabled', 'true')
824
825       const filePath = join(__dirname, '..', '..', 'api', 'fixtures', 'video_short.webm')
826
827       await req.attach('videofile', filePath)
828         .expect(200)
829
830       await wait(25000)
831
832       for (const server of servers) {
833         const res = await getVideosList(server.url)
834         const video = res.body.data.find(v => v.name === 'minimum parameters')
835
836         const isLocal = server.url === 'http://localhost:9002'
837         const checkAttributes = {
838           name: 'minimum parameters',
839           category: null,
840           licence: null,
841           language: null,
842           nsfw: false,
843           description: null,
844           host: 'localhost:9002',
845           account: 'root',
846           isLocal,
847           duration: 5,
848           commentsEnabled: true,
849           tags: [ ],
850           privacy: VideoPrivacy.PUBLIC,
851           channel: {
852             name: 'Default root channel',
853             description: '',
854             isLocal
855           },
856           fixture: 'video_short.webm',
857           files: [
858             {
859               resolution: 720,
860               size: 40315
861             },
862             {
863               resolution: 480,
864               size: 22808
865             },
866             {
867               resolution: 360,
868               size: 18617
869             },
870             {
871               resolution: 240,
872               size: 15217
873             }
874           ]
875         }
876         await completeVideoCheck(server.url, video, checkAttributes)
877       }
878     })
879   })
880
881   after(async function () {
882     killallServers(servers)
883
884     // Keep the logs if the test failed
885     if (this['ok']) {
886       await flushTests()
887     }
888   })
889 })