4 var request = require('supertest')
5 var chai = require('chai')
6 var expect = chai.expect
7 var async = require('async')
9 var utils = require('../utils')
10 var webtorrent = require(__dirname + '/../../src/webTorrentNode')
11 webtorrent.silent = true
13 describe('Test multiple pods', function () {
14 var path = '/api/videos'
19 function getVideosList (url, end) {
22 .set('Accept', 'application/json')
24 .expect('Content-Type', /json/)
28 function uploadVideo (url, name, description, fixture, end) {
31 .set('Accept', 'application/json')
33 .field('description', description)
34 .attach('input_video', __dirname + '/../fixtures/' + fixture)
39 before(function (done) {
41 var path_friends = '/api/pods/makefriends'
43 utils.runMultipleServers(3, function (apps_run, urls_run) {
47 // The second pod make friend with the third
50 .set('Accept', 'application/json')
52 .end(function (err, res) {
55 // Wait for the request between pods
56 setTimeout(function () {
59 .set('Accept', 'application/json')
61 .end(function (err, res) {
64 webtorrent.create({ host: 'client', port: '1' }, function () {
73 it('Should not have videos for all pods', function (done) {
74 async.each(urls, function (url, callback) {
75 getVideosList(url, function (err, res) {
78 expect(res.body).to.be.an('array')
79 expect(res.body.length).to.equal(0)
90 describe('Should upload the video and propagate on each pod', function () {
91 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
94 uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) {
97 setTimeout(function () {
98 // All pods should have this video
99 async.each(urls, function (url, callback) {
100 var base_magnet = null
102 getVideosList(url, function (err, res) {
105 var videos = res.body
106 expect(videos).to.be.an('array')
107 expect(videos.length).to.equal(1)
108 var video = videos[0]
109 expect(video.name).to.equal('my super name for pod 1')
110 expect(video.description).to.equal('my super description for pod 1')
111 expect(video.podUrl).to.equal('http://localhost:9001')
112 expect(video.magnetUri).to.exist
114 // All pods should have the same magnet Uri
115 if (base_magnet === null) {
116 base_magnet = video.magnetUri
118 expect(video.magnetUri).to.equal.magnetUri
132 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
135 uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) {
138 setTimeout(function () {
139 // All pods should have this video
140 async.each(urls, function (url, callback) {
141 var base_magnet = null
143 getVideosList(url, function (err, res) {
146 var videos = res.body
147 expect(videos).to.be.an('array')
148 expect(videos.length).to.equal(2)
149 var video = videos[1]
150 expect(video.name).to.equal('my super name for pod 2')
151 expect(video.description).to.equal('my super description for pod 2')
152 expect(video.podUrl).to.equal('http://localhost:9002')
153 expect(video.magnetUri).to.exist
155 // All pods should have the same magnet Uri
156 if (base_magnet === null) {
157 base_magnet = video.magnetUri
159 expect(video.magnetUri).to.equal.magnetUri
173 it('Should upload the video on pod 3 and propagate on each pod', function (done) {
176 uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) {
179 setTimeout(function () {
180 var base_magnet = null
181 // All pods should have this video
182 async.each(urls, function (url, callback) {
183 getVideosList(url, function (err, res) {
186 var videos = res.body
187 expect(videos).to.be.an('array')
188 expect(videos.length).to.equal(3)
189 var video = videos[2]
190 expect(video.name).to.equal('my super name for pod 3')
191 expect(video.description).to.equal('my super description for pod 3')
192 expect(video.podUrl).to.equal('http://localhost:9003')
193 expect(video.magnetUri).to.exist
195 // All pods should have the same magnet Uri
196 if (base_magnet === null) {
197 base_magnet = video.magnetUri
199 expect(video.magnetUri).to.equal.magnetUri
214 describe('Should seed the uploaded video', function () {
215 it('Should add the file 1 by asking pod 3', function (done) {
216 // Yes, this could be long
219 getVideosList(urls[2], function (err, res) {
222 var video = res.body[0]
223 webtorrent.add(video.magnetUri, function (torrent) {
224 expect(torrent.files).to.exist
225 expect(torrent.files.length).to.equal(1)
226 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
233 it('Should add the file 2 by asking pod 1', function (done) {
234 // Yes, this could be long
237 getVideosList(urls[0], function (err, res) {
240 var video = res.body[1]
242 webtorrent.add(video.magnetUri, function (torrent) {
243 expect(torrent.files).to.exist
244 expect(torrent.files.length).to.equal(1)
245 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
252 it('Should add the file 3 by asking pod 2', function (done) {
253 // Yes, this could be long
256 getVideosList(urls[1], function (err, res) {
259 var video = res.body[2]
260 video_id = res.body[1]._id
262 webtorrent.add(video.magnetUri, function (torrent) {
263 expect(torrent.files).to.exist
264 expect(torrent.files.length).to.equal(1)
265 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
272 it('Should remove the file 2 by asking pod 2', function (done) {
274 .delete(path + '/' + video_id)
275 .set('Accept', 'application/json')
277 .end(function (err, res) {
280 // Wait the propagation to the other pods
281 setTimeout(function () {
287 it('Should have videos 1 and 3 on each pod', function (done) {
288 async.each(urls, function (url, callback) {
289 getVideosList(url, function (err, res) {
292 var videos = res.body
293 expect(videos).to.be.an('array')
294 expect(videos.length).to.equal(2)
295 expect(videos[0]._id).not.to.equal(videos[1]._id)
296 expect(videos[0]._id).not.to.equal(video_id)
297 expect(videos[1]._id).not.to.equal(video_id)
309 after(function (done) {
310 apps.forEach(function (app) {
311 process.kill(-app.pid)
313 process.kill(-webtorrent.app.pid)
315 // Keep the logs if the test failed
317 utils.flushTests(function () {