]>
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
) {
93 url: 'http://coucou.com'
95 makePostBodyRequest(path
, data
, done
)
98 it('Should fail without an url', function (done
) {
100 publicKey: 'mysuperpublickey'
102 makePostBodyRequest(path
, data
, done
)
105 it('Should fail with an incorrect url', function (done
) {
108 publicKey: 'mysuperpublickey'
110 makePostBodyRequest(path
, data
, function () {
111 data
.url
= 'http://coucou'
112 makePostBodyRequest(path
, data
, function () {
114 makePostBodyRequest(path
, data
, done
)
119 it('Should succeed with the correct parameters', function (done
) {
121 url: 'http://coucou.com',
122 publicKey: 'mysuperpublickey'
124 makePostBodyRequest(path
, data
, done
, false)
129 describe('Of the videos API', function () {
130 const path
= '/api/v1/videos/'
132 describe('When listing a video', function () {
133 it('Should fail with a bad start pagination', function (done
) {
136 .query({ start: 'hello' })
137 .set('Accept', 'application/json')
141 it('Should fail with a bad count pagination', function (done
) {
144 .query({ count: 'hello' })
145 .set('Accept', 'application/json')
149 it('Should fail with an incorrect sort', function (done
) {
152 .query({ sort: 'hello' })
153 .set('Accept', 'application/json')
158 describe('When searching a video', function () {
159 it('Should fail with nothing', function (done
) {
161 .get(pathUtils
.join(path
, 'search'))
162 .set('Accept', 'application/json')
166 it('Should fail with a bad start pagination', function (done
) {
168 .get(pathUtils
.join(path
, 'search', 'test'))
169 .query({ start: 'hello' })
170 .set('Accept', 'application/json')
174 it('Should fail with a bad count pagination', function (done
) {
176 .get(pathUtils
.join(path
, 'search', 'test'))
177 .query({ count: 'hello' })
178 .set('Accept', 'application/json')
182 it('Should fail with an incorrect sort', function (done
) {
184 .get(pathUtils
.join(path
, 'search', 'test'))
185 .query({ sort: 'hello' })
186 .set('Accept', 'application/json')
191 describe('When adding a video', function () {
192 it('Should fail with nothing', function (done
) {
195 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
198 it('Should fail without name', function (done
) {
200 description: 'my super description',
201 tags: [ 'tag1', 'tag2' ]
204 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
206 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
209 it('Should fail with a long name', function (done
) {
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 tags: [ 'tag1', 'tag2' ]
216 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
218 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
221 it('Should fail without description', function (done
) {
223 name: 'my super name',
224 tags: [ 'tag1', 'tag2' ]
227 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
229 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
232 it('Should fail with a long description', function (done
) {
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' +
237 'very very very very very very very very very very very very very very very long',
238 tags: [ 'tag1', 'tag2' ]
241 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
243 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
246 it('Should fail without tags', function (done
) {
248 name: 'my super name',
249 description: 'my super description'
252 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
254 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
257 it('Should fail with too many tags', function (done
) {
259 name: 'my super name',
260 description: 'my super description',
261 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
264 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
266 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
269 it('Should fail with not enough tags', function (done
) {
271 name: 'my super name',
272 description: 'my super description',
276 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
278 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
281 it('Should fail with a tag length too low', function (done
) {
283 name: 'my super name',
284 description: 'my super description',
285 tags: [ 'tag1', 't' ]
288 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
290 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
293 it('Should fail with a tag length too big', function (done
) {
295 name: 'my super name',
296 description: 'my super description',
297 tags: [ 'mysupertagtoolong', 'tag1' ]
300 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
302 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
305 it('Should fail with malformed tags', function (done
) {
307 name: 'my super name',
308 description: 'my super description',
312 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
314 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
317 it('Should fail without an input file', function (done
) {
319 name: 'my super name',
320 description: 'my super description',
321 tags: [ 'tag1', 'tag2' ]
324 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
327 it('Should fail without an incorrect input file', function (done
) {
329 name: 'my super name',
330 description: 'my super description',
331 tags: [ 'tag1', 'tag2' ]
334 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
336 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
339 it('Should fail with a too big duration', function (done
) {
341 name: 'my super name',
342 description: 'my super description',
343 tags: [ 'tag1', 'tag2' ]
346 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
348 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
351 it('Should succeed with the correct parameters', function (done
) {
353 name: 'my super name',
354 description: 'my super description',
355 tags: [ 'tag1', 'tag2' ]
358 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
360 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
361 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
362 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
363 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
364 makePostRequest(path
, server
.accessToken
, data
, attach
, done
, false)
370 describe('When getting a video', function () {
371 it('Should return the list of the videos with nothing', function (done
) {
374 .set('Accept', 'application/json')
376 .expect('Content-Type', /json/)
377 .end(function (err
, res
) {
380 expect(res
.body
.data
).to
.be
.an('array')
381 expect(res
.body
.data
.length
).to
.equal(3)
387 it('Should fail without a mongodb id', function (done
) {
389 .get(path
+ 'coucou')
390 .set('Accept', 'application/json')
394 it('Should return 404 with an incorrect video', function (done
) {
396 .get(path
+ '123456789012345678901234')
397 .set('Accept', 'application/json')
401 it('Should succeed with the correct parameters')
404 describe('When removing a video', function () {
405 it('Should have 404 with nothing', function (done
) {
408 .set('Authorization', 'Bearer ' + server
.accessToken
)
412 it('Should fail without a mongodb id', function (done
) {
414 .delete(path
+ 'hello')
415 .set('Authorization', 'Bearer ' + server
.accessToken
)
419 it('Should fail with a video which does not exist', function (done
) {
421 .delete(path
+ '123456789012345678901234')
422 .set('Authorization', 'Bearer ' + server
.accessToken
)
426 it('Should fail with a video of another pod')
428 it('Should succeed with the correct parameters')
432 describe('Of the remote videos API', function () {
433 describe('When making a secure request', function () {
434 it('Should check a secure request')
437 describe('When adding a video', function () {
438 it('Should check when adding a video')
441 describe('When removing a video', function () {
442 it('Should check when removing a video')
446 after(function (done
) {
447 process
.kill(-server
.app
.pid
)
449 // Keep the logs if the test failed
451 utils
.flushTests(done
)