Use lodash v4
[oweals/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, attach, done, fail) {
15     let status_code = 400
16     if (fail !== undefined && fail === false) status_code = 200
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     req.expect(status_code, done)
30   }
31
32   function makePostBodyRequest (path, fields, done, fail) {
33     let status_code = 400
34     if (fail !== undefined && fail === false) status_code = 200
35
36     request(server.url)
37       .post(path)
38       .set('Accept', 'application/json')
39       .send(fields)
40       .expect(status_code, done)
41   }
42
43   // ---------------------------------------------------------------
44
45   before(function (done) {
46     this.timeout(20000)
47
48     async.series([
49       function (next) {
50         utils.flushTests(next)
51       },
52       function (next) {
53         utils.runServer(1, function (server1) {
54           server = server1
55
56           next()
57         })
58       },
59       function (next) {
60         utils.loginAndGetAccessToken(server, function (err, token) {
61           if (err) throw err
62           server.access_token = token
63
64           next()
65         })
66       }
67     ], done)
68   })
69
70   describe('Of the pods API', function () {
71     const path = '/api/v1/pods/'
72
73     describe('When adding a pod', function () {
74       it('Should fail with nothing', function (done) {
75         const data = {}
76         makePostBodyRequest(path, data, done)
77       })
78
79       it('Should fail without public key', function (done) {
80         const data = {
81           data: {
82             url: 'http://coucou.com'
83           }
84         }
85         makePostBodyRequest(path, data, done)
86       })
87
88       it('Should fail without an url', function (done) {
89         const data = {
90           data: {
91             publicKey: 'mysuperpublickey'
92           }
93         }
94         makePostBodyRequest(path, data, done)
95       })
96
97       it('Should fail with an incorrect url', function (done) {
98         const data = {
99           data: {
100             url: 'coucou.com',
101             publicKey: 'mysuperpublickey'
102           }
103         }
104         makePostBodyRequest(path, data, function () {
105           data.data.url = 'http://coucou'
106           makePostBodyRequest(path, data, function () {
107             data.data.url = 'coucou'
108             makePostBodyRequest(path, data, done)
109           })
110         })
111       })
112
113       it('Should succeed with the correct parameters', function (done) {
114         const data = {
115           data: {
116             url: 'http://coucou.com',
117             publicKey: 'mysuperpublickey'
118           }
119         }
120         makePostBodyRequest(path, data, done, false)
121       })
122     })
123   })
124
125   describe('Of the videos API', function () {
126     const path = '/api/v1/videos/'
127
128     describe('When searching a video', function () {
129       it('Should fail with nothing', function (done) {
130         request(server.url)
131           .get(pathUtils.join(path, 'search'))
132           .set('Accept', 'application/json')
133           .expect(400, done)
134       })
135     })
136
137     describe('When adding a video', function () {
138       it('Should fail with nothing', function (done) {
139         const data = {}
140         const attach = {}
141         makePostRequest(path, server.access_token, data, attach, done)
142       })
143
144       it('Should fail without name', function (done) {
145         const data = {
146           description: 'my super description'
147         }
148         const attach = {
149           'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
150         }
151         makePostRequest(path, server.access_token, data, attach, done)
152       })
153
154       it('Should fail with a long name', function (done) {
155         const data = {
156           name: 'My very very very very very very very very very very very very very very very very long name',
157           description: 'my super description'
158         }
159         const attach = {
160           'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
161         }
162         makePostRequest(path, server.access_token, data, attach, done)
163       })
164
165       it('Should fail without description', function (done) {
166         const data = {
167           name: 'my super name'
168         }
169         const attach = {
170           'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
171         }
172         makePostRequest(path, server.access_token, data, attach, done)
173       })
174
175       it('Should fail with a long description', function (done) {
176         const data = {
177           name: 'my super name',
178           description: 'my super description which is very very very very very very very very very very very very very very' +
179                        'very very very very very very very very very very very very very very very very very very very very very' +
180                        'very very very very very very very very very very very very very very very long'
181         }
182         const attach = {
183           'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
184         }
185         makePostRequest(path, server.access_token, data, attach, done)
186       })
187
188       it('Should fail without an input file', function (done) {
189         const data = {
190           name: 'my super name',
191           description: 'my super description'
192         }
193         const attach = {}
194         makePostRequest(path, server.access_token, data, attach, done)
195       })
196
197       it('Should fail without an incorrect input file', function (done) {
198         const data = {
199           name: 'my super name',
200           description: 'my super description'
201         }
202         const attach = {
203           'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
204         }
205         makePostRequest(path, server.access_token, data, attach, done)
206       })
207
208       it('Should succeed with the correct parameters', function (done) {
209         const data = {
210           name: 'my super name',
211           description: 'my super description'
212         }
213         const attach = {
214           'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
215         }
216         makePostRequest(path, server.access_token, data, attach, function () {
217           attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
218           makePostRequest(path, server.access_token, data, attach, function () {
219             attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
220             makePostRequest(path, server.access_token, data, attach, done, true)
221           }, true)
222         }, true)
223       })
224     })
225
226     describe('When getting a video', function () {
227       it('Should return the list of the videos with nothing', function (done) {
228         request(server.url)
229           .get(path)
230           .set('Accept', 'application/json')
231           .expect(200)
232           .expect('Content-Type', /json/)
233           .end(function (err, res) {
234             if (err) throw err
235
236             expect(res.body).to.be.an('array')
237             expect(res.body.length).to.equal(0)
238
239             done()
240           })
241       })
242
243       it('Should fail without a mongodb id', function (done) {
244         request(server.url)
245           .get(path + 'coucou')
246           .set('Accept', 'application/json')
247           .expect(400, done)
248       })
249
250       it('Should return 404 with an incorrect video', function (done) {
251         request(server.url)
252           .get(path + '123456789012345678901234')
253           .set('Accept', 'application/json')
254           .expect(404, done)
255       })
256
257       it('Should succeed with the correct parameters')
258     })
259
260     describe('When removing a video', function () {
261       it('Should have 404 with nothing', function (done) {
262         request(server.url)
263           .delete(path)
264           .set('Authorization', 'Bearer ' + server.access_token)
265           .expect(400, done)
266       })
267
268       it('Should fail without a mongodb id', function (done) {
269         request(server.url)
270           .delete(path + 'hello')
271           .set('Authorization', 'Bearer ' + server.access_token)
272           .expect(400, done)
273       })
274
275       it('Should fail with a video which does not exist', function (done) {
276         request(server.url)
277           .delete(path + '123456789012345678901234')
278           .set('Authorization', 'Bearer ' + server.access_token)
279           .expect(404, done)
280       })
281
282       it('Should fail with a video of another pod')
283
284       it('Should succeed with the correct parameters')
285     })
286   })
287
288   describe('Of the remote videos API', function () {
289     describe('When making a secure request', function () {
290       it('Should check a secure request')
291     })
292
293     describe('When adding a video', function () {
294       it('Should check when adding a video')
295     })
296
297     describe('When removing a video', function () {
298       it('Should check when removing a video')
299     })
300   })
301
302   after(function (done) {
303     process.kill(-server.app.pid)
304
305     // Keep the logs if the test failed
306     if (this.ok) {
307       utils.flushTests(done)
308     } else {
309       done()
310     }
311   })
312 })