1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
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'
11 addVideoChannel, checkVideoFilesWereRemoved, completeVideoCheck, createUser, dateIsValid, doubleFollow, flushAndRunMultipleServers,
13 getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testImage,
14 updateVideo, uploadVideo, userLogin, viewVideo, wait, webtorrentAdd
17 addVideoCommentReply, addVideoCommentThread, deleteVideoComment, getVideoCommentThreads,
18 getVideoThreadComments
19 } from '../../utils/videos/video-comments'
21 const expect = chai.expect
23 describe('Test multiple servers', function () {
24 let servers: ServerInfo[] = []
27 let videoChannelId: number
29 before(async function () {
32 servers = await flushAndRunMultipleServers(3)
34 // Get the access tokens
35 await setAccessTokensToServers(servers)
37 const videoChannel = {
39 description: 'super channel'
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
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])
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)
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 () {
66 const videoAttributes = {
67 name: 'my super name for server 1',
72 description: 'my super description for server 1',
73 tags: [ 'tag1p1', 'tag2p1' ],
74 channelId: videoChannelId,
75 fixture: 'video_short1.webm'
77 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
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',
90 description: 'my super description for server 1',
91 host: 'localhost:9001',
95 tags: [ 'tag1p1', 'tag2p1' ],
96 privacy: VideoPrivacy.PUBLIC,
97 commentsEnabled: true,
100 description: 'super channel',
103 fixture: 'video_short1.webm',
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]
118 await completeVideoCheck(server.url, video, checkAttributes)
122 it('Should upload the video on server 2 and propagate on each server', async function () {
127 password: 'super_password'
129 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
130 const userAccessToken = await userLogin(servers[1], user)
132 const videoAttributes = {
133 name: 'my super name for server 2',
138 description: 'my super description for server 2',
139 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
140 fixture: 'video_short2.webm'
142 await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
147 // All servers should have this video
148 for (const server of servers) {
149 const isLocal = server.url === 'http://localhost:9002'
150 const checkAttributes = {
151 name: 'my super name for server 2',
156 description: 'my super description for server 2',
157 host: 'localhost:9002',
160 commentsEnabled: true,
162 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
163 privacy: VideoPrivacy.PUBLIC,
165 name: 'Default user1 channel',
166 description: 'super channel',
169 fixture: 'video_short2.webm',
190 const res = await getVideosList(server.url)
191 const videos = res.body.data
192 expect(videos).to.be.an('array')
193 expect(videos.length).to.equal(2)
194 const video = videos[1]
196 await completeVideoCheck(server.url, video, checkAttributes)
200 it('Should upload two videos on server 3 and propagate on each server', async function () {
203 const videoAttributes1 = {
204 name: 'my super name for server 3',
209 description: 'my super description for server 3',
211 fixture: 'video_short3.webm'
213 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
215 const videoAttributes2 = {
216 name: 'my super name for server 3-2',
221 description: 'my super description for server 3-2',
222 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
223 fixture: 'video_short.webm'
225 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
229 // All servers should have this video
230 for (const server of servers) {
231 const isLocal = server.url === 'http://localhost:9003'
232 const res = await getVideosList(server.url)
234 const videos = res.body.data
235 expect(videos).to.be.an('array')
236 expect(videos.length).to.equal(4)
238 // We not sure about the order of the two last uploads
241 if (videos[2].name === 'my super name for server 3') {
249 const checkAttributesVideo1 = {
250 name: 'my super name for server 3',
255 description: 'my super description for server 3',
256 host: 'localhost:9003',
260 commentsEnabled: true,
262 privacy: VideoPrivacy.PUBLIC,
264 name: 'Default root channel',
268 fixture: 'video_short3.webm',
276 await completeVideoCheck(server.url, video1, checkAttributesVideo1)
278 const checkAttributesVideo2 = {
279 name: 'my super name for server 3-2',
284 description: 'my super description for server 3-2',
285 host: 'localhost:9003',
287 commentsEnabled: true,
290 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
291 privacy: VideoPrivacy.PUBLIC,
293 name: 'Default root channel',
297 fixture: 'video_short.webm',
305 await completeVideoCheck(server.url, video2, checkAttributesVideo2)
310 describe('Should seed the uploaded video', function () {
311 it('Should add the file 1 by asking server 3', async function () {
314 const res = await getVideosList(servers[2].url)
316 const video = res.body.data[0]
317 toRemove.push(res.body.data[2])
318 toRemove.push(res.body.data[3])
320 const res2 = await getVideo(servers[2].url, video.id)
321 const videoDetails = res2.body
323 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
324 expect(torrent.files).to.be.an('array')
325 expect(torrent.files.length).to.equal(1)
326 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
329 it('Should add the file 2 by asking server 1', async function () {
332 const res = await getVideosList(servers[0].url)
334 const video = res.body.data[1]
335 const res2 = await getVideo(servers[0].url, video.id)
336 const videoDetails = res2.body
338 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
339 expect(torrent.files).to.be.an('array')
340 expect(torrent.files.length).to.equal(1)
341 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
344 it('Should add the file 3 by asking server 2', async function () {
347 const res = await getVideosList(servers[1].url)
349 const video = res.body.data[2]
350 const res2 = await getVideo(servers[1].url, video.id)
351 const videoDetails = res2.body
353 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
354 expect(torrent.files).to.be.an('array')
355 expect(torrent.files.length).to.equal(1)
356 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
359 it('Should add the file 3-2 by asking server 1', async function () {
362 const res = await getVideosList(servers[0].url)
364 const video = res.body.data[3]
365 const res2 = await getVideo(servers[0].url, video.id)
366 const videoDetails = res2.body
368 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
369 expect(torrent.files).to.be.an('array')
370 expect(torrent.files.length).to.equal(1)
371 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
374 it('Should add the file 2 in 360p by asking server 1', async function () {
377 const res = await getVideosList(servers[0].url)
379 const video = res.body.data.find(v => v.name === 'my super name for server 2')
380 const res2 = await getVideo(servers[0].url, video.id)
381 const videoDetails = res2.body
383 const file = videoDetails.files.find(f => f.resolution === 360)
384 expect(file).not.to.be.undefined
386 const torrent = await webtorrentAdd(file.magnetUri)
387 expect(torrent.files).to.be.an('array')
388 expect(torrent.files.length).to.equal(1)
389 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
393 describe('Should update video views, likes and dislikes', function () {
394 let localVideosServer3 = []
395 let remoteVideosServer1 = []
396 let remoteVideosServer2 = []
397 let remoteVideosServer3 = []
399 before(async function () {
400 const res1 = await getVideosList(servers[0].url)
401 remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
403 const res2 = await getVideosList(servers[1].url)
404 remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
406 const res3 = await getVideosList(servers[2].url)
407 localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid)
408 remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
411 it('Should view multiple videos on owned servers', async function () {
414 const tasks: Promise<any>[] = []
415 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
416 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
417 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
418 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
420 await Promise.all(tasks)
424 for (const server of servers) {
425 const res = await getVideosList(server.url)
427 const videos = res.body.data
428 const video0 = videos.find(v => v.uuid === localVideosServer3[0])
429 const video1 = videos.find(v => v.uuid === localVideosServer3[1])
431 expect(video0.views).to.equal(3)
432 expect(video1.views).to.equal(1)
436 it('Should view multiple videos on each servers', async function () {
439 const tasks: Promise<any>[] = []
440 tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
441 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
442 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
443 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
444 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
445 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
446 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
447 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
448 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
449 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
451 await Promise.all(tasks)
455 let baseVideos = null
457 for (const server of servers) {
458 const res = await getVideosList(server.url)
460 const videos = res.body.data
462 // Initialize base videos for future comparisons
463 if (baseVideos === null) {
468 for (const baseVideo of baseVideos) {
469 const sameVideo = videos.find(video => video.name === baseVideo.name)
470 expect(baseVideo.views).to.equal(sameVideo.views)
475 it('Should like and dislikes videos on different services', async function () {
478 const tasks: Promise<any>[] = []
479 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
480 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike'))
481 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
482 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like'))
483 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike'))
484 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike'))
485 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like'))
487 await Promise.all(tasks)
491 let baseVideos = null
492 for (const server of servers) {
493 const res = await getVideosList(server.url)
495 const videos = res.body.data
497 // Initialize base videos for future comparisons
498 if (baseVideos === null) {
503 for (const baseVideo of baseVideos) {
504 const sameVideo = videos.find(video => video.name === baseVideo.name)
505 expect(baseVideo.likes).to.equal(sameVideo.likes)
506 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
512 describe('Should manipulate these videos', function () {
513 it('Should update the video 3 by asking server 3', async function () {
517 name: 'my super video updated',
522 description: 'my super description updated',
523 tags: [ 'tag_up_1', 'tag_up_2' ]
526 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
531 it('Should have the video 3 updated on each server', async function () {
534 for (const server of servers) {
535 const res = await getVideosList(server.url)
537 const videos = res.body.data
538 const videoUpdated = videos.find(video => video.name === 'my super video updated')
539 expect(!!videoUpdated).to.be.true
541 const isLocal = server.url === 'http://localhost:9003'
542 const checkAttributes = {
543 name: 'my super video updated',
548 description: 'my super description updated',
549 host: 'localhost:9003',
553 commentsEnabled: true,
554 tags: [ 'tag_up_1', 'tag_up_2' ],
555 privacy: VideoPrivacy.PUBLIC,
557 name: 'Default root channel',
561 fixture: 'video_short3.webm',
569 await completeVideoCheck(server.url, videoUpdated, checkAttributes)
573 it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
576 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
577 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
582 it('Should not have files of videos 3 and 3-2 on each server', async function () {
583 for (const server of servers) {
584 await checkVideoFilesWereRemoved(toRemove[0].uuid, server.serverNumber)
585 await checkVideoFilesWereRemoved(toRemove[1].uuid, server.serverNumber)
589 it('Should have videos 1 and 3 on each server', async function () {
590 for (const server of servers) {
591 const res = await getVideosList(server.url)
593 const videos = res.body.data
594 expect(videos).to.be.an('array')
595 expect(videos.length).to.equal(2)
596 expect(videos[0].name).not.to.equal(videos[1].name)
597 expect(videos[0].name).not.to.equal(toRemove[0].name)
598 expect(videos[1].name).not.to.equal(toRemove[0].name)
599 expect(videos[0].name).not.to.equal(toRemove[1].name)
600 expect(videos[1].name).not.to.equal(toRemove[1].name)
602 videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
606 it('Should get the same video by UUID on each server', async function () {
608 for (const server of servers) {
609 const res = await getVideo(server.url, videoUUID)
611 const video = res.body
613 if (baseVideo === null) {
618 expect(baseVideo.name).to.equal(video.name)
619 expect(baseVideo.uuid).to.equal(video.uuid)
620 expect(baseVideo.category).to.equal(video.category)
621 expect(baseVideo.language).to.equal(video.language)
622 expect(baseVideo.licence).to.equal(video.licence)
623 expect(baseVideo.category).to.equal(video.category)
624 expect(baseVideo.nsfw).to.equal(video.nsfw)
625 expect(baseVideo.accountName).to.equal(video.accountName)
626 expect(baseVideo.tags).to.deep.equal(video.tags)
630 it('Should get the preview from each server', async function () {
631 for (const server of servers) {
632 const res = await getVideo(server.url, videoUUID)
633 const video = res.body
635 const test = await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
636 expect(test).to.equal(true)
641 describe('Should comment these videos', function () {
642 it('Should add comment (threads and replies)', async function () {
646 const text = 'my super first comment'
647 await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
651 const text = 'my super second comment'
652 await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
658 const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
659 const threadId = res.body.data.find(c => c.text === 'my super first comment').id
661 const text = 'my super answer to thread 1'
662 await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
668 const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
669 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
671 const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
672 const childCommentId = res2.body.children[0].comment.id
674 const text3 = 'my second answer to thread 1'
675 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
677 const text2 = 'my super answer to answer of thread 1'
678 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
684 it('Should have these threads', async function () {
685 for (const server of servers) {
686 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
688 expect(res.body.total).to.equal(2)
689 expect(res.body.data).to.be.an('array')
690 expect(res.body.data).to.have.lengthOf(2)
693 const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
694 expect(comment).to.not.be.undefined
695 expect(comment.inReplyToCommentId).to.be.null
696 expect(comment.account.name).to.equal('root')
697 expect(comment.account.host).to.equal('localhost:9001')
698 expect(comment.totalReplies).to.equal(3)
699 expect(dateIsValid(comment.createdAt as string)).to.be.true
700 expect(dateIsValid(comment.updatedAt as string)).to.be.true
704 const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
705 expect(comment).to.not.be.undefined
706 expect(comment.inReplyToCommentId).to.be.null
707 expect(comment.account.name).to.equal('root')
708 expect(comment.account.host).to.equal('localhost:9003')
709 expect(comment.totalReplies).to.equal(0)
710 expect(dateIsValid(comment.createdAt as string)).to.be.true
711 expect(dateIsValid(comment.updatedAt as string)).to.be.true
716 it('Should have these comments', async function () {
717 for (const server of servers) {
718 const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
719 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
721 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
723 const tree: VideoCommentThreadTree = res2.body
724 expect(tree.comment.text).equal('my super first comment')
725 expect(tree.comment.account.name).equal('root')
726 expect(tree.comment.account.host).equal('localhost:9001')
727 expect(tree.children).to.have.lengthOf(2)
729 const firstChild = tree.children[0]
730 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
731 expect(firstChild.comment.account.name).equal('root')
732 expect(firstChild.comment.account.host).equal('localhost:9002')
733 expect(firstChild.children).to.have.lengthOf(1)
735 const childOfFirstChild = firstChild.children[0]
736 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
737 expect(childOfFirstChild.comment.account.name).equal('root')
738 expect(childOfFirstChild.comment.account.host).equal('localhost:9003')
739 expect(childOfFirstChild.children).to.have.lengthOf(0)
741 const secondChild = tree.children[1]
742 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
743 expect(secondChild.comment.account.name).equal('root')
744 expect(secondChild.comment.account.host).equal('localhost:9003')
745 expect(secondChild.children).to.have.lengthOf(0)
749 it('Should delete the thread comments', async function () {
752 const res1 = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5)
753 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
754 await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId)
759 it('Should have the thread comments deleted on other servers too', async function () {
760 for (const server of servers) {
761 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
763 expect(res.body.total).to.equal(1)
764 expect(res.body.data).to.be.an('array')
765 expect(res.body.data).to.have.lengthOf(1)
768 const comment: VideoComment = res.body.data[0]
769 expect(comment).to.not.be.undefined
770 expect(comment.inReplyToCommentId).to.be.null
771 expect(comment.account.name).to.equal('root')
772 expect(comment.account.host).to.equal('localhost:9003')
773 expect(comment.totalReplies).to.equal(0)
774 expect(dateIsValid(comment.createdAt as string)).to.be.true
775 expect(dateIsValid(comment.updatedAt as string)).to.be.true
780 it('Should disable comments', async function () {
784 commentsEnabled: false
787 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes)
791 for (const server of servers) {
792 const res = await getVideo(server.url, videoUUID)
793 expect(res.body.commentsEnabled).to.be.false
795 const text = 'my super forbidden comment'
796 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, 409)
801 describe('With minimum parameters', function () {
802 it('Should upload and propagate the video', async function () {
805 const path = '/api/v1/videos/upload'
807 const req = request(servers[1].url)
809 .set('Accept', 'application/json')
810 .set('Authorization', 'Bearer ' + servers[1].accessToken)
811 .field('name', 'minimum parameters')
812 .field('privacy', '1')
813 .field('nsfw', 'false')
814 .field('channelId', '1')
815 .field('commentsEnabled', 'true')
817 const filePath = join(__dirname, '..', '..', 'api', 'fixtures', 'video_short.webm')
819 await req.attach('videofile', filePath)
824 for (const server of servers) {
825 const res = await getVideosList(server.url)
826 const video = res.body.data.find(v => v.name === 'minimum parameters')
828 const isLocal = server.url === 'http://localhost:9002'
829 const checkAttributes = {
830 name: 'minimum parameters',
836 host: 'localhost:9002',
840 commentsEnabled: true,
842 privacy: VideoPrivacy.PUBLIC,
844 name: 'Default root channel',
848 fixture: 'video_short.webm',
868 await completeVideoCheck(server.url, video, checkAttributes)
873 after(async function () {
874 killallServers(servers)
876 // Keep the logs if the test failed