Fix typo
[oweals/peertube.git] / test / api / multiplePods.js
1 ;(function () {
2   'use strict'
3
4   var request = require('supertest')
5   var chai = require('chai')
6   var expect = chai.expect
7   var async = require('async')
8
9   var utils = require('../utils')
10   var webtorrent = require(__dirname + '/../../src/webTorrentNode')
11   webtorrent.silent = true
12
13   describe('Test multiple pods', function () {
14     var path = '/api/videos'
15     var apps = []
16     var urls = []
17     var video_id = -1
18
19     function getVideosList (url, end) {
20       request(url)
21         .get(path)
22         .set('Accept', 'application/json')
23         .expect(200)
24         .expect('Content-Type', /json/)
25         .end(end)
26     }
27
28     function uploadVideo (url, name, description, fixture, end) {
29       request(url)
30         .post(path)
31         .set('Accept', 'application/json')
32         .field('name', name)
33         .field('description', description)
34         .attach('input_video', __dirname + '/../fixtures/' + fixture)
35         .expect(201)
36         .end(end)
37     }
38
39     before(function (done) {
40       this.timeout(30000)
41       var path_friends = '/api/pods/makefriends'
42
43       utils.runMultipleServers(3, function (apps_run, urls_run) {
44         apps = apps_run
45         urls = urls_run
46
47         // The second pod make friend with the third
48         request(urls[1])
49           .get(path_friends)
50           .set('Accept', 'application/json')
51           .expect(204)
52           .end(function (err, res) {
53             if (err) throw err
54
55             // Wait for the request between pods
56             setTimeout(function () {
57               request(urls[0])
58                 .get(path_friends)
59                 .set('Accept', 'application/json')
60                 .expect(204)
61                 .end(function (err, res) {
62                   if (err) throw err
63
64                   webtorrent.create({ host: 'client', port: '1' }, function () {
65                     done()
66                   })
67                 })
68             }, 10000)
69           })
70       })
71     })
72
73     it('Should not have videos for all pods', function (done) {
74       async.each(urls, function (url, callback) {
75         getVideosList(url, function (err, res) {
76           if (err) throw err
77
78           expect(res.body).to.be.an('array')
79           expect(res.body.length).to.equal(0)
80
81           callback()
82         })
83       }, function (err) {
84         if (err) throw err
85
86         done()
87       })
88     })
89
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) {
92         this.timeout(5000)
93
94         uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) {
95           if (err) throw err
96
97           setTimeout(function () {
98             // All pods should have this video
99             async.each(urls, function (url, callback) {
100               var base_magnet = null
101
102               getVideosList(url, function (err, res) {
103                 if (err) throw err
104
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
113
114                 // All pods should have the same magnet Uri
115                 if (base_magnet === null) {
116                   base_magnet = video.magnetUri
117                 } else {
118                   expect(video.magnetUri).to.equal.magnetUri
119                 }
120
121                 callback()
122               })
123             }, function (err) {
124               if (err) throw err
125
126               done()
127             })
128           }, 1000)
129         })
130       })
131
132       it('Should upload the video on pod 2 and propagate on each pod', function (done) {
133         this.timeout(5000)
134
135         uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) {
136           if (err) throw err
137
138           setTimeout(function () {
139             // All pods should have this video
140             async.each(urls, function (url, callback) {
141               var base_magnet = null
142
143               getVideosList(url, function (err, res) {
144                 if (err) throw err
145
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
154
155                 // All pods should have the same magnet Uri
156                 if (base_magnet === null) {
157                   base_magnet = video.magnetUri
158                 } else {
159                   expect(video.magnetUri).to.equal.magnetUri
160                 }
161
162                 callback()
163               })
164             }, function (err) {
165               if (err) throw err
166
167               done()
168             })
169           }, 1000)
170         })
171       })
172
173       it('Should upload the video on pod 3 and propagate on each pod', function (done) {
174         this.timeout(5000)
175
176         uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) {
177           if (err) throw err
178
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) {
184                 if (err) throw err
185
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
194
195                 // All pods should have the same magnet Uri
196                 if (base_magnet === null) {
197                   base_magnet = video.magnetUri
198                 } else {
199                   expect(video.magnetUri).to.equal.magnetUri
200                 }
201
202                 callback()
203               })
204             }, function (err) {
205               if (err) throw err
206
207               done()
208             })
209           }, 1000)
210         })
211       })
212     })
213
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
217         this.timeout(200000)
218
219         getVideosList(urls[2], function (err, res) {
220           if (err) throw err
221
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('')
227
228             done()
229           })
230         })
231       })
232
233       it('Should add the file 2 by asking pod 1', function (done) {
234         // Yes, this could be long
235         this.timeout(200000)
236
237         getVideosList(urls[0], function (err, res) {
238           if (err) throw err
239
240           var video = res.body[1]
241
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('')
246
247             done()
248           })
249         })
250       })
251
252       it('Should add the file 3 by asking pod 2', function (done) {
253         // Yes, this could be long
254         this.timeout(200000)
255
256         getVideosList(urls[1], function (err, res) {
257           if (err) throw err
258
259           var video = res.body[2]
260           video_id = res.body[1]._id
261
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('')
266
267             done()
268           })
269         })
270       })
271
272       it('Should remove the file 2 by asking pod 2', function (done) {
273         request(urls[1])
274           .delete(path + '/' + video_id)
275           .set('Accept', 'application/json')
276           .expect(204)
277           .end(function (err, res) {
278             if (err) throw err
279
280             // Wait the propagation to the other pods
281             setTimeout(function () {
282               done()
283             }, 1000)
284           })
285       })
286
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) {
290             if (err) throw err
291
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)
298
299             callback()
300           })
301         }, function (err) {
302           if (err) throw err
303
304           done()
305         })
306       })
307     })
308
309     after(function (done) {
310       apps.forEach(function (app) {
311         process.kill(-app.pid)
312       })
313       process.kill(-webtorrent.app.pid)
314
315       // Keep the logs if the test failed
316       if (this.ok) {
317         utils.flushTests(function () {
318           done()
319         })
320       } else {
321         done()
322       }
323     })
324   })
325 })()