diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-01-30 17:05:22 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-01-30 17:05:22 +0100 |
commit | cda021079ff455cc0fd0eb95a5395fa808ab63d1 (patch) | |
tree | 056716de7460462b74b861051a5e9da6e2633fce /tests/api | |
parent | 86435b9baedfe300a28ea4545511c1b50d4119f6 (diff) | |
download | PeerTube-cda021079ff455cc0fd0eb95a5395fa808ab63d1.tar.gz PeerTube-cda021079ff455cc0fd0eb95a5395fa808ab63d1.tar.zst PeerTube-cda021079ff455cc0fd0eb95a5395fa808ab63d1.zip |
New directory organization
Diffstat (limited to 'tests/api')
-rw-r--r-- | tests/api/checkParams.js | 301 | ||||
-rw-r--r-- | tests/api/fixtures/video_short.mp4 | bin | 0 -> 38783 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short.ogv | bin | 0 -> 140849 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short.webm | bin | 0 -> 218910 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short1.webm | bin | 0 -> 572456 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short2.webm | bin | 0 -> 942961 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short3.webm | bin | 0 -> 292677 bytes | |||
-rw-r--r-- | tests/api/fixtures/video_short_fake.webm | 1 | ||||
-rw-r--r-- | tests/api/friendsAdvanced.js | 252 | ||||
-rw-r--r-- | tests/api/friendsBasic.js | 187 | ||||
-rw-r--r-- | tests/api/index.js | 10 | ||||
-rw-r--r-- | tests/api/multiplePods.js | 329 | ||||
-rw-r--r-- | tests/api/singlePod.js | 147 | ||||
-rw-r--r-- | tests/api/utils.js | 182 |
14 files changed, 1409 insertions, 0 deletions
diff --git a/tests/api/checkParams.js b/tests/api/checkParams.js new file mode 100644 index 000000000..11fc68ff9 --- /dev/null +++ b/tests/api/checkParams.js | |||
@@ -0,0 +1,301 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var chai = require('chai') | ||
6 | var expect = chai.expect | ||
7 | var request = require('supertest') | ||
8 | |||
9 | var utils = require('./utils') | ||
10 | |||
11 | describe('Test parameters validator', function () { | ||
12 | var app = null | ||
13 | var url = '' | ||
14 | |||
15 | function makePostRequest (path, fields, attach, done, fail) { | ||
16 | var status_code = 400 | ||
17 | if (fail !== undefined && fail === false) status_code = 200 | ||
18 | |||
19 | var req = request(url) | ||
20 | .post(path) | ||
21 | .set('Accept', 'application/json') | ||
22 | |||
23 | Object.keys(fields).forEach(function (field) { | ||
24 | var value = fields[field] | ||
25 | req.field(field, value) | ||
26 | }) | ||
27 | |||
28 | req.expect(status_code, done) | ||
29 | } | ||
30 | |||
31 | function makePostBodyRequest (path, fields, done, fail) { | ||
32 | var status_code = 400 | ||
33 | if (fail !== undefined && fail === false) status_code = 200 | ||
34 | |||
35 | request(url) | ||
36 | .post(path) | ||
37 | .set('Accept', 'application/json') | ||
38 | .send(fields) | ||
39 | .expect(status_code, done) | ||
40 | } | ||
41 | |||
42 | // --------------------------------------------------------------- | ||
43 | |||
44 | before(function (done) { | ||
45 | this.timeout(20000) | ||
46 | |||
47 | async.series([ | ||
48 | function (next) { | ||
49 | utils.flushTests(next) | ||
50 | }, | ||
51 | function (next) { | ||
52 | utils.runServer(1, function (app1, url1) { | ||
53 | app = app1 | ||
54 | url = url1 | ||
55 | next() | ||
56 | }) | ||
57 | } | ||
58 | ], done) | ||
59 | }) | ||
60 | |||
61 | describe('Of the pods API', function () { | ||
62 | var path = '/api/v1/pods/' | ||
63 | |||
64 | describe('When adding a pod', function () { | ||
65 | it('Should fail with nothing', function (done) { | ||
66 | var data = {} | ||
67 | makePostBodyRequest(path, data, done) | ||
68 | }) | ||
69 | |||
70 | it('Should fail without public key', function (done) { | ||
71 | var data = { | ||
72 | data: { | ||
73 | url: 'http://coucou.com' | ||
74 | } | ||
75 | } | ||
76 | makePostBodyRequest(path, data, done) | ||
77 | }) | ||
78 | |||
79 | it('Should fail without an url', function (done) { | ||
80 | var data = { | ||
81 | data: { | ||
82 | publicKey: 'mysuperpublickey' | ||
83 | } | ||
84 | } | ||
85 | makePostBodyRequest(path, data, done) | ||
86 | }) | ||
87 | |||
88 | it('Should fail with an incorrect url', function (done) { | ||
89 | var data = { | ||
90 | data: { | ||
91 | url: 'coucou.com', | ||
92 | publicKey: 'mysuperpublickey' | ||
93 | } | ||
94 | } | ||
95 | makePostBodyRequest(path, data, function () { | ||
96 | data.data.url = 'http://coucou' | ||
97 | makePostBodyRequest(path, data, function () { | ||
98 | data.data.url = 'coucou' | ||
99 | makePostBodyRequest(path, data, done) | ||
100 | }) | ||
101 | }) | ||
102 | }) | ||
103 | |||
104 | it('Should succeed with the correct parameters', function (done) { | ||
105 | var data = { | ||
106 | data: { | ||
107 | url: 'http://coucou.com', | ||
108 | publicKey: 'mysuperpublickey' | ||
109 | } | ||
110 | } | ||
111 | makePostBodyRequest(path, data, done, false) | ||
112 | }) | ||
113 | }) | ||
114 | }) | ||
115 | |||
116 | describe('Of the videos API', function () { | ||
117 | var path = '/api/v1/videos/' | ||
118 | |||
119 | describe('When searching a video', function () { | ||
120 | it('Should fail with nothing', function (done) { | ||
121 | request(url) | ||
122 | .get(path + '/search/') | ||
123 | .set('Accept', 'application/json') | ||
124 | .expect(400, done) | ||
125 | }) | ||
126 | }) | ||
127 | |||
128 | describe('When adding a video', function () { | ||
129 | it('Should fail with nothing', function (done) { | ||
130 | var data = {} | ||
131 | var attach = {} | ||
132 | makePostRequest(path, data, attach, done) | ||
133 | }) | ||
134 | |||
135 | it('Should fail without name', function (done) { | ||
136 | var data = { | ||
137 | description: 'my super description' | ||
138 | } | ||
139 | var attach = { | ||
140 | 'input_video': __dirname + '/fixtures/video_short.webm' | ||
141 | } | ||
142 | makePostRequest(path, data, attach, done) | ||
143 | }) | ||
144 | |||
145 | it('Should fail with a long name', function (done) { | ||
146 | var data = { | ||
147 | name: 'My very very very very very very very very very very very very very very very very long name', | ||
148 | description: 'my super description' | ||
149 | } | ||
150 | var attach = { | ||
151 | 'input_video': __dirname + '/fixtures/video_short.webm' | ||
152 | } | ||
153 | makePostRequest(path, data, attach, done) | ||
154 | }) | ||
155 | |||
156 | it('Should fail without description', function (done) { | ||
157 | var data = { | ||
158 | name: 'my super name' | ||
159 | } | ||
160 | var attach = { | ||
161 | 'input_video': __dirname + '/fixtures/video_short.webm' | ||
162 | } | ||
163 | makePostRequest(path, data, attach, done) | ||
164 | }) | ||
165 | |||
166 | it('Should fail with a long description', function (done) { | ||
167 | var data = { | ||
168 | name: 'my super name', | ||
169 | description: 'my super description which is very very very very very very very very very very very very very very' + | ||
170 | 'very very very very very very very very very very very very very very very very very very very very very' + | ||
171 | 'very very very very very very very very very very very very very very very long' | ||
172 | } | ||
173 | var attach = { | ||
174 | 'input_video': __dirname + '/fixtures/video_short.webm' | ||
175 | } | ||
176 | makePostRequest(path, data, attach, done) | ||
177 | }) | ||
178 | |||
179 | it('Should fail without an input file', function (done) { | ||
180 | var data = { | ||
181 | name: 'my super name', | ||
182 | description: 'my super description' | ||
183 | } | ||
184 | var attach = {} | ||
185 | makePostRequest(path, data, attach, done) | ||
186 | }) | ||
187 | |||
188 | it('Should fail without an incorrect input file', function (done) { | ||
189 | var data = { | ||
190 | name: 'my super name', | ||
191 | description: 'my super description' | ||
192 | } | ||
193 | var attach = { | ||
194 | 'input_video': __dirname + '/../fixtures/video_short_fake.webm' | ||
195 | } | ||
196 | makePostRequest(path, data, attach, done) | ||
197 | }) | ||
198 | |||
199 | it('Should succeed with the correct parameters', function (done) { | ||
200 | var data = { | ||
201 | name: 'my super name', | ||
202 | description: 'my super description' | ||
203 | } | ||
204 | var attach = { | ||
205 | 'input_video': __dirname + '/fixtures/video_short.webm' | ||
206 | } | ||
207 | makePostRequest(path, data, attach, function () { | ||
208 | attach.input_video = __dirname + '/fixtures/video_short.mp4' | ||
209 | makePostRequest(path, data, attach, function () { | ||
210 | attach.input_video = __dirname + '/fixtures/video_short.ogv' | ||
211 | makePostRequest(path, data, attach, done, true) | ||
212 | }, true) | ||
213 | }, true) | ||
214 | }) | ||
215 | }) | ||
216 | |||
217 | describe('When getting a video', function () { | ||
218 | it('Should return the list of the videos with nothing', function (done) { | ||
219 | request(url) | ||
220 | .get(path) | ||
221 | .set('Accept', 'application/json') | ||
222 | .expect(200) | ||
223 | .expect('Content-Type', /json/) | ||
224 | .end(function (err, res) { | ||
225 | if (err) throw err | ||
226 | |||
227 | expect(res.body).to.be.an('array') | ||
228 | expect(res.body.length).to.equal(0) | ||
229 | |||
230 | done() | ||
231 | }) | ||
232 | }) | ||
233 | |||
234 | it('Should fail without a mongodb id', function (done) { | ||
235 | request(url) | ||
236 | .get(path + 'coucou') | ||
237 | .set('Accept', 'application/json') | ||
238 | .expect(400, done) | ||
239 | }) | ||
240 | |||
241 | it('Should return 404 with an incorrect video', function (done) { | ||
242 | request(url) | ||
243 | .get(path + '123456789012345678901234') | ||
244 | .set('Accept', 'application/json') | ||
245 | .expect(404, done) | ||
246 | }) | ||
247 | |||
248 | it('Should succeed with the correct parameters') | ||
249 | }) | ||
250 | |||
251 | describe('When removing a video', function () { | ||
252 | it('Should have 404 with nothing', function (done) { | ||
253 | request(url) | ||
254 | .delete(path) | ||
255 | .expect(404, done) | ||
256 | }) | ||
257 | |||
258 | it('Should fail without a mongodb id', function (done) { | ||
259 | request(url) | ||
260 | .delete(path + 'hello') | ||
261 | .expect(400, done) | ||
262 | }) | ||
263 | |||
264 | it('Should fail with a video which does not exist', function (done) { | ||
265 | request(url) | ||
266 | .delete(path + '123456789012345678901234') | ||
267 | .expect(404, done) | ||
268 | }) | ||
269 | |||
270 | it('Should fail with a video of another pod') | ||
271 | |||
272 | it('Should succeed with the correct parameters') | ||
273 | }) | ||
274 | }) | ||
275 | |||
276 | describe('Of the remote videos API', function () { | ||
277 | describe('When making a secure request', function () { | ||
278 | it('Should check a secure request') | ||
279 | }) | ||
280 | |||
281 | describe('When adding a video', function () { | ||
282 | it('Should check when adding a video') | ||
283 | }) | ||
284 | |||
285 | describe('When removing a video', function () { | ||
286 | it('Should check when removing a video') | ||
287 | }) | ||
288 | }) | ||
289 | |||
290 | after(function (done) { | ||
291 | process.kill(-app.pid) | ||
292 | |||
293 | // Keep the logs if the test failed | ||
294 | if (this.ok) { | ||
295 | utils.flushTests(done) | ||
296 | } else { | ||
297 | done() | ||
298 | } | ||
299 | }) | ||
300 | }) | ||
301 | })() | ||
diff --git a/tests/api/fixtures/video_short.mp4 b/tests/api/fixtures/video_short.mp4 new file mode 100644 index 000000000..35678362b --- /dev/null +++ b/tests/api/fixtures/video_short.mp4 | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short.ogv b/tests/api/fixtures/video_short.ogv new file mode 100644 index 000000000..9e253da82 --- /dev/null +++ b/tests/api/fixtures/video_short.ogv | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short.webm b/tests/api/fixtures/video_short.webm new file mode 100644 index 000000000..bf4b0ab6c --- /dev/null +++ b/tests/api/fixtures/video_short.webm | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short1.webm b/tests/api/fixtures/video_short1.webm new file mode 100644 index 000000000..70ac0c644 --- /dev/null +++ b/tests/api/fixtures/video_short1.webm | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short2.webm b/tests/api/fixtures/video_short2.webm new file mode 100644 index 000000000..13d72dff7 --- /dev/null +++ b/tests/api/fixtures/video_short2.webm | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short3.webm b/tests/api/fixtures/video_short3.webm new file mode 100644 index 000000000..cde5dcd58 --- /dev/null +++ b/tests/api/fixtures/video_short3.webm | |||
Binary files differ | |||
diff --git a/tests/api/fixtures/video_short_fake.webm b/tests/api/fixtures/video_short_fake.webm new file mode 100644 index 000000000..d85290ae5 --- /dev/null +++ b/tests/api/fixtures/video_short_fake.webm | |||
@@ -0,0 +1 @@ | |||
this is a fake video mouahahah | |||
diff --git a/tests/api/friendsAdvanced.js b/tests/api/friendsAdvanced.js new file mode 100644 index 000000000..61483bee6 --- /dev/null +++ b/tests/api/friendsAdvanced.js | |||
@@ -0,0 +1,252 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var chai = require('chai') | ||
6 | var expect = chai.expect | ||
7 | |||
8 | var utils = require('./utils') | ||
9 | |||
10 | describe('Test advanced friends', function () { | ||
11 | var apps = [] | ||
12 | var urls = [] | ||
13 | |||
14 | function makeFriends (pod_number, callback) { | ||
15 | return utils.makeFriends(urls[pod_number - 1], callback) | ||
16 | } | ||
17 | |||
18 | function quitFriends (pod_number, callback) { | ||
19 | return utils.quitFriends(urls[pod_number - 1], callback) | ||
20 | } | ||
21 | |||
22 | function getFriendsList (pod_number, end) { | ||
23 | return utils.getFriendsList(urls[pod_number - 1], end) | ||
24 | } | ||
25 | |||
26 | function uploadVideo (pod_number, callback) { | ||
27 | var name = 'my super video' | ||
28 | var description = 'my super description' | ||
29 | var fixture = 'video_short.webm' | ||
30 | |||
31 | return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) | ||
32 | } | ||
33 | |||
34 | function getVideos (pod_number, callback) { | ||
35 | return utils.getVideosList(urls[pod_number - 1], callback) | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------- | ||
39 | |||
40 | before(function (done) { | ||
41 | this.timeout(30000) | ||
42 | utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { | ||
43 | apps = apps_run | ||
44 | urls = urls_run | ||
45 | done() | ||
46 | }) | ||
47 | }) | ||
48 | |||
49 | it('Should make friends with two pod each in a different group', function (done) { | ||
50 | this.timeout(20000) | ||
51 | |||
52 | async.series([ | ||
53 | // Pod 3 makes friend with the first one | ||
54 | function (next) { | ||
55 | makeFriends(3, next) | ||
56 | }, | ||
57 | // Pod 4 makes friend with the second one | ||
58 | function (next) { | ||
59 | makeFriends(4, next) | ||
60 | }, | ||
61 | // Now if the fifth wants to make friends with the third et the first | ||
62 | function (next) { | ||
63 | makeFriends(5, next) | ||
64 | }, | ||
65 | function (next) { | ||
66 | setTimeout(next, 11000) | ||
67 | }], | ||
68 | function (err) { | ||
69 | if (err) throw err | ||
70 | |||
71 | // It should have 0 friends | ||
72 | getFriendsList(5, function (err, res) { | ||
73 | if (err) throw err | ||
74 | |||
75 | expect(res.body.length).to.equal(0) | ||
76 | |||
77 | done() | ||
78 | }) | ||
79 | } | ||
80 | ) | ||
81 | }) | ||
82 | |||
83 | it('Should quit all friends', function (done) { | ||
84 | this.timeout(10000) | ||
85 | |||
86 | async.series([ | ||
87 | function (next) { | ||
88 | quitFriends(1, next) | ||
89 | }, | ||
90 | function (next) { | ||
91 | quitFriends(2, next) | ||
92 | }], | ||
93 | function (err) { | ||
94 | if (err) throw err | ||
95 | |||
96 | async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) { | ||
97 | getFriendsList(i, function (err, res) { | ||
98 | if (err) throw err | ||
99 | |||
100 | expect(res.body.length).to.equal(0) | ||
101 | |||
102 | callback() | ||
103 | }) | ||
104 | }, done) | ||
105 | } | ||
106 | ) | ||
107 | }) | ||
108 | |||
109 | it('Should make friends with the pods 1, 2, 3', function (done) { | ||
110 | this.timeout(150000) | ||
111 | |||
112 | async.series([ | ||
113 | // Pods 1, 2, 3 and 4 become friends | ||
114 | function (next) { | ||
115 | makeFriends(2, next) | ||
116 | }, | ||
117 | function (next) { | ||
118 | makeFriends(1, next) | ||
119 | }, | ||
120 | function (next) { | ||
121 | makeFriends(4, next) | ||
122 | }, | ||
123 | // Kill pod 4 | ||
124 | function (next) { | ||
125 | apps[3].kill() | ||
126 | next() | ||
127 | }, | ||
128 | // Expulse pod 4 from pod 1 and 2 | ||
129 | function (next) { | ||
130 | uploadVideo(1, next) | ||
131 | }, | ||
132 | function (next) { | ||
133 | uploadVideo(2, next) | ||
134 | }, | ||
135 | function (next) { | ||
136 | setTimeout(next, 11000) | ||
137 | }, | ||
138 | function (next) { | ||
139 | uploadVideo(1, next) | ||
140 | }, | ||
141 | function (next) { | ||
142 | uploadVideo(2, next) | ||
143 | }, | ||
144 | function (next) { | ||
145 | setTimeout(next, 20000) | ||
146 | }, | ||
147 | // Rerun server 4 | ||
148 | function (next) { | ||
149 | utils.runServer(4, function (app, url) { | ||
150 | apps[3] = app | ||
151 | next() | ||
152 | }) | ||
153 | }, | ||
154 | function (next) { | ||
155 | getFriendsList(4, function (err, res) { | ||
156 | if (err) throw err | ||
157 | |||
158 | // Pod 4 didn't know pod 1 and 2 removed it | ||
159 | expect(res.body.length).to.equal(3) | ||
160 | |||
161 | next() | ||
162 | }) | ||
163 | }, | ||
164 | // Pod 6 ask pod 1, 2 and 3 | ||
165 | function (next) { | ||
166 | makeFriends(6, next) | ||
167 | }], | ||
168 | function (err) { | ||
169 | if (err) throw err | ||
170 | |||
171 | getFriendsList(6, function (err, res) { | ||
172 | if (err) throw err | ||
173 | |||
174 | // Pod 4 should not be our friend | ||
175 | var result = res.body | ||
176 | expect(result.length).to.equal(3) | ||
177 | for (var pod of result) { | ||
178 | expect(pod.url).not.equal(urls[3]) | ||
179 | } | ||
180 | |||
181 | done() | ||
182 | }) | ||
183 | } | ||
184 | ) | ||
185 | }) | ||
186 | |||
187 | it('Should pod 1 quit friends', function (done) { | ||
188 | this.timeout(25000) | ||
189 | |||
190 | async.series([ | ||
191 | // Upload a video on server 3 for aditionnal tests | ||
192 | function (next) { | ||
193 | uploadVideo(3, next) | ||
194 | }, | ||
195 | function (next) { | ||
196 | setTimeout(next, 15000) | ||
197 | }, | ||
198 | function (next) { | ||
199 | quitFriends(1, next) | ||
200 | }, | ||
201 | // Remove pod 1 from pod 2 | ||
202 | function (next) { | ||
203 | getVideos(1, function (err, res) { | ||
204 | if (err) throw err | ||
205 | expect(res.body).to.be.an('array') | ||
206 | expect(res.body.length).to.equal(2) | ||
207 | |||
208 | next() | ||
209 | }) | ||
210 | }], | ||
211 | function (err) { | ||
212 | if (err) throw err | ||
213 | |||
214 | getVideos(2, function (err, res) { | ||
215 | if (err) throw err | ||
216 | expect(res.body).to.be.an('array') | ||
217 | expect(res.body.length).to.equal(3) | ||
218 | done() | ||
219 | }) | ||
220 | } | ||
221 | ) | ||
222 | }) | ||
223 | |||
224 | it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { | ||
225 | this.timeout(20000) | ||
226 | makeFriends(1, function () { | ||
227 | setTimeout(function () { | ||
228 | getVideos(1, function (err, res) { | ||
229 | if (err) throw err | ||
230 | |||
231 | expect(res.body).to.be.an('array') | ||
232 | expect(res.body.length).to.equal(5) | ||
233 | |||
234 | done() | ||
235 | }) | ||
236 | }, 5000) | ||
237 | }) | ||
238 | }) | ||
239 | |||
240 | after(function (done) { | ||
241 | apps.forEach(function (app) { | ||
242 | process.kill(-app.pid) | ||
243 | }) | ||
244 | |||
245 | if (this.ok) { | ||
246 | utils.flushTests(done) | ||
247 | } else { | ||
248 | done() | ||
249 | } | ||
250 | }) | ||
251 | }) | ||
252 | })() | ||
diff --git a/tests/api/friendsBasic.js b/tests/api/friendsBasic.js new file mode 100644 index 000000000..dbc918383 --- /dev/null +++ b/tests/api/friendsBasic.js | |||
@@ -0,0 +1,187 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var chai = require('chai') | ||
6 | var expect = chai.expect | ||
7 | var request = require('supertest') | ||
8 | |||
9 | var utils = require('./utils') | ||
10 | |||
11 | describe('Test basic friends', function () { | ||
12 | var apps = [] | ||
13 | var urls = [] | ||
14 | |||
15 | function testMadeFriends (urls, url_to_test, callback) { | ||
16 | var friends = [] | ||
17 | for (var i = 0; i < urls.length; i++) { | ||
18 | if (urls[i] === url_to_test) continue | ||
19 | friends.push(urls[i]) | ||
20 | } | ||
21 | |||
22 | utils.getFriendsList(url_to_test, function (err, res) { | ||
23 | if (err) throw err | ||
24 | |||
25 | var result = res.body | ||
26 | var result_urls = [ result[0].url, result[1].url ] | ||
27 | expect(result).to.be.an('array') | ||
28 | expect(result.length).to.equal(2) | ||
29 | expect(result_urls[0]).to.not.equal(result_urls[1]) | ||
30 | |||
31 | var error_string = 'Friends url do not correspond for ' + url_to_test | ||
32 | expect(friends).to.contain(result_urls[0], error_string) | ||
33 | expect(friends).to.contain(result_urls[1], error_string) | ||
34 | callback() | ||
35 | }) | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------- | ||
39 | |||
40 | before(function (done) { | ||
41 | this.timeout(20000) | ||
42 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { | ||
43 | apps = apps_run | ||
44 | urls = urls_run | ||
45 | done() | ||
46 | }) | ||
47 | }) | ||
48 | |||
49 | it('Should not have friends', function (done) { | ||
50 | async.each(urls, function (url, callback) { | ||
51 | utils.getFriendsList(url, function (err, res) { | ||
52 | if (err) throw err | ||
53 | |||
54 | var result = res.body | ||
55 | expect(result).to.be.an('array') | ||
56 | expect(result.length).to.equal(0) | ||
57 | callback() | ||
58 | }) | ||
59 | }, done) | ||
60 | }) | ||
61 | |||
62 | it('Should make friends', function (done) { | ||
63 | this.timeout(10000) | ||
64 | |||
65 | var path = '/api/v1/pods/makefriends' | ||
66 | |||
67 | async.series([ | ||
68 | // The second pod make friend with the third | ||
69 | function (next) { | ||
70 | request(urls[1]) | ||
71 | .get(path) | ||
72 | .set('Accept', 'application/json') | ||
73 | .expect(204) | ||
74 | .end(next) | ||
75 | }, | ||
76 | // Wait for the request between pods | ||
77 | function (next) { | ||
78 | setTimeout(next, 1000) | ||
79 | }, | ||
80 | // The second pod should have the third as a friend | ||
81 | function (next) { | ||
82 | utils.getFriendsList(urls[1], function (err, res) { | ||
83 | if (err) throw err | ||
84 | |||
85 | var result = res.body | ||
86 | expect(result).to.be.an('array') | ||
87 | expect(result.length).to.equal(1) | ||
88 | expect(result[0].url).to.be.equal(urls[2]) | ||
89 | |||
90 | next() | ||
91 | }) | ||
92 | }, | ||
93 | // Same here, the third pod should have the second pod as a friend | ||
94 | function (next) { | ||
95 | utils.getFriendsList(urls[2], function (err, res) { | ||
96 | if (err) throw err | ||
97 | |||
98 | var result = res.body | ||
99 | expect(result).to.be.an('array') | ||
100 | expect(result.length).to.equal(1) | ||
101 | expect(result[0].url).to.be.equal(urls[1]) | ||
102 | |||
103 | next() | ||
104 | }) | ||
105 | }, | ||
106 | // Finally the first pod make friend with the second pod | ||
107 | function (next) { | ||
108 | request(urls[0]) | ||
109 | .get(path) | ||
110 | .set('Accept', 'application/json') | ||
111 | .expect(204) | ||
112 | .end(next) | ||
113 | }, | ||
114 | // Wait for the request between pods | ||
115 | function (next) { | ||
116 | setTimeout(next, 1000) | ||
117 | } | ||
118 | ], | ||
119 | // Now each pod should be friend with the other ones | ||
120 | function (err) { | ||
121 | if (err) throw err | ||
122 | async.each(urls, function (url, callback) { | ||
123 | testMadeFriends(urls, url, callback) | ||
124 | }, done) | ||
125 | }) | ||
126 | }) | ||
127 | |||
128 | it('Should not be allowed to make friend again', function (done) { | ||
129 | utils.makeFriends(urls[1], 409, done) | ||
130 | }) | ||
131 | |||
132 | it('Should quit friends of pod 2', function (done) { | ||
133 | async.series([ | ||
134 | // Pod 1 quit friends | ||
135 | function (next) { | ||
136 | utils.quitFriends(urls[1], next) | ||
137 | }, | ||
138 | // Pod 1 should not have friends anymore | ||
139 | function (next) { | ||
140 | utils.getFriendsList(urls[1], function (err, res) { | ||
141 | if (err) throw err | ||
142 | |||
143 | var result = res.body | ||
144 | expect(result).to.be.an('array') | ||
145 | expect(result.length).to.equal(0) | ||
146 | |||
147 | next() | ||
148 | }) | ||
149 | }, | ||
150 | // Other pods shouldn't have pod 1 too | ||
151 | function (next) { | ||
152 | async.each([ urls[0], urls[2] ], function (url, callback) { | ||
153 | utils.getFriendsList(url, function (err, res) { | ||
154 | if (err) throw err | ||
155 | |||
156 | var result = res.body | ||
157 | expect(result).to.be.an('array') | ||
158 | expect(result.length).to.equal(1) | ||
159 | expect(result[0].url).not.to.be.equal(urls[1]) | ||
160 | callback() | ||
161 | }) | ||
162 | }, next) | ||
163 | } | ||
164 | ], done) | ||
165 | }) | ||
166 | |||
167 | it('Should allow pod 2 to make friend again', function (done) { | ||
168 | utils.makeFriends(urls[1], function () { | ||
169 | async.each(urls, function (url, callback) { | ||
170 | testMadeFriends(urls, url, callback) | ||
171 | }, done) | ||
172 | }) | ||
173 | }) | ||
174 | |||
175 | after(function (done) { | ||
176 | apps.forEach(function (app) { | ||
177 | process.kill(-app.pid) | ||
178 | }) | ||
179 | |||
180 | if (this.ok) { | ||
181 | utils.flushTests(done) | ||
182 | } else { | ||
183 | done() | ||
184 | } | ||
185 | }) | ||
186 | }) | ||
187 | })() | ||
diff --git a/tests/api/index.js b/tests/api/index.js new file mode 100644 index 000000000..3bdcdae2d --- /dev/null +++ b/tests/api/index.js | |||
@@ -0,0 +1,10 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | // Order of the tests we want to execute | ||
5 | require('./checkParams') | ||
6 | require('./friendsBasic') | ||
7 | require('./singlePod') | ||
8 | require('./multiplePods') | ||
9 | require('./friendsAdvanced') | ||
10 | })() | ||
diff --git a/tests/api/multiplePods.js b/tests/api/multiplePods.js new file mode 100644 index 000000000..c27f7121e --- /dev/null +++ b/tests/api/multiplePods.js | |||
@@ -0,0 +1,329 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var chai = require('chai') | ||
6 | var expect = chai.expect | ||
7 | |||
8 | var utils = require('./utils') | ||
9 | var webtorrent = require(__dirname + '/../../lib/webTorrentNode') | ||
10 | webtorrent.silent = true | ||
11 | |||
12 | describe('Test multiple pods', function () { | ||
13 | var apps = [] | ||
14 | var urls = [] | ||
15 | var to_remove = [] | ||
16 | |||
17 | before(function (done) { | ||
18 | this.timeout(30000) | ||
19 | |||
20 | async.series([ | ||
21 | // Run servers | ||
22 | function (next) { | ||
23 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { | ||
24 | apps = apps_run | ||
25 | urls = urls_run | ||
26 | next() | ||
27 | }) | ||
28 | }, | ||
29 | // The second pod make friend with the third | ||
30 | function (next) { | ||
31 | utils.makeFriends(urls[1], next) | ||
32 | }, | ||
33 | // Wait for the request between pods | ||
34 | function (next) { | ||
35 | setTimeout(next, 10000) | ||
36 | }, | ||
37 | // Pod 1 make friends too | ||
38 | function (next) { | ||
39 | utils.makeFriends(urls[0], next) | ||
40 | }, | ||
41 | function (next) { | ||
42 | webtorrent.create({ host: 'client', port: '1' }, next) | ||
43 | } | ||
44 | ], done) | ||
45 | }) | ||
46 | |||
47 | it('Should not have videos for all pods', function (done) { | ||
48 | async.each(urls, function (url, callback) { | ||
49 | utils.getVideosList(url, function (err, res) { | ||
50 | if (err) throw err | ||
51 | |||
52 | expect(res.body).to.be.an('array') | ||
53 | expect(res.body.length).to.equal(0) | ||
54 | |||
55 | callback() | ||
56 | }) | ||
57 | }, done) | ||
58 | }) | ||
59 | |||
60 | describe('Should upload the video and propagate on each pod', function () { | ||
61 | it('Should upload the video on pod 1 and propagate on each pod', function (done) { | ||
62 | this.timeout(15000) | ||
63 | |||
64 | async.series([ | ||
65 | function (next) { | ||
66 | utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) | ||
67 | }, | ||
68 | function (next) { | ||
69 | setTimeout(next, 11000) | ||
70 | }], | ||
71 | // All pods should have this video | ||
72 | function (err) { | ||
73 | if (err) throw err | ||
74 | |||
75 | async.each(urls, function (url, callback) { | ||
76 | var base_magnet = null | ||
77 | |||
78 | utils.getVideosList(url, function (err, res) { | ||
79 | if (err) throw err | ||
80 | |||
81 | var videos = res.body | ||
82 | expect(videos).to.be.an('array') | ||
83 | expect(videos.length).to.equal(1) | ||
84 | var video = videos[0] | ||
85 | expect(video.name).to.equal('my super name for pod 1') | ||
86 | expect(video.description).to.equal('my super description for pod 1') | ||
87 | expect(video.podUrl).to.equal('http://localhost:9001') | ||
88 | expect(video.magnetUri).to.exist | ||
89 | |||
90 | // All pods should have the same magnet Uri | ||
91 | if (base_magnet === null) { | ||
92 | base_magnet = video.magnetUri | ||
93 | } else { | ||
94 | expect(video.magnetUri).to.equal.magnetUri | ||
95 | } | ||
96 | |||
97 | callback() | ||
98 | }) | ||
99 | }, done) | ||
100 | } | ||
101 | ) | ||
102 | }) | ||
103 | |||
104 | it('Should upload the video on pod 2 and propagate on each pod', function (done) { | ||
105 | this.timeout(15000) | ||
106 | |||
107 | async.series([ | ||
108 | function (next) { | ||
109 | utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) | ||
110 | }, | ||
111 | function (next) { | ||
112 | setTimeout(next, 11000) | ||
113 | }], | ||
114 | // All pods should have this video | ||
115 | function (err) { | ||
116 | if (err) throw err | ||
117 | |||
118 | async.each(urls, function (url, callback) { | ||
119 | var base_magnet = null | ||
120 | |||
121 | utils.getVideosList(url, function (err, res) { | ||
122 | if (err) throw err | ||
123 | |||
124 | var videos = res.body | ||
125 | expect(videos).to.be.an('array') | ||
126 | expect(videos.length).to.equal(2) | ||
127 | var video = videos[1] | ||
128 | expect(video.name).to.equal('my super name for pod 2') | ||
129 | expect(video.description).to.equal('my super description for pod 2') | ||
130 | expect(video.podUrl).to.equal('http://localhost:9002') | ||
131 | expect(video.magnetUri).to.exist | ||
132 | |||
133 | // All pods should have the same magnet Uri | ||
134 | if (base_magnet === null) { | ||
135 | base_magnet = video.magnetUri | ||
136 | } else { | ||
137 | expect(video.magnetUri).to.equal.magnetUri | ||
138 | } | ||
139 | |||
140 | callback() | ||
141 | }) | ||
142 | }, done) | ||
143 | } | ||
144 | ) | ||
145 | }) | ||
146 | |||
147 | it('Should upload two videos on pod 3 and propagate on each pod', function (done) { | ||
148 | this.timeout(30000) | ||
149 | |||
150 | async.series([ | ||
151 | function (next) { | ||
152 | utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) | ||
153 | }, | ||
154 | function (next) { | ||
155 | utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) | ||
156 | }, | ||
157 | function (next) { | ||
158 | setTimeout(next, 22000) | ||
159 | }], | ||
160 | function (err) { | ||
161 | if (err) throw err | ||
162 | |||
163 | var base_magnet = null | ||
164 | // All pods should have this video | ||
165 | async.each(urls, function (url, callback) { | ||
166 | utils.getVideosList(url, function (err, res) { | ||
167 | if (err) throw err | ||
168 | |||
169 | var videos = res.body | ||
170 | expect(videos).to.be.an('array') | ||
171 | expect(videos.length).to.equal(4) | ||
172 | var video = videos[2] | ||
173 | expect(video.name).to.equal('my super name for pod 3') | ||
174 | expect(video.description).to.equal('my super description for pod 3') | ||
175 | expect(video.podUrl).to.equal('http://localhost:9003') | ||
176 | expect(video.magnetUri).to.exist | ||
177 | |||
178 | video = videos[3] | ||
179 | expect(video.name).to.equal('my super name for pod 3-2') | ||
180 | expect(video.description).to.equal('my super description for pod 3-2') | ||
181 | expect(video.podUrl).to.equal('http://localhost:9003') | ||
182 | expect(video.magnetUri).to.exist | ||
183 | |||
184 | // All pods should have the same magnet Uri | ||
185 | if (base_magnet === null) { | ||
186 | base_magnet = video.magnetUri | ||
187 | } else { | ||
188 | expect(video.magnetUri).to.equal.magnetUri | ||
189 | } | ||
190 | |||
191 | callback() | ||
192 | }) | ||
193 | }, done) | ||
194 | } | ||
195 | ) | ||
196 | }) | ||
197 | }) | ||
198 | |||
199 | describe('Should seed the uploaded video', function () { | ||
200 | it('Should add the file 1 by asking pod 3', function (done) { | ||
201 | // Yes, this could be long | ||
202 | this.timeout(200000) | ||
203 | |||
204 | utils.getVideosList(urls[2], function (err, res) { | ||
205 | if (err) throw err | ||
206 | |||
207 | var video = res.body[0] | ||
208 | to_remove.push(res.body[2]._id) | ||
209 | to_remove.push(res.body[3]._id) | ||
210 | |||
211 | webtorrent.add(video.magnetUri, function (torrent) { | ||
212 | expect(torrent.files).to.exist | ||
213 | expect(torrent.files.length).to.equal(1) | ||
214 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
215 | |||
216 | done() | ||
217 | }) | ||
218 | }) | ||
219 | }) | ||
220 | |||
221 | it('Should add the file 2 by asking pod 1', function (done) { | ||
222 | // Yes, this could be long | ||
223 | this.timeout(200000) | ||
224 | |||
225 | utils.getVideosList(urls[0], function (err, res) { | ||
226 | if (err) throw err | ||
227 | |||
228 | var video = res.body[1] | ||
229 | |||
230 | webtorrent.add(video.magnetUri, function (torrent) { | ||
231 | expect(torrent.files).to.exist | ||
232 | expect(torrent.files.length).to.equal(1) | ||
233 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
234 | |||
235 | done() | ||
236 | }) | ||
237 | }) | ||
238 | }) | ||
239 | |||
240 | it('Should add the file 3 by asking pod 2', function (done) { | ||
241 | // Yes, this could be long | ||
242 | this.timeout(200000) | ||
243 | |||
244 | utils.getVideosList(urls[1], function (err, res) { | ||
245 | if (err) throw err | ||
246 | |||
247 | var video = res.body[2] | ||
248 | |||
249 | webtorrent.add(video.magnetUri, function (torrent) { | ||
250 | expect(torrent.files).to.exist | ||
251 | expect(torrent.files.length).to.equal(1) | ||
252 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
253 | |||
254 | done() | ||
255 | }) | ||
256 | }) | ||
257 | }) | ||
258 | |||
259 | it('Should add the file 3-2 by asking pod 1', function (done) { | ||
260 | // Yes, this could be long | ||
261 | this.timeout(200000) | ||
262 | |||
263 | utils.getVideosList(urls[0], function (err, res) { | ||
264 | if (err) throw err | ||
265 | |||
266 | var video = res.body[3] | ||
267 | |||
268 | webtorrent.add(video.magnetUri, function (torrent) { | ||
269 | expect(torrent.files).to.exist | ||
270 | expect(torrent.files.length).to.equal(1) | ||
271 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
272 | |||
273 | done() | ||
274 | }) | ||
275 | }) | ||
276 | }) | ||
277 | |||
278 | it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { | ||
279 | this.timeout(15000) | ||
280 | |||
281 | async.series([ | ||
282 | function (next) { | ||
283 | utils.removeVideo(urls[2], to_remove[0], next) | ||
284 | }, | ||
285 | function (next) { | ||
286 | utils.removeVideo(urls[2], to_remove[1], next) | ||
287 | }], | ||
288 | function (err) { | ||
289 | if (err) throw err | ||
290 | setTimeout(done, 11000) | ||
291 | } | ||
292 | ) | ||
293 | }) | ||
294 | |||
295 | it('Should have videos 1 and 3 on each pod', function (done) { | ||
296 | async.each(urls, function (url, callback) { | ||
297 | utils.getVideosList(url, function (err, res) { | ||
298 | if (err) throw err | ||
299 | |||
300 | var videos = res.body | ||
301 | expect(videos).to.be.an('array') | ||
302 | expect(videos.length).to.equal(2) | ||
303 | expect(videos[0]._id).not.to.equal(videos[1]._id) | ||
304 | expect(videos[0]._id).not.to.equal(to_remove[0]) | ||
305 | expect(videos[1]._id).not.to.equal(to_remove[0]) | ||
306 | expect(videos[0]._id).not.to.equal(to_remove[1]) | ||
307 | expect(videos[1]._id).not.to.equal(to_remove[1]) | ||
308 | |||
309 | callback() | ||
310 | }) | ||
311 | }, done) | ||
312 | }) | ||
313 | }) | ||
314 | |||
315 | after(function (done) { | ||
316 | apps.forEach(function (app) { | ||
317 | process.kill(-app.pid) | ||
318 | }) | ||
319 | process.kill(-webtorrent.app.pid) | ||
320 | |||
321 | // Keep the logs if the test failed | ||
322 | if (this.ok) { | ||
323 | utils.flushTests(done) | ||
324 | } else { | ||
325 | done() | ||
326 | } | ||
327 | }) | ||
328 | }) | ||
329 | })() | ||
diff --git a/tests/api/singlePod.js b/tests/api/singlePod.js new file mode 100644 index 000000000..ce3ca80f2 --- /dev/null +++ b/tests/api/singlePod.js | |||
@@ -0,0 +1,147 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var chai = require('chai') | ||
6 | var fs = require('fs') | ||
7 | var expect = chai.expect | ||
8 | |||
9 | var webtorrent = require(__dirname + '/../../lib/webTorrentNode') | ||
10 | webtorrent.silent = true | ||
11 | |||
12 | var utils = require('./utils') | ||
13 | |||
14 | describe('Test a single pod', function () { | ||
15 | var app = null | ||
16 | var url = '' | ||
17 | var video_id = -1 | ||
18 | |||
19 | before(function (done) { | ||
20 | this.timeout(20000) | ||
21 | |||
22 | async.series([ | ||
23 | function (next) { | ||
24 | utils.flushTests(next) | ||
25 | }, | ||
26 | function (next) { | ||
27 | utils.runServer(1, function (app1, url1) { | ||
28 | app = app1 | ||
29 | url = url1 | ||
30 | next() | ||
31 | }) | ||
32 | }, | ||
33 | function (next) { | ||
34 | webtorrent.create({ host: 'client', port: '1' }, next) | ||
35 | } | ||
36 | ], done) | ||
37 | }) | ||
38 | |||
39 | it('Should not have videos', function (done) { | ||
40 | utils.getVideosList(url, function (err, res) { | ||
41 | if (err) throw err | ||
42 | |||
43 | expect(res.body).to.be.an('array') | ||
44 | expect(res.body.length).to.equal(0) | ||
45 | |||
46 | done() | ||
47 | }) | ||
48 | }) | ||
49 | |||
50 | it('Should upload the video', function (done) { | ||
51 | this.timeout(5000) | ||
52 | utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done) | ||
53 | }) | ||
54 | |||
55 | it('Should seed the uploaded video', function (done) { | ||
56 | // Yes, this could be long | ||
57 | this.timeout(60000) | ||
58 | |||
59 | utils.getVideosList(url, function (err, res) { | ||
60 | if (err) throw err | ||
61 | |||
62 | expect(res.body).to.be.an('array') | ||
63 | expect(res.body.length).to.equal(1) | ||
64 | |||
65 | var video = res.body[0] | ||
66 | expect(video.name).to.equal('my super name') | ||
67 | expect(video.description).to.equal('my super description') | ||
68 | expect(video.podUrl).to.equal('http://localhost:9001') | ||
69 | expect(video.magnetUri).to.exist | ||
70 | |||
71 | video_id = video._id | ||
72 | |||
73 | webtorrent.add(video.magnetUri, function (torrent) { | ||
74 | expect(torrent.files).to.exist | ||
75 | expect(torrent.files.length).to.equal(1) | ||
76 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
77 | |||
78 | done() | ||
79 | }) | ||
80 | }) | ||
81 | }) | ||
82 | |||
83 | it('Should search the video', function (done) { | ||
84 | utils.searchVideo(url, 'my', function (err, res) { | ||
85 | if (err) throw err | ||
86 | |||
87 | expect(res.body).to.be.an('array') | ||
88 | expect(res.body.length).to.equal(1) | ||
89 | |||
90 | var video = res.body[0] | ||
91 | expect(video.name).to.equal('my super name') | ||
92 | expect(video.description).to.equal('my super description') | ||
93 | expect(video.podUrl).to.equal('http://localhost:9001') | ||
94 | expect(video.magnetUri).to.exist | ||
95 | |||
96 | done() | ||
97 | }) | ||
98 | }) | ||
99 | |||
100 | it('Should not find a search', function (done) { | ||
101 | utils.searchVideo(url, 'hello', function (err, res) { | ||
102 | if (err) throw err | ||
103 | |||
104 | expect(res.body).to.be.an('array') | ||
105 | expect(res.body.length).to.equal(0) | ||
106 | |||
107 | done() | ||
108 | }) | ||
109 | }) | ||
110 | |||
111 | it('Should remove the video', function (done) { | ||
112 | utils.removeVideo(url, video_id, function (err) { | ||
113 | if (err) throw err | ||
114 | |||
115 | fs.readdir(__dirname + '/../../test1/uploads/', function (err, files) { | ||
116 | if (err) throw err | ||
117 | |||
118 | expect(files.length).to.equal(0) | ||
119 | done() | ||
120 | }) | ||
121 | }) | ||
122 | }) | ||
123 | |||
124 | it('Should not have videos', function (done) { | ||
125 | utils.getVideosList(url, function (err, res) { | ||
126 | if (err) throw err | ||
127 | |||
128 | expect(res.body).to.be.an('array') | ||
129 | expect(res.body.length).to.equal(0) | ||
130 | |||
131 | done() | ||
132 | }) | ||
133 | }) | ||
134 | |||
135 | after(function (done) { | ||
136 | process.kill(-app.pid) | ||
137 | process.kill(-webtorrent.app.pid) | ||
138 | |||
139 | // Keep the logs if the test failed | ||
140 | if (this.ok) { | ||
141 | utils.flushTests(done) | ||
142 | } else { | ||
143 | done() | ||
144 | } | ||
145 | }) | ||
146 | }) | ||
147 | })() | ||
diff --git a/tests/api/utils.js b/tests/api/utils.js new file mode 100644 index 000000000..afb0abb33 --- /dev/null +++ b/tests/api/utils.js | |||
@@ -0,0 +1,182 @@ | |||
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 | module.exports = { | ||
10 | flushTests: flushTests, | ||
11 | getFriendsList: getFriendsList, | ||
12 | getVideosList: getVideosList, | ||
13 | makeFriends: makeFriends, | ||
14 | quitFriends: quitFriends, | ||
15 | removeVideo: removeVideo, | ||
16 | flushAndRunMultipleServers: flushAndRunMultipleServers, | ||
17 | runServer: runServer, | ||
18 | searchVideo: searchVideo, | ||
19 | uploadVideo: uploadVideo | ||
20 | } | ||
21 | |||
22 | // ---------------------- Export functions -------------------- | ||
23 | |||
24 | function flushTests (callback) { | ||
25 | exec(__dirname + '/../../scripts/clean_test.sh', callback) | ||
26 | } | ||
27 | |||
28 | function getFriendsList (url, end) { | ||
29 | var path = '/api/v1/pods/' | ||
30 | |||
31 | request(url) | ||
32 | .get(path) | ||
33 | .set('Accept', 'application/json') | ||
34 | .expect(200) | ||
35 | .expect('Content-Type', /json/) | ||
36 | .end(end) | ||
37 | } | ||
38 | |||
39 | function getVideosList (url, end) { | ||
40 | var path = '/api/v1/videos' | ||
41 | |||
42 | request(url) | ||
43 | .get(path) | ||
44 | .set('Accept', 'application/json') | ||
45 | .expect(200) | ||
46 | .expect('Content-Type', /json/) | ||
47 | .end(end) | ||
48 | } | ||
49 | |||
50 | function makeFriends (url, expected_status, callback) { | ||
51 | if (!callback) { | ||
52 | callback = expected_status | ||
53 | expected_status = 204 | ||
54 | } | ||
55 | |||
56 | var path = '/api/v1/pods/makefriends' | ||
57 | |||
58 | // The first pod make friend with the third | ||
59 | request(url) | ||
60 | .get(path) | ||
61 | .set('Accept', 'application/json') | ||
62 | .expect(expected_status) | ||
63 | .end(function (err, res) { | ||
64 | if (err) throw err | ||
65 | |||
66 | // Wait for the request between pods | ||
67 | setTimeout(callback, 1000) | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | function quitFriends (url, callback) { | ||
72 | var path = '/api/v1/pods/quitfriends' | ||
73 | |||
74 | // The first pod make friend with the third | ||
75 | request(url) | ||
76 | .get(path) | ||
77 | .set('Accept', 'application/json') | ||
78 | .expect(204) | ||
79 | .end(function (err, res) { | ||
80 | if (err) throw err | ||
81 | |||
82 | // Wait for the request between pods | ||
83 | setTimeout(callback, 1000) | ||
84 | }) | ||
85 | } | ||
86 | |||
87 | function removeVideo (url, id, end) { | ||
88 | var path = '/api/v1/videos' | ||
89 | |||
90 | request(url) | ||
91 | .delete(path + '/' + id) | ||
92 | .set('Accept', 'application/json') | ||
93 | .expect(204) | ||
94 | .end(end) | ||
95 | } | ||
96 | |||
97 | function flushAndRunMultipleServers (total_servers, serversRun) { | ||
98 | var apps = [] | ||
99 | var urls = [] | ||
100 | var i = 0 | ||
101 | |||
102 | function anotherServerDone (number, app, url) { | ||
103 | apps[number - 1] = app | ||
104 | urls[number - 1] = url | ||
105 | i++ | ||
106 | if (i === total_servers) { | ||
107 | serversRun(apps, urls) | ||
108 | } | ||
109 | } | ||
110 | |||
111 | flushTests(function () { | ||
112 | for (var j = 1; j <= total_servers; j++) { | ||
113 | (function (k) { // TODO: ES6 with let | ||
114 | // For the virtual buffer | ||
115 | setTimeout(function () { | ||
116 | runServer(k, function (app, url) { | ||
117 | anotherServerDone(k, app, url) | ||
118 | }) | ||
119 | }, 1000 * k) | ||
120 | })(j) | ||
121 | } | ||
122 | }) | ||
123 | } | ||
124 | |||
125 | function runServer (number, callback) { | ||
126 | var port = 9000 + number | ||
127 | var server_run_string = { | ||
128 | 'Connected to mongodb': false, | ||
129 | 'Server listening on port': false | ||
130 | } | ||
131 | |||
132 | // Share the environment | ||
133 | var env = Object.create(process.env) | ||
134 | env.NODE_ENV = 'test' | ||
135 | env.NODE_APP_INSTANCE = number | ||
136 | var options = { | ||
137 | silent: true, | ||
138 | env: env, | ||
139 | detached: true | ||
140 | } | ||
141 | |||
142 | var app = fork(__dirname + '/../../server.js', [], options) | ||
143 | app.stdout.on('data', function onStdout (data) { | ||
144 | var dont_continue = false | ||
145 | // Check if all required sentences are here | ||
146 | for (var key of Object.keys(server_run_string)) { | ||
147 | if (data.toString().indexOf(key) !== -1) server_run_string[key] = true | ||
148 | if (server_run_string[key] === false) dont_continue = true | ||
149 | } | ||
150 | |||
151 | // If no, there is maybe one thing not already initialized (mongodb...) | ||
152 | if (dont_continue === true) return | ||
153 | |||
154 | app.stdout.removeListener('data', onStdout) | ||
155 | callback(app, 'http://localhost:' + port) | ||
156 | }) | ||
157 | } | ||
158 | |||
159 | function searchVideo (url, search, end) { | ||
160 | var path = '/api/v1/videos' | ||
161 | |||
162 | request(url) | ||
163 | .get(path + '/search/' + search) | ||
164 | .set('Accept', 'application/json') | ||
165 | .expect(200) | ||
166 | .expect('Content-Type', /json/) | ||
167 | .end(end) | ||
168 | } | ||
169 | |||
170 | function uploadVideo (url, name, description, fixture, end) { | ||
171 | var path = '/api/v1/videos' | ||
172 | |||
173 | request(url) | ||
174 | .post(path) | ||
175 | .set('Accept', 'application/json') | ||
176 | .field('name', name) | ||
177 | .field('description', description) | ||
178 | .attach('input_video', __dirname + '/fixtures/' + fixture) | ||
179 | .expect(201) | ||
180 | .end(end) | ||
181 | } | ||
182 | })() | ||