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, completeVideoCheck, createUser, dateIsValid, doubleFollow, flushAndRunMultipleServers, flushTests, getVideo,
12 getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testVideoImage,
13 updateVideo, uploadVideo, userLogin, viewVideo, wait, webtorrentAdd
16 addVideoCommentReply, addVideoCommentThread, deleteVideoComment, getVideoCommentThreads,
17 getVideoThreadComments
18 } from '../../utils/videos/video-comments'
20 const expect = chai.expect
22 describe('Test multiple servers', function () {
23 let servers: ServerInfo[] = []
26 let videoChannelId: number
28 before(async function () {
31 servers = await flushAndRunMultipleServers(3)
33 // Get the access tokens
34 await setAccessTokensToServers(servers)
36 const videoChannel = {
38 description: 'super channel'
40 await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
41 const channelRes = await getVideoChannelsList(servers[0].url, 0, 1)
42 videoChannelId = channelRes.body.data[0].id
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
46 // Server 1 and server 3 follow each other
47 await doubleFollow(servers[0], servers[2])
48 // Server 2 and server 3 follow each other
49 await doubleFollow(servers[1], servers[2])
52 it('Should not have videos for all servers', async function () {
53 for (const server of servers) {
54 const res = await getVideosList(server.url)
55 const videos = res.body.data
56 expect(videos).to.be.an('array')
57 expect(videos.length).to.equal(0)
61 describe('Should upload the video and propagate on each server', function () {
62 it('Should upload the video on server 1 and propagate on each server', async function () {
65 const videoAttributes = {
66 name: 'my super name for server 1',
71 description: 'my super description for server 1',
72 tags: [ 'tag1p1', 'tag2p1' ],
73 channelId: videoChannelId,
74 fixture: 'video_short1.webm'
76 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
80 // All servers should have this video
81 for (const server of servers) {
82 const isLocal = server.url === 'http://localhost:9001'
83 const checkAttributes = {
84 name: 'my super name for server 1',
89 description: 'my super description for server 1',
90 host: 'localhost:9001',
94 tags: [ 'tag1p1', 'tag2p1' ],
95 privacy: VideoPrivacy.PUBLIC,
96 commentsEnabled: true,
99 description: 'super channel',
102 fixture: 'video_short1.webm',
111 const res = await getVideosList(server.url)
112 const videos = res.body.data
113 expect(videos).to.be.an('array')
114 expect(videos.length).to.equal(1)
115 const video = videos[0]
117 await completeVideoCheck(server.url, video, checkAttributes)
121 it('Should upload the video on server 2 and propagate on each server', async function () {
126 password: 'super_password'
128 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
129 const userAccessToken = await userLogin(servers[1], user)
131 const videoAttributes = {
132 name: 'my super name for server 2',
137 description: 'my super description for server 2',
138 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
139 fixture: 'video_short2.webm'
141 await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
146 // All servers should have this video
147 for (const server of servers) {
148 const isLocal = server.url === 'http://localhost:9002'
149 const checkAttributes = {
150 name: 'my super name for server 2',
155 description: 'my super description for server 2',
156 host: 'localhost:9002',
159 commentsEnabled: true,
161 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
162 privacy: VideoPrivacy.PUBLIC,
164 name: 'Default user1 channel',
165 description: 'super channel',
168 fixture: 'video_short2.webm',
189 const res = await getVideosList(server.url)
190 const videos = res.body.data
191 expect(videos).to.be.an('array')
192 expect(videos.length).to.equal(2)
193 const video = videos[1]
195 await completeVideoCheck(server.url, video, checkAttributes)
199 it('Should upload two videos on server 3 and propagate on each server', async function () {
202 const videoAttributes1 = {
203 name: 'my super name for server 3',
208 description: 'my super description for server 3',
210 fixture: 'video_short3.webm'
212 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
214 const videoAttributes2 = {
215 name: 'my super name for server 3-2',
220 description: 'my super description for server 3-2',
221 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
222 fixture: 'video_short.webm'
224 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
228 // All servers should have this video
229 for (const server of servers) {
230 const isLocal = server.url === 'http://localhost:9003'
231 const res = await getVideosList(server.url)
233 const videos = res.body.data
234 expect(videos).to.be.an('array')
235 expect(videos.length).to.equal(4)
237 // We not sure about the order of the two last uploads
240 if (videos[2].name === 'my super name for server 3') {
248 const checkAttributesVideo1 = {
249 name: 'my super name for server 3',
254 description: 'my super description for server 3',
255 host: 'localhost:9003',
259 commentsEnabled: true,
261 privacy: VideoPrivacy.PUBLIC,
263 name: 'Default root channel',
267 fixture: 'video_short3.webm',
275 await completeVideoCheck(server.url, video1, checkAttributesVideo1)
277 const checkAttributesVideo2 = {
278 name: 'my super name for server 3-2',
283 description: 'my super description for server 3-2',
284 host: 'localhost:9003',
286 commentsEnabled: true,
289 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
290 privacy: VideoPrivacy.PUBLIC,
292 name: 'Default root channel',
296 fixture: 'video_short.webm',
304 await completeVideoCheck(server.url, video2, checkAttributesVideo2)
309 describe('Should seed the uploaded video', function () {
310 it('Should add the file 1 by asking server 3', async function () {
313 const res = await getVideosList(servers[2].url)
315 const video = res.body.data[0]
316 toRemove.push(res.body.data[2])
317 toRemove.push(res.body.data[3])
319 const res2 = await getVideo(servers[2].url, video.id)
320 const videoDetails = res2.body
322 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
323 expect(torrent.files).to.be.an('array')
324 expect(torrent.files.length).to.equal(1)
325 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
328 it('Should add the file 2 by asking server 1', async function () {
331 const res = await getVideosList(servers[0].url)
333 const video = res.body.data[1]
334 const res2 = await getVideo(servers[0].url, video.id)
335 const videoDetails = res2.body
337 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
338 expect(torrent.files).to.be.an('array')
339 expect(torrent.files.length).to.equal(1)
340 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
343 it('Should add the file 3 by asking server 2', async function () {
346 const res = await getVideosList(servers[1].url)
348 const video = res.body.data[2]
349 const res2 = await getVideo(servers[1].url, video.id)
350 const videoDetails = res2.body
352 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
353 expect(torrent.files).to.be.an('array')
354 expect(torrent.files.length).to.equal(1)
355 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
358 it('Should add the file 3-2 by asking server 1', async function () {
361 const res = await getVideosList(servers[0].url)
363 const video = res.body.data[3]
364 const res2 = await getVideo(servers[0].url, video.id)
365 const videoDetails = res2.body
367 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
368 expect(torrent.files).to.be.an('array')
369 expect(torrent.files.length).to.equal(1)
370 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
373 it('Should add the file 2 in 360p by asking server 1', async function () {
376 const res = await getVideosList(servers[0].url)
378 const video = res.body.data.find(v => v.name === 'my super name for server 2')
379 const res2 = await getVideo(servers[0].url, video.id)
380 const videoDetails = res2.body
382 const file = videoDetails.files.find(f => f.resolution === 360)
383 expect(file).not.to.be.undefined
385 const torrent = await webtorrentAdd(file.magnetUri)
386 expect(torrent.files).to.be.an('array')
387 expect(torrent.files.length).to.equal(1)
388 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
392 describe('Should update video views, likes and dislikes', function () {
393 let localVideosServer3 = []
394 let remoteVideosServer1 = []
395 let remoteVideosServer2 = []
396 let remoteVideosServer3 = []
398 before(async function () {
399 const res1 = await getVideosList(servers[0].url)
400 remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
402 const res2 = await getVideosList(servers[1].url)
403 remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
405 const res3 = await getVideosList(servers[2].url)
406 localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid)
407 remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
410 it('Should view multiple videos on owned servers', async function () {
413 const tasks: Promise<any>[] = []
414 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
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[1]))
419 await Promise.all(tasks)
423 for (const server of servers) {
424 const res = await getVideosList(server.url)
426 const videos = res.body.data
427 const video0 = videos.find(v => v.uuid === localVideosServer3[0])
428 const video1 = videos.find(v => v.uuid === localVideosServer3[1])
430 expect(video0.views).to.equal(3)
431 expect(video1.views).to.equal(1)
435 it('Should view multiple videos on each servers', async function () {
438 const tasks: Promise<any>[] = []
439 tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
440 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
441 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
442 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
443 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
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, localVideosServer3[1]))
447 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
448 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
450 await Promise.all(tasks)
454 let baseVideos = null
456 for (const server of servers) {
457 const res = await getVideosList(server.url)
459 const videos = res.body.data
461 // Initialize base videos for future comparisons
462 if (baseVideos === null) {
467 for (const baseVideo of baseVideos) {
468 const sameVideo = videos.find(video => video.name === baseVideo.name)
469 expect(baseVideo.views).to.equal(sameVideo.views)
474 it('Should like and dislikes videos on different services', async function () {
477 const tasks: Promise<any>[] = []
478 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
479 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike'))
480 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
481 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like'))
482 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike'))
483 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike'))
484 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like'))
486 await Promise.all(tasks)
490 let baseVideos = null
491 for (const server of servers) {
492 const res = await getVideosList(server.url)
494 const videos = res.body.data
496 // Initialize base videos for future comparisons
497 if (baseVideos === null) {
502 for (const baseVideo of baseVideos) {
503 const sameVideo = videos.find(video => video.name === baseVideo.name)
504 expect(baseVideo.likes).to.equal(sameVideo.likes)
505 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
511 describe('Should manipulate these videos', function () {
512 it('Should update the video 3 by asking server 3', async function () {
516 name: 'my super video updated',
521 description: 'my super description updated',
522 tags: [ 'tag_up_1', 'tag_up_2' ]
525 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
530 it('Should have the video 3 updated on each server', async function () {
533 for (const server of servers) {
534 const res = await getVideosList(server.url)
536 const videos = res.body.data
537 const videoUpdated = videos.find(video => video.name === 'my super video updated')
538 expect(!!videoUpdated).to.be.true
540 const isLocal = server.url === 'http://localhost:9003'
541 const checkAttributes = {
542 name: 'my super video updated',
547 description: 'my super description updated',
548 host: 'localhost:9003',
552 commentsEnabled: true,
553 tags: [ 'tag_up_1', 'tag_up_2' ],
554 privacy: VideoPrivacy.PUBLIC,
556 name: 'Default root channel',
560 fixture: 'video_short3.webm',
568 await completeVideoCheck(server.url, videoUpdated, checkAttributes)
572 it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
575 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
576 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
581 it('Should have videos 1 and 3 on each server', async function () {
582 for (const server of servers) {
583 const res = await getVideosList(server.url)
585 const videos = res.body.data
586 expect(videos).to.be.an('array')
587 expect(videos.length).to.equal(2)
588 expect(videos[0].name).not.to.equal(videos[1].name)
589 expect(videos[0].name).not.to.equal(toRemove[0].name)
590 expect(videos[1].name).not.to.equal(toRemove[0].name)
591 expect(videos[0].name).not.to.equal(toRemove[1].name)
592 expect(videos[1].name).not.to.equal(toRemove[1].name)
594 videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
598 it('Should get the same video by UUID on each server', async function () {
600 for (const server of servers) {
601 const res = await getVideo(server.url, videoUUID)
603 const video = res.body
605 if (baseVideo === null) {
610 expect(baseVideo.name).to.equal(video.name)
611 expect(baseVideo.uuid).to.equal(video.uuid)
612 expect(baseVideo.category).to.equal(video.category)
613 expect(baseVideo.language).to.equal(video.language)
614 expect(baseVideo.licence).to.equal(video.licence)
615 expect(baseVideo.category).to.equal(video.category)
616 expect(baseVideo.nsfw).to.equal(video.nsfw)
617 expect(baseVideo.accountName).to.equal(video.accountName)
618 expect(baseVideo.tags).to.deep.equal(video.tags)
622 it('Should get the preview from each server', async function () {
623 for (const server of servers) {
624 const res = await getVideo(server.url, videoUUID)
625 const video = res.body
627 const test = await testVideoImage(server.url, 'video_short1-preview.webm', video.previewPath)
628 expect(test).to.equal(true)
633 describe('Should comment these videos', function () {
634 it('Should add comment (threads and replies)', async function () {
638 const text = 'my super first comment'
639 await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
643 const text = 'my super second comment'
644 await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
650 const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
651 const threadId = res.body.data.find(c => c.text === 'my super first comment').id
653 const text = 'my super answer to thread 1'
654 await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
660 const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
661 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
663 const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
664 const childCommentId = res2.body.children[0].comment.id
666 const text3 = 'my second answer to thread 1'
667 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
669 const text2 = 'my super answer to answer of thread 1'
670 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
676 it('Should have these threads', async function () {
677 for (const server of servers) {
678 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
680 expect(res.body.total).to.equal(2)
681 expect(res.body.data).to.be.an('array')
682 expect(res.body.data).to.have.lengthOf(2)
685 const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
686 expect(comment).to.not.be.undefined
687 expect(comment.inReplyToCommentId).to.be.null
688 expect(comment.account.name).to.equal('root')
689 expect(comment.account.host).to.equal('localhost:9001')
690 expect(comment.totalReplies).to.equal(3)
691 expect(dateIsValid(comment.createdAt as string)).to.be.true
692 expect(dateIsValid(comment.updatedAt as string)).to.be.true
696 const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
697 expect(comment).to.not.be.undefined
698 expect(comment.inReplyToCommentId).to.be.null
699 expect(comment.account.name).to.equal('root')
700 expect(comment.account.host).to.equal('localhost:9003')
701 expect(comment.totalReplies).to.equal(0)
702 expect(dateIsValid(comment.createdAt as string)).to.be.true
703 expect(dateIsValid(comment.updatedAt as string)).to.be.true
708 it('Should have these comments', async function () {
709 for (const server of servers) {
710 const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
711 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
713 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
715 const tree: VideoCommentThreadTree = res2.body
716 expect(tree.comment.text).equal('my super first comment')
717 expect(tree.comment.account.name).equal('root')
718 expect(tree.comment.account.host).equal('localhost:9001')
719 expect(tree.children).to.have.lengthOf(2)
721 const firstChild = tree.children[0]
722 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
723 expect(firstChild.comment.account.name).equal('root')
724 expect(firstChild.comment.account.host).equal('localhost:9002')
725 expect(firstChild.children).to.have.lengthOf(1)
727 const childOfFirstChild = firstChild.children[0]
728 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
729 expect(childOfFirstChild.comment.account.name).equal('root')
730 expect(childOfFirstChild.comment.account.host).equal('localhost:9003')
731 expect(childOfFirstChild.children).to.have.lengthOf(0)
733 const secondChild = tree.children[1]
734 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
735 expect(secondChild.comment.account.name).equal('root')
736 expect(secondChild.comment.account.host).equal('localhost:9003')
737 expect(secondChild.children).to.have.lengthOf(0)
741 it('Should delete the thread comments', async function () {
744 const res1 = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 5)
745 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
746 await deleteVideoComment(servers[0].url, servers[0].accessToken, videoUUID, threadId)
751 it('Should have the thread comments deleted on other servers too', async function () {
752 for (const server of servers) {
753 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
755 expect(res.body.total).to.equal(1)
756 expect(res.body.data).to.be.an('array')
757 expect(res.body.data).to.have.lengthOf(1)
760 const comment: VideoComment = res.body.data[0]
761 expect(comment).to.not.be.undefined
762 expect(comment.inReplyToCommentId).to.be.null
763 expect(comment.account.name).to.equal('root')
764 expect(comment.account.host).to.equal('localhost:9003')
765 expect(comment.totalReplies).to.equal(0)
766 expect(dateIsValid(comment.createdAt as string)).to.be.true
767 expect(dateIsValid(comment.updatedAt as string)).to.be.true
772 it('Should disable comments', async function () {
776 commentsEnabled: false
779 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes)
783 for (const server of servers) {
784 const res = await getVideo(server.url, videoUUID)
785 expect(res.body.commentsEnabled).to.be.false
787 const text = 'my super forbidden comment'
788 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, 409)
793 describe('With minimum parameters', function () {
794 it('Should upload and propagate the video', async function () {
797 const path = '/api/v1/videos/upload'
799 const req = request(servers[1].url)
801 .set('Accept', 'application/json')
802 .set('Authorization', 'Bearer ' + servers[1].accessToken)
803 .field('name', 'minimum parameters')
804 .field('privacy', '1')
805 .field('nsfw', 'false')
806 .field('channelId', '1')
807 .field('commentsEnabled', 'true')
809 const filePath = join(__dirname, '..', '..', 'api', 'fixtures', 'video_short.webm')
811 await req.attach('videofile', filePath)
816 for (const server of servers) {
817 const res = await getVideosList(server.url)
818 const video = res.body.data.find(v => v.name === 'minimum parameters')
820 const isLocal = server.url === 'http://localhost:9002'
821 const checkAttributes = {
822 name: 'minimum parameters',
828 host: 'localhost:9002',
832 commentsEnabled: true,
834 privacy: VideoPrivacy.PUBLIC,
836 name: 'Default root channel',
840 fixture: 'video_short.webm',
860 await completeVideoCheck(server.url, video, checkAttributes)
865 after(async function () {
866 killallServers(servers)
868 // Keep the logs if the test failed