]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
34ca3b52 | 2 | |
f0f5567b C |
3 | const async = require('async') |
4 | const chai = require('chai') | |
5 | const expect = chai.expect | |
6 | const pathUtils = require('path') | |
7 | const request = require('supertest') | |
34ca3b52 | 8 | |
f0f5567b | 9 | const utils = require('./utils') |
34ca3b52 | 10 | |
9f10b292 | 11 | describe('Test parameters validator', function () { |
0c1cbbfe | 12 | let server = null |
34ca3b52 | 13 | |
67100f1f | 14 | function makePostRequest (path, token, fields, attaches, done, fail) { |
bc503c2a | 15 | let statusCode = 400 |
67100f1f | 16 | if (fail !== undefined && fail === false) statusCode = 204 |
34ca3b52 | 17 | |
0c1cbbfe | 18 | const req = request(server.url) |
9f10b292 C |
19 | .post(path) |
20 | .set('Accept', 'application/json') | |
34ca3b52 | 21 | |
0c1cbbfe C |
22 | if (token) req.set('Authorization', 'Bearer ' + token) |
23 | ||
9f10b292 | 24 | Object.keys(fields).forEach(function (field) { |
f0f5567b | 25 | const value = fields[field] |
9f10b292 | 26 | req.field(field, value) |
ee66c593 C |
27 | }) |
28 | ||
67100f1f C |
29 | Object.keys(attaches).forEach(function (attach) { |
30 | const value = attaches[attach] | |
31 | req.attach(attach, value) | |
32 | }) | |
33 | ||
bc503c2a | 34 | req.expect(statusCode, done) |
9f10b292 C |
35 | } |
36 | ||
37 | function makePostBodyRequest (path, fields, done, fail) { | |
bc503c2a C |
38 | let statusCode = 400 |
39 | if (fail !== undefined && fail === false) statusCode = 200 | |
9f10b292 | 40 | |
0c1cbbfe | 41 | request(server.url) |
9f10b292 C |
42 | .post(path) |
43 | .set('Accept', 'application/json') | |
44 | .send(fields) | |
bc503c2a | 45 | .expect(statusCode, done) |
9f10b292 C |
46 | } |
47 | ||
48 | // --------------------------------------------------------------- | |
49 | ||
50 | before(function (done) { | |
51 | this.timeout(20000) | |
52 | ||
53 | async.series([ | |
54 | function (next) { | |
55 | utils.flushTests(next) | |
56 | }, | |
57 | function (next) { | |
0c1cbbfe C |
58 | utils.runServer(1, function (server1) { |
59 | server = server1 | |
60 | ||
61 | next() | |
62 | }) | |
63 | }, | |
64 | function (next) { | |
65 | utils.loginAndGetAccessToken(server, function (err, token) { | |
66 | if (err) throw err | |
b6c6f935 | 67 | server.accessToken = token |
0c1cbbfe | 68 | |
9f10b292 | 69 | next() |
34ca3b52 | 70 | }) |
9f10b292 C |
71 | } |
72 | ], done) | |
73 | }) | |
74 | ||
75 | describe('Of the pods API', function () { | |
f0f5567b | 76 | const path = '/api/v1/pods/' |
9f10b292 C |
77 | |
78 | describe('When adding a pod', function () { | |
79 | it('Should fail with nothing', function (done) { | |
f0f5567b | 80 | const data = {} |
9f10b292 C |
81 | makePostBodyRequest(path, data, done) |
82 | }) | |
34ca3b52 | 83 | |
9f10b292 | 84 | it('Should fail without public key', function (done) { |
f0f5567b | 85 | const data = { |
9f10b292 C |
86 | data: { |
87 | url: 'http://coucou.com' | |
34ca3b52 | 88 | } |
9f10b292 C |
89 | } |
90 | makePostBodyRequest(path, data, done) | |
91 | }) | |
34ca3b52 | 92 | |
9f10b292 | 93 | it('Should fail without an url', function (done) { |
f0f5567b | 94 | const data = { |
9f10b292 C |
95 | data: { |
96 | publicKey: 'mysuperpublickey' | |
34ca3b52 | 97 | } |
9f10b292 C |
98 | } |
99 | makePostBodyRequest(path, data, done) | |
100 | }) | |
34ca3b52 | 101 | |
9f10b292 | 102 | it('Should fail with an incorrect url', function (done) { |
f0f5567b | 103 | const data = { |
9f10b292 C |
104 | data: { |
105 | url: 'coucou.com', | |
106 | publicKey: 'mysuperpublickey' | |
34ca3b52 | 107 | } |
9f10b292 C |
108 | } |
109 | makePostBodyRequest(path, data, function () { | |
110 | data.data.url = 'http://coucou' | |
34ca3b52 | 111 | makePostBodyRequest(path, data, function () { |
9f10b292 C |
112 | data.data.url = 'coucou' |
113 | makePostBodyRequest(path, data, done) | |
34ca3b52 C |
114 | }) |
115 | }) | |
9f10b292 | 116 | }) |
34ca3b52 | 117 | |
9f10b292 | 118 | it('Should succeed with the correct parameters', function (done) { |
f0f5567b | 119 | const data = { |
9f10b292 C |
120 | data: { |
121 | url: 'http://coucou.com', | |
122 | publicKey: 'mysuperpublickey' | |
34ca3b52 | 123 | } |
9f10b292 C |
124 | } |
125 | makePostBodyRequest(path, data, done, false) | |
34ca3b52 C |
126 | }) |
127 | }) | |
9f10b292 | 128 | }) |
34ca3b52 | 129 | |
9f10b292 | 130 | describe('Of the videos API', function () { |
f0f5567b | 131 | const path = '/api/v1/videos/' |
34ca3b52 | 132 | |
a877d5ac C |
133 | describe('When listing a video', function () { |
134 | it('Should fail with a bad start pagination', function (done) { | |
135 | request(server.url) | |
136 | .get(path) | |
137 | .query({ start: 'hello' }) | |
138 | .set('Accept', 'application/json') | |
139 | .expect(400, done) | |
140 | }) | |
141 | ||
142 | it('Should fail with a bad count pagination', function (done) { | |
143 | request(server.url) | |
144 | .get(path) | |
145 | .query({ count: 'hello' }) | |
146 | .set('Accept', 'application/json') | |
147 | .expect(400, done) | |
148 | }) | |
149 | ||
150 | it('Should fail with an incorrect sort', function (done) { | |
151 | request(server.url) | |
152 | .get(path) | |
153 | .query({ sort: 'hello' }) | |
154 | .set('Accept', 'application/json') | |
155 | .expect(400, done) | |
156 | }) | |
157 | }) | |
158 | ||
9f10b292 C |
159 | describe('When searching a video', function () { |
160 | it('Should fail with nothing', function (done) { | |
0c1cbbfe | 161 | request(server.url) |
9f10b292 C |
162 | .get(pathUtils.join(path, 'search')) |
163 | .set('Accept', 'application/json') | |
164 | .expect(400, done) | |
34ca3b52 | 165 | }) |
a877d5ac C |
166 | |
167 | it('Should fail with a bad start pagination', function (done) { | |
168 | request(server.url) | |
169 | .get(pathUtils.join(path, 'search', 'test')) | |
170 | .query({ start: 'hello' }) | |
171 | .set('Accept', 'application/json') | |
172 | .expect(400, done) | |
173 | }) | |
174 | ||
175 | it('Should fail with a bad count pagination', function (done) { | |
176 | request(server.url) | |
177 | .get(pathUtils.join(path, 'search', 'test')) | |
178 | .query({ count: 'hello' }) | |
179 | .set('Accept', 'application/json') | |
180 | .expect(400, done) | |
181 | }) | |
182 | ||
183 | it('Should fail with an incorrect sort', function (done) { | |
184 | request(server.url) | |
185 | .get(pathUtils.join(path, 'search', 'test')) | |
186 | .query({ sort: 'hello' }) | |
187 | .set('Accept', 'application/json') | |
188 | .expect(400, done) | |
189 | }) | |
9f10b292 | 190 | }) |
34ca3b52 | 191 | |
9f10b292 C |
192 | describe('When adding a video', function () { |
193 | it('Should fail with nothing', function (done) { | |
f0f5567b C |
194 | const data = {} |
195 | const attach = {} | |
b6c6f935 | 196 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 197 | }) |
34ca3b52 | 198 | |
9f10b292 | 199 | it('Should fail without name', function (done) { |
f0f5567b | 200 | const data = { |
9f10b292 C |
201 | description: 'my super description' |
202 | } | |
f0f5567b | 203 | const attach = { |
8c9c1942 | 204 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
9f10b292 | 205 | } |
b6c6f935 | 206 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 207 | }) |
34ca3b52 | 208 | |
9f10b292 | 209 | it('Should fail with a long name', function (done) { |
f0f5567b | 210 | const data = { |
9f10b292 C |
211 | name: 'My very very very very very very very very very very very very very very very very long name', |
212 | description: 'my super description' | |
213 | } | |
f0f5567b | 214 | const attach = { |
8c9c1942 | 215 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
9f10b292 | 216 | } |
b6c6f935 | 217 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 218 | }) |
34ca3b52 | 219 | |
9f10b292 | 220 | it('Should fail without description', function (done) { |
f0f5567b | 221 | const data = { |
9f10b292 C |
222 | name: 'my super name' |
223 | } | |
f0f5567b | 224 | const attach = { |
8c9c1942 | 225 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
9f10b292 | 226 | } |
b6c6f935 | 227 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 228 | }) |
34ca3b52 | 229 | |
9f10b292 | 230 | it('Should fail with a long description', function (done) { |
f0f5567b | 231 | const data = { |
9f10b292 C |
232 | name: 'my super name', |
233 | description: 'my super description which is very very very very very very very very very very very very very very' + | |
234 | 'very very very very very very very very very very very very very very very very very very very very very' + | |
235 | 'very very very very very very very very very very very very very very very long' | |
236 | } | |
f0f5567b | 237 | const attach = { |
8c9c1942 | 238 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
9f10b292 | 239 | } |
b6c6f935 | 240 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 241 | }) |
34ca3b52 | 242 | |
9f10b292 | 243 | it('Should fail without an input file', function (done) { |
f0f5567b | 244 | const data = { |
9f10b292 C |
245 | name: 'my super name', |
246 | description: 'my super description' | |
247 | } | |
f0f5567b | 248 | const attach = {} |
b6c6f935 | 249 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 250 | }) |
34ca3b52 | 251 | |
9f10b292 | 252 | it('Should fail without an incorrect input file', function (done) { |
f0f5567b | 253 | const data = { |
9f10b292 C |
254 | name: 'my super name', |
255 | description: 'my super description' | |
256 | } | |
f0f5567b | 257 | const attach = { |
67100f1f C |
258 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm') |
259 | } | |
260 | makePostRequest(path, server.accessToken, data, attach, done) | |
261 | }) | |
262 | ||
263 | it('Should fail with a too big duration', function (done) { | |
264 | const data = { | |
265 | name: 'my super name', | |
266 | description: 'my super description' | |
267 | } | |
268 | const attach = { | |
269 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm') | |
9f10b292 | 270 | } |
b6c6f935 | 271 | makePostRequest(path, server.accessToken, data, attach, done) |
9f10b292 | 272 | }) |
34ca3b52 | 273 | |
9f10b292 | 274 | it('Should succeed with the correct parameters', function (done) { |
f0f5567b | 275 | const data = { |
9f10b292 C |
276 | name: 'my super name', |
277 | description: 'my super description' | |
278 | } | |
f0f5567b | 279 | const attach = { |
8c9c1942 | 280 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
9f10b292 | 281 | } |
b6c6f935 | 282 | makePostRequest(path, server.accessToken, data, attach, function () { |
8c9c1942 | 283 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') |
b6c6f935 | 284 | makePostRequest(path, server.accessToken, data, attach, function () { |
8c9c1942 | 285 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') |
67100f1f C |
286 | makePostRequest(path, server.accessToken, data, attach, done, false) |
287 | }, false) | |
288 | }, false) | |
34ca3b52 | 289 | }) |
9f10b292 | 290 | }) |
34ca3b52 | 291 | |
9f10b292 C |
292 | describe('When getting a video', function () { |
293 | it('Should return the list of the videos with nothing', function (done) { | |
0c1cbbfe | 294 | request(server.url) |
9f10b292 C |
295 | .get(path) |
296 | .set('Accept', 'application/json') | |
297 | .expect(200) | |
298 | .expect('Content-Type', /json/) | |
299 | .end(function (err, res) { | |
300 | if (err) throw err | |
34ca3b52 | 301 | |
9f10b292 | 302 | expect(res.body).to.be.an('array') |
67100f1f | 303 | expect(res.body.length).to.equal(3) |
34ca3b52 | 304 | |
9f10b292 C |
305 | done() |
306 | }) | |
307 | }) | |
34ca3b52 | 308 | |
9f10b292 | 309 | it('Should fail without a mongodb id', function (done) { |
0c1cbbfe | 310 | request(server.url) |
9f10b292 C |
311 | .get(path + 'coucou') |
312 | .set('Accept', 'application/json') | |
313 | .expect(400, done) | |
34ca3b52 C |
314 | }) |
315 | ||
9f10b292 | 316 | it('Should return 404 with an incorrect video', function (done) { |
0c1cbbfe | 317 | request(server.url) |
9f10b292 C |
318 | .get(path + '123456789012345678901234') |
319 | .set('Accept', 'application/json') | |
34ca3b52 | 320 | .expect(404, done) |
34ca3b52 | 321 | }) |
9f10b292 C |
322 | |
323 | it('Should succeed with the correct parameters') | |
34ca3b52 C |
324 | }) |
325 | ||
9f10b292 C |
326 | describe('When removing a video', function () { |
327 | it('Should have 404 with nothing', function (done) { | |
0c1cbbfe C |
328 | request(server.url) |
329 | .delete(path) | |
b6c6f935 | 330 | .set('Authorization', 'Bearer ' + server.accessToken) |
0c1cbbfe | 331 | .expect(400, done) |
34ca3b52 C |
332 | }) |
333 | ||
9f10b292 | 334 | it('Should fail without a mongodb id', function (done) { |
0c1cbbfe | 335 | request(server.url) |
9f10b292 | 336 | .delete(path + 'hello') |
b6c6f935 | 337 | .set('Authorization', 'Bearer ' + server.accessToken) |
9f10b292 | 338 | .expect(400, done) |
34ca3b52 C |
339 | }) |
340 | ||
9f10b292 | 341 | it('Should fail with a video which does not exist', function (done) { |
0c1cbbfe | 342 | request(server.url) |
9f10b292 | 343 | .delete(path + '123456789012345678901234') |
b6c6f935 | 344 | .set('Authorization', 'Bearer ' + server.accessToken) |
9f10b292 | 345 | .expect(404, done) |
34ca3b52 | 346 | }) |
9f10b292 C |
347 | |
348 | it('Should fail with a video of another pod') | |
349 | ||
350 | it('Should succeed with the correct parameters') | |
34ca3b52 | 351 | }) |
9f10b292 | 352 | }) |
34ca3b52 | 353 | |
9f10b292 C |
354 | describe('Of the remote videos API', function () { |
355 | describe('When making a secure request', function () { | |
356 | it('Should check a secure request') | |
357 | }) | |
34ca3b52 | 358 | |
9f10b292 C |
359 | describe('When adding a video', function () { |
360 | it('Should check when adding a video') | |
34ca3b52 | 361 | }) |
9f10b292 C |
362 | |
363 | describe('When removing a video', function () { | |
364 | it('Should check when removing a video') | |
365 | }) | |
366 | }) | |
367 | ||
368 | after(function (done) { | |
0c1cbbfe | 369 | process.kill(-server.app.pid) |
9f10b292 C |
370 | |
371 | // Keep the logs if the test failed | |
372 | if (this.ok) { | |
373 | utils.flushTests(done) | |
374 | } else { | |
375 | done() | |
376 | } | |
34ca3b52 | 377 | }) |
9f10b292 | 378 | }) |