]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Change api output for 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 () {
f0f5567b
C
12 let app = null
13 let url = ''
34ca3b52 14
9f10b292 15 function makePostRequest (path, fields, attach, done, fail) {
f0f5567b 16 let status_code = 400
9f10b292 17 if (fail !== undefined && fail === false) status_code = 200
34ca3b52 18
f0f5567b 19 const req = request(url)
9f10b292
C
20 .post(path)
21 .set('Accept', 'application/json')
34ca3b52 22
9f10b292 23 Object.keys(fields).forEach(function (field) {
f0f5567b 24 const value = fields[field]
9f10b292 25 req.field(field, value)
ee66c593
C
26 })
27
9f10b292
C
28 req.expect(status_code, done)
29 }
30
31 function makePostBodyRequest (path, fields, done, fail) {
f0f5567b 32 let status_code = 400
9f10b292
C
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()
34ca3b52 56 })
9f10b292
C
57 }
58 ], done)
59 })
60
61 describe('Of the pods API', function () {
f0f5567b 62 const path = '/api/v1/pods/'
9f10b292
C
63
64 describe('When adding a pod', function () {
65 it('Should fail with nothing', function (done) {
f0f5567b 66 const data = {}
9f10b292
C
67 makePostBodyRequest(path, data, done)
68 })
34ca3b52 69
9f10b292 70 it('Should fail without public key', function (done) {
f0f5567b 71 const data = {
9f10b292
C
72 data: {
73 url: 'http://coucou.com'
34ca3b52 74 }
9f10b292
C
75 }
76 makePostBodyRequest(path, data, done)
77 })
34ca3b52 78
9f10b292 79 it('Should fail without an url', function (done) {
f0f5567b 80 const data = {
9f10b292
C
81 data: {
82 publicKey: 'mysuperpublickey'
34ca3b52 83 }
9f10b292
C
84 }
85 makePostBodyRequest(path, data, done)
86 })
34ca3b52 87
9f10b292 88 it('Should fail with an incorrect url', function (done) {
f0f5567b 89 const data = {
9f10b292
C
90 data: {
91 url: 'coucou.com',
92 publicKey: 'mysuperpublickey'
34ca3b52 93 }
9f10b292
C
94 }
95 makePostBodyRequest(path, data, function () {
96 data.data.url = 'http://coucou'
34ca3b52 97 makePostBodyRequest(path, data, function () {
9f10b292
C
98 data.data.url = 'coucou'
99 makePostBodyRequest(path, data, done)
34ca3b52
C
100 })
101 })
9f10b292 102 })
34ca3b52 103
9f10b292 104 it('Should succeed with the correct parameters', function (done) {
f0f5567b 105 const data = {
9f10b292
C
106 data: {
107 url: 'http://coucou.com',
108 publicKey: 'mysuperpublickey'
34ca3b52 109 }
9f10b292
C
110 }
111 makePostBodyRequest(path, data, done, false)
34ca3b52
C
112 })
113 })
9f10b292 114 })
34ca3b52 115
9f10b292 116 describe('Of the videos API', function () {
f0f5567b 117 const path = '/api/v1/videos/'
34ca3b52 118
9f10b292
C
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)
34ca3b52 125 })
9f10b292 126 })
34ca3b52 127
9f10b292
C
128 describe('When adding a video', function () {
129 it('Should fail with nothing', function (done) {
f0f5567b
C
130 const data = {}
131 const attach = {}
9f10b292
C
132 makePostRequest(path, data, attach, done)
133 })
34ca3b52 134
9f10b292 135 it('Should fail without name', function (done) {
f0f5567b 136 const data = {
9f10b292
C
137 description: 'my super description'
138 }
f0f5567b 139 const attach = {
9f10b292
C
140 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
141 }
142 makePostRequest(path, data, attach, done)
143 })
34ca3b52 144
9f10b292 145 it('Should fail with a long name', function (done) {
f0f5567b 146 const data = {
9f10b292
C
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 }
f0f5567b 150 const attach = {
9f10b292
C
151 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
152 }
153 makePostRequest(path, data, attach, done)
154 })
34ca3b52 155
9f10b292 156 it('Should fail without description', function (done) {
f0f5567b 157 const data = {
9f10b292
C
158 name: 'my super name'
159 }
f0f5567b 160 const attach = {
9f10b292
C
161 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
162 }
163 makePostRequest(path, data, attach, done)
164 })
34ca3b52 165
9f10b292 166 it('Should fail with a long description', function (done) {
f0f5567b 167 const data = {
9f10b292
C
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 }
f0f5567b 173 const attach = {
9f10b292
C
174 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
175 }
176 makePostRequest(path, data, attach, done)
177 })
34ca3b52 178
9f10b292 179 it('Should fail without an input file', function (done) {
f0f5567b 180 const data = {
9f10b292
C
181 name: 'my super name',
182 description: 'my super description'
183 }
f0f5567b 184 const attach = {}
9f10b292
C
185 makePostRequest(path, data, attach, done)
186 })
34ca3b52 187
9f10b292 188 it('Should fail without an incorrect input file', function (done) {
f0f5567b 189 const data = {
9f10b292
C
190 name: 'my super name',
191 description: 'my super description'
192 }
f0f5567b 193 const attach = {
9f10b292
C
194 'input_video': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
195 }
196 makePostRequest(path, data, attach, done)
197 })
34ca3b52 198
9f10b292 199 it('Should succeed with the correct parameters', function (done) {
f0f5567b 200 const data = {
9f10b292
C
201 name: 'my super name',
202 description: 'my super description'
203 }
f0f5567b 204 const attach = {
9f10b292
C
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')
34ca3b52 209 makePostRequest(path, data, attach, function () {
9f10b292
C
210 attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
211 makePostRequest(path, data, attach, done, true)
34ca3b52 212 }, true)
9f10b292 213 }, true)
34ca3b52 214 })
9f10b292 215 })
34ca3b52 216
9f10b292
C
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
34ca3b52 226
9f10b292
C
227 expect(res.body).to.be.an('array')
228 expect(res.body.length).to.equal(0)
34ca3b52 229
9f10b292
C
230 done()
231 })
232 })
34ca3b52 233
9f10b292
C
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)
34ca3b52
C
239 })
240
9f10b292
C
241 it('Should return 404 with an incorrect video', function (done) {
242 request(url)
243 .get(path + '123456789012345678901234')
244 .set('Accept', 'application/json')
34ca3b52 245 .expect(404, done)
34ca3b52 246 })
9f10b292
C
247
248 it('Should succeed with the correct parameters')
34ca3b52
C
249 })
250
9f10b292
C
251 describe('When removing a video', function () {
252 it('Should have 404 with nothing', function (done) {
253 request(url)
254 .delete(path)
3d446a26 255 .expect(400, done)
34ca3b52
C
256 })
257
9f10b292
C
258 it('Should fail without a mongodb id', function (done) {
259 request(url)
260 .delete(path + 'hello')
261 .expect(400, done)
34ca3b52
C
262 })
263
9f10b292
C
264 it('Should fail with a video which does not exist', function (done) {
265 request(url)
266 .delete(path + '123456789012345678901234')
267 .expect(404, done)
34ca3b52 268 })
9f10b292
C
269
270 it('Should fail with a video of another pod')
271
272 it('Should succeed with the correct parameters')
34ca3b52 273 })
9f10b292 274 })
34ca3b52 275
9f10b292
C
276 describe('Of the remote videos API', function () {
277 describe('When making a secure request', function () {
278 it('Should check a secure request')
279 })
34ca3b52 280
9f10b292
C
281 describe('When adding a video', function () {
282 it('Should check when adding a video')
34ca3b52 283 })
9f10b292
C
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 }
34ca3b52 299 })
9f10b292 300})