]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Add a check for the duration of videos
[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]
9f10b292 26 req.field(field, value)
ee66c593
C
27 })
28
67100f1f
C
29 Object.keys(attaches).forEach(function (attach) {
30 const value = attaches[attach]
31 req.attach(attach, value)
32 })
33
bc503c2a 34 req.expect(statusCode, done)
9f10b292
C
35 }
36
37 function makePostBodyRequest (path, fields, done, fail) {
bc503c2a
C
38 let statusCode = 400
39 if (fail !== undefined && fail === false) statusCode = 200
9f10b292 40
0c1cbbfe 41 request(server.url)
9f10b292
C
42 .post(path)
43 .set('Accept', 'application/json')
44 .send(fields)
bc503c2a 45 .expect(statusCode, done)
9f10b292
C
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) {
0c1cbbfe
C
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
b6c6f935 67 server.accessToken = token
0c1cbbfe 68
9f10b292 69 next()
34ca3b52 70 })
9f10b292
C
71 }
72 ], done)
73 })
74
75 describe('Of the pods API', function () {
f0f5567b 76 const path = '/api/v1/pods/'
9f10b292
C
77
78 describe('When adding a pod', function () {
79 it('Should fail with nothing', function (done) {
f0f5567b 80 const data = {}
9f10b292
C
81 makePostBodyRequest(path, data, done)
82 })
34ca3b52 83
9f10b292 84 it('Should fail without public key', function (done) {
f0f5567b 85 const data = {
9f10b292
C
86 data: {
87 url: 'http://coucou.com'
34ca3b52 88 }
9f10b292
C
89 }
90 makePostBodyRequest(path, data, done)
91 })
34ca3b52 92
9f10b292 93 it('Should fail without an url', function (done) {
f0f5567b 94 const data = {
9f10b292
C
95 data: {
96 publicKey: 'mysuperpublickey'
34ca3b52 97 }
9f10b292
C
98 }
99 makePostBodyRequest(path, data, done)
100 })
34ca3b52 101
9f10b292 102 it('Should fail with an incorrect url', function (done) {
f0f5567b 103 const data = {
9f10b292
C
104 data: {
105 url: 'coucou.com',
106 publicKey: 'mysuperpublickey'
34ca3b52 107 }
9f10b292
C
108 }
109 makePostBodyRequest(path, data, function () {
110 data.data.url = 'http://coucou'
34ca3b52 111 makePostBodyRequest(path, data, function () {
9f10b292
C
112 data.data.url = 'coucou'
113 makePostBodyRequest(path, data, done)
34ca3b52
C
114 })
115 })
9f10b292 116 })
34ca3b52 117
9f10b292 118 it('Should succeed with the correct parameters', function (done) {
f0f5567b 119 const data = {
9f10b292
C
120 data: {
121 url: 'http://coucou.com',
122 publicKey: 'mysuperpublickey'
34ca3b52 123 }
9f10b292
C
124 }
125 makePostBodyRequest(path, data, done, false)
34ca3b52
C
126 })
127 })
9f10b292 128 })
34ca3b52 129
9f10b292 130 describe('Of the videos API', function () {
f0f5567b 131 const path = '/api/v1/videos/'
34ca3b52 132
9f10b292
C
133 describe('When searching a video', function () {
134 it('Should fail with nothing', function (done) {
0c1cbbfe 135 request(server.url)
9f10b292
C
136 .get(pathUtils.join(path, 'search'))
137 .set('Accept', 'application/json')
138 .expect(400, done)
34ca3b52 139 })
9f10b292 140 })
34ca3b52 141
9f10b292
C
142 describe('When adding a video', function () {
143 it('Should fail with nothing', function (done) {
f0f5567b
C
144 const data = {}
145 const attach = {}
b6c6f935 146 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 147 })
34ca3b52 148
9f10b292 149 it('Should fail without name', function (done) {
f0f5567b 150 const data = {
9f10b292
C
151 description: 'my super description'
152 }
f0f5567b 153 const attach = {
8c9c1942 154 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 155 }
b6c6f935 156 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 157 })
34ca3b52 158
9f10b292 159 it('Should fail with a long name', function (done) {
f0f5567b 160 const data = {
9f10b292
C
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 }
f0f5567b 164 const attach = {
8c9c1942 165 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 166 }
b6c6f935 167 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 168 })
34ca3b52 169
9f10b292 170 it('Should fail without description', function (done) {
f0f5567b 171 const data = {
9f10b292
C
172 name: 'my super name'
173 }
f0f5567b 174 const attach = {
8c9c1942 175 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 176 }
b6c6f935 177 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 178 })
34ca3b52 179
9f10b292 180 it('Should fail with a long description', function (done) {
f0f5567b 181 const data = {
9f10b292
C
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 }
f0f5567b 187 const attach = {
8c9c1942 188 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 189 }
b6c6f935 190 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 191 })
34ca3b52 192
9f10b292 193 it('Should fail without an input file', function (done) {
f0f5567b 194 const data = {
9f10b292
C
195 name: 'my super name',
196 description: 'my super description'
197 }
f0f5567b 198 const attach = {}
b6c6f935 199 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 200 })
34ca3b52 201
9f10b292 202 it('Should fail without an incorrect input file', function (done) {
f0f5567b 203 const data = {
9f10b292
C
204 name: 'my super name',
205 description: 'my super description'
206 }
f0f5567b 207 const attach = {
67100f1f
C
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')
9f10b292 220 }
b6c6f935 221 makePostRequest(path, server.accessToken, data, attach, done)
9f10b292 222 })
34ca3b52 223
9f10b292 224 it('Should succeed with the correct parameters', function (done) {
f0f5567b 225 const data = {
9f10b292
C
226 name: 'my super name',
227 description: 'my super description'
228 }
f0f5567b 229 const attach = {
8c9c1942 230 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 231 }
b6c6f935 232 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 233 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
b6c6f935 234 makePostRequest(path, server.accessToken, data, attach, function () {
8c9c1942 235 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
67100f1f
C
236 makePostRequest(path, server.accessToken, data, attach, done, false)
237 }, false)
238 }, false)
34ca3b52 239 })
9f10b292 240 })
34ca3b52 241
9f10b292
C
242 describe('When getting a video', function () {
243 it('Should return the list of the videos with nothing', function (done) {
0c1cbbfe 244 request(server.url)
9f10b292
C
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
34ca3b52 251
9f10b292 252 expect(res.body).to.be.an('array')
67100f1f 253 expect(res.body.length).to.equal(3)
34ca3b52 254
9f10b292
C
255 done()
256 })
257 })
34ca3b52 258
9f10b292 259 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 260 request(server.url)
9f10b292
C
261 .get(path + 'coucou')
262 .set('Accept', 'application/json')
263 .expect(400, done)
34ca3b52
C
264 })
265
9f10b292 266 it('Should return 404 with an incorrect video', function (done) {
0c1cbbfe 267 request(server.url)
9f10b292
C
268 .get(path + '123456789012345678901234')
269 .set('Accept', 'application/json')
34ca3b52 270 .expect(404, done)
34ca3b52 271 })
9f10b292
C
272
273 it('Should succeed with the correct parameters')
34ca3b52
C
274 })
275
9f10b292
C
276 describe('When removing a video', function () {
277 it('Should have 404 with nothing', function (done) {
0c1cbbfe
C
278 request(server.url)
279 .delete(path)
b6c6f935 280 .set('Authorization', 'Bearer ' + server.accessToken)
0c1cbbfe 281 .expect(400, done)
34ca3b52
C
282 })
283
9f10b292 284 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 285 request(server.url)
9f10b292 286 .delete(path + 'hello')
b6c6f935 287 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 288 .expect(400, done)
34ca3b52
C
289 })
290
9f10b292 291 it('Should fail with a video which does not exist', function (done) {
0c1cbbfe 292 request(server.url)
9f10b292 293 .delete(path + '123456789012345678901234')
b6c6f935 294 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 295 .expect(404, done)
34ca3b52 296 })
9f10b292
C
297
298 it('Should fail with a video of another pod')
299
300 it('Should succeed with the correct parameters')
34ca3b52 301 })
9f10b292 302 })
34ca3b52 303
9f10b292
C
304 describe('Of the remote videos API', function () {
305 describe('When making a secure request', function () {
306 it('Should check a secure request')
307 })
34ca3b52 308
9f10b292
C
309 describe('When adding a video', function () {
310 it('Should check when adding a video')
34ca3b52 311 })
9f10b292
C
312
313 describe('When removing a video', function () {
314 it('Should check when removing a video')
315 })
316 })
317
318 after(function (done) {
0c1cbbfe 319 process.kill(-server.app.pid)
9f10b292
C
320
321 // Keep the logs if the test failed
322 if (this.ok) {
323 utils.flushTests(done)
324 } else {
325 done()
326 }
34ca3b52 327 })
9f10b292 328})