aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/api/friends.js149
-rw-r--r--test/api/multiplePods.js326
-rw-r--r--test/api/singlePod.js135
-rw-r--r--test/fixtures/video_short.webmbin0 -> 218910 bytes
-rw-r--r--test/fixtures/video_short1.webmbin0 -> 572456 bytes
-rw-r--r--test/fixtures/video_short2.webmbin0 -> 942961 bytes
-rw-r--r--test/fixtures/video_short3.webmbin0 -> 292677 bytes
-rw-r--r--test/utils.js81
8 files changed, 691 insertions, 0 deletions
diff --git a/test/api/friends.js b/test/api/friends.js
new file mode 100644
index 000000000..033d3799a
--- /dev/null
+++ b/test/api/friends.js
@@ -0,0 +1,149 @@
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
11 function getFriendsList (url, end) {
12 var path = '/api/pods/'
13
14 request(url)
15 .get(path)
16 .set('Accept', 'application/json')
17 .expect(200)
18 .expect('Content-Type', /json/)
19 .end(end)
20 }
21
22 describe('Test friends', function () {
23 var apps = []
24 var urls = []
25
26 before(function (done) {
27 this.timeout(20000)
28 utils.runMultipleServers(3, function (apps_run, urls_run) {
29 apps = apps_run
30 urls = urls_run
31 done()
32 })
33 })
34
35 it('Should not have friends', function (done) {
36 async.each(urls, function (url, callback) {
37 getFriendsList(url, function (err, res) {
38 if (err) throw err
39
40 var result = res.body
41 expect(result).to.be.an('array')
42 expect(result.length).to.equal(0)
43 callback()
44 })
45 }, function (err) {
46 if (err) throw err
47
48 done()
49 })
50 })
51
52 it('Should make friends', function (done) {
53 this.timeout(10000)
54
55 function testMadeFriends (urls, url_to_test, callback) {
56 var friends = []
57 for (var i = 0; i < urls.length; i++) {
58 if (urls[i] === url_to_test) continue
59 friends.push(urls[i])
60 }
61
62 getFriendsList(url_to_test, function (err, res) {
63 if (err) throw err
64
65 var result = res.body
66 var result_urls = [ result[0].url, result[1].url ]
67 expect(result).to.be.an('array')
68 expect(result.length).to.equal(2)
69 expect(result_urls[0]).to.not.equal(result_urls[1])
70
71 var error_string = 'Friends url do not correspond for ' + url_to_test
72 expect(friends).to.contain(result_urls[0], error_string)
73 expect(friends).to.contain(result_urls[1], error_string)
74 callback()
75 })
76 }
77
78 var path = '/api/pods/makefriends'
79
80 // The second pod make friend with the third
81 request(urls[1])
82 .get(path)
83 .set('Accept', 'application/json')
84 .expect(204)
85 .end(function (err, res) {
86 if (err) throw err
87
88 // Wait for the request between pods
89 setTimeout(function () {
90 // The second pod should have the third as a friend
91 getFriendsList(urls[1], function (err, res) {
92 if (err) throw err
93
94 var result = res.body
95 expect(result).to.be.an('array')
96 expect(result.length).to.equal(1)
97 expect(result[0].url).to.be.equal(urls[2])
98
99 // Same here, the third pod should have the second pod as a friend
100 getFriendsList(urls[2], function (err, res) {
101 if (err) throw err
102
103 var result = res.body
104 expect(result).to.be.an('array')
105 expect(result.length).to.equal(1)
106 expect(result[0].url).to.be.equal(urls[1])
107
108 // Finally the first pod make friend with the second pod
109 request(urls[0])
110 .get(path)
111 .set('Accept', 'application/json')
112 .expect(204)
113 .end(function (err, res) {
114 if (err) throw err
115
116 setTimeout(function () {
117 // Now each pod should be friend with the other ones
118 async.each(urls, function (url, callback) {
119 testMadeFriends(urls, url, callback)
120 }, function (err) {
121 if (err) throw err
122 done()
123 })
124 }, 1000)
125 })
126 })
127 })
128 }, 1000)
129 })
130 })
131
132 // TODO
133 it('Should not be able to make friends again')
134
135 after(function (done) {
136 apps.forEach(function (app) {
137 process.kill(-app.pid)
138 })
139
140 if (this.ok) {
141 utils.flushTests(function () {
142 done()
143 })
144 } else {
145 done()
146 }
147 })
148 })
149})()
diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js
new file mode 100644
index 000000000..fff179006
--- /dev/null
+++ b/test/api/multiplePods.js
@@ -0,0 +1,326 @@
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(20000)
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(function () {
65 done()
66 })
67 })
68 }, 1000)
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(60000)
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(60000)
236
237 getVideosList(urls[0], function (err, res) {
238 if (err) throw err
239
240 var video = res.body[1]
241
242 video_id = video._id
243
244 webtorrent.add(video.magnetUri, function (torrent) {
245 expect(torrent.files).to.exist
246 expect(torrent.files.length).to.equal(1)
247 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
248
249 done()
250 })
251 })
252 })
253
254 it('Should add the file 3 by asking pod 2', function (done) {
255 // Yes, this could be long
256 this.timeout(60000)
257
258 getVideosList(urls[1], function (err, res) {
259 if (err) throw err
260
261 var video = res.body[2]
262
263 webtorrent.add(video.magnetUri, function (torrent) {
264 expect(torrent.files).to.exist
265 expect(torrent.files.length).to.equal(1)
266 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
267
268 done()
269 })
270 })
271 })
272
273 it('Should remove the file 2 by asking pod 2', function (done) {
274 request(urls[1])
275 .delete(path + '/' + video_id)
276 .set('Accept', 'application/json')
277 .expect(204)
278 .end(function (err, res) {
279 if (err) throw err
280
281 // Wait the propagation to the other pods
282 setTimeout(function () {
283 done()
284 }, 1000)
285 })
286 })
287
288 it('Should have videos 1 and 2 on each pod', function (done) {
289 async.each(urls, function (url, callback) {
290 getVideosList(url, function (err, res) {
291 if (err) throw err
292
293 var videos = res.body
294 expect(videos).to.be.an('array')
295 expect(videos.length).to.equal(2)
296 expect(videos[0]._id).not.to.equal(videos[1]._id)
297 expect(videos[0]._id).not.to.equal(video_id)
298 expect(videos[1]._id).not.to.equal(video_id)
299
300 callback()
301 })
302 }, function (err) {
303 if (err) throw err
304
305 done()
306 })
307 })
308 })
309
310 after(function (done) {
311 apps.forEach(function (app) {
312 process.kill(-app.pid)
313 })
314 process.kill(-webtorrent.app.pid)
315
316 // Keep the logs if the test failed
317 if (this.ok) {
318 utils.flushTests(function () {
319 done()
320 })
321 } else {
322 done()
323 }
324 })
325 })
326})()
diff --git a/test/api/singlePod.js b/test/api/singlePod.js
new file mode 100644
index 000000000..59a617970
--- /dev/null
+++ b/test/api/singlePod.js
@@ -0,0 +1,135 @@
1;(function () {
2 'use strict'
3
4 var request = require('supertest')
5 var chai = require('chai')
6 var expect = chai.expect
7 var webtorrent = require(__dirname + '/../../src/webTorrentNode')
8 webtorrent.silent = true
9
10 var utils = require('../utils')
11
12 describe('Test a single pod', function () {
13 var path = '/api/videos'
14 var app = null
15 var url = ''
16 var video_id = -1
17
18 before(function (done) {
19 this.timeout(10000)
20
21 utils.flushTests(function () {
22 utils.runServer(1, function (app1, url1) {
23 app = app1
24 url = url1
25
26 webtorrent.create(function () {
27 done()
28 })
29 })
30 })
31 })
32
33 it('Should not have videos', function (done) {
34 request(url)
35 .get(path)
36 .set('Accept', 'application/json')
37 .expect(200)
38 .expect('Content-Type', /json/)
39 .end(function (err, res) {
40 if (err) throw err
41
42 expect(res.body).to.be.an('array')
43 expect(res.body.length).to.equal(0)
44
45 done()
46 })
47 })
48
49 it('Should upload the video', function (done) {
50 this.timeout(5000)
51
52 request(url)
53 .post(path)
54 .set('Accept', 'application/json')
55 .field('name', 'my super name')
56 .field('description', 'my super description')
57 .attach('input_video', __dirname + '/../fixtures/video_short.webm')
58 .expect(201, done)
59 })
60
61 it('Should seed the uploaded video', function (done) {
62 // Yes, this could be long
63 this.timeout(60000)
64
65 request(url)
66 .get(path)
67 .set('Accept', 'application/json')
68 .expect(200)
69 .expect('Content-Type', /json/)
70 .end(function (err, res) {
71 if (err) throw err
72
73 expect(res.body).to.be.an('array')
74 expect(res.body.length).to.equal(1)
75
76 var video = res.body[0]
77 expect(video.name).to.equal('my super name')
78 expect(video.description).to.equal('my super description')
79 expect(video.podUrl).to.equal('http://localhost:9001')
80 expect(video.magnetUri).to.exist
81
82 video_id = video._id
83
84 webtorrent.add(video.magnetUri, function (torrent) {
85 expect(torrent.files).to.exist
86 expect(torrent.files.length).to.equal(1)
87 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
88
89 done()
90 })
91 })
92 })
93
94 it('Should remove the video', function (done) {
95 request(url)
96 .delete(path + '/' + video_id)
97 .set('Accept', 'application/json')
98 .expect(204)
99 .end(function (err, res) {
100 if (err) throw err
101 done()
102 })
103 })
104
105 it('Should not have videos', function (done) {
106 request(url)
107 .get(path)
108 .set('Accept', 'application/json')
109 .expect(200)
110 .expect('Content-Type', /json/)
111 .end(function (err, res) {
112 if (err) throw err
113
114 expect(res.body).to.be.an('array')
115 expect(res.body.length).to.equal(0)
116
117 done()
118 })
119 })
120
121 after(function (done) {
122 process.kill(-app.pid)
123 process.kill(-webtorrent.app.pid)
124
125 // Keep the logs if the test failed
126 if (this.ok) {
127 utils.flushTests(function () {
128 done()
129 })
130 } else {
131 done()
132 }
133 })
134 })
135})()
diff --git a/test/fixtures/video_short.webm b/test/fixtures/video_short.webm
new file mode 100644
index 000000000..bf4b0ab6c
--- /dev/null
+++ b/test/fixtures/video_short.webm
Binary files differ
diff --git a/test/fixtures/video_short1.webm b/test/fixtures/video_short1.webm
new file mode 100644
index 000000000..70ac0c644
--- /dev/null
+++ b/test/fixtures/video_short1.webm
Binary files differ
diff --git a/test/fixtures/video_short2.webm b/test/fixtures/video_short2.webm
new file mode 100644
index 000000000..13d72dff7
--- /dev/null
+++ b/test/fixtures/video_short2.webm
Binary files differ
diff --git a/test/fixtures/video_short3.webm b/test/fixtures/video_short3.webm
new file mode 100644
index 000000000..cde5dcd58
--- /dev/null
+++ b/test/fixtures/video_short3.webm
Binary files differ
diff --git a/test/utils.js b/test/utils.js
new file mode 100644
index 000000000..69f43d731
--- /dev/null
+++ b/test/utils.js
@@ -0,0 +1,81 @@
1;(function () {
2 'use strict'
3
4 var child_process = require('child_process')
5 var fork = child_process.fork
6 var exec = child_process.exec
7
8 function runMultipleServers (total_servers, serversRun) {
9 var apps = []
10 var urls = []
11 var i = 0
12
13 function anotherServerDone (number, app, url) {
14 apps[number - 1] = app
15 urls[number - 1] = url
16 i++
17 if (i === total_servers) {
18 serversRun(apps, urls)
19 }
20 }
21
22 flushTests(function () {
23 for (var j = 1; j <= total_servers; j++) {
24 (function (k) { // TODO: ES6 with let
25 // For the virtual buffer
26 setTimeout(function () {
27 runServer(k, function (app, url) {
28 anotherServerDone(k, app, url)
29 })
30 }, 1000 * k)
31 })(j)
32 }
33 })
34 }
35
36 function runServer (number, callback) {
37 var port = 9000 + number
38 var server_run_string = {
39 'Connected to mongodb': false,
40 'Server listening on port': false
41 }
42
43 // Share the environment
44 var env = Object.create(process.env)
45 env.NODE_ENV = 'test'
46 env.NODE_APP_INSTANCE = number
47 var options = {
48 silent: true,
49 env: env,
50 detached: true
51 }
52
53 var app = fork(__dirname + '/../server.js', [], options)
54 app.stdout.on('data', function onStdout (data) {
55 var dont_continue = false
56 // Check if all required sentences are here
57 for (var key of Object.keys(server_run_string)) {
58 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
59 if (server_run_string[key] === false) dont_continue = true
60 }
61
62 // If no, there is maybe one thing not already initialized (mongodb...)
63 if (dont_continue === true) return
64
65 app.stdout.removeListener('data', onStdout)
66 callback(app, 'http://localhost:' + port)
67 })
68 }
69
70 function flushTests (callback) {
71 exec(__dirname + '/../scripts/clean_test.sh', function () {
72 callback()
73 })
74 }
75
76 module.exports = {
77 runMultipleServers: runMultipleServers,
78 runServer: runServer,
79 flushTests: flushTests
80 }
81})()