]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.js
Extends the search feature by customizing the search field (name,
[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 req.field(field, value)
27 })
28
29 Object.keys(attaches).forEach(function (attach) {
30 const value = attaches[attach]
31 req.attach(attach, value)
32 })
33
34 req.expect(statusCode, done)
35 }
36
37 function makePostBodyRequest (path, fields, done, fail) {
38 let statusCode = 400
39 if (fail !== undefined && fail === false) statusCode = 200
40
41 request(server.url)
42 .post(path)
43 .set('Accept', 'application/json')
44 .send(fields)
45 .expect(statusCode, done)
46 }
47
48 // ---------------------------------------------------------------
49
50 before(function (done) {
51 this.timeout(20000)
52
53 async.series([
54 function (next) {
55 utils.flushTests(next)
56 },
57 function (next) {
58 utils.runServer(1, function (server1) {
59 server = server1
60
61 next()
62 })
63 },
64 function (next) {
65 utils.loginAndGetAccessToken(server, function (err, token) {
66 if (err) throw err
67 server.accessToken = token
68
69 next()
70 })
71 }
72 ], done)
73 })
74
75 describe('Of the pods API', function () {
76 const path = '/api/v1/pods/'
77
78 describe('When adding a pod', function () {
79 it('Should fail with nothing', function (done) {
80 const data = {}
81 makePostBodyRequest(path, data, done)
82 })
83
84 it('Should fail without public key', function (done) {
85 const data = {
86 data: {
87 url: 'http://coucou.com'
88 }
89 }
90 makePostBodyRequest(path, data, done)
91 })
92
93 it('Should fail without an url', function (done) {
94 const data = {
95 data: {
96 publicKey: 'mysuperpublickey'
97 }
98 }
99 makePostBodyRequest(path, data, done)
100 })
101
102 it('Should fail with an incorrect url', function (done) {
103 const data = {
104 data: {
105 url: 'coucou.com',
106 publicKey: 'mysuperpublickey'
107 }
108 }
109 makePostBodyRequest(path, data, function () {
110 data.data.url = 'http://coucou'
111 makePostBodyRequest(path, data, function () {
112 data.data.url = 'coucou'
113 makePostBodyRequest(path, data, done)
114 })
115 })
116 })
117
118 it('Should succeed with the correct parameters', function (done) {
119 const data = {
120 data: {
121 url: 'http://coucou.com',
122 publicKey: 'mysuperpublickey'
123 }
124 }
125 makePostBodyRequest(path, data, done, false)
126 })
127 })
128 })
129
130 describe('Of the videos API', function () {
131 const path = '/api/v1/videos/'
132
133 describe('When listing a video', function () {
134 it('Should fail with a bad start pagination', function (done) {
135 request(server.url)
136 .get(path)
137 .query({ start: 'hello' })
138 .set('Accept', 'application/json')
139 .expect(400, done)
140 })
141
142 it('Should fail with a bad count pagination', function (done) {
143 request(server.url)
144 .get(path)
145 .query({ count: 'hello' })
146 .set('Accept', 'application/json')
147 .expect(400, done)
148 })
149
150 it('Should fail with an incorrect sort', function (done) {
151 request(server.url)
152 .get(path)
153 .query({ sort: 'hello' })
154 .set('Accept', 'application/json')
155 .expect(400, done)
156 })
157 })
158
159 describe('When searching a video', function () {
160 it('Should fail with nothing', function (done) {
161 request(server.url)
162 .get(pathUtils.join(path, 'search'))
163 .set('Accept', 'application/json')
164 .expect(400, done)
165 })
166
167 it('Should fail with a bad start pagination', function (done) {
168 request(server.url)
169 .get(pathUtils.join(path, 'search', 'test'))
170 .query({ start: 'hello' })
171 .set('Accept', 'application/json')
172 .expect(400, done)
173 })
174
175 it('Should fail with a bad count pagination', function (done) {
176 request(server.url)
177 .get(pathUtils.join(path, 'search', 'test'))
178 .query({ count: 'hello' })
179 .set('Accept', 'application/json')
180 .expect(400, done)
181 })
182
183 it('Should fail with an incorrect sort', function (done) {
184 request(server.url)
185 .get(pathUtils.join(path, 'search', 'test'))
186 .query({ sort: 'hello' })
187 .set('Accept', 'application/json')
188 .expect(400, done)
189 })
190 })
191
192 describe('When adding a video', function () {
193 it('Should fail with nothing', function (done) {
194 const data = {}
195 const attach = {}
196 makePostRequest(path, server.accessToken, data, attach, done)
197 })
198
199 it('Should fail without name', function (done) {
200 const data = {
201 description: 'my super description'
202 }
203 const attach = {
204 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
205 }
206 makePostRequest(path, server.accessToken, data, attach, done)
207 })
208
209 it('Should fail with a long name', function (done) {
210 const data = {
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 }
214 const attach = {
215 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
216 }
217 makePostRequest(path, server.accessToken, data, attach, done)
218 })
219
220 it('Should fail without description', function (done) {
221 const data = {
222 name: 'my super name'
223 }
224 const attach = {
225 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
226 }
227 makePostRequest(path, server.accessToken, data, attach, done)
228 })
229
230 it('Should fail with a long description', function (done) {
231 const data = {
232 name: 'my super name',
233 description: 'my super description which is very very very very very very very very very very very very very very' +
234 'very very very very very very very very very very very very very very very very very very very very very' +
235 'very very very very very very very very very very very very very very very long'
236 }
237 const attach = {
238 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
239 }
240 makePostRequest(path, server.accessToken, data, attach, done)
241 })
242
243 it('Should fail without an input file', function (done) {
244 const data = {
245 name: 'my super name',
246 description: 'my super description'
247 }
248 const attach = {}
249 makePostRequest(path, server.accessToken, data, attach, done)
250 })
251
252 it('Should fail without an incorrect input file', function (done) {
253 const data = {
254 name: 'my super name',
255 description: 'my super description'
256 }
257 const attach = {
258 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
259 }
260 makePostRequest(path, server.accessToken, data, attach, done)
261 })
262
263 it('Should fail with a too big duration', function (done) {
264 const data = {
265 name: 'my super name',
266 description: 'my super description'
267 }
268 const attach = {
269 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
270 }
271 makePostRequest(path, server.accessToken, data, attach, done)
272 })
273
274 it('Should succeed with the correct parameters', function (done) {
275 const data = {
276 name: 'my super name',
277 description: 'my super description'
278 }
279 const attach = {
280 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
281 }
282 makePostRequest(path, server.accessToken, data, attach, function () {
283 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
284 makePostRequest(path, server.accessToken, data, attach, function () {
285 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
286 makePostRequest(path, server.accessToken, data, attach, done, false)
287 }, false)
288 }, false)
289 })
290 })
291
292 describe('When getting a video', function () {
293 it('Should return the list of the videos with nothing', function (done) {
294 request(server.url)
295 .get(path)
296 .set('Accept', 'application/json')
297 .expect(200)
298 .expect('Content-Type', /json/)
299 .end(function (err, res) {
300 if (err) throw err
301
302 expect(res.body.data).to.be.an('array')
303 expect(res.body.data.length).to.equal(3)
304
305 done()
306 })
307 })
308
309 it('Should fail without a mongodb id', function (done) {
310 request(server.url)
311 .get(path + 'coucou')
312 .set('Accept', 'application/json')
313 .expect(400, done)
314 })
315
316 it('Should return 404 with an incorrect video', function (done) {
317 request(server.url)
318 .get(path + '123456789012345678901234')
319 .set('Accept', 'application/json')
320 .expect(404, done)
321 })
322
323 it('Should succeed with the correct parameters')
324 })
325
326 describe('When removing a video', function () {
327 it('Should have 404 with nothing', function (done) {
328 request(server.url)
329 .delete(path)
330 .set('Authorization', 'Bearer ' + server.accessToken)
331 .expect(400, done)
332 })
333
334 it('Should fail without a mongodb id', function (done) {
335 request(server.url)
336 .delete(path + 'hello')
337 .set('Authorization', 'Bearer ' + server.accessToken)
338 .expect(400, done)
339 })
340
341 it('Should fail with a video which does not exist', function (done) {
342 request(server.url)
343 .delete(path + '123456789012345678901234')
344 .set('Authorization', 'Bearer ' + server.accessToken)
345 .expect(404, done)
346 })
347
348 it('Should fail with a video of another pod')
349
350 it('Should succeed with the correct parameters')
351 })
352 })
353
354 describe('Of the remote videos API', function () {
355 describe('When making a secure request', function () {
356 it('Should check a secure request')
357 })
358
359 describe('When adding a video', function () {
360 it('Should check when adding a video')
361 })
362
363 describe('When removing a video', function () {
364 it('Should check when removing a video')
365 })
366 })
367
368 after(function (done) {
369 process.kill(-server.app.pid)
370
371 // Keep the logs if the test failed
372 if (this.ok) {
373 utils.flushTests(done)
374 } else {
375 done()
376 }
377 })
378 })