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',
141 thumbnailfile: 'thumbnail.jpg',
142 previewfile: 'preview.jpg'
144 await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
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',
158 description: 'my super description for server 2',
159 host: 'localhost:9002',
162 commentsEnabled: true,
164 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
165 privacy: VideoPrivacy.PUBLIC,
167 name: 'Default user1 channel',
168 description: 'super channel',
171 fixture: 'video_short2.webm',
190 thumbnailfile: 'thumbnail',
191 previewfile: 'preview'
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]
200 await completeVideoCheck(server.url, video, checkAttributes)
204 it('Should upload two videos on server 3 and propagate on each server', async function () {
207 const videoAttributes1 = {
208 name: 'my super name for server 3',
213 description: 'my super description for server 3',
215 fixture: 'video_short3.webm'
217 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
219 const videoAttributes2 = {
220 name: 'my super name for server 3-2',
225 description: 'my super description for server 3-2',
226 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
227 fixture: 'video_short.webm'
229 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
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)
238 const videos = res.body.data
239 expect(videos).to.be.an('array')
240 expect(videos.length).to.equal(4)
242 // We not sure about the order of the two last uploads
245 if (videos[2].name === 'my super name for server 3') {
253 const checkAttributesVideo1 = {
254 name: 'my super name for server 3',
259 description: 'my super description for server 3',
260 host: 'localhost:9003',
264 commentsEnabled: true,
266 privacy: VideoPrivacy.PUBLIC,
268 name: 'Default root channel',
272 fixture: 'video_short3.webm',
280 await completeVideoCheck(server.url, video1, checkAttributesVideo1)
282 const checkAttributesVideo2 = {
283 name: 'my super name for server 3-2',
288 description: 'my super description for server 3-2',
289 host: 'localhost:9003',
291 commentsEnabled: true,
294 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
295 privacy: VideoPrivacy.PUBLIC,
297 name: 'Default root channel',
301 fixture: 'video_short.webm',
309 await completeVideoCheck(server.url, video2, checkAttributesVideo2)
314 describe('Should seed the uploaded video', function () {
315 it('Should add the file 1 by asking server 3', async function () {
318 const res = await getVideosList(servers[2].url)
320 const video = res.body.data[0]
321 toRemove.push(res.body.data[2])
322 toRemove.push(res.body.data[3])
324 const res2 = await getVideo(servers[2].url, video.id)
325 const videoDetails = res2.body
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('')
333 it('Should add the file 2 by asking server 1', async function () {
336 const res = await getVideosList(servers[0].url)
338 const video = res.body.data[1]
339 const res2 = await getVideo(servers[0].url, video.id)
340 const videoDetails = res2.body
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('')
348 it('Should add the file 3 by asking server 2', async function () {
351 const res = await getVideosList(servers[1].url)
353 const video = res.body.data[2]
354 const res2 = await getVideo(servers[1].url, video.id)
355 const videoDetails = res2.body
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('')
363 it('Should add the file 3-2 by asking server 1', async function () {
366 const res = await getVideosList(servers[0].url)
368 const video = res.body.data[3]
369 const res2 = await getVideo(servers[0].url, video.id)
370 const videoDetails = res2.body
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('')
378 it('Should add the file 2 in 360p by asking server 1', async function () {
381 const res = await getVideosList(servers[0].url)
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
387 const file = videoDetails.files.find(f => f.resolution === 360)
388 expect(file).not.to.be.undefined
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('')
397 describe('Should update video views, likes and dislikes', function () {
398 let localVideosServer3 = []
399 let remoteVideosServer1 = []
400 let remoteVideosServer2 = []
401 let remoteVideosServer3 = []
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)
407 const res2 = await getVideosList(servers[1].url)
408 remoteVideosServer2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.uuid)
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)
415 it('Should view multiple videos on owned servers', async function () {
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]))
424 await Promise.all(tasks)
428 for (const server of servers) {
429 const res = await getVideosList(server.url)
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])
435 expect(video0.views).to.equal(3)
436 expect(video1.views).to.equal(1)
440 it('Should view multiple videos on each servers', async function () {
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]))
455 await Promise.all(tasks)
459 let baseVideos = null
461 for (const server of servers) {
462 const res = await getVideosList(server.url)
464 const videos = res.body.data
466 // Initialize base videos for future comparisons
467 if (baseVideos === null) {
472 for (const baseVideo of baseVideos) {
473 const sameVideo = videos.find(video => video.name === baseVideo.name)
474 expect(baseVideo.views).to.equal(sameVideo.views)
479 it('Should like and dislikes videos on different services', async function () {
482 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
484 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'dislike')
486 await rateVideo(servers[0].url, servers[0].accessToken, remoteVideosServer1[0], 'like')
487 await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'like')
489 await rateVideo(servers[2].url, servers[2].accessToken, localVideosServer3[1], 'dislike')
490 await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[1], 'dislike')
492 await rateVideo(servers[2].url, servers[2].accessToken, remoteVideosServer3[0], 'like')
496 let baseVideos = null
497 for (const server of servers) {
498 const res = await getVideosList(server.url)
500 const videos = res.body.data
502 // Initialize base videos for future comparisons
503 if (baseVideos === null) {
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)
517 describe('Should manipulate these videos', function () {
518 it('Should update the video 3 by asking server 3', async function () {
522 name: 'my super video updated',
527 description: 'my super description updated',
528 tags: [ 'tag_up_1', 'tag_up_2' ],
529 thumbnailfile: 'thumbnail.jpg',
530 previewfile: 'preview.jpg'
533 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
538 it('Should have the video 3 updated on each server', async function () {
541 for (const server of servers) {
542 const res = await getVideosList(server.url)
544 const videos = res.body.data
545 const videoUpdated = videos.find(video => video.name === 'my super video updated')
546 expect(!!videoUpdated).to.be.true
548 const isLocal = server.url === 'http://localhost:9003'
549 const checkAttributes = {
550 name: 'my super video updated',
555 description: 'my super description updated',
556 host: 'localhost:9003',
560 commentsEnabled: true,
561 tags: [ 'tag_up_1', 'tag_up_2' ],
562 privacy: VideoPrivacy.PUBLIC,
564 name: 'Default root channel',
568 fixture: 'video_short3.webm',
575 thumbnailfile: 'thumbnail',
576 previewfile: 'preview'
578 await completeVideoCheck(server.url, videoUpdated, checkAttributes)
582 it('Should remove the videos 3 and 3-2 by asking server 3', async function () {
585 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
586 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
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)
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)
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)
611 videoUUID = videos.find(video => video.name === 'my super name for server 1').uuid
615 it('Should get the same video by UUID on each server', async function () {
617 for (const server of servers) {
618 const res = await getVideo(server.url, videoUUID)
620 const video = res.body
622 if (baseVideo === null) {
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)
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
644 await testImage(server.url, 'video_short1-preview.webm', video.previewPath)
649 describe('Should comment these videos', function () {
650 it('Should add comment (threads and replies)', async function () {
654 const text = 'my super first comment'
655 await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
659 const text = 'my super second comment'
660 await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
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
669 const text = 'my super answer to thread 1'
670 await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
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
679 const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
680 const childCommentId = res2.body.children[0].comment.id
682 const text3 = 'my second answer to thread 1'
683 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
685 const text2 = 'my super answer to answer of thread 1'
686 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
692 it('Should have these threads', async function () {
693 for (const server of servers) {
694 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
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)
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
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
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
729 const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
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)
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)
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)
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)
757 it('Should delete the thread comments', async function () {
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)
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)
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)
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
788 it('Should disable comments', async function () {
792 commentsEnabled: false
795 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, attributes)
799 for (const server of servers) {
800 const res = await getVideo(server.url, videoUUID)
801 expect(res.body.commentsEnabled).to.be.false
803 const text = 'my super forbidden comment'
804 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text, 409)
809 describe('With minimum parameters', function () {
810 it('Should upload and propagate the video', async function () {
813 const path = '/api/v1/videos/upload'
815 const req = request(servers[1].url)
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')
825 const filePath = join(__dirname, '..', '..', 'api', 'fixtures', 'video_short.webm')
827 await req.attach('videofile', filePath)
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')
836 const isLocal = server.url === 'http://localhost:9002'
837 const checkAttributes = {
838 name: 'minimum parameters',
844 host: 'localhost:9002',
848 commentsEnabled: true,
850 privacy: VideoPrivacy.PUBLIC,
852 name: 'Default root channel',
856 fixture: 'video_short.webm',
876 await completeVideoCheck(server.url, video, checkAttributes)
881 after(async function () {
882 killallServers(servers)
884 // Keep the logs if the test failed