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