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, 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,
98 description: 'super channel',
101 fixture: 'video_short.webm',
110 const res = await getVideosList(server.url)
111 const videos = res.body.data
112 expect(videos).to.be.an('array')
113 expect(videos.length).to.equal(1)
114 const video = videos[0]
116 await completeVideoCheck(server.url, video, checkAttributes)
120 it('Should upload the video on server 2 and propagate on each server', async function () {
125 password: 'super_password'
127 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
128 const userAccessToken = await userLogin(servers[1], user)
130 const videoAttributes = {
131 name: 'my super name for server 2',
136 description: 'my super description for server 2',
137 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
138 fixture: 'video_short2.webm'
140 await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
145 // All servers should have this video
146 for (const server of servers) {
147 const isLocal = server.url === 'http://localhost:9002'
148 const checkAttributes = {
149 name: 'my super name for server 2',
154 description: 'my super description for server 2',
155 host: 'localhost:9002',
159 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
160 privacy: VideoPrivacy.PUBLIC,
162 name: 'Default user1 channel',
163 description: 'super channel',
166 fixture: 'video_short2.webm',
187 const res = await getVideosList(server.url)
188 const videos = res.body.data
189 expect(videos).to.be.an('array')
190 expect(videos.length).to.equal(2)
191 const video = videos[1]
193 await completeVideoCheck(server.url, video, checkAttributes)
197 it('Should upload two videos on server 3 and propagate on each server', async function () {
200 const videoAttributes1 = {
201 name: 'my super name for server 3',
206 description: 'my super description for server 3',
208 fixture: 'video_short3.webm'
210 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
212 const videoAttributes2 = {
213 name: 'my super name for server 3-2',
218 description: 'my super description for server 3-2',
219 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
220 fixture: 'video_short.webm'
222 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
226 // All servers should have this video
227 for (const server of servers) {
228 const isLocal = server.url === 'http://localhost:9003'
229 const res = await getVideosList(server.url)
231 const videos = res.body.data
232 expect(videos).to.be.an('array')
233 expect(videos.length).to.equal(4)
235 // We not sure about the order of the two last uploads
238 if (videos[2].name === 'my super name for server 3') {
246 const checkAttributesVideo1 = {
247 name: 'my super name for server 3',
252 description: 'my super description for server 3',
253 host: 'localhost:9003',
258 privacy: VideoPrivacy.PUBLIC,
260 name: 'Default root channel',
264 fixture: 'video_short3.webm',
272 await completeVideoCheck(server.url, video1, checkAttributesVideo1)
274 const checkAttributesVideo2 = {
275 name: 'my super name for server 3-2',
280 description: 'my super description for server 3-2',
281 host: 'localhost:9003',
285 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
286 privacy: VideoPrivacy.PUBLIC,
288 name: 'Default root channel',
292 fixture: 'video_short3.webm',
300 await completeVideoCheck(server.url, video2, checkAttributesVideo2)
305 describe('Should seed the uploaded video', function () {
306 it('Should add the file 1 by asking server 3', async function () {
309 const res = await getVideosList(servers[2].url)
311 const video = res.body.data[0]
312 toRemove.push(res.body.data[2])
313 toRemove.push(res.body.data[3])
315 const res2 = await getVideo(servers[2].url, video.id)
316 const videoDetails = res2.body
318 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
319 expect(torrent.files).to.be.an('array')
320 expect(torrent.files.length).to.equal(1)
321 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
324 it('Should add the file 2 by asking server 1', async function () {
327 const res = await getVideosList(servers[0].url)
329 const video = res.body.data[1]
330 const res2 = await getVideo(servers[0].url, video.id)
331 const videoDetails = res2.body
333 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
334 expect(torrent.files).to.be.an('array')
335 expect(torrent.files.length).to.equal(1)
336 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
339 it('Should add the file 3 by asking server 2', async function () {
342 const res = await getVideosList(servers[1].url)
344 const video = res.body.data[2]
345 const res2 = await getVideo(servers[1].url, video.id)
346 const videoDetails = res2.body
348 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri, true)
349 expect(torrent.files).to.be.an('array')
350 expect(torrent.files.length).to.equal(1)
351 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
354 it('Should add the file 3-2 by asking server 1', async function () {
357 const res = await getVideosList(servers[0].url)
359 const video = res.body.data[3]
360 const res2 = await getVideo(servers[0].url, video.id)
361 const videoDetails = res2.body
363 const torrent = await webtorrentAdd(videoDetails.files[0].magnetUri)
364 expect(torrent.files).to.be.an('array')
365 expect(torrent.files.length).to.equal(1)
366 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
369 it('Should add the file 2 in 360p by asking server 1', async function () {
372 const res = await getVideosList(servers[0].url)
374 const video = res.body.data.find(v => v.name === 'my super name for server 2')
375 const res2 = await getVideo(servers[0].url, video.id)
376 const videoDetails = res2.body
378 const file = videoDetails.files.find(f => f.resolution === 360)
379 expect(file).not.to.be.undefined
381 const torrent = await webtorrentAdd(file.magnetUri)
382 expect(torrent.files).to.be.an('array')
383 expect(torrent.files.length).to.equal(1)
384 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
388 describe('Should update video views, likes and dislikes', function () {
389 let localVideosServer3 = []
390 let remoteVideosServer1 = []
391 let remoteVideosServer2 = []
392 let remoteVideosServer3 = []
394 before(async function () {
395 const res1 = await getVideosList(servers[0].url)
396 remoteVideosServer1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
398 const res2 = await getVideosList(servers[1].url)
399 remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
401 const res3 = await getVideosList(servers[2].url)
402 localVideosServer3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.uuid)
403 remoteVideosServer3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
406 it('Should view multiple videos on owned servers', async function () {
409 const tasks: Promise<any>[] = []
410 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
411 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
412 tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
413 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
415 await Promise.all(tasks)
419 for (const server of servers) {
420 const res = await getVideosList(server.url)
422 const videos = res.body.data
423 const video0 = videos.find(v => v.uuid === localVideosServer3[0])
424 const video1 = videos.find(v => v.uuid === localVideosServer3[1])
426 expect(video0.views).to.equal(3)
427 expect(video1.views).to.equal(1)
431 it('Should view multiple videos on each servers', async function () {
434 const tasks: Promise<any>[] = []
435 tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
436 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
437 tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
438 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
439 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
440 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
441 tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
442 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
443 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
444 tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
446 await Promise.all(tasks)
450 let baseVideos = null
452 for (const server of servers) {
453 const res = await getVideosList(server.url)
455 const videos = res.body.data
457 // Initialize base videos for future comparisons
458 if (baseVideos === null) {
463 for (const baseVideo of baseVideos) {
464 const sameVideo = videos.find(video => video.name === baseVideo.name)
465 expect(baseVideo.views).to.equal(sameVideo.views)
470 it('Should like and dislikes videos on different services', async function () {
473 const tasks: Promise<any>[] = []
474 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
475 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike'))
476 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like'))
477 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like'))
478 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike'))
479 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike'))
480 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like'))
482 await Promise.all(tasks)
486 let baseVideos = null
487 for (const server of servers) {
488 const res = await getVideosList(server.url)
490 const videos = res.body.data
492 // Initialize base videos for future comparisons
493 if (baseVideos === null) {
498 for (const baseVideo of baseVideos) {
499 const sameVideo = videos.find(video => video.name === baseVideo.name)
500 expect(baseVideo.likes).to.equal(sameVideo.likes)
501 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
507 describe('Should manipulate these videos', function () {
508 it('Should update the video 3 by asking server 3', async function () {
512 name: 'my super video updated',
517 description: 'my super description updated',
518 tags: [ 'tag_up_1', 'tag_up_2' ]
521 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
526 it('Should have the video 3 updated on each server', async function () {
529 for (const server of servers) {
530 const res = await getVideosList(server.url)
532 const videos = res.body.data
533 const videoUpdated = videos.find(video => video.name === 'my super video updated')
534 expect(!!videoUpdated).to.be.true
536 const isLocal = server.url === 'http://localhost:9003'
537 const checkAttributes = {
538 name: 'my super video updated',
543 description: 'my super description updated',
544 host: 'localhost:9003',
548 tags: [ 'tag_up_1', 'tag_up_2' ],
549 privacy: VideoPrivacy.PUBLIC,
551 name: 'Default root channel',
555 fixture: 'video_short3.webm',
563 await completeVideoCheck(server.url, videoUpdated, checkAttributes)
567 it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
570 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
571 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
576 it('Should have videos 1 and 3 on each server', async function () {
577 for (const server of servers) {
578 const res = await getVideosList(server.url)
580 const videos = res.body.data
581 expect(videos).to.be.an('array')
582 expect(videos.length).to.equal(2)
583 expect(videos[0].name).not.to.equal(videos[1].name)
584 expect(videos[0].name).not.to.equal(toRemove[0].name)
585 expect(videos[1].name).not.to.equal(toRemove[0].name)
586 expect(videos[0].name).not.to.equal(toRemove[1].name)
587 expect(videos[1].name).not.to.equal(toRemove[1].name)
589 videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
593 it('Should get the same video by UUID on each server', async function () {
595 for (const server of servers) {
596 const res = await getVideo(server.url, videoUUID)
598 const video = res.body
600 if (baseVideo === null) {
605 expect(baseVideo.name).to.equal(video.name)
606 expect(baseVideo.uuid).to.equal(video.uuid)
607 expect(baseVideo.category).to.equal(video.category)
608 expect(baseVideo.language).to.equal(video.language)
609 expect(baseVideo.licence).to.equal(video.licence)
610 expect(baseVideo.category).to.equal(video.category)
611 expect(baseVideo.nsfw).to.equal(video.nsfw)
612 expect(baseVideo.accountName).to.equal(video.accountName)
613 expect(baseVideo.tags).to.deep.equal(video.tags)
617 it('Should get the preview from each server', async function () {
618 for (const server of servers) {
619 const res = await getVideo(server.url, videoUUID)
620 const video = res.body
622 const test = await testVideoImage(server.url, 'video_short1-preview.webm', video.previewPath)
623 expect(test).to.equal(true)
628 describe('Should comment these videos', function () {
629 it('Should add comment (threads and replies)', async function () {
633 const text = 'my super first comment'
634 await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
638 const text = 'my super second comment'
639 await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
645 const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
646 const threadId = res.body.data.find(c => c.text === 'my super first comment').id
648 const text = 'my super answer to thread 1'
649 await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
655 const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
656 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
658 const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
659 const childCommentId = res2.body.children[0].comment.id
661 const text3 = 'my second answer to thread 1'
662 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
664 const text2 = 'my super answer to answer of thread 1'
665 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
671 it('Should have these threads', async function () {
672 for (const server of servers) {
673 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
675 expect(res.body.total).to.equal(2)
676 expect(res.body.data).to.be.an('array')
677 expect(res.body.data).to.have.lengthOf(2)
680 const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
681 expect(comment).to.not.be.undefined
682 expect(comment.inReplyToCommentId).to.be.null
683 expect(comment.account.name).to.equal('root')
684 expect(comment.account.host).to.equal('localhost:9001')
685 expect(comment.totalReplies).to.equal(3)
686 expect(dateIsValid(comment.createdAt as string)).to.be.true
687 expect(dateIsValid(comment.updatedAt as string)).to.be.true
691 const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
692 expect(comment).to.not.be.undefined
693 expect(comment.inReplyToCommentId).to.be.null
694 expect(comment.account.name).to.equal('root')
695 expect(comment.account.host).to.equal('localhost:9003')
696 expect(comment.totalReplies).to.equal(0)
697 expect(dateIsValid(comment.createdAt as string)).to.be.true
698 expect(dateIsValid(comment.updatedAt as string)).to.be.true
703 it('Should have these comments', async function () {
704 for (const server of servers) {
705 const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
706 const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
708 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
710 const tree: VideoCommentThreadTree = res2.body
711 expect(tree.comment.text).equal('my super first comment')
712 expect(tree.comment.account.name).equal('root')
713 expect(tree.comment.account.host).equal('localhost:9001')
714 expect(tree.children).to.have.lengthOf(2)
716 const firstChild = tree.children[0]
717 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
718 expect(firstChild.comment.account.name).equal('root')
719 expect(firstChild.comment.account.host).equal('localhost:9002')
720 expect(firstChild.children).to.have.lengthOf(1)
722 const childOfFirstChild = firstChild.children[0]
723 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
724 expect(childOfFirstChild.comment.account.name).equal('root')
725 expect(childOfFirstChild.comment.account.host).equal('localhost:9003')
726 expect(childOfFirstChild.children).to.have.lengthOf(0)
728 const secondChild = tree.children[1]
729 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
730 expect(secondChild.comment.account.name).equal('root')
731 expect(secondChild.comment.account.host).equal('localhost:9003')
732 expect(secondChild.children).to.have.lengthOf(0)
737 describe('With minimum parameters', function () {
738 it('Should upload and propagate the video', async function () {
741 const path = '/api/v1/videos/upload'
743 const req = request(servers[1].url)
745 .set('Accept', 'application/json')
746 .set('Authorization', 'Bearer ' + servers[1].accessToken)
747 .field('name', 'minimum parameters')
748 .field('privacy', '1')
749 .field('nsfw', 'false')
750 .field('channelId', '1')
752 const filePath = join(__dirname, '..', '..', 'api', 'fixtures', 'video_short.webm')
754 await req.attach('videofile', filePath)
759 for (const server of servers) {
760 const res = await getVideosList(server.url)
761 const video = res.body.data.find(v => v.name === 'minimum parameters')
763 const isLocal = server.url === 'http://localhost:9002'
764 const checkAttributes = {
765 name: 'minimum parameters',
771 host: 'localhost:9002',
776 privacy: VideoPrivacy.PUBLIC,
778 name: 'Default root channel',
782 fixture: 'video_short.webm',
802 await completeVideoCheck(server.url, video, checkAttributes)
807 after(async function () {
808 killallServers(servers)
810 // Keep the logs if the test failed