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')
9 const utils = require('./utils')
11 describe('Test parameters validator', function () {
14 function makePostRequest (path, token, fields, attach, done, fail) {
16 if (fail !== undefined && fail === false) status_code = 200
18 const req = request(server.url)
20 .set('Accept', 'application/json')
22 if (token) req.set('Authorization', 'Bearer ' + token)
24 Object.keys(fields).forEach(function (field) {
25 const value = fields[field]
26 req.field(field, value)
29 req.expect(status_code, done)
32 function makePostBodyRequest (path, fields, done, fail) {
34 if (fail !== undefined && fail === false) status_code = 200
38 .set('Accept', 'application/json')
40 .expect(status_code, done)
43 // ---------------------------------------------------------------
45 before(function (done) {
50 utils.flushTests(next)
53 utils.runServer(1, function (server1) {
60 utils.loginAndGetAccessToken(server, function (err, token) {
62 server.access_token = token
70 describe('Of the pods API', function () {
71 const path = '/api/v1/pods/'
73 describe('When adding a pod', function () {
74 it('Should fail with nothing', function (done) {
76 makePostBodyRequest(path, data, done)
79 it('Should fail without public key', function (done) {
82 url: 'http://coucou.com'
85 makePostBodyRequest(path, data, done)
88 it('Should fail without an url', function (done) {
91 publicKey: 'mysuperpublickey'
94 makePostBodyRequest(path, data, done)
97 it('Should fail with an incorrect url', function (done) {
101 publicKey: 'mysuperpublickey'
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)
113 it('Should succeed with the correct parameters', function (done) {
116 url: 'http://coucou.com',
117 publicKey: 'mysuperpublickey'
120 makePostBodyRequest(path, data, done, false)
125 describe('Of the videos API', function () {
126 const path = '/api/v1/videos/'
128 describe('When searching a video', function () {
129 it('Should fail with nothing', function (done) {
131 .get(pathUtils.join(path, 'search'))
132 .set('Accept', 'application/json')
137 describe('When adding a video', function () {
138 it('Should fail with nothing', function (done) {
141 makePostRequest(path, server.access_token, data, attach, done)
144 it('Should fail without name', function (done) {
146 description: 'my super description'
149 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
151 makePostRequest(path, server.access_token, data, attach, done)
154 it('Should fail with a long name', function (done) {
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'
160 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
162 makePostRequest(path, server.access_token, data, attach, done)
165 it('Should fail without description', function (done) {
167 name: 'my super name'
170 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
172 makePostRequest(path, server.access_token, data, attach, done)
175 it('Should fail with a long description', function (done) {
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'
183 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
185 makePostRequest(path, server.access_token, data, attach, done)
188 it('Should fail without an input file', function (done) {
190 name: 'my super name',
191 description: 'my super description'
194 makePostRequest(path, server.access_token, data, attach, done)
197 it('Should fail without an incorrect input file', function (done) {
199 name: 'my super name',
200 description: 'my super description'
203 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
205 makePostRequest(path, server.access_token, data, attach, done)
208 it('Should succeed with the correct parameters', function (done) {
210 name: 'my super name',
211 description: 'my super description'
214 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
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)
226 describe('When getting a video', function () {
227 it('Should return the list of the videos with nothing', function (done) {
230 .set('Accept', 'application/json')
232 .expect('Content-Type', /json/)
233 .end(function (err, res) {
236 expect(res.body).to.be.an('array')
237 expect(res.body.length).to.equal(0)
243 it('Should fail without a mongodb id', function (done) {
245 .get(path + 'coucou')
246 .set('Accept', 'application/json')
250 it('Should return 404 with an incorrect video', function (done) {
252 .get(path + '123456789012345678901234')
253 .set('Accept', 'application/json')
257 it('Should succeed with the correct parameters')
260 describe('When removing a video', function () {
261 it('Should have 404 with nothing', function (done) {
264 .set('Authorization', 'Bearer ' + server.access_token)
268 it('Should fail without a mongodb id', function (done) {
270 .delete(path + 'hello')
271 .set('Authorization', 'Bearer ' + server.access_token)
275 it('Should fail with a video which does not exist', function (done) {
277 .delete(path + '123456789012345678901234')
278 .set('Authorization', 'Bearer ' + server.access_token)
282 it('Should fail with a video of another pod')
284 it('Should succeed with the correct parameters')
288 describe('Of the remote videos API', function () {
289 describe('When making a secure request', function () {
290 it('Should check a secure request')
293 describe('When adding a video', function () {
294 it('Should check when adding a video')
297 describe('When removing a video', function () {
298 it('Should check when removing a video')
302 after(function (done) {
303 process.kill(-server.app.pid)
305 // Keep the logs if the test failed
307 utils.flushTests(done)