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