]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Add tag search support to server
[github/Chocobozzz/PeerTube.git] / server / tests / api / checkParams.js
CommitLineData
9f10b292 1'use strict'
34ca3b52 2
f0f5567b
C
3const async = require('async')
4const chai = require('chai')
5const expect = chai.expect
6const pathUtils = require('path')
7const request = require('supertest')
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
60 async.series([
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 = {
9f10b292
C
93 data: {
94 url: 'http://coucou.com'
34ca3b52 95 }
9f10b292
C
96 }
97 makePostBodyRequest(path, data, done)
98 })
34ca3b52 99
9f10b292 100 it('Should fail without an url', function (done) {
f0f5567b 101 const data = {
9f10b292
C
102 data: {
103 publicKey: 'mysuperpublickey'
34ca3b52 104 }
9f10b292
C
105 }
106 makePostBodyRequest(path, data, done)
107 })
34ca3b52 108
9f10b292 109 it('Should fail with an incorrect url', function (done) {
f0f5567b 110 const data = {
9f10b292
C
111 data: {
112 url: 'coucou.com',
113 publicKey: 'mysuperpublickey'
34ca3b52 114 }
9f10b292
C
115 }
116 makePostBodyRequest(path, data, function () {
117 data.data.url = 'http://coucou'
34ca3b52 118 makePostBodyRequest(path, data, function () {
9f10b292
C
119 data.data.url = 'coucou'
120 makePostBodyRequest(path, data, done)
34ca3b52
C
121 })
122 })
9f10b292 123 })
34ca3b52 124
9f10b292 125 it('Should succeed with the correct parameters', function (done) {
f0f5567b 126 const data = {
9f10b292
C
127 data: {
128 url: 'http://coucou.com',
129 publicKey: 'mysuperpublickey'
34ca3b52 130 }
9f10b292
C
131 }
132 makePostBodyRequest(path, data, done, false)
34ca3b52
C
133 })
134 })
9f10b292 135 })
34ca3b52 136
9f10b292 137 describe('Of the videos API', function () {
f0f5567b 138 const path = '/api/v1/videos/'
34ca3b52 139
a877d5ac
C
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
9f10b292
C
166 describe('When searching a video', function () {
167 it('Should fail with nothing', function (done) {
0c1cbbfe 168 request(server.url)
9f10b292
C
169 .get(pathUtils.join(path, 'search'))
170 .set('Accept', 'application/json')
171 .expect(400, done)
34ca3b52 172 })
a877d5ac
C
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 })
9f10b292 197 })
34ca3b52 198
9f10b292
C
199 describe('When adding a video', function () {
200 it('Should fail with nothing', function (done) {
f0f5567b
C
201 const data = {}
202 const attach = {}
b6c6f935 203 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 204 })
34ca3b52 205
9f10b292 206 it('Should fail without name', function (done) {
f0f5567b 207 const data = {
be587647
C
208 description: 'my super description',
209 tags: [ 'tag1', 'tag2' ]
9f10b292 210 }
f0f5567b 211 const attach = {
8c9c1942 212 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 213 }
b6c6f935 214 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 215 })
34ca3b52 216
9f10b292 217 it('Should fail with a long name', function (done) {
f0f5567b 218 const data = {
9f10b292 219 name: 'My very very very very very very very very very very very very very very very very long name',
be587647
C
220 description: 'my super description',
221 tags: [ 'tag1', 'tag2' ]
9f10b292 222 }
f0f5567b 223 const attach = {
8c9c1942 224 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 225 }
b6c6f935 226 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 227 })
34ca3b52 228
9f10b292 229 it('Should fail without description', function (done) {
f0f5567b 230 const data = {
be587647
C
231 name: 'my super name',
232 tags: [ 'tag1', 'tag2' ]
9f10b292 233 }
f0f5567b 234 const attach = {
8c9c1942 235 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 236 }
b6c6f935 237 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 238 })
34ca3b52 239
9f10b292 240 it('Should fail with a long description', function (done) {
f0f5567b 241 const data = {
9f10b292
C
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' +
be587647
C
245 'very very very very very very very very very very very very very very very long',
246 tags: [ 'tag1', 'tag2' ]
9f10b292 247 }
f0f5567b 248 const attach = {
8c9c1942 249 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 250 }
b6c6f935 251 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 252 })
34ca3b52 253
be587647 254 it('Should fail without tags', function (done) {
f0f5567b 255 const data = {
9f10b292
C
256 name: 'my super name',
257 description: 'my super description'
258 }
be587647
C
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 }
f0f5567b 331 const attach = {}
b6c6f935 332 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 333 })
34ca3b52 334
9f10b292 335 it('Should fail without an incorrect input file', function (done) {
f0f5567b 336 const data = {
9f10b292 337 name: 'my super name',
be587647
C
338 description: 'my super description',
339 tags: [ 'tag1', 'tag2' ]
9f10b292 340 }
f0f5567b 341 const attach = {
67100f1f
C
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',
be587647
C
350 description: 'my super description',
351 tags: [ 'tag1', 'tag2' ]
67100f1f
C
352 }
353 const attach = {
354 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
9f10b292 355 }
b6c6f935 356 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 357 })
34ca3b52 358
9f10b292 359 it('Should succeed with the correct parameters', function (done) {
f0f5567b 360 const data = {
9f10b292 361 name: 'my super name',
be587647
C
362 description: 'my super description',
363 tags: [ 'tag1', 'tag2' ]
9f10b292 364 }
f0f5567b 365 const attach = {
8c9c1942 366 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 367 }
b6c6f935 368 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 369 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
b6c6f935 370 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 371 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
67100f1f
C
372 makePostRequest(path, server.accessToken, data, attach, done, false)
373 }, false)
374 }, false)
34ca3b52 375 })
9f10b292 376 })
34ca3b52 377
9f10b292
C
378 describe('When getting a video', function () {
379 it('Should return the list of the videos with nothing', function (done) {
0c1cbbfe 380 request(server.url)
9f10b292
C
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
34ca3b52 387
68ce3ae0
C
388 expect(res.body.data).to.be.an('array')
389 expect(res.body.data.length).to.equal(3)
34ca3b52 390
9f10b292
C
391 done()
392 })
393 })
34ca3b52 394
9f10b292 395 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 396 request(server.url)
9f10b292
C
397 .get(path + 'coucou')
398 .set('Accept', 'application/json')
399 .expect(400, done)
34ca3b52
C
400 })
401
9f10b292 402 it('Should return 404 with an incorrect video', function (done) {
0c1cbbfe 403 request(server.url)
9f10b292
C
404 .get(path + '123456789012345678901234')
405 .set('Accept', 'application/json')
34ca3b52 406 .expect(404, done)
34ca3b52 407 })
9f10b292
C
408
409 it('Should succeed with the correct parameters')
34ca3b52
C
410 })
411
9f10b292
C
412 describe('When removing a video', function () {
413 it('Should have 404 with nothing', function (done) {
0c1cbbfe
C
414 request(server.url)
415 .delete(path)
b6c6f935 416 .set('Authorization', 'Bearer ' + server.accessToken)
0c1cbbfe 417 .expect(400, done)
34ca3b52
C
418 })
419
9f10b292 420 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 421 request(server.url)
9f10b292 422 .delete(path + 'hello')
b6c6f935 423 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 424 .expect(400, done)
34ca3b52
C
425 })
426
9f10b292 427 it('Should fail with a video which does not exist', function (done) {
0c1cbbfe 428 request(server.url)
9f10b292 429 .delete(path + '123456789012345678901234')
b6c6f935 430 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 431 .expect(404, done)
34ca3b52 432 })
9f10b292
C
433
434 it('Should fail with a video of another pod')
435
436 it('Should succeed with the correct parameters')
34ca3b52 437 })
9f10b292 438 })
34ca3b52 439
9f10b292
C
440 describe('Of the remote videos API', function () {
441 describe('When making a secure request', function () {
442 it('Should check a secure request')
443 })
34ca3b52 444
9f10b292
C
445 describe('When adding a video', function () {
446 it('Should check when adding a video')
34ca3b52 447 })
9f10b292
C
448
449 describe('When removing a video', function () {
450 it('Should check when removing a video')
451 })
452 })
453
454 after(function (done) {
0c1cbbfe 455 process.kill(-server.app.pid)
9f10b292
C
456
457 // Keep the logs if the test failed
458 if (this.ok) {
459 utils.flushTests(done)
460 } else {
461 done()
462 }
34ca3b52 463 })
9f10b292 464})