]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.js
b63091910e37a50e38801b28236c2e4fe4b2a010
[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 searching a video', function () {
134 it('Should fail with nothing', function (done) {
135 request(server.url)
136 .get(pathUtils.join(path, 'search'))
137 .set('Accept', 'application/json')
138 .expect(400, done)
139 })
140 })
141
142 describe('When adding a video', function () {
143 it('Should fail with nothing', function (done) {
144 const data = {}
145 const attach = {}
146 makePostRequest(path, server.accessToken, data, attach, done)
147 })
148
149 it('Should fail without name', function (done) {
150 const data = {
151 description: 'my super description'
152 }
153 const attach = {
154 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
155 }
156 makePostRequest(path, server.accessToken, data, attach, done)
157 })
158
159 it('Should fail with a long name', function (done) {
160 const data = {
161 name: 'My very very very very very very very very very very very very very very very very long name',
162 description: 'my super description'
163 }
164 const attach = {
165 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
166 }
167 makePostRequest(path, server.accessToken, data, attach, done)
168 })
169
170 it('Should fail without description', function (done) {
171 const data = {
172 name: 'my super name'
173 }
174 const attach = {
175 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
176 }
177 makePostRequest(path, server.accessToken, data, attach, done)
178 })
179
180 it('Should fail with a long description', function (done) {
181 const data = {
182 name: 'my super name',
183 description: 'my super description which is very very very very very very very very very very very very very very' +
184 'very very very very very very very very very very very very very very very very very very very very very' +
185 'very very very very very very very very very very very very very very very long'
186 }
187 const attach = {
188 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
189 }
190 makePostRequest(path, server.accessToken, data, attach, done)
191 })
192
193 it('Should fail without an input file', function (done) {
194 const data = {
195 name: 'my super name',
196 description: 'my super description'
197 }
198 const attach = {}
199 makePostRequest(path, server.accessToken, data, attach, done)
200 })
201
202 it('Should fail without an incorrect input file', function (done) {
203 const data = {
204 name: 'my super name',
205 description: 'my super description'
206 }
207 const attach = {
208 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
209 }
210 makePostRequest(path, server.accessToken, data, attach, done)
211 })
212
213 it('Should fail with a too big duration', function (done) {
214 const data = {
215 name: 'my super name',
216 description: 'my super description'
217 }
218 const attach = {
219 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
220 }
221 makePostRequest(path, server.accessToken, data, attach, done)
222 })
223
224 it('Should succeed with the correct parameters', function (done) {
225 const data = {
226 name: 'my super name',
227 description: 'my super description'
228 }
229 const attach = {
230 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
231 }
232 makePostRequest(path, server.accessToken, data, attach, function () {
233 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
234 makePostRequest(path, server.accessToken, data, attach, function () {
235 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
236 makePostRequest(path, server.accessToken, data, attach, done, false)
237 }, false)
238 }, false)
239 })
240 })
241
242 describe('When getting a video', function () {
243 it('Should return the list of the videos with nothing', function (done) {
244 request(server.url)
245 .get(path)
246 .set('Accept', 'application/json')
247 .expect(200)
248 .expect('Content-Type', /json/)
249 .end(function (err, res) {
250 if (err) throw err
251
252 expect(res.body).to.be.an('array')
253 expect(res.body.length).to.equal(3)
254
255 done()
256 })
257 })
258
259 it('Should fail without a mongodb id', function (done) {
260 request(server.url)
261 .get(path + 'coucou')
262 .set('Accept', 'application/json')
263 .expect(400, done)
264 })
265
266 it('Should return 404 with an incorrect video', function (done) {
267 request(server.url)
268 .get(path + '123456789012345678901234')
269 .set('Accept', 'application/json')
270 .expect(404, done)
271 })
272
273 it('Should succeed with the correct parameters')
274 })
275
276 describe('When removing a video', function () {
277 it('Should have 404 with nothing', function (done) {
278 request(server.url)
279 .delete(path)
280 .set('Authorization', 'Bearer ' + server.accessToken)
281 .expect(400, done)
282 })
283
284 it('Should fail without a mongodb id', function (done) {
285 request(server.url)
286 .delete(path + 'hello')
287 .set('Authorization', 'Bearer ' + server.accessToken)
288 .expect(400, done)
289 })
290
291 it('Should fail with a video which does not exist', function (done) {
292 request(server.url)
293 .delete(path + '123456789012345678901234')
294 .set('Authorization', 'Bearer ' + server.accessToken)
295 .expect(404, done)
296 })
297
298 it('Should fail with a video of another pod')
299
300 it('Should succeed with the correct parameters')
301 })
302 })
303
304 describe('Of the remote videos API', function () {
305 describe('When making a secure request', function () {
306 it('Should check a secure request')
307 })
308
309 describe('When adding a video', function () {
310 it('Should check when adding a video')
311 })
312
313 describe('When removing a video', function () {
314 it('Should check when removing a video')
315 })
316 })
317
318 after(function (done) {
319 process.kill(-server.app.pid)
320
321 // Keep the logs if the test failed
322 if (this.ok) {
323 utils.flushTests(done)
324 } else {
325 done()
326 }
327 })
328 })