3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const series = require('async/series')
7 const webtorrent = new (require('webtorrent'))()
9 const loginUtils = require('../utils/login')
10 const miscsUtils = require('../utils/miscs')
11 const podsUtils = require('../utils/pods')
12 const serversUtils = require('../utils/servers')
13 const videosUtils = require('../utils/videos')
15 describe('Test multiple pods', function () {
19 before(function (done) {
25 serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
30 // Get the access tokens
32 each(servers, function (server, callbackEach) {
33 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
34 if (err) return callbackEach(err)
36 server.accessToken = accessToken
41 // The second pod make friend with the third
43 const server = servers[1]
44 podsUtils.makeFriends(server.url, server.accessToken, next)
46 // Wait for the request between pods
48 setTimeout(next, 10000)
50 // Pod 1 make friends too
52 const server = servers[0]
53 podsUtils.makeFriends(server.url, server.accessToken, next)
58 it('Should not have videos for all pods', function (done) {
59 each(servers, function (server, callback) {
60 videosUtils.getVideosList(server.url, function (err, res) {
63 const videos = res.body.data
64 expect(videos).to.be.an('array')
65 expect(videos.length).to.equal(0)
72 describe('Should upload the video and propagate on each pod', function () {
73 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
78 const name = 'my super name for pod 1'
79 const description = 'my super description for pod 1'
80 const tags = [ 'tag1p1', 'tag2p1' ]
81 const file = 'video_short1.webm'
82 videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next)
85 setTimeout(next, 11000)
87 // All pods should have this video
91 each(servers, function (server, callback) {
94 videosUtils.getVideosList(server.url, function (err, res) {
97 const videos = res.body.data
98 expect(videos).to.be.an('array')
99 expect(videos.length).to.equal(1)
100 const video = videos[0]
101 expect(video.name).to.equal('my super name for pod 1')
102 expect(video.description).to.equal('my super description for pod 1')
103 expect(video.podHost).to.equal('localhost:9001')
104 expect(video.magnetUri).to.exist
105 expect(video.duration).to.equal(10)
106 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
107 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
108 expect(video.author).to.equal('root')
110 if (server.url !== 'http://localhost:9001') {
111 expect(video.isLocal).to.be.false
113 expect(video.isLocal).to.be.true
116 // All pods should have the same magnet Uri
117 if (baseMagnet === null) {
118 baseMagnet = video.magnetUri
120 expect(video.magnetUri).to.equal.magnetUri
123 videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
125 expect(test).to.equal(true)
135 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
140 const name = 'my super name for pod 2'
141 const description = 'my super description for pod 2'
142 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ]
143 const file = 'video_short2.webm'
144 videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
147 setTimeout(next, 11000)
149 // All pods should have this video
153 each(servers, function (server, callback) {
154 let baseMagnet = null
156 videosUtils.getVideosList(server.url, function (err, res) {
159 const videos = res.body.data
160 expect(videos).to.be.an('array')
161 expect(videos.length).to.equal(2)
162 const video = videos[1]
163 expect(video.name).to.equal('my super name for pod 2')
164 expect(video.description).to.equal('my super description for pod 2')
165 expect(video.podHost).to.equal('localhost:9002')
166 expect(video.magnetUri).to.exist
167 expect(video.duration).to.equal(5)
168 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
169 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
170 expect(video.author).to.equal('root')
172 if (server.url !== 'http://localhost:9002') {
173 expect(video.isLocal).to.be.false
175 expect(video.isLocal).to.be.true
178 // All pods should have the same magnet Uri
179 if (baseMagnet === null) {
180 baseMagnet = video.magnetUri
182 expect(video.magnetUri).to.equal.magnetUri
185 videosUtils.testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
187 expect(test).to.equal(true)
197 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
202 const name = 'my super name for pod 3'
203 const description = 'my super description for pod 3'
204 const tags = [ 'tag1p3' ]
205 const file = 'video_short3.webm'
206 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
209 const name = 'my super name for pod 3-2'
210 const description = 'my super description for pod 3-2'
211 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ]
212 const file = 'video_short.webm'
213 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
216 setTimeout(next, 22000)
221 let baseMagnet = null
222 // All pods should have this video
223 each(servers, function (server, callback) {
224 videosUtils.getVideosList(server.url, function (err, res) {
227 const videos = res.body.data
228 expect(videos).to.be.an('array')
229 expect(videos.length).to.equal(4)
231 // We not sure about the order of the two last uploads
234 if (videos[2].name === 'my super name for pod 3') {
242 expect(video1.name).to.equal('my super name for pod 3')
243 expect(video1.description).to.equal('my super description for pod 3')
244 expect(video1.podHost).to.equal('localhost:9003')
245 expect(video1.magnetUri).to.exist
246 expect(video1.duration).to.equal(5)
247 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
248 expect(video1.author).to.equal('root')
249 expect(miscsUtils.dateIsValid(video1.createdDate)).to.be.true
251 expect(video2.name).to.equal('my super name for pod 3-2')
252 expect(video2.description).to.equal('my super description for pod 3-2')
253 expect(video2.podHost).to.equal('localhost:9003')
254 expect(video2.magnetUri).to.exist
255 expect(video2.duration).to.equal(5)
256 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
257 expect(video2.author).to.equal('root')
258 expect(miscsUtils.dateIsValid(video2.createdDate)).to.be.true
260 if (server.url !== 'http://localhost:9003') {
261 expect(video1.isLocal).to.be.false
262 expect(video2.isLocal).to.be.false
264 expect(video1.isLocal).to.be.true
265 expect(video2.isLocal).to.be.true
268 // All pods should have the same magnet Uri
269 if (baseMagnet === null) {
270 baseMagnet = video2.magnetUri
272 expect(video2.magnetUri).to.equal.magnetUri
275 videosUtils.testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
277 expect(test).to.equal(true)
279 videosUtils.testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
281 expect(test).to.equal(true)
293 describe('Should seed the uploaded video', function () {
294 it('Should add the file 1 by asking pod 3', function (done) {
295 // Yes, this could be long
298 videosUtils.getVideosList(servers[2].url, function (err, res) {
301 const video = res.body.data[0]
302 toRemove.push(res.body.data[2].id)
303 toRemove.push(res.body.data[3].id)
305 webtorrent.add(video.magnetUri, function (torrent) {
306 expect(torrent.files).to.exist
307 expect(torrent.files.length).to.equal(1)
308 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
315 it('Should add the file 2 by asking pod 1', function (done) {
316 // Yes, this could be long
319 videosUtils.getVideosList(servers[0].url, function (err, res) {
322 const video = res.body.data[1]
324 webtorrent.add(video.magnetUri, function (torrent) {
325 expect(torrent.files).to.exist
326 expect(torrent.files.length).to.equal(1)
327 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
334 it('Should add the file 3 by asking pod 2', function (done) {
335 // Yes, this could be long
338 videosUtils.getVideosList(servers[1].url, function (err, res) {
341 const video = res.body.data[2]
343 webtorrent.add(video.magnetUri, function (torrent) {
344 expect(torrent.files).to.exist
345 expect(torrent.files.length).to.equal(1)
346 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
353 it('Should add the file 3-2 by asking pod 1', function (done) {
354 // Yes, this could be long
357 videosUtils.getVideosList(servers[0].url, function (err, res) {
360 const video = res.body.data[3]
362 webtorrent.add(video.magnetUri, function (torrent) {
363 expect(torrent.files).to.exist
364 expect(torrent.files.length).to.equal(1)
365 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
372 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
377 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
380 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
384 setTimeout(done, 11000)
389 it('Should have videos 1 and 3 on each pod', function (done) {
390 each(servers, function (server, callback) {
391 videosUtils.getVideosList(server.url, function (err, res) {
394 const videos = res.body.data
395 expect(videos).to.be.an('array')
396 expect(videos.length).to.equal(2)
397 expect(videos[0].id).not.to.equal(videos[1].id)
398 expect(videos[0].id).not.to.equal(toRemove[0])
399 expect(videos[1].id).not.to.equal(toRemove[0])
400 expect(videos[0].id).not.to.equal(toRemove[1])
401 expect(videos[1].id).not.to.equal(toRemove[1])
409 after(function (done) {
410 servers.forEach(function (server) {
411 process.kill(-server.app.pid)
414 // Keep the logs if the test failed
416 serversUtils.flushTests(done)