]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.js
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')
9 const utils
= require('./utils')
11 describe('Test parameters validator', function () {
14 function makePostRequest (path
, token
, fields
, attaches
, done
, fail
) {
16 if (fail
!== undefined && fail
=== false) statusCode
= 204
18 const req
= request(server
.url
)
20 .set('Accept', 'application/json')
22 if (token
) req
.set('Authorization', 'Bearer ' + token
)
24 Object
.keys(fields
).forEach(function (field
) {
25 const value
= fields
[field
]
27 if (Array
.isArray(value
)) {
28 for (let i
= 0; i
< value
.length
; i
++) {
29 req
.field(field
+ '[' + i
+ ']', value
[i
])
32 req
.field(field
, value
)
36 Object
.keys(attaches
).forEach(function (attach
) {
37 const value
= attaches
[attach
]
38 req
.attach(attach
, value
)
41 req
.expect(statusCode
, done
)
44 function makePostBodyRequest (path
, fields
, done
, fail
) {
46 if (fail
!== undefined && fail
=== false) statusCode
= 200
50 .set('Accept', 'application/json')
52 .expect(statusCode
, done
)
55 // ---------------------------------------------------------------
57 before(function (done
) {
62 utils
.flushTests(next
)
65 utils
.runServer(1, function (server1
) {
72 utils
.loginAndGetAccessToken(server
, function (err
, token
) {
74 server
.accessToken
= token
82 describe('Of the pods API', function () {
83 const path
= '/api/v1/pods/'
85 describe('When adding a pod', function () {
86 it('Should fail with nothing', function (done
) {
88 makePostBodyRequest(path
, data
, done
)
91 it('Should fail without public key', function (done
) {
94 url: 'http://coucou.com'
97 makePostBodyRequest(path
, data
, done
)
100 it('Should fail without an url', function (done
) {
103 publicKey: 'mysuperpublickey'
106 makePostBodyRequest(path
, data
, done
)
109 it('Should fail with an incorrect url', function (done
) {
113 publicKey: 'mysuperpublickey'
116 makePostBodyRequest(path
, data
, function () {
117 data
.data
.url
= 'http://coucou'
118 makePostBodyRequest(path
, data
, function () {
119 data
.data
.url
= 'coucou'
120 makePostBodyRequest(path
, data
, done
)
125 it('Should succeed with the correct parameters', function (done
) {
128 url: 'http://coucou.com',
129 publicKey: 'mysuperpublickey'
132 makePostBodyRequest(path
, data
, done
, false)
137 describe('Of the videos API', function () {
138 const path
= '/api/v1/videos/'
140 describe('When listing a video', function () {
141 it('Should fail with a bad start pagination', function (done
) {
144 .query({ start: 'hello' })
145 .set('Accept', 'application/json')
149 it('Should fail with a bad count pagination', function (done
) {
152 .query({ count: 'hello' })
153 .set('Accept', 'application/json')
157 it('Should fail with an incorrect sort', function (done
) {
160 .query({ sort: 'hello' })
161 .set('Accept', 'application/json')
166 describe('When searching a video', function () {
167 it('Should fail with nothing', function (done
) {
169 .get(pathUtils
.join(path
, 'search'))
170 .set('Accept', 'application/json')
174 it('Should fail with a bad start pagination', function (done
) {
176 .get(pathUtils
.join(path
, 'search', 'test'))
177 .query({ start: 'hello' })
178 .set('Accept', 'application/json')
182 it('Should fail with a bad count pagination', function (done
) {
184 .get(pathUtils
.join(path
, 'search', 'test'))
185 .query({ count: 'hello' })
186 .set('Accept', 'application/json')
190 it('Should fail with an incorrect sort', function (done
) {
192 .get(pathUtils
.join(path
, 'search', 'test'))
193 .query({ sort: 'hello' })
194 .set('Accept', 'application/json')
199 describe('When adding a video', function () {
200 it('Should fail with nothing', function (done
) {
203 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
206 it('Should fail without name', function (done
) {
208 description: 'my super description',
209 tags: [ 'tag1', 'tag2' ]
212 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
214 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
217 it('Should fail with a long name', function (done
) {
219 name: 'My very very very very very very very very very very very very very very very very long name',
220 description: 'my super description',
221 tags: [ 'tag1', 'tag2' ]
224 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
226 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
229 it('Should fail without description', function (done
) {
231 name: 'my super name',
232 tags: [ 'tag1', 'tag2' ]
235 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
237 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
240 it('Should fail with a long description', function (done
) {
242 name: 'my super name',
243 description: 'my super description which is very very very very very very very very very very very very very very' +
244 'very very very very very very very very very very very very very very very very very very very very very' +
245 'very very very very very very very very very very very very very very very long',
246 tags: [ 'tag1', 'tag2' ]
249 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
251 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
254 it('Should fail without tags', function (done
) {
256 name: 'my super name',
257 description: 'my super description'
260 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
262 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
265 it('Should fail with too many tags', function (done
) {
267 name: 'my super name',
268 description: 'my super description',
269 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
272 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
274 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
277 it('Should fail with not enough tags', function (done
) {
279 name: 'my super name',
280 description: 'my super description',
284 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
286 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
289 it('Should fail with a tag length too low', function (done
) {
291 name: 'my super name',
292 description: 'my super description',
293 tags: [ 'tag1', 't' ]
296 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
298 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
301 it('Should fail with a tag length too big', function (done
) {
303 name: 'my super name',
304 description: 'my super description',
305 tags: [ 'mysupertagtoolong', 'tag1' ]
308 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
310 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
313 it('Should fail with malformed tags', function (done
) {
315 name: 'my super name',
316 description: 'my super description',
320 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
322 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
325 it('Should fail without an input file', function (done
) {
327 name: 'my super name',
328 description: 'my super description',
329 tags: [ 'tag1', 'tag2' ]
332 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
335 it('Should fail without an incorrect input file', function (done
) {
337 name: 'my super name',
338 description: 'my super description',
339 tags: [ 'tag1', 'tag2' ]
342 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
344 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
347 it('Should fail with a too big duration', function (done
) {
349 name: 'my super name',
350 description: 'my super description',
351 tags: [ 'tag1', 'tag2' ]
354 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
356 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
359 it('Should succeed with the correct parameters', function (done
) {
361 name: 'my super name',
362 description: 'my super description',
363 tags: [ 'tag1', 'tag2' ]
366 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
368 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
369 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
370 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
371 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
372 makePostRequest(path
, server
.accessToken
, data
, attach
, done
, false)
378 describe('When getting a video', function () {
379 it('Should return the list of the videos with nothing', function (done
) {
382 .set('Accept', 'application/json')
384 .expect('Content-Type', /json/)
385 .end(function (err
, res
) {
388 expect(res
.body
.data
).to
.be
.an('array')
389 expect(res
.body
.data
.length
).to
.equal(3)
395 it('Should fail without a mongodb id', function (done
) {
397 .get(path
+ 'coucou')
398 .set('Accept', 'application/json')
402 it('Should return 404 with an incorrect video', function (done
) {
404 .get(path
+ '123456789012345678901234')
405 .set('Accept', 'application/json')
409 it('Should succeed with the correct parameters')
412 describe('When removing a video', function () {
413 it('Should have 404 with nothing', function (done
) {
416 .set('Authorization', 'Bearer ' + server
.accessToken
)
420 it('Should fail without a mongodb id', function (done
) {
422 .delete(path
+ 'hello')
423 .set('Authorization', 'Bearer ' + server
.accessToken
)
427 it('Should fail with a video which does not exist', function (done
) {
429 .delete(path
+ '123456789012345678901234')
430 .set('Authorization', 'Bearer ' + server
.accessToken
)
434 it('Should fail with a video of another pod')
436 it('Should succeed with the correct parameters')
440 describe('Of the remote videos API', function () {
441 describe('When making a secure request', function () {
442 it('Should check a secure request')
445 describe('When adding a video', function () {
446 it('Should check when adding a video')
449 describe('When removing a video', function () {
450 it('Should check when removing a video')
454 after(function (done
) {
455 process
.kill(-server
.app
.pid
)
457 // Keep the logs if the test failed
459 utils
.flushTests(done
)