]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Server: udpate async to 2.0.0
[github/Chocobozzz/PeerTube.git] / server / tests / api / checkParams.js
CommitLineData
9f10b292 1'use strict'
34ca3b52 2
f0f5567b
C
3const chai = require('chai')
4const expect = chai.expect
5const pathUtils = require('path')
6const request = require('supertest')
1a42c9e2 7const series = require('async/series')
34ca3b52 8
f0f5567b 9const utils = require('./utils')
34ca3b52 10
9f10b292 11describe('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]
be587647
C
26
27 if (Array.isArray(value)) {
28 for (let i = 0; i < value.length; i++) {
29 req.field(field + '[' + i + ']', value[i])
30 }
31 } else {
32 req.field(field, value)
33 }
ee66c593
C
34 })
35
67100f1f
C
36 Object.keys(attaches).forEach(function (attach) {
37 const value = attaches[attach]
38 req.attach(attach, value)
39 })
40
bc503c2a 41 req.expect(statusCode, done)
9f10b292
C
42 }
43
44 function makePostBodyRequest (path, fields, done, fail) {
bc503c2a
C
45 let statusCode = 400
46 if (fail !== undefined && fail === false) statusCode = 200
9f10b292 47
0c1cbbfe 48 request(server.url)
9f10b292
C
49 .post(path)
50 .set('Accept', 'application/json')
51 .send(fields)
bc503c2a 52 .expect(statusCode, done)
9f10b292
C
53 }
54
55 // ---------------------------------------------------------------
56
57 before(function (done) {
58 this.timeout(20000)
59
1a42c9e2 60 series([
9f10b292
C
61 function (next) {
62 utils.flushTests(next)
63 },
64 function (next) {
0c1cbbfe
C
65 utils.runServer(1, function (server1) {
66 server = server1
67
68 next()
69 })
70 },
71 function (next) {
72 utils.loginAndGetAccessToken(server, function (err, token) {
73 if (err) throw err
b6c6f935 74 server.accessToken = token
0c1cbbfe 75
9f10b292 76 next()
34ca3b52 77 })
9f10b292
C
78 }
79 ], done)
80 })
81
82 describe('Of the pods API', function () {
f0f5567b 83 const path = '/api/v1/pods/'
9f10b292
C
84
85 describe('When adding a pod', function () {
86 it('Should fail with nothing', function (done) {
f0f5567b 87 const data = {}
9f10b292
C
88 makePostBodyRequest(path, data, done)
89 })
34ca3b52 90
9f10b292 91 it('Should fail without public key', function (done) {
f0f5567b 92 const data = {
528a9efa 93 url: 'http://coucou.com'
9f10b292
C
94 }
95 makePostBodyRequest(path, data, done)
96 })
34ca3b52 97
9f10b292 98 it('Should fail without an url', function (done) {
f0f5567b 99 const data = {
528a9efa 100 publicKey: 'mysuperpublickey'
9f10b292
C
101 }
102 makePostBodyRequest(path, data, done)
103 })
34ca3b52 104
9f10b292 105 it('Should fail with an incorrect url', function (done) {
f0f5567b 106 const data = {
528a9efa
C
107 url: 'coucou.com',
108 publicKey: 'mysuperpublickey'
9f10b292
C
109 }
110 makePostBodyRequest(path, data, function () {
528a9efa 111 data.url = 'http://coucou'
34ca3b52 112 makePostBodyRequest(path, data, function () {
528a9efa 113 data.url = 'coucou'
9f10b292 114 makePostBodyRequest(path, data, done)
34ca3b52
C
115 })
116 })
9f10b292 117 })
34ca3b52 118
9f10b292 119 it('Should succeed with the correct parameters', function (done) {
f0f5567b 120 const data = {
528a9efa
C
121 url: 'http://coucou.com',
122 publicKey: 'mysuperpublickey'
9f10b292
C
123 }
124 makePostBodyRequest(path, data, done, false)
34ca3b52
C
125 })
126 })
9f10b292 127 })
34ca3b52 128
9f10b292 129 describe('Of the videos API', function () {
f0f5567b 130 const path = '/api/v1/videos/'
34ca3b52 131
a877d5ac
C
132 describe('When listing a video', function () {
133 it('Should fail with a bad start pagination', function (done) {
134 request(server.url)
135 .get(path)
136 .query({ start: 'hello' })
137 .set('Accept', 'application/json')
138 .expect(400, done)
139 })
140
141 it('Should fail with a bad count pagination', function (done) {
142 request(server.url)
143 .get(path)
144 .query({ count: 'hello' })
145 .set('Accept', 'application/json')
146 .expect(400, done)
147 })
148
149 it('Should fail with an incorrect sort', function (done) {
150 request(server.url)
151 .get(path)
152 .query({ sort: 'hello' })
153 .set('Accept', 'application/json')
154 .expect(400, done)
155 })
156 })
157
9f10b292
C
158 describe('When searching a video', function () {
159 it('Should fail with nothing', function (done) {
0c1cbbfe 160 request(server.url)
9f10b292
C
161 .get(pathUtils.join(path, 'search'))
162 .set('Accept', 'application/json')
163 .expect(400, done)
34ca3b52 164 })
a877d5ac
C
165
166 it('Should fail with a bad start pagination', function (done) {
167 request(server.url)
168 .get(pathUtils.join(path, 'search', 'test'))
169 .query({ start: 'hello' })
170 .set('Accept', 'application/json')
171 .expect(400, done)
172 })
173
174 it('Should fail with a bad count pagination', function (done) {
175 request(server.url)
176 .get(pathUtils.join(path, 'search', 'test'))
177 .query({ count: 'hello' })
178 .set('Accept', 'application/json')
179 .expect(400, done)
180 })
181
182 it('Should fail with an incorrect sort', function (done) {
183 request(server.url)
184 .get(pathUtils.join(path, 'search', 'test'))
185 .query({ sort: 'hello' })
186 .set('Accept', 'application/json')
187 .expect(400, done)
188 })
9f10b292 189 })
34ca3b52 190
9f10b292
C
191 describe('When adding a video', function () {
192 it('Should fail with nothing', function (done) {
f0f5567b
C
193 const data = {}
194 const attach = {}
b6c6f935 195 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 196 })
34ca3b52 197
9f10b292 198 it('Should fail without name', function (done) {
f0f5567b 199 const data = {
be587647
C
200 description: 'my super description',
201 tags: [ 'tag1', 'tag2' ]
9f10b292 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 211 name: 'My very very very very very very very very very very very very very very very very long name',
be587647
C
212 description: 'my super description',
213 tags: [ 'tag1', 'tag2' ]
9f10b292 214 }
f0f5567b 215 const attach = {
8c9c1942 216 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 217 }
b6c6f935 218 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 219 })
34ca3b52 220
9f10b292 221 it('Should fail without description', function (done) {
f0f5567b 222 const data = {
be587647
C
223 name: 'my super name',
224 tags: [ 'tag1', 'tag2' ]
9f10b292 225 }
f0f5567b 226 const attach = {
8c9c1942 227 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 228 }
b6c6f935 229 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 230 })
34ca3b52 231
9f10b292 232 it('Should fail with a long description', function (done) {
f0f5567b 233 const data = {
9f10b292
C
234 name: 'my super name',
235 description: 'my super description which is very very very very very very very very very very very very very very' +
236 'very very very very very very very very very very very very very very very very very very very very very' +
be587647
C
237 'very very very very very very very very very very very very very very very long',
238 tags: [ 'tag1', 'tag2' ]
9f10b292 239 }
f0f5567b 240 const attach = {
8c9c1942 241 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 242 }
b6c6f935 243 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 244 })
34ca3b52 245
be587647 246 it('Should fail without tags', function (done) {
f0f5567b 247 const data = {
9f10b292
C
248 name: 'my super name',
249 description: 'my super description'
250 }
be587647
C
251 const attach = {
252 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
253 }
254 makePostRequest(path, server.accessToken, data, attach, done)
255 })
256
257 it('Should fail with too many tags', function (done) {
258 const data = {
259 name: 'my super name',
260 description: 'my super description',
261 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
262 }
263 const attach = {
264 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
265 }
266 makePostRequest(path, server.accessToken, data, attach, done)
267 })
268
269 it('Should fail with not enough tags', function (done) {
270 const data = {
271 name: 'my super name',
272 description: 'my super description',
273 tags: [ ]
274 }
275 const attach = {
276 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
277 }
278 makePostRequest(path, server.accessToken, data, attach, done)
279 })
280
281 it('Should fail with a tag length too low', function (done) {
282 const data = {
283 name: 'my super name',
284 description: 'my super description',
285 tags: [ 'tag1', 't' ]
286 }
287 const attach = {
288 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
289 }
290 makePostRequest(path, server.accessToken, data, attach, done)
291 })
292
293 it('Should fail with a tag length too big', function (done) {
294 const data = {
295 name: 'my super name',
296 description: 'my super description',
297 tags: [ 'mysupertagtoolong', 'tag1' ]
298 }
299 const attach = {
300 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
301 }
302 makePostRequest(path, server.accessToken, data, attach, done)
303 })
304
305 it('Should fail with malformed tags', function (done) {
306 const data = {
307 name: 'my super name',
308 description: 'my super description',
309 tags: [ 'my tag' ]
310 }
311 const attach = {
312 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
313 }
314 makePostRequest(path, server.accessToken, data, attach, done)
315 })
316
317 it('Should fail without an input file', function (done) {
318 const data = {
319 name: 'my super name',
320 description: 'my super description',
321 tags: [ 'tag1', 'tag2' ]
322 }
f0f5567b 323 const attach = {}
b6c6f935 324 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 325 })
34ca3b52 326
9f10b292 327 it('Should fail without an incorrect input file', function (done) {
f0f5567b 328 const data = {
9f10b292 329 name: 'my super name',
be587647
C
330 description: 'my super description',
331 tags: [ 'tag1', 'tag2' ]
9f10b292 332 }
f0f5567b 333 const attach = {
67100f1f
C
334 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
335 }
336 makePostRequest(path, server.accessToken, data, attach, done)
337 })
338
339 it('Should fail with a too big duration', function (done) {
340 const data = {
341 name: 'my super name',
be587647
C
342 description: 'my super description',
343 tags: [ 'tag1', 'tag2' ]
67100f1f
C
344 }
345 const attach = {
346 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
9f10b292 347 }
b6c6f935 348 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 349 })
34ca3b52 350
9f10b292 351 it('Should succeed with the correct parameters', function (done) {
f0f5567b 352 const data = {
9f10b292 353 name: 'my super name',
be587647
C
354 description: 'my super description',
355 tags: [ 'tag1', 'tag2' ]
9f10b292 356 }
f0f5567b 357 const attach = {
8c9c1942 358 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 359 }
b6c6f935 360 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 361 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
b6c6f935 362 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 363 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
67100f1f
C
364 makePostRequest(path, server.accessToken, data, attach, done, false)
365 }, false)
366 }, false)
34ca3b52 367 })
9f10b292 368 })
34ca3b52 369
9f10b292
C
370 describe('When getting a video', function () {
371 it('Should return the list of the videos with nothing', function (done) {
0c1cbbfe 372 request(server.url)
9f10b292
C
373 .get(path)
374 .set('Accept', 'application/json')
375 .expect(200)
376 .expect('Content-Type', /json/)
377 .end(function (err, res) {
378 if (err) throw err
34ca3b52 379
68ce3ae0
C
380 expect(res.body.data).to.be.an('array')
381 expect(res.body.data.length).to.equal(3)
34ca3b52 382
9f10b292
C
383 done()
384 })
385 })
34ca3b52 386
9f10b292 387 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 388 request(server.url)
9f10b292
C
389 .get(path + 'coucou')
390 .set('Accept', 'application/json')
391 .expect(400, done)
34ca3b52
C
392 })
393
9f10b292 394 it('Should return 404 with an incorrect video', function (done) {
0c1cbbfe 395 request(server.url)
9f10b292
C
396 .get(path + '123456789012345678901234')
397 .set('Accept', 'application/json')
34ca3b52 398 .expect(404, done)
34ca3b52 399 })
9f10b292
C
400
401 it('Should succeed with the correct parameters')
34ca3b52
C
402 })
403
9f10b292
C
404 describe('When removing a video', function () {
405 it('Should have 404 with nothing', function (done) {
0c1cbbfe
C
406 request(server.url)
407 .delete(path)
b6c6f935 408 .set('Authorization', 'Bearer ' + server.accessToken)
0c1cbbfe 409 .expect(400, done)
34ca3b52
C
410 })
411
9f10b292 412 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 413 request(server.url)
9f10b292 414 .delete(path + 'hello')
b6c6f935 415 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 416 .expect(400, done)
34ca3b52
C
417 })
418
9f10b292 419 it('Should fail with a video which does not exist', function (done) {
0c1cbbfe 420 request(server.url)
9f10b292 421 .delete(path + '123456789012345678901234')
b6c6f935 422 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 423 .expect(404, done)
34ca3b52 424 })
9f10b292
C
425
426 it('Should fail with a video of another pod')
427
428 it('Should succeed with the correct parameters')
34ca3b52 429 })
9f10b292 430 })
34ca3b52 431
9f10b292
C
432 describe('Of the remote videos API', function () {
433 describe('When making a secure request', function () {
434 it('Should check a secure request')
435 })
34ca3b52 436
9f10b292
C
437 describe('When adding a video', function () {
438 it('Should check when adding a video')
34ca3b52 439 })
9f10b292
C
440
441 describe('When removing a video', function () {
442 it('Should check when removing a video')
443 })
444 })
445
446 after(function (done) {
0c1cbbfe 447 process.kill(-server.app.pid)
9f10b292
C
448
449 // Keep the logs if the test failed
450 if (this.ok) {
451 utils.flushTests(done)
452 } else {
453 done()
454 }
34ca3b52 455 })
9f10b292 456})