aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/api
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2015-12-06 17:09:07 +0100
committerChocobozzz <florian.bigard@gmail.com>2015-12-07 17:22:36 +0100
commit876d1bcfd07b14d47ba377474e3bb680872d1f7a (patch)
tree4400850a7e4e6b7140ef9cea289784eb21f972b3 /test/api
parent4d5f813870683c29af6b9959efa9fd4c4ea5e3b7 (diff)
downloadPeerTube-876d1bcfd07b14d47ba377474e3bb680872d1f7a.tar.gz
PeerTube-876d1bcfd07b14d47ba377474e3bb680872d1f7a.tar.zst
PeerTube-876d1bcfd07b14d47ba377474e3bb680872d1f7a.zip
Tests refractoring
Diffstat (limited to 'test/api')
-rw-r--r--test/api/checkParams.js18
-rw-r--r--test/api/fixtures/video_short.mp4bin0 -> 38783 bytes
-rw-r--r--test/api/fixtures/video_short.ogvbin0 -> 140849 bytes
-rw-r--r--test/api/fixtures/video_short.webmbin0 -> 218910 bytes
-rw-r--r--test/api/fixtures/video_short1.webmbin0 -> 572456 bytes
-rw-r--r--test/api/fixtures/video_short2.webmbin0 -> 942961 bytes
-rw-r--r--test/api/fixtures/video_short3.webmbin0 -> 292677 bytes
-rw-r--r--test/api/fixtures/video_short_fake.webm1
-rw-r--r--test/api/friendsAdvanced.js47
-rw-r--r--test/api/friendsBasic.js25
-rw-r--r--test/api/multiplePods.js97
-rw-r--r--test/api/singlePod.js147
-rw-r--r--test/api/utils.js162
13 files changed, 271 insertions, 226 deletions
diff --git a/test/api/checkParams.js b/test/api/checkParams.js
index a06e32bbc..0fa83655f 100644
--- a/test/api/checkParams.js
+++ b/test/api/checkParams.js
@@ -1,11 +1,11 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var request = require('supertest')
5 var chai = require('chai') 4 var chai = require('chai')
6 var expect = chai.expect 5 var expect = chai.expect
6 var request = require('supertest')
7 7
8 var utils = require('../utils') 8 var utils = require('./utils')
9 9
10 describe('Test parameters validator', function () { 10 describe('Test parameters validator', function () {
11 var app = null 11 var app = null
@@ -129,7 +129,7 @@
129 description: 'my super description' 129 description: 'my super description'
130 } 130 }
131 var attach = { 131 var attach = {
132 'input_video': __dirname + '/../fixtures/video_short.webm' 132 'input_video': __dirname + '/fixtures/video_short.webm'
133 } 133 }
134 makePostRequest(path, data, attach, done) 134 makePostRequest(path, data, attach, done)
135 }) 135 })
@@ -140,7 +140,7 @@
140 description: 'my super description' 140 description: 'my super description'
141 } 141 }
142 var attach = { 142 var attach = {
143 'input_video': __dirname + '/../fixtures/video_short.webm' 143 'input_video': __dirname + '/fixtures/video_short.webm'
144 } 144 }
145 makePostRequest(path, data, attach, done) 145 makePostRequest(path, data, attach, done)
146 }) 146 })
@@ -150,7 +150,7 @@
150 name: 'my super name' 150 name: 'my super name'
151 } 151 }
152 var attach = { 152 var attach = {
153 'input_video': __dirname + '/../fixtures/video_short.webm' 153 'input_video': __dirname + '/fixtures/video_short.webm'
154 } 154 }
155 makePostRequest(path, data, attach, done) 155 makePostRequest(path, data, attach, done)
156 }) 156 })
@@ -163,7 +163,7 @@
163 'very very very very very very very very very very very very very very very long' 163 'very very very very very very very very very very very very very very very long'
164 } 164 }
165 var attach = { 165 var attach = {
166 'input_video': __dirname + '/../fixtures/video_short.webm' 166 'input_video': __dirname + '/fixtures/video_short.webm'
167 } 167 }
168 makePostRequest(path, data, attach, done) 168 makePostRequest(path, data, attach, done)
169 }) 169 })
@@ -194,12 +194,12 @@
194 description: 'my super description' 194 description: 'my super description'
195 } 195 }
196 var attach = { 196 var attach = {
197 'input_video': __dirname + '/../fixtures/video_short.webm' 197 'input_video': __dirname + '/fixtures/video_short.webm'
198 } 198 }
199 makePostRequest(path, data, attach, function () { 199 makePostRequest(path, data, attach, function () {
200 attach.input_video = __dirname + '/../fixtures/video_short.mp4' 200 attach.input_video = __dirname + '/fixtures/video_short.mp4'
201 makePostRequest(path, data, attach, function () { 201 makePostRequest(path, data, attach, function () {
202 attach.input_video = __dirname + '/../fixtures/video_short.ogv' 202 attach.input_video = __dirname + '/fixtures/video_short.ogv'
203 makePostRequest(path, data, attach, done, true) 203 makePostRequest(path, data, attach, done, true)
204 }, true) 204 }, true)
205 }, true) 205 }, true)
diff --git a/test/api/fixtures/video_short.mp4 b/test/api/fixtures/video_short.mp4
new file mode 100644
index 000000000..35678362b
--- /dev/null
+++ b/test/api/fixtures/video_short.mp4
Binary files differ
diff --git a/test/api/fixtures/video_short.ogv b/test/api/fixtures/video_short.ogv
new file mode 100644
index 000000000..9e253da82
--- /dev/null
+++ b/test/api/fixtures/video_short.ogv
Binary files differ
diff --git a/test/api/fixtures/video_short.webm b/test/api/fixtures/video_short.webm
new file mode 100644
index 000000000..bf4b0ab6c
--- /dev/null
+++ b/test/api/fixtures/video_short.webm
Binary files differ
diff --git a/test/api/fixtures/video_short1.webm b/test/api/fixtures/video_short1.webm
new file mode 100644
index 000000000..70ac0c644
--- /dev/null
+++ b/test/api/fixtures/video_short1.webm
Binary files differ
diff --git a/test/api/fixtures/video_short2.webm b/test/api/fixtures/video_short2.webm
new file mode 100644
index 000000000..13d72dff7
--- /dev/null
+++ b/test/api/fixtures/video_short2.webm
Binary files differ
diff --git a/test/api/fixtures/video_short3.webm b/test/api/fixtures/video_short3.webm
new file mode 100644
index 000000000..cde5dcd58
--- /dev/null
+++ b/test/api/fixtures/video_short3.webm
Binary files differ
diff --git a/test/api/fixtures/video_short_fake.webm b/test/api/fixtures/video_short_fake.webm
new file mode 100644
index 000000000..d85290ae5
--- /dev/null
+++ b/test/api/fixtures/video_short_fake.webm
@@ -0,0 +1 @@
this is a fake video mouahahah
diff --git a/test/api/friendsAdvanced.js b/test/api/friendsAdvanced.js
index 680d90aee..7a0940647 100644
--- a/test/api/friendsAdvanced.js
+++ b/test/api/friendsAdvanced.js
@@ -1,60 +1,29 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var request = require('supertest')
5 var chai = require('chai') 4 var chai = require('chai')
6 var expect = chai.expect 5 var expect = chai.expect
7 6
8 var utils = require('../utils') 7 var utils = require('./utils')
9 8
10 describe('Test advanced friends', function () { 9 describe('Test advanced friends', function () {
11 var path = '/api/v1/pods/makefriends'
12 var apps = [] 10 var apps = []
13 var urls = [] 11 var urls = []
14 12
15 function makeFriend (pod_number, callback) { 13 function makeFriend (pod_number, callback) {
16 // The first pod make friend with the third 14 return utils.makeFriend(urls[pod_number - 1], callback)
17 request(urls[pod_number - 1])
18 .get(path)
19 .set('Accept', 'application/json')
20 .expect(204)
21 .end(function (err, res) {
22 if (err) throw err
23
24 // Wait for the request between pods
25 setTimeout(function () {
26 callback()
27 }, 1000)
28 })
29 } 15 }
30 16
31 function getFriendsList (pod_number, end) { 17 function getFriendsList (pod_number, end) {
32 var path = '/api/v1/pods/' 18 return utils.getFriendsList(urls[pod_number - 1], end)
33
34 request(urls[pod_number - 1])
35 .get(path)
36 .set('Accept', 'application/json')
37 .expect(200)
38 .expect('Content-Type', /json/)
39 .end(end)
40 } 19 }
41 20
42 function uploadVideo (pod_number, callback) { 21 function uploadVideo (pod_number, callback) {
43 var path = '/api/v1/videos' 22 var name = 'my super video'
44 23 var description = 'my super description'
45 request(urls[pod_number - 1]) 24 var fixture = 'video_short.webm'
46 .post(path) 25
47 .set('Accept', 'application/json') 26 return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback)
48 .field('name', 'my super video')
49 .field('description', 'my super description')
50 .attach('input_video', __dirname + '/../fixtures/video_short.webm')
51 .expect(201)
52 .end(function (err) {
53 if (err) throw err
54
55 // Wait for the retry requests
56 setTimeout(callback, 10000)
57 })
58 } 27 }
59 28
60 beforeEach(function (done) { 29 beforeEach(function (done) {
diff --git a/test/api/friendsBasic.js b/test/api/friendsBasic.js
index 40ed34199..43ec41633 100644
--- a/test/api/friendsBasic.js
+++ b/test/api/friendsBasic.js
@@ -1,23 +1,12 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var request = require('supertest') 4 var async = require('async')
5 var chai = require('chai') 5 var chai = require('chai')
6 var expect = chai.expect 6 var expect = chai.expect
7 var async = require('async') 7 var request = require('supertest')
8
9 var utils = require('../utils')
10
11 function getFriendsList (url, end) {
12 var path = '/api/v1/pods/'
13 8
14 request(url) 9 var utils = require('./utils')
15 .get(path)
16 .set('Accept', 'application/json')
17 .expect(200)
18 .expect('Content-Type', /json/)
19 .end(end)
20 }
21 10
22 describe('Test basic friends', function () { 11 describe('Test basic friends', function () {
23 var apps = [] 12 var apps = []
@@ -34,7 +23,7 @@
34 23
35 it('Should not have friends', function (done) { 24 it('Should not have friends', function (done) {
36 async.each(urls, function (url, callback) { 25 async.each(urls, function (url, callback) {
37 getFriendsList(url, function (err, res) { 26 utils.getFriendsList(url, function (err, res) {
38 if (err) throw err 27 if (err) throw err
39 28
40 var result = res.body 29 var result = res.body
@@ -59,7 +48,7 @@
59 friends.push(urls[i]) 48 friends.push(urls[i])
60 } 49 }
61 50
62 getFriendsList(url_to_test, function (err, res) { 51 utils.getFriendsList(url_to_test, function (err, res) {
63 if (err) throw err 52 if (err) throw err
64 53
65 var result = res.body 54 var result = res.body
@@ -88,7 +77,7 @@
88 // Wait for the request between pods 77 // Wait for the request between pods
89 setTimeout(function () { 78 setTimeout(function () {
90 // The second pod should have the third as a friend 79 // The second pod should have the third as a friend
91 getFriendsList(urls[1], function (err, res) { 80 utils.getFriendsList(urls[1], function (err, res) {
92 if (err) throw err 81 if (err) throw err
93 82
94 var result = res.body 83 var result = res.body
@@ -97,7 +86,7 @@
97 expect(result[0].url).to.be.equal(urls[2]) 86 expect(result[0].url).to.be.equal(urls[2])
98 87
99 // Same here, the third pod should have the second pod as a friend 88 // Same here, the third pod should have the second pod as a friend
100 getFriendsList(urls[2], function (err, res) { 89 utils.getFriendsList(urls[2], function (err, res) {
101 if (err) throw err 90 if (err) throw err
102 91
103 var result = res.body 92 var result = res.body
diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js
index 3ce57afa8..dd666d73e 100644
--- a/test/api/multiplePods.js
+++ b/test/api/multiplePods.js
@@ -1,86 +1,47 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var request = require('supertest') 4 var async = require('async')
5 var chai = require('chai') 5 var chai = require('chai')
6 var expect = chai.expect 6 var expect = chai.expect
7 var async = require('async')
8 7
9 var utils = require('../utils') 8 var utils = require('./utils')
10 var webtorrent = require(__dirname + '/../../src/webTorrentNode') 9 var webtorrent = require(__dirname + '/../../src/webTorrentNode')
11 webtorrent.silent = true 10 webtorrent.silent = true
12 11
13 describe('Test multiple pods', function () { 12 describe('Test multiple pods', function () {
14 var path = '/api/v1/videos'
15 var apps = [] 13 var apps = []
16 var urls = [] 14 var urls = []
17 var to_remove = [] 15 var to_remove = []
18 16
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 function removeVideo (url, id, end) {
40 request(url)
41 .delete(path + '/' + id)
42 .set('Accept', 'application/json')
43 .expect(204)
44 .end(end)
45 }
46
47 before(function (done) { 17 before(function (done) {
48 this.timeout(30000) 18 this.timeout(30000)
49 var path_friends = '/api/v1/pods/makefriends'
50 19
51 utils.runMultipleServers(3, function (apps_run, urls_run) { 20 utils.runMultipleServers(3, function (apps_run, urls_run) {
52 apps = apps_run 21 apps = apps_run
53 urls = urls_run 22 urls = urls_run
54 23
55 // The second pod make friend with the third 24 // The second pod make friend with the third
56 request(urls[1]) 25 utils.makeFriend(urls[1], function (err, res) {
57 .get(path_friends) 26 if (err) throw err
58 .set('Accept', 'application/json')
59 .expect(204)
60 .end(function (err, res) {
61 if (err) throw err
62 27
63 // Wait for the request between pods 28 // Wait for the request between pods
64 setTimeout(function () { 29 setTimeout(function () {
65 request(urls[0]) 30 utils.makeFriend(urls[0], function (err, res) {
66 .get(path_friends) 31 if (err) throw err
67 .set('Accept', 'application/json')
68 .expect(204)
69 .end(function (err, res) {
70 if (err) throw err
71 32
72 webtorrent.create({ host: 'client', port: '1' }, function () { 33 webtorrent.create({ host: 'client', port: '1' }, function () {
73 done() 34 done()
74 }) 35 })
75 }) 36 })
76 }, 10000) 37 }, 10000)
77 }) 38 })
78 }) 39 })
79 }) 40 })
80 41
81 it('Should not have videos for all pods', function (done) { 42 it('Should not have videos for all pods', function (done) {
82 async.each(urls, function (url, callback) { 43 async.each(urls, function (url, callback) {
83 getVideosList(url, function (err, res) { 44 utils.getVideosList(url, function (err, res) {
84 if (err) throw err 45 if (err) throw err
85 46
86 expect(res.body).to.be.an('array') 47 expect(res.body).to.be.an('array')
@@ -99,7 +60,7 @@
99 it('Should upload the video on pod 1 and propagate on each pod', function (done) { 60 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
100 this.timeout(15000) 61 this.timeout(15000)
101 62
102 uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { 63 utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) {
103 if (err) throw err 64 if (err) throw err
104 65
105 setTimeout(function () { 66 setTimeout(function () {
@@ -107,7 +68,7 @@
107 async.each(urls, function (url, callback) { 68 async.each(urls, function (url, callback) {
108 var base_magnet = null 69 var base_magnet = null
109 70
110 getVideosList(url, function (err, res) { 71 utils.getVideosList(url, function (err, res) {
111 if (err) throw err 72 if (err) throw err
112 73
113 var videos = res.body 74 var videos = res.body
@@ -140,7 +101,7 @@
140 it('Should upload the video on pod 2 and propagate on each pod', function (done) { 101 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
141 this.timeout(15000) 102 this.timeout(15000)
142 103
143 uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { 104 utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) {
144 if (err) throw err 105 if (err) throw err
145 106
146 setTimeout(function () { 107 setTimeout(function () {
@@ -148,7 +109,7 @@
148 async.each(urls, function (url, callback) { 109 async.each(urls, function (url, callback) {
149 var base_magnet = null 110 var base_magnet = null
150 111
151 getVideosList(url, function (err, res) { 112 utils.getVideosList(url, function (err, res) {
152 if (err) throw err 113 if (err) throw err
153 114
154 var videos = res.body 115 var videos = res.body
@@ -181,16 +142,16 @@
181 it('Should upload two videos on pod 3 and propagate on each pod', function (done) { 142 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
182 this.timeout(15000) 143 this.timeout(15000)
183 144
184 uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { 145 utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) {
185 if (err) throw err 146 if (err) throw err
186 uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', function (err) { 147 utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', function (err) {
187 if (err) throw err 148 if (err) throw err
188 149
189 setTimeout(function () { 150 setTimeout(function () {
190 var base_magnet = null 151 var base_magnet = null
191 // All pods should have this video 152 // All pods should have this video
192 async.each(urls, function (url, callback) { 153 async.each(urls, function (url, callback) {
193 getVideosList(url, function (err, res) { 154 utils.getVideosList(url, function (err, res) {
194 if (err) throw err 155 if (err) throw err
195 156
196 var videos = res.body 157 var videos = res.body
@@ -233,7 +194,7 @@
233 // Yes, this could be long 194 // Yes, this could be long
234 this.timeout(200000) 195 this.timeout(200000)
235 196
236 getVideosList(urls[2], function (err, res) { 197 utils.getVideosList(urls[2], function (err, res) {
237 if (err) throw err 198 if (err) throw err
238 199
239 var video = res.body[0] 200 var video = res.body[0]
@@ -254,7 +215,7 @@
254 // Yes, this could be long 215 // Yes, this could be long
255 this.timeout(200000) 216 this.timeout(200000)
256 217
257 getVideosList(urls[0], function (err, res) { 218 utils.getVideosList(urls[0], function (err, res) {
258 if (err) throw err 219 if (err) throw err
259 220
260 var video = res.body[1] 221 var video = res.body[1]
@@ -273,7 +234,7 @@
273 // Yes, this could be long 234 // Yes, this could be long
274 this.timeout(200000) 235 this.timeout(200000)
275 236
276 getVideosList(urls[1], function (err, res) { 237 utils.getVideosList(urls[1], function (err, res) {
277 if (err) throw err 238 if (err) throw err
278 239
279 var video = res.body[2] 240 var video = res.body[2]
@@ -292,7 +253,7 @@
292 // Yes, this could be long 253 // Yes, this could be long
293 this.timeout(200000) 254 this.timeout(200000)
294 255
295 getVideosList(urls[0], function (err, res) { 256 utils.getVideosList(urls[0], function (err, res) {
296 if (err) throw err 257 if (err) throw err
297 258
298 var video = res.body[3] 259 var video = res.body[3]
@@ -310,9 +271,9 @@
310 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { 271 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
311 this.timeout(15000) 272 this.timeout(15000)
312 273
313 removeVideo(urls[2], to_remove[0], function (err) { 274 utils.removeVideo(urls[2], to_remove[0], function (err) {
314 if (err) throw err 275 if (err) throw err
315 removeVideo(urls[2], to_remove[1], function (err) { 276 utils.removeVideo(urls[2], to_remove[1], function (err) {
316 if (err) throw err 277 if (err) throw err
317 278
318 // Wait the propagation to the other pods 279 // Wait the propagation to the other pods
@@ -325,7 +286,7 @@
325 286
326 it('Should have videos 1 and 3 on each pod', function (done) { 287 it('Should have videos 1 and 3 on each pod', function (done) {
327 async.each(urls, function (url, callback) { 288 async.each(urls, function (url, callback) {
328 getVideosList(url, function (err, res) { 289 utils.getVideosList(url, function (err, res) {
329 if (err) throw err 290 if (err) throw err
330 291
331 var videos = res.body 292 var videos = res.body
diff --git a/test/api/singlePod.js b/test/api/singlePod.js
index 301065f39..be944d8da 100644
--- a/test/api/singlePod.js
+++ b/test/api/singlePod.js
@@ -1,17 +1,16 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var request = require('supertest')
5 var chai = require('chai') 4 var chai = require('chai')
6 var fs = require('fs') 5 var fs = require('fs')
7 var expect = chai.expect 6 var expect = chai.expect
7
8 var webtorrent = require(__dirname + '/../../src/webTorrentNode') 8 var webtorrent = require(__dirname + '/../../src/webTorrentNode')
9 webtorrent.silent = true 9 webtorrent.silent = true
10 10
11 var utils = require('../utils') 11 var utils = require('./utils')
12 12
13 describe('Test a single pod', function () { 13 describe('Test a single pod', function () {
14 var path = '/api/v1/videos'
15 var app = null 14 var app = null
16 var url = '' 15 var url = ''
17 var video_id = -1 16 var video_id = -1
@@ -32,135 +31,99 @@
32 }) 31 })
33 32
34 it('Should not have videos', function (done) { 33 it('Should not have videos', function (done) {
35 request(url) 34 utils.getVideosList(url, function (err, res) {
36 .get(path) 35 if (err) throw err
37 .set('Accept', 'application/json')
38 .expect(200)
39 .expect('Content-Type', /json/)
40 .end(function (err, res) {
41 if (err) throw err
42 36
43 expect(res.body).to.be.an('array') 37 expect(res.body).to.be.an('array')
44 expect(res.body.length).to.equal(0) 38 expect(res.body.length).to.equal(0)
45 39
46 done() 40 done()
47 }) 41 })
48 }) 42 })
49 43
50 it('Should upload the video', function (done) { 44 it('Should upload the video', function (done) {
51 this.timeout(5000) 45 this.timeout(5000)
52 46 utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done)
53 request(url)
54 .post(path)
55 .set('Accept', 'application/json')
56 .field('name', 'my super name')
57 .field('description', 'my super description')
58 .attach('input_video', __dirname + '/../fixtures/video_short.webm')
59 .expect(201, done)
60 }) 47 })
61 48
62 it('Should seed the uploaded video', function (done) { 49 it('Should seed the uploaded video', function (done) {
63 // Yes, this could be long 50 // Yes, this could be long
64 this.timeout(60000) 51 this.timeout(60000)
65 52
66 request(url) 53 utils.getVideosList(url, function (err, res) {
67 .get(path) 54 if (err) throw err
68 .set('Accept', 'application/json')
69 .expect(200)
70 .expect('Content-Type', /json/)
71 .end(function (err, res) {
72 if (err) throw err
73 55
74 expect(res.body).to.be.an('array') 56 expect(res.body).to.be.an('array')
75 expect(res.body.length).to.equal(1) 57 expect(res.body.length).to.equal(1)
76 58
77 var video = res.body[0] 59 var video = res.body[0]
78 expect(video.name).to.equal('my super name') 60 expect(video.name).to.equal('my super name')
79 expect(video.description).to.equal('my super description') 61 expect(video.description).to.equal('my super description')
80 expect(video.podUrl).to.equal('http://localhost:9001') 62 expect(video.podUrl).to.equal('http://localhost:9001')
81 expect(video.magnetUri).to.exist 63 expect(video.magnetUri).to.exist
82 64
83 video_id = video._id 65 video_id = video._id
84 66
85 webtorrent.add(video.magnetUri, function (torrent) { 67 webtorrent.add(video.magnetUri, function (torrent) {
86 expect(torrent.files).to.exist 68 expect(torrent.files).to.exist
87 expect(torrent.files.length).to.equal(1) 69 expect(torrent.files.length).to.equal(1)
88 expect(torrent.files[0].path).to.exist.and.to.not.equal('') 70 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
89 71
90 done() 72 done()
91 })
92 }) 73 })
74 })
93 }) 75 })
94 76
95 it('Should search the video', function (done) { 77 it('Should search the video', function (done) {
96 request(url) 78 utils.searchVideo(url, 'my', function (err, res) {
97 .get(path + '/search/my') 79 if (err) throw err
98 .set('Accept', 'application/json')
99 .expect(200)
100 .expect('Content-Type', /json/)
101 .end(function (err, res) {
102 if (err) throw err
103 80
104 expect(res.body).to.be.an('array') 81 expect(res.body).to.be.an('array')
105 expect(res.body.length).to.equal(1) 82 expect(res.body.length).to.equal(1)
106 83
107 var video = res.body[0] 84 var video = res.body[0]
108 expect(video.name).to.equal('my super name') 85 expect(video.name).to.equal('my super name')
109 expect(video.description).to.equal('my super description') 86 expect(video.description).to.equal('my super description')
110 expect(video.podUrl).to.equal('http://localhost:9001') 87 expect(video.podUrl).to.equal('http://localhost:9001')
111 expect(video.magnetUri).to.exist 88 expect(video.magnetUri).to.exist
112 89
113 done() 90 done()
114 }) 91 })
115 }) 92 })
116 93
117 it('Should not find a search', function (done) { 94 it('Should not find a search', function (done) {
118 request(url) 95 utils.searchVideo(url, 'hello', function (err, res) {
119 .get(path + '/search/hello') 96 if (err) throw err
120 .set('Accept', 'application/json')
121 .expect(200)
122 .expect('Content-Type', /json/)
123 .end(function (err, res) {
124 if (err) throw err
125 97
126 expect(res.body).to.be.an('array') 98 expect(res.body).to.be.an('array')
127 expect(res.body.length).to.equal(0) 99 expect(res.body.length).to.equal(0)
128 100
129 done() 101 done()
130 }) 102 })
131 }) 103 })
132 104
133 it('Should remove the video', function (done) { 105 it('Should remove the video', function (done) {
134 request(url) 106 utils.removeVideo(url, video_id, function (err) {
135 .delete(path + '/' + video_id) 107 if (err) throw err
136 .set('Accept', 'application/json')
137 .expect(204)
138 .end(function (err, res) {
139 if (err) throw err
140 108
141 fs.readdir(__dirname + '/../../test1/uploads/', function (err, files) { 109 fs.readdir(__dirname + '/../../test1/uploads/', function (err, files) {
142 if (err) throw err 110 if (err) throw err
143 111
144 expect(files.length).to.equal(0) 112 expect(files.length).to.equal(0)
145 done() 113 done()
146 })
147 }) 114 })
115 })
148 }) 116 })
149 117
150 it('Should not have videos', function (done) { 118 it('Should not have videos', function (done) {
151 request(url) 119 utils.getVideosList(url, function (err, res) {
152 .get(path) 120 if (err) throw err
153 .set('Accept', 'application/json')
154 .expect(200)
155 .expect('Content-Type', /json/)
156 .end(function (err, res) {
157 if (err) throw err
158 121
159 expect(res.body).to.be.an('array') 122 expect(res.body).to.be.an('array')
160 expect(res.body.length).to.equal(0) 123 expect(res.body.length).to.equal(0)
161 124
162 done() 125 done()
163 }) 126 })
164 }) 127 })
165 128
166 after(function (done) { 129 after(function (done) {
diff --git a/test/api/utils.js b/test/api/utils.js
new file mode 100644
index 000000000..8d059b01c
--- /dev/null
+++ b/test/api/utils.js
@@ -0,0 +1,162 @@
1;(function () {
2 'use strict'
3
4 var child_process = require('child_process')
5 var exec = child_process.exec
6 var fork = child_process.fork
7 var request = require('supertest')
8
9 function flushTests (callback) {
10 exec(__dirname + '/../../scripts/clean_test.sh', function () {
11 callback()
12 })
13 }
14
15 function getFriendsList (url, end) {
16 var path = '/api/v1/pods/'
17
18 request(url)
19 .get(path)
20 .set('Accept', 'application/json')
21 .expect(200)
22 .expect('Content-Type', /json/)
23 .end(end)
24 }
25
26 function getVideosList (url, end) {
27 var path = '/api/v1/videos'
28
29 request(url)
30 .get(path)
31 .set('Accept', 'application/json')
32 .expect(200)
33 .expect('Content-Type', /json/)
34 .end(end)
35 }
36
37 function makeFriend (url, callback) {
38 var path = '/api/v1/pods/makefriends'
39
40 // The first pod make friend with the third
41 request(url)
42 .get(path)
43 .set('Accept', 'application/json')
44 .expect(204)
45 .end(function (err, res) {
46 if (err) throw err
47
48 // Wait for the request between pods
49 setTimeout(function () {
50 callback()
51 }, 1000)
52 })
53 }
54
55 function uploadVideo (url, name, description, fixture, end) {
56 var path = '/api/v1/videos'
57
58 request(url)
59 .post(path)
60 .set('Accept', 'application/json')
61 .field('name', name)
62 .field('description', description)
63 .attach('input_video', __dirname + '/fixtures/' + fixture)
64 .expect(201)
65 .end(end)
66 }
67
68 function removeVideo (url, id, end) {
69 var path = '/api/v1/videos'
70
71 request(url)
72 .delete(path + '/' + id)
73 .set('Accept', 'application/json')
74 .expect(204)
75 .end(end)
76 }
77
78 function runMultipleServers (total_servers, serversRun) {
79 var apps = []
80 var urls = []
81 var i = 0
82
83 function anotherServerDone (number, app, url) {
84 apps[number - 1] = app
85 urls[number - 1] = url
86 i++
87 if (i === total_servers) {
88 serversRun(apps, urls)
89 }
90 }
91
92 flushTests(function () {
93 for (var j = 1; j <= total_servers; j++) {
94 (function (k) { // TODO: ES6 with let
95 // For the virtual buffer
96 setTimeout(function () {
97 runServer(k, function (app, url) {
98 anotherServerDone(k, app, url)
99 })
100 }, 1000 * k)
101 })(j)
102 }
103 })
104 }
105
106 function runServer (number, callback) {
107 var port = 9000 + number
108 var server_run_string = {
109 'Connected to mongodb': false,
110 'Server listening on port': false
111 }
112
113 // Share the environment
114 var env = Object.create(process.env)
115 env.NODE_ENV = 'test'
116 env.NODE_APP_INSTANCE = number
117 var options = {
118 silent: true,
119 env: env,
120 detached: true
121 }
122
123 var app = fork(__dirname + '/../../server.js', [], options)
124 app.stdout.on('data', function onStdout (data) {
125 var dont_continue = false
126 // Check if all required sentences are here
127 for (var key of Object.keys(server_run_string)) {
128 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
129 if (server_run_string[key] === false) dont_continue = true
130 }
131
132 // If no, there is maybe one thing not already initialized (mongodb...)
133 if (dont_continue === true) return
134
135 app.stdout.removeListener('data', onStdout)
136 callback(app, 'http://localhost:' + port)
137 })
138 }
139
140 function searchVideo (url, search, end) {
141 var path = '/api/v1/videos'
142
143 request(url)
144 .get(path + '/search/' + search)
145 .set('Accept', 'application/json')
146 .expect(200)
147 .expect('Content-Type', /json/)
148 .end(end)
149 }
150
151 module.exports = {
152 flushTests: flushTests,
153 getFriendsList: getFriendsList,
154 getVideosList: getVideosList,
155 makeFriend: makeFriend,
156 removeVideo: removeVideo,
157 runMultipleServers: runMultipleServers,
158 runServer: runServer,
159 searchVideo: searchVideo,
160 uploadVideo: uploadVideo
161 }
162})()