From 9f10b2928df655c3672d9607e864e667d4bc903a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Feb 2016 11:23:23 +0100 Subject: Remove useless anonymous functions of files --- tests/api/checkParams.js | 490 +++++++++++++++++++------------------- tests/api/friendsAdvanced.js | 424 +++++++++++++++++---------------- tests/api/friendsBasic.js | 300 ++++++++++++------------ tests/api/index.js | 16 +- tests/api/multiplePods.js | 544 +++++++++++++++++++++---------------------- tests/api/singlePod.js | 224 +++++++++--------- tests/api/utils.js | 340 ++++++++++++++------------- 7 files changed, 1162 insertions(+), 1176 deletions(-) (limited to 'tests') diff --git a/tests/api/checkParams.js b/tests/api/checkParams.js index 8ce1bd476..1c1ec71b3 100644 --- a/tests/api/checkParams.js +++ b/tests/api/checkParams.js @@ -1,302 +1,300 @@ -;(function () { - 'use strict' +'use strict' - var async = require('async') - var chai = require('chai') - var expect = chai.expect - var pathUtils = require('path') - var request = require('supertest') +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var pathUtils = require('path') +var request = require('supertest') - var utils = require('./utils') +var utils = require('./utils') - describe('Test parameters validator', function () { - var app = null - var url = '' +describe('Test parameters validator', function () { + var app = null + var url = '' - function makePostRequest (path, fields, attach, done, fail) { - var status_code = 400 - if (fail !== undefined && fail === false) status_code = 200 + function makePostRequest (path, fields, attach, done, fail) { + var status_code = 400 + if (fail !== undefined && fail === false) status_code = 200 - var req = request(url) - .post(path) - .set('Accept', 'application/json') + var req = request(url) + .post(path) + .set('Accept', 'application/json') - Object.keys(fields).forEach(function (field) { - var value = fields[field] - req.field(field, value) - }) - - req.expect(status_code, done) - } - - function makePostBodyRequest (path, fields, done, fail) { - var status_code = 400 - if (fail !== undefined && fail === false) status_code = 200 - - request(url) - .post(path) - .set('Accept', 'application/json') - .send(fields) - .expect(status_code, done) - } - - // --------------------------------------------------------------- - - before(function (done) { - this.timeout(20000) - - async.series([ - function (next) { - utils.flushTests(next) - }, - function (next) { - utils.runServer(1, function (app1, url1) { - app = app1 - url = url1 - next() - }) - } - ], done) + Object.keys(fields).forEach(function (field) { + var value = fields[field] + req.field(field, value) }) - describe('Of the pods API', function () { - var path = '/api/v1/pods/' - - describe('When adding a pod', function () { - it('Should fail with nothing', function (done) { - var data = {} - makePostBodyRequest(path, data, done) + req.expect(status_code, done) + } + + function makePostBodyRequest (path, fields, done, fail) { + var status_code = 400 + if (fail !== undefined && fail === false) status_code = 200 + + request(url) + .post(path) + .set('Accept', 'application/json') + .send(fields) + .expect(status_code, done) + } + + // --------------------------------------------------------------- + + before(function (done) { + this.timeout(20000) + + async.series([ + function (next) { + utils.flushTests(next) + }, + function (next) { + utils.runServer(1, function (app1, url1) { + app = app1 + url = url1 + next() }) + } + ], done) + }) + + describe('Of the pods API', function () { + var path = '/api/v1/pods/' + + describe('When adding a pod', function () { + it('Should fail with nothing', function (done) { + var data = {} + makePostBodyRequest(path, data, done) + }) - it('Should fail without public key', function (done) { - var data = { - data: { - url: 'http://coucou.com' - } + it('Should fail without public key', function (done) { + var data = { + data: { + url: 'http://coucou.com' } - makePostBodyRequest(path, data, done) - }) + } + makePostBodyRequest(path, data, done) + }) - it('Should fail without an url', function (done) { - var data = { - data: { - publicKey: 'mysuperpublickey' - } + it('Should fail without an url', function (done) { + var data = { + data: { + publicKey: 'mysuperpublickey' } - makePostBodyRequest(path, data, done) - }) + } + makePostBodyRequest(path, data, done) + }) - it('Should fail with an incorrect url', function (done) { - var data = { - data: { - url: 'coucou.com', - publicKey: 'mysuperpublickey' - } + it('Should fail with an incorrect url', function (done) { + var data = { + data: { + url: 'coucou.com', + publicKey: 'mysuperpublickey' } + } + makePostBodyRequest(path, data, function () { + data.data.url = 'http://coucou' makePostBodyRequest(path, data, function () { - data.data.url = 'http://coucou' - makePostBodyRequest(path, data, function () { - data.data.url = 'coucou' - makePostBodyRequest(path, data, done) - }) + data.data.url = 'coucou' + makePostBodyRequest(path, data, done) }) }) + }) - it('Should succeed with the correct parameters', function (done) { - var data = { - data: { - url: 'http://coucou.com', - publicKey: 'mysuperpublickey' - } + it('Should succeed with the correct parameters', function (done) { + var data = { + data: { + url: 'http://coucou.com', + publicKey: 'mysuperpublickey' } - makePostBodyRequest(path, data, done, false) - }) + } + makePostBodyRequest(path, data, done, false) }) }) + }) - describe('Of the videos API', function () { - var path = '/api/v1/videos/' + describe('Of the videos API', function () { + var path = '/api/v1/videos/' - describe('When searching a video', function () { - it('Should fail with nothing', function (done) { - request(url) - .get(pathUtils.join(path, 'search')) - .set('Accept', 'application/json') - .expect(400, done) - }) + describe('When searching a video', function () { + it('Should fail with nothing', function (done) { + request(url) + .get(pathUtils.join(path, 'search')) + .set('Accept', 'application/json') + .expect(400, done) }) + }) - describe('When adding a video', function () { - it('Should fail with nothing', function (done) { - var data = {} - var attach = {} - makePostRequest(path, data, attach, done) - }) + describe('When adding a video', function () { + it('Should fail with nothing', function (done) { + var data = {} + var attach = {} + makePostRequest(path, data, attach, done) + }) - it('Should fail without name', function (done) { - var data = { - description: 'my super description' - } - var attach = { - 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') - } - makePostRequest(path, data, attach, done) - }) + it('Should fail without name', function (done) { + var data = { + description: 'my super description' + } + var attach = { + 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, data, attach, done) + }) - it('Should fail with a long name', function (done) { - var data = { - name: 'My very very very very very very very very very very very very very very very very long name', - description: 'my super description' - } - var attach = { - 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') - } - makePostRequest(path, data, attach, done) - }) + it('Should fail with a long name', function (done) { + var data = { + name: 'My very very very very very very very very very very very very very very very very long name', + description: 'my super description' + } + var attach = { + 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, data, attach, done) + }) - it('Should fail without description', function (done) { - var data = { - name: 'my super name' - } - var attach = { - 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') - } - makePostRequest(path, data, attach, done) - }) + it('Should fail without description', function (done) { + var data = { + name: 'my super name' + } + var attach = { + 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, data, attach, done) + }) - it('Should fail with a long description', function (done) { - var data = { - name: 'my super name', - description: 'my super description which is very very very very very very very very very very very very very very' + - 'very very very very very very very very very very very very very very very very very very very very very' + - 'very very very very very very very very very very very very very very very long' - } - var attach = { - 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') - } - makePostRequest(path, data, attach, done) - }) + it('Should fail with a long description', function (done) { + var data = { + name: 'my super name', + description: 'my super description which is very very very very very very very very very very very very very very' + + 'very very very very very very very very very very very very very very very very very very very very very' + + 'very very very very very very very very very very very very very very very long' + } + var attach = { + 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, data, attach, done) + }) - it('Should fail without an input file', function (done) { - var data = { - name: 'my super name', - description: 'my super description' - } - var attach = {} - makePostRequest(path, data, attach, done) - }) + it('Should fail without an input file', function (done) { + var data = { + name: 'my super name', + description: 'my super description' + } + var attach = {} + makePostRequest(path, data, attach, done) + }) - it('Should fail without an incorrect input file', function (done) { - var data = { - name: 'my super name', - description: 'my super description' - } - var attach = { - 'input_video': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm') - } - makePostRequest(path, data, attach, done) - }) + it('Should fail without an incorrect input file', function (done) { + var data = { + name: 'my super name', + description: 'my super description' + } + var attach = { + 'input_video': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm') + } + makePostRequest(path, data, attach, done) + }) - it('Should succeed with the correct parameters', function (done) { - var data = { - name: 'my super name', - description: 'my super description' - } - var attach = { - 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') - } + it('Should succeed with the correct parameters', function (done) { + var data = { + name: 'my super name', + description: 'my super description' + } + var attach = { + 'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, data, attach, function () { + attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') makePostRequest(path, data, attach, function () { - attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') - makePostRequest(path, data, attach, function () { - attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') - makePostRequest(path, data, attach, done, true) - }, true) + attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') + makePostRequest(path, data, attach, done, true) }, true) - }) + }, true) }) + }) - describe('When getting a video', function () { - it('Should return the list of the videos with nothing', function (done) { - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) throw err - - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(0) - - done() - }) - }) + describe('When getting a video', function () { + it('Should return the list of the videos with nothing', function (done) { + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) throw err - it('Should fail without a mongodb id', function (done) { - request(url) - .get(path + 'coucou') - .set('Accept', 'application/json') - .expect(400, done) - }) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) - it('Should return 404 with an incorrect video', function (done) { - request(url) - .get(path + '123456789012345678901234') - .set('Accept', 'application/json') - .expect(404, done) - }) + done() + }) + }) - it('Should succeed with the correct parameters') + it('Should fail without a mongodb id', function (done) { + request(url) + .get(path + 'coucou') + .set('Accept', 'application/json') + .expect(400, done) }) - describe('When removing a video', function () { - it('Should have 404 with nothing', function (done) { - request(url) - .delete(path) + it('Should return 404 with an incorrect video', function (done) { + request(url) + .get(path + '123456789012345678901234') + .set('Accept', 'application/json') .expect(404, done) - }) - - it('Should fail without a mongodb id', function (done) { - request(url) - .delete(path + 'hello') - .expect(400, done) - }) - - it('Should fail with a video which does not exist', function (done) { - request(url) - .delete(path + '123456789012345678901234') - .expect(404, done) - }) - - it('Should fail with a video of another pod') - - it('Should succeed with the correct parameters') }) + + it('Should succeed with the correct parameters') }) - describe('Of the remote videos API', function () { - describe('When making a secure request', function () { - it('Should check a secure request') + describe('When removing a video', function () { + it('Should have 404 with nothing', function (done) { + request(url) + .delete(path) + .expect(404, done) }) - describe('When adding a video', function () { - it('Should check when adding a video') + it('Should fail without a mongodb id', function (done) { + request(url) + .delete(path + 'hello') + .expect(400, done) }) - describe('When removing a video', function () { - it('Should check when removing a video') + it('Should fail with a video which does not exist', function (done) { + request(url) + .delete(path + '123456789012345678901234') + .expect(404, done) }) + + it('Should fail with a video of another pod') + + it('Should succeed with the correct parameters') }) + }) - after(function (done) { - process.kill(-app.pid) + describe('Of the remote videos API', function () { + describe('When making a secure request', function () { + it('Should check a secure request') + }) - // Keep the logs if the test failed - if (this.ok) { - utils.flushTests(done) - } else { - done() - } + describe('When adding a video', function () { + it('Should check when adding a video') }) + + describe('When removing a video', function () { + it('Should check when removing a video') + }) + }) + + after(function (done) { + process.kill(-app.pid) + + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(done) + } else { + done() + } }) -})() +}) diff --git a/tests/api/friendsAdvanced.js b/tests/api/friendsAdvanced.js index 61483bee6..9838d890f 100644 --- a/tests/api/friendsAdvanced.js +++ b/tests/api/friendsAdvanced.js @@ -1,252 +1,250 @@ -;(function () { - 'use strict' +'use strict' - var async = require('async') - var chai = require('chai') - var expect = chai.expect +var async = require('async') +var chai = require('chai') +var expect = chai.expect - var utils = require('./utils') +var utils = require('./utils') - describe('Test advanced friends', function () { - var apps = [] - var urls = [] +describe('Test advanced friends', function () { + var apps = [] + var urls = [] - function makeFriends (pod_number, callback) { - return utils.makeFriends(urls[pod_number - 1], callback) - } + function makeFriends (pod_number, callback) { + return utils.makeFriends(urls[pod_number - 1], callback) + } - function quitFriends (pod_number, callback) { - return utils.quitFriends(urls[pod_number - 1], callback) - } + function quitFriends (pod_number, callback) { + return utils.quitFriends(urls[pod_number - 1], callback) + } - function getFriendsList (pod_number, end) { - return utils.getFriendsList(urls[pod_number - 1], end) - } + function getFriendsList (pod_number, end) { + return utils.getFriendsList(urls[pod_number - 1], end) + } - function uploadVideo (pod_number, callback) { - var name = 'my super video' - var description = 'my super description' - var fixture = 'video_short.webm' + function uploadVideo (pod_number, callback) { + var name = 'my super video' + var description = 'my super description' + var fixture = 'video_short.webm' - return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) - } + return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) + } - function getVideos (pod_number, callback) { - return utils.getVideosList(urls[pod_number - 1], callback) - } + function getVideos (pod_number, callback) { + return utils.getVideosList(urls[pod_number - 1], callback) + } - // --------------------------------------------------------------- + // --------------------------------------------------------------- - before(function (done) { - this.timeout(30000) - utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { - apps = apps_run - urls = urls_run - done() - }) + before(function (done) { + this.timeout(30000) + utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + done() }) + }) - it('Should make friends with two pod each in a different group', function (done) { - this.timeout(20000) - - async.series([ - // Pod 3 makes friend with the first one - function (next) { - makeFriends(3, next) - }, - // Pod 4 makes friend with the second one - function (next) { - makeFriends(4, next) - }, - // Now if the fifth wants to make friends with the third et the first - function (next) { - makeFriends(5, next) - }, - function (next) { - setTimeout(next, 11000) - }], - function (err) { + it('Should make friends with two pod each in a different group', function (done) { + this.timeout(20000) + + async.series([ + // Pod 3 makes friend with the first one + function (next) { + makeFriends(3, next) + }, + // Pod 4 makes friend with the second one + function (next) { + makeFriends(4, next) + }, + // Now if the fifth wants to make friends with the third et the first + function (next) { + makeFriends(5, next) + }, + function (next) { + setTimeout(next, 11000) + }], + function (err) { + if (err) throw err + + // It should have 0 friends + getFriendsList(5, function (err, res) { if (err) throw err - // It should have 0 friends - getFriendsList(5, function (err, res) { + expect(res.body.length).to.equal(0) + + done() + }) + } + ) + }) + + it('Should quit all friends', function (done) { + this.timeout(10000) + + async.series([ + function (next) { + quitFriends(1, next) + }, + function (next) { + quitFriends(2, next) + }], + function (err) { + if (err) throw err + + async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) { + getFriendsList(i, function (err, res) { if (err) throw err expect(res.body.length).to.equal(0) - done() + callback() }) - } - ) - }) + }, done) + } + ) + }) - it('Should quit all friends', function (done) { - this.timeout(10000) - - async.series([ - function (next) { - quitFriends(1, next) - }, - function (next) { - quitFriends(2, next) - }], - function (err) { + it('Should make friends with the pods 1, 2, 3', function (done) { + this.timeout(150000) + + async.series([ + // Pods 1, 2, 3 and 4 become friends + function (next) { + makeFriends(2, next) + }, + function (next) { + makeFriends(1, next) + }, + function (next) { + makeFriends(4, next) + }, + // Kill pod 4 + function (next) { + apps[3].kill() + next() + }, + // Expulse pod 4 from pod 1 and 2 + function (next) { + uploadVideo(1, next) + }, + function (next) { + uploadVideo(2, next) + }, + function (next) { + setTimeout(next, 11000) + }, + function (next) { + uploadVideo(1, next) + }, + function (next) { + uploadVideo(2, next) + }, + function (next) { + setTimeout(next, 20000) + }, + // Rerun server 4 + function (next) { + utils.runServer(4, function (app, url) { + apps[3] = app + next() + }) + }, + function (next) { + getFriendsList(4, function (err, res) { if (err) throw err - async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) { - getFriendsList(i, function (err, res) { - if (err) throw err - - expect(res.body.length).to.equal(0) + // Pod 4 didn't know pod 1 and 2 removed it + expect(res.body.length).to.equal(3) - callback() - }) - }, done) - } - ) - }) - - it('Should make friends with the pods 1, 2, 3', function (done) { - this.timeout(150000) - - async.series([ - // Pods 1, 2, 3 and 4 become friends - function (next) { - makeFriends(2, next) - }, - function (next) { - makeFriends(1, next) - }, - function (next) { - makeFriends(4, next) - }, - // Kill pod 4 - function (next) { - apps[3].kill() next() - }, - // Expulse pod 4 from pod 1 and 2 - function (next) { - uploadVideo(1, next) - }, - function (next) { - uploadVideo(2, next) - }, - function (next) { - setTimeout(next, 11000) - }, - function (next) { - uploadVideo(1, next) - }, - function (next) { - uploadVideo(2, next) - }, - function (next) { - setTimeout(next, 20000) - }, - // Rerun server 4 - function (next) { - utils.runServer(4, function (app, url) { - apps[3] = app - next() - }) - }, - function (next) { - getFriendsList(4, function (err, res) { - if (err) throw err - - // Pod 4 didn't know pod 1 and 2 removed it - expect(res.body.length).to.equal(3) - - next() - }) - }, - // Pod 6 ask pod 1, 2 and 3 - function (next) { - makeFriends(6, next) - }], - function (err) { + }) + }, + // Pod 6 ask pod 1, 2 and 3 + function (next) { + makeFriends(6, next) + }], + function (err) { + if (err) throw err + + getFriendsList(6, function (err, res) { if (err) throw err - getFriendsList(6, function (err, res) { - if (err) throw err + // Pod 4 should not be our friend + var result = res.body + expect(result.length).to.equal(3) + for (var pod of result) { + expect(pod.url).not.equal(urls[3]) + } - // Pod 4 should not be our friend - var result = res.body - expect(result.length).to.equal(3) - for (var pod of result) { - expect(pod.url).not.equal(urls[3]) - } + done() + }) + } + ) + }) - done() - }) - } - ) - }) + it('Should pod 1 quit friends', function (done) { + this.timeout(25000) + + async.series([ + // Upload a video on server 3 for aditionnal tests + function (next) { + uploadVideo(3, next) + }, + function (next) { + setTimeout(next, 15000) + }, + function (next) { + quitFriends(1, next) + }, + // Remove pod 1 from pod 2 + function (next) { + getVideos(1, function (err, res) { + if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(2) - it('Should pod 1 quit friends', function (done) { - this.timeout(25000) - - async.series([ - // Upload a video on server 3 for aditionnal tests - function (next) { - uploadVideo(3, next) - }, - function (next) { - setTimeout(next, 15000) - }, - function (next) { - quitFriends(1, next) - }, - // Remove pod 1 from pod 2 - function (next) { - getVideos(1, function (err, res) { - if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(2) + next() + }) + }], + function (err) { + if (err) throw err - next() - }) - }], - function (err) { + getVideos(2, function (err, res) { if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(3) + done() + }) + } + ) + }) - getVideos(2, function (err, res) { - if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(3) - done() - }) - } - ) - }) - - it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { - this.timeout(20000) - makeFriends(1, function () { - setTimeout(function () { - getVideos(1, function (err, res) { - if (err) throw err + it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { + this.timeout(20000) + makeFriends(1, function () { + setTimeout(function () { + getVideos(1, function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(5) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(5) - done() - }) - }, 5000) - }) + done() + }) + }, 5000) }) + }) - after(function (done) { - apps.forEach(function (app) { - process.kill(-app.pid) - }) - - if (this.ok) { - utils.flushTests(done) - } else { - done() - } + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) }) + + if (this.ok) { + utils.flushTests(done) + } else { + done() + } }) -})() +}) diff --git a/tests/api/friendsBasic.js b/tests/api/friendsBasic.js index dbc918383..328724936 100644 --- a/tests/api/friendsBasic.js +++ b/tests/api/friendsBasic.js @@ -1,187 +1,185 @@ -;(function () { - 'use strict' +'use strict' - var async = require('async') - var chai = require('chai') - var expect = chai.expect - var request = require('supertest') +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var request = require('supertest') - var utils = require('./utils') +var utils = require('./utils') - describe('Test basic friends', function () { - var apps = [] - var urls = [] +describe('Test basic friends', function () { + var apps = [] + var urls = [] - function testMadeFriends (urls, url_to_test, callback) { - var friends = [] - for (var i = 0; i < urls.length; i++) { - if (urls[i] === url_to_test) continue - friends.push(urls[i]) - } + function testMadeFriends (urls, url_to_test, callback) { + var friends = [] + for (var i = 0; i < urls.length; i++) { + if (urls[i] === url_to_test) continue + friends.push(urls[i]) + } + + utils.getFriendsList(url_to_test, function (err, res) { + if (err) throw err + + var result = res.body + var result_urls = [ result[0].url, result[1].url ] + expect(result).to.be.an('array') + expect(result.length).to.equal(2) + expect(result_urls[0]).to.not.equal(result_urls[1]) - utils.getFriendsList(url_to_test, function (err, res) { + var error_string = 'Friends url do not correspond for ' + url_to_test + expect(friends).to.contain(result_urls[0], error_string) + expect(friends).to.contain(result_urls[1], error_string) + callback() + }) + } + + // --------------------------------------------------------------- + + before(function (done) { + this.timeout(20000) + utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + done() + }) + }) + + it('Should not have friends', function (done) { + async.each(urls, function (url, callback) { + utils.getFriendsList(url, function (err, res) { if (err) throw err var result = res.body - var result_urls = [ result[0].url, result[1].url ] expect(result).to.be.an('array') - expect(result.length).to.equal(2) - expect(result_urls[0]).to.not.equal(result_urls[1]) - - var error_string = 'Friends url do not correspond for ' + url_to_test - expect(friends).to.contain(result_urls[0], error_string) - expect(friends).to.contain(result_urls[1], error_string) + expect(result.length).to.equal(0) callback() }) - } + }, done) + }) - // --------------------------------------------------------------- + it('Should make friends', function (done) { + this.timeout(10000) + + var path = '/api/v1/pods/makefriends' + + async.series([ + // The second pod make friend with the third + function (next) { + request(urls[1]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 1000) + }, + // The second pod should have the third as a friend + function (next) { + utils.getFriendsList(urls[1], function (err, res) { + if (err) throw err - before(function (done) { - this.timeout(20000) - utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { - apps = apps_run - urls = urls_run - done() - }) - }) + var result = res.body + expect(result).to.be.an('array') + expect(result.length).to.equal(1) + expect(result[0].url).to.be.equal(urls[2]) - it('Should not have friends', function (done) { - async.each(urls, function (url, callback) { - utils.getFriendsList(url, function (err, res) { + next() + }) + }, + // Same here, the third pod should have the second pod as a friend + function (next) { + utils.getFriendsList(urls[2], function (err, res) { if (err) throw err var result = res.body expect(result).to.be.an('array') - expect(result.length).to.equal(0) - callback() + expect(result.length).to.equal(1) + expect(result[0].url).to.be.equal(urls[1]) + + next() }) + }, + // Finally the first pod make friend with the second pod + function (next) { + request(urls[0]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 1000) + } + ], + // Now each pod should be friend with the other ones + function (err) { + if (err) throw err + async.each(urls, function (url, callback) { + testMadeFriends(urls, url, callback) }, done) }) + }) - it('Should make friends', function (done) { - this.timeout(10000) - - var path = '/api/v1/pods/makefriends' - - async.series([ - // The second pod make friend with the third - function (next) { - request(urls[1]) - .get(path) - .set('Accept', 'application/json') - .expect(204) - .end(next) - }, - // Wait for the request between pods - function (next) { - setTimeout(next, 1000) - }, - // The second pod should have the third as a friend - function (next) { - utils.getFriendsList(urls[1], function (err, res) { - if (err) throw err + it('Should not be allowed to make friend again', function (done) { + utils.makeFriends(urls[1], 409, done) + }) - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(urls[2]) + it('Should quit friends of pod 2', function (done) { + async.series([ + // Pod 1 quit friends + function (next) { + utils.quitFriends(urls[1], next) + }, + // Pod 1 should not have friends anymore + function (next) { + utils.getFriendsList(urls[1], function (err, res) { + if (err) throw err - next() - }) - }, - // Same here, the third pod should have the second pod as a friend - function (next) { - utils.getFriendsList(urls[2], function (err, res) { + var result = res.body + expect(result).to.be.an('array') + expect(result.length).to.equal(0) + + next() + }) + }, + // Other pods shouldn't have pod 1 too + function (next) { + async.each([ urls[0], urls[2] ], function (url, callback) { + utils.getFriendsList(url, function (err, res) { if (err) throw err var result = res.body expect(result).to.be.an('array') expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(urls[1]) - - next() + expect(result[0].url).not.to.be.equal(urls[1]) + callback() }) - }, - // Finally the first pod make friend with the second pod - function (next) { - request(urls[0]) - .get(path) - .set('Accept', 'application/json') - .expect(204) - .end(next) - }, - // Wait for the request between pods - function (next) { - setTimeout(next, 1000) - } - ], - // Now each pod should be friend with the other ones - function (err) { - if (err) throw err - async.each(urls, function (url, callback) { - testMadeFriends(urls, url, callback) - }, done) - }) - }) - - it('Should not be allowed to make friend again', function (done) { - utils.makeFriends(urls[1], 409, done) - }) - - it('Should quit friends of pod 2', function (done) { - async.series([ - // Pod 1 quit friends - function (next) { - utils.quitFriends(urls[1], next) - }, - // Pod 1 should not have friends anymore - function (next) { - utils.getFriendsList(urls[1], function (err, res) { - if (err) throw err - - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(0) + }, next) + } + ], done) + }) - next() - }) - }, - // Other pods shouldn't have pod 1 too - function (next) { - async.each([ urls[0], urls[2] ], function (url, callback) { - utils.getFriendsList(url, function (err, res) { - if (err) throw err - - var result = res.body - expect(result).to.be.an('array') - expect(result.length).to.equal(1) - expect(result[0].url).not.to.be.equal(urls[1]) - callback() - }) - }, next) - } - ], done) + it('Should allow pod 2 to make friend again', function (done) { + utils.makeFriends(urls[1], function () { + async.each(urls, function (url, callback) { + testMadeFriends(urls, url, callback) + }, done) }) + }) - it('Should allow pod 2 to make friend again', function (done) { - utils.makeFriends(urls[1], function () { - async.each(urls, function (url, callback) { - testMadeFriends(urls, url, callback) - }, done) - }) + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) }) - after(function (done) { - apps.forEach(function (app) { - process.kill(-app.pid) - }) - - if (this.ok) { - utils.flushTests(done) - } else { - done() - } - }) + if (this.ok) { + utils.flushTests(done) + } else { + done() + } }) -})() +}) diff --git a/tests/api/index.js b/tests/api/index.js index 3bdcdae2d..9c4fdd48a 100644 --- a/tests/api/index.js +++ b/tests/api/index.js @@ -1,10 +1,8 @@ -;(function () { - 'use strict' +'use strict' - // Order of the tests we want to execute - require('./checkParams') - require('./friendsBasic') - require('./singlePod') - require('./multiplePods') - require('./friendsAdvanced') -})() +// Order of the tests we want to execute +require('./checkParams') +require('./friendsBasic') +require('./singlePod') +require('./multiplePods') +require('./friendsAdvanced') diff --git a/tests/api/multiplePods.js b/tests/api/multiplePods.js index 7949da80a..9fdd0f308 100644 --- a/tests/api/multiplePods.js +++ b/tests/api/multiplePods.js @@ -1,330 +1,328 @@ -;(function () { - 'use strict' +'use strict' + +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var pathUtils = require('path') + +var utils = require('./utils') +var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) +webtorrent.silent = true + +describe('Test multiple pods', function () { + var apps = [] + var urls = [] + var to_remove = [] + + before(function (done) { + this.timeout(30000) + + async.series([ + // Run servers + function (next) { + utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + next() + }) + }, + // The second pod make friend with the third + function (next) { + utils.makeFriends(urls[1], next) + }, + // Wait for the request between pods + function (next) { + setTimeout(next, 10000) + }, + // Pod 1 make friends too + function (next) { + utils.makeFriends(urls[0], next) + }, + function (next) { + webtorrent.create({ host: 'client', port: '1' }, next) + } + ], done) + }) - var async = require('async') - var chai = require('chai') - var expect = chai.expect - var pathUtils = require('path') + it('Should not have videos for all pods', function (done) { + async.each(urls, function (url, callback) { + utils.getVideosList(url, function (err, res) { + if (err) throw err - var utils = require('./utils') - var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) - webtorrent.silent = true + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) - describe('Test multiple pods', function () { - var apps = [] - var urls = [] - var to_remove = [] + callback() + }) + }, done) + }) - before(function (done) { - this.timeout(30000) + describe('Should upload the video and propagate on each pod', function () { + it('Should upload the video on pod 1 and propagate on each pod', function (done) { + this.timeout(15000) async.series([ - // Run servers - function (next) { - utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { - apps = apps_run - urls = urls_run - next() - }) - }, - // The second pod make friend with the third function (next) { - utils.makeFriends(urls[1], next) + utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) }, - // Wait for the request between pods function (next) { - setTimeout(next, 10000) - }, - // Pod 1 make friends too + setTimeout(next, 11000) + }], + // All pods should have this video + function (err) { + if (err) throw err + + async.each(urls, function (url, callback) { + var base_magnet = null + + utils.getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(1) + var video = videos[0] + expect(video.name).to.equal('my super name for pod 1') + expect(video.description).to.equal('my super description for pod 1') + expect(video.podUrl).to.equal('http://localhost:9001') + expect(video.magnetUri).to.exist + + // All pods should have the same magnet Uri + if (base_magnet === null) { + base_magnet = video.magnetUri + } else { + expect(video.magnetUri).to.equal.magnetUri + } + + callback() + }) + }, done) + } + ) + }) + + it('Should upload the video on pod 2 and propagate on each pod', function (done) { + this.timeout(15000) + + async.series([ function (next) { - utils.makeFriends(urls[0], next) + utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) }, function (next) { - webtorrent.create({ host: 'client', port: '1' }, next) + setTimeout(next, 11000) + }], + // All pods should have this video + function (err) { + if (err) throw err + + async.each(urls, function (url, callback) { + var base_magnet = null + + utils.getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(2) + var video = videos[1] + expect(video.name).to.equal('my super name for pod 2') + expect(video.description).to.equal('my super description for pod 2') + expect(video.podUrl).to.equal('http://localhost:9002') + expect(video.magnetUri).to.exist + + // All pods should have the same magnet Uri + if (base_magnet === null) { + base_magnet = video.magnetUri + } else { + expect(video.magnetUri).to.equal.magnetUri + } + + callback() + }) + }, done) } - ], done) + ) }) - it('Should not have videos for all pods', function (done) { - async.each(urls, function (url, callback) { - utils.getVideosList(url, function (err, res) { - if (err) throw err + it('Should upload two videos on pod 3 and propagate on each pod', function (done) { + this.timeout(30000) - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(0) + async.series([ + function (next) { + utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) + }, + function (next) { + utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) + }, + function (next) { + setTimeout(next, 22000) + }], + function (err) { + if (err) throw err - callback() - }) - }, done) + var base_magnet = null + // All pods should have this video + async.each(urls, function (url, callback) { + utils.getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(4) + var video = videos[2] + expect(video.name).to.equal('my super name for pod 3') + expect(video.description).to.equal('my super description for pod 3') + expect(video.podUrl).to.equal('http://localhost:9003') + expect(video.magnetUri).to.exist + + video = videos[3] + expect(video.name).to.equal('my super name for pod 3-2') + expect(video.description).to.equal('my super description for pod 3-2') + expect(video.podUrl).to.equal('http://localhost:9003') + expect(video.magnetUri).to.exist + + // All pods should have the same magnet Uri + if (base_magnet === null) { + base_magnet = video.magnetUri + } else { + expect(video.magnetUri).to.equal.magnetUri + } + + callback() + }) + }, done) + } + ) }) + }) - describe('Should upload the video and propagate on each pod', function () { - it('Should upload the video on pod 1 and propagate on each pod', function (done) { - this.timeout(15000) - - async.series([ - function (next) { - utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) - }, - function (next) { - setTimeout(next, 11000) - }], - // All pods should have this video - function (err) { - if (err) throw err - - async.each(urls, function (url, callback) { - var base_magnet = null - - utils.getVideosList(url, function (err, res) { - if (err) throw err - - var videos = res.body - expect(videos).to.be.an('array') - expect(videos.length).to.equal(1) - var video = videos[0] - expect(video.name).to.equal('my super name for pod 1') - expect(video.description).to.equal('my super description for pod 1') - expect(video.podUrl).to.equal('http://localhost:9001') - expect(video.magnetUri).to.exist - - // All pods should have the same magnet Uri - if (base_magnet === null) { - base_magnet = video.magnetUri - } else { - expect(video.magnetUri).to.equal.magnetUri - } - - callback() - }) - }, done) - } - ) - }) + describe('Should seed the uploaded video', function () { + it('Should add the file 1 by asking pod 3', function (done) { + // Yes, this could be long + this.timeout(200000) - it('Should upload the video on pod 2 and propagate on each pod', function (done) { - this.timeout(15000) + utils.getVideosList(urls[2], function (err, res) { + if (err) throw err - async.series([ - function (next) { - utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) - }, - function (next) { - setTimeout(next, 11000) - }], - // All pods should have this video - function (err) { - if (err) throw err - - async.each(urls, function (url, callback) { - var base_magnet = null - - utils.getVideosList(url, function (err, res) { - if (err) throw err - - var videos = res.body - expect(videos).to.be.an('array') - expect(videos.length).to.equal(2) - var video = videos[1] - expect(video.name).to.equal('my super name for pod 2') - expect(video.description).to.equal('my super description for pod 2') - expect(video.podUrl).to.equal('http://localhost:9002') - expect(video.magnetUri).to.exist - - // All pods should have the same magnet Uri - if (base_magnet === null) { - base_magnet = video.magnetUri - } else { - expect(video.magnetUri).to.equal.magnetUri - } - - callback() - }) - }, done) - } - ) - }) + var video = res.body[0] + to_remove.push(res.body[2]._id) + to_remove.push(res.body[3]._id) - it('Should upload two videos on pod 3 and propagate on each pod', function (done) { - this.timeout(30000) - - async.series([ - function (next) { - utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) - }, - function (next) { - utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) - }, - function (next) { - setTimeout(next, 22000) - }], - function (err) { - if (err) throw err + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') - var base_magnet = null - // All pods should have this video - async.each(urls, function (url, callback) { - utils.getVideosList(url, function (err, res) { - if (err) throw err - - var videos = res.body - expect(videos).to.be.an('array') - expect(videos.length).to.equal(4) - var video = videos[2] - expect(video.name).to.equal('my super name for pod 3') - expect(video.description).to.equal('my super description for pod 3') - expect(video.podUrl).to.equal('http://localhost:9003') - expect(video.magnetUri).to.exist - - video = videos[3] - expect(video.name).to.equal('my super name for pod 3-2') - expect(video.description).to.equal('my super description for pod 3-2') - expect(video.podUrl).to.equal('http://localhost:9003') - expect(video.magnetUri).to.exist - - // All pods should have the same magnet Uri - if (base_magnet === null) { - base_magnet = video.magnetUri - } else { - expect(video.magnetUri).to.equal.magnetUri - } - - callback() - }) - }, done) - } - ) + done() + }) }) }) - describe('Should seed the uploaded video', function () { - it('Should add the file 1 by asking pod 3', function (done) { - // Yes, this could be long - this.timeout(200000) + it('Should add the file 2 by asking pod 1', function (done) { + // Yes, this could be long + this.timeout(200000) - utils.getVideosList(urls[2], function (err, res) { - if (err) throw err + utils.getVideosList(urls[0], function (err, res) { + if (err) throw err - var video = res.body[0] - to_remove.push(res.body[2]._id) - to_remove.push(res.body[3]._id) + var video = res.body[1] - webtorrent.add(video.magnetUri, function (torrent) { - expect(torrent.files).to.exist - expect(torrent.files.length).to.equal(1) - expect(torrent.files[0].path).to.exist.and.to.not.equal('') + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') - done() - }) + done() }) }) + }) - it('Should add the file 2 by asking pod 1', function (done) { - // Yes, this could be long - this.timeout(200000) + it('Should add the file 3 by asking pod 2', function (done) { + // Yes, this could be long + this.timeout(200000) - utils.getVideosList(urls[0], function (err, res) { - if (err) throw err + utils.getVideosList(urls[1], function (err, res) { + if (err) throw err - var video = res.body[1] + var video = res.body[2] - webtorrent.add(video.magnetUri, function (torrent) { - expect(torrent.files).to.exist - expect(torrent.files.length).to.equal(1) - expect(torrent.files[0].path).to.exist.and.to.not.equal('') + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') - done() - }) + done() }) }) + }) - it('Should add the file 3 by asking pod 2', function (done) { - // Yes, this could be long - this.timeout(200000) + it('Should add the file 3-2 by asking pod 1', function (done) { + // Yes, this could be long + this.timeout(200000) - utils.getVideosList(urls[1], function (err, res) { - if (err) throw err + utils.getVideosList(urls[0], function (err, res) { + if (err) throw err - var video = res.body[2] + var video = res.body[3] - webtorrent.add(video.magnetUri, function (torrent) { - expect(torrent.files).to.exist - expect(torrent.files.length).to.equal(1) - expect(torrent.files[0].path).to.exist.and.to.not.equal('') + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') - done() - }) + done() }) }) + }) - it('Should add the file 3-2 by asking pod 1', function (done) { - // Yes, this could be long - this.timeout(200000) + it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { + this.timeout(15000) - utils.getVideosList(urls[0], function (err, res) { + async.series([ + function (next) { + utils.removeVideo(urls[2], to_remove[0], next) + }, + function (next) { + utils.removeVideo(urls[2], to_remove[1], next) + }], + function (err) { if (err) throw err + setTimeout(done, 11000) + } + ) + }) - var video = res.body[3] + it('Should have videos 1 and 3 on each pod', function (done) { + async.each(urls, function (url, callback) { + utils.getVideosList(url, function (err, res) { + if (err) throw err - webtorrent.add(video.magnetUri, function (torrent) { - expect(torrent.files).to.exist - expect(torrent.files.length).to.equal(1) - expect(torrent.files[0].path).to.exist.and.to.not.equal('') + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(2) + expect(videos[0]._id).not.to.equal(videos[1]._id) + expect(videos[0]._id).not.to.equal(to_remove[0]) + expect(videos[1]._id).not.to.equal(to_remove[0]) + expect(videos[0]._id).not.to.equal(to_remove[1]) + expect(videos[1]._id).not.to.equal(to_remove[1]) - done() - }) + callback() }) - }) - - it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { - this.timeout(15000) - - async.series([ - function (next) { - utils.removeVideo(urls[2], to_remove[0], next) - }, - function (next) { - utils.removeVideo(urls[2], to_remove[1], next) - }], - function (err) { - if (err) throw err - setTimeout(done, 11000) - } - ) - }) - - it('Should have videos 1 and 3 on each pod', function (done) { - async.each(urls, function (url, callback) { - utils.getVideosList(url, function (err, res) { - if (err) throw err - - var videos = res.body - expect(videos).to.be.an('array') - expect(videos.length).to.equal(2) - expect(videos[0]._id).not.to.equal(videos[1]._id) - expect(videos[0]._id).not.to.equal(to_remove[0]) - expect(videos[1]._id).not.to.equal(to_remove[0]) - expect(videos[0]._id).not.to.equal(to_remove[1]) - expect(videos[1]._id).not.to.equal(to_remove[1]) - - callback() - }) - }, done) - }) + }, done) }) + }) - after(function (done) { - apps.forEach(function (app) { - process.kill(-app.pid) - }) - process.kill(-webtorrent.app.pid) - - // Keep the logs if the test failed - if (this.ok) { - utils.flushTests(done) - } else { - done() - } + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) }) + process.kill(-webtorrent.app.pid) + + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(done) + } else { + done() + } }) -})() +}) diff --git a/tests/api/singlePod.js b/tests/api/singlePod.js index f33aa8c7a..3dd72c01b 100644 --- a/tests/api/singlePod.js +++ b/tests/api/singlePod.js @@ -1,148 +1,146 @@ -;(function () { - 'use strict' - - var async = require('async') - var chai = require('chai') - var expect = chai.expect - var fs = require('fs') - var pathUtils = require('path') - - var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) - webtorrent.silent = true - - var utils = require('./utils') - - describe('Test a single pod', function () { - var app = null - var url = '' - var video_id = -1 - - before(function (done) { - this.timeout(20000) - - async.series([ - function (next) { - utils.flushTests(next) - }, - function (next) { - utils.runServer(1, function (app1, url1) { - app = app1 - url = url1 - next() - }) - }, - function (next) { - webtorrent.create({ host: 'client', port: '1' }, next) - } - ], done) - }) +'use strict' + +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var fs = require('fs') +var pathUtils = require('path') + +var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) +webtorrent.silent = true + +var utils = require('./utils') + +describe('Test a single pod', function () { + var app = null + var url = '' + var video_id = -1 + + before(function (done) { + this.timeout(20000) + + async.series([ + function (next) { + utils.flushTests(next) + }, + function (next) { + utils.runServer(1, function (app1, url1) { + app = app1 + url = url1 + next() + }) + }, + function (next) { + webtorrent.create({ host: 'client', port: '1' }, next) + } + ], done) + }) - it('Should not have videos', function (done) { - utils.getVideosList(url, function (err, res) { - if (err) throw err + it('Should not have videos', function (done) { + utils.getVideosList(url, function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(0) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) - done() - }) + done() }) + }) - it('Should upload the video', function (done) { - this.timeout(5000) - utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done) - }) + it('Should upload the video', function (done) { + this.timeout(5000) + utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done) + }) - it('Should seed the uploaded video', function (done) { - // Yes, this could be long - this.timeout(60000) + it('Should seed the uploaded video', function (done) { + // Yes, this could be long + this.timeout(60000) - utils.getVideosList(url, function (err, res) { - if (err) throw err + utils.getVideosList(url, function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(1) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(1) - var video = res.body[0] - expect(video.name).to.equal('my super name') - expect(video.description).to.equal('my super description') - expect(video.podUrl).to.equal('http://localhost:9001') - expect(video.magnetUri).to.exist + var video = res.body[0] + expect(video.name).to.equal('my super name') + expect(video.description).to.equal('my super description') + expect(video.podUrl).to.equal('http://localhost:9001') + expect(video.magnetUri).to.exist - video_id = video._id + video_id = video._id - webtorrent.add(video.magnetUri, function (torrent) { - expect(torrent.files).to.exist - expect(torrent.files.length).to.equal(1) - expect(torrent.files[0].path).to.exist.and.to.not.equal('') + webtorrent.add(video.magnetUri, function (torrent) { + expect(torrent.files).to.exist + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') - done() - }) + done() }) }) + }) - it('Should search the video', function (done) { - utils.searchVideo(url, 'my', function (err, res) { - if (err) throw err + it('Should search the video', function (done) { + utils.searchVideo(url, 'my', function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(1) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(1) - var video = res.body[0] - expect(video.name).to.equal('my super name') - expect(video.description).to.equal('my super description') - expect(video.podUrl).to.equal('http://localhost:9001') - expect(video.magnetUri).to.exist + var video = res.body[0] + expect(video.name).to.equal('my super name') + expect(video.description).to.equal('my super description') + expect(video.podUrl).to.equal('http://localhost:9001') + expect(video.magnetUri).to.exist - done() - }) + done() }) + }) - it('Should not find a search', function (done) { - utils.searchVideo(url, 'hello', function (err, res) { - if (err) throw err + it('Should not find a search', function (done) { + utils.searchVideo(url, 'hello', function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(0) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) - done() - }) + done() }) + }) - it('Should remove the video', function (done) { - utils.removeVideo(url, video_id, function (err) { - if (err) throw err + it('Should remove the video', function (done) { + utils.removeVideo(url, video_id, function (err) { + if (err) throw err - fs.readdir(pathUtils.join(__dirname, '../../test1/uploads/'), function (err, files) { - if (err) throw err + fs.readdir(pathUtils.join(__dirname, '../../test1/uploads/'), function (err, files) { + if (err) throw err - expect(files.length).to.equal(0) - done() - }) + expect(files.length).to.equal(0) + done() }) }) + }) - it('Should not have videos', function (done) { - utils.getVideosList(url, function (err, res) { - if (err) throw err + it('Should not have videos', function (done) { + utils.getVideosList(url, function (err, res) { + if (err) throw err - expect(res.body).to.be.an('array') - expect(res.body.length).to.equal(0) + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) - done() - }) + done() }) + }) - after(function (done) { - process.kill(-app.pid) - process.kill(-webtorrent.app.pid) + after(function (done) { + process.kill(-app.pid) + process.kill(-webtorrent.app.pid) - // Keep the logs if the test failed - if (this.ok) { - utils.flushTests(done) - } else { - done() - } - }) + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(done) + } else { + done() + } }) -})() +}) diff --git a/tests/api/utils.js b/tests/api/utils.js index b71e943ed..47b706294 100644 --- a/tests/api/utils.js +++ b/tests/api/utils.js @@ -1,187 +1,185 @@ -;(function () { - 'use strict' - - var child_process = require('child_process') - var exec = child_process.exec - var fork = child_process.fork - var pathUtils = require('path') - var request = require('supertest') - - var testUtils = { - flushTests: flushTests, - getFriendsList: getFriendsList, - getVideosList: getVideosList, - makeFriends: makeFriends, - quitFriends: quitFriends, - removeVideo: removeVideo, - flushAndRunMultipleServers: flushAndRunMultipleServers, - runServer: runServer, - searchVideo: searchVideo, - uploadVideo: uploadVideo +'use strict' + +var child_process = require('child_process') +var exec = child_process.exec +var fork = child_process.fork +var pathUtils = require('path') +var request = require('supertest') + +var testUtils = { + flushTests: flushTests, + getFriendsList: getFriendsList, + getVideosList: getVideosList, + makeFriends: makeFriends, + quitFriends: quitFriends, + removeVideo: removeVideo, + flushAndRunMultipleServers: flushAndRunMultipleServers, + runServer: runServer, + searchVideo: searchVideo, + uploadVideo: uploadVideo +} + +// ---------------------- Export functions -------------------- + +function flushTests (callback) { + exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback) +} + +function getFriendsList (url, end) { + var path = '/api/v1/pods/' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function getVideosList (url, end) { + var path = '/api/v1/videos' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function makeFriends (url, expected_status, callback) { + if (!callback) { + callback = expected_status + expected_status = 204 } - // ---------------------- Export functions -------------------- + var path = '/api/v1/pods/makefriends' - function flushTests (callback) { - exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback) - } - - function getFriendsList (url, end) { - var path = '/api/v1/pods/' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) - } - - function getVideosList (url, end) { - var path = '/api/v1/videos' - - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) - } - - function makeFriends (url, expected_status, callback) { - if (!callback) { - callback = expected_status - expected_status = 204 - } - - var path = '/api/v1/pods/makefriends' - - // The first pod make friend with the third - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(expected_status) - .end(function (err, res) { - if (err) throw err - - // Wait for the request between pods - setTimeout(callback, 1000) - }) - } - - function quitFriends (url, callback) { - var path = '/api/v1/pods/quitfriends' + // The first pod make friend with the third + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(expected_status) + .end(function (err, res) { + if (err) throw err - // The first pod make friend with the third - request(url) - .get(path) - .set('Accept', 'application/json') - .expect(204) - .end(function (err, res) { - if (err) throw err - - // Wait for the request between pods - setTimeout(callback, 1000) - }) - } - - function removeVideo (url, id, end) { - var path = '/api/v1/videos' - - request(url) - .delete(path + '/' + id) - .set('Accept', 'application/json') - .expect(204) - .end(end) - } - - function flushAndRunMultipleServers (total_servers, serversRun) { - var apps = [] - var urls = [] - var i = 0 - - function anotherServerDone (number, app, url) { - apps[number - 1] = app - urls[number - 1] = url - i++ - if (i === total_servers) { - serversRun(apps, urls) - } - } - - flushTests(function () { - for (var j = 1; j <= total_servers; j++) { - (function (k) { // TODO: ES6 with let - // For the virtual buffer - setTimeout(function () { - runServer(k, function (app, url) { - anotherServerDone(k, app, url) - }) - }, 1000 * k) - })(j) - } + // Wait for the request between pods + setTimeout(callback, 1000) }) - } +} - function runServer (number, callback) { - var port = 9000 + number - var server_run_string = { - 'Connected to mongodb': false, - 'Server listening on port': false - } +function quitFriends (url, callback) { + var path = '/api/v1/pods/quitfriends' - // Share the environment - var env = Object.create(process.env) - env.NODE_ENV = 'test' - env.NODE_APP_INSTANCE = number - var options = { - silent: true, - env: env, - detached: true - } - - var app = fork(pathUtils.join(__dirname, '../../server.js'), [], options) - app.stdout.on('data', function onStdout (data) { - var dont_continue = false - // Check if all required sentences are here - for (var key of Object.keys(server_run_string)) { - if (data.toString().indexOf(key) !== -1) server_run_string[key] = true - if (server_run_string[key] === false) dont_continue = true - } + // The first pod make friend with the third + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err - // If no, there is maybe one thing not already initialized (mongodb...) - if (dont_continue === true) return - - app.stdout.removeListener('data', onStdout) - callback(app, 'http://localhost:' + port) + // Wait for the request between pods + setTimeout(callback, 1000) }) +} + +function removeVideo (url, id, end) { + var path = '/api/v1/videos' + + request(url) + .delete(path + '/' + id) + .set('Accept', 'application/json') + .expect(204) + .end(end) +} + +function flushAndRunMultipleServers (total_servers, serversRun) { + var apps = [] + var urls = [] + var i = 0 + + function anotherServerDone (number, app, url) { + apps[number - 1] = app + urls[number - 1] = url + i++ + if (i === total_servers) { + serversRun(apps, urls) + } } - function searchVideo (url, search, end) { - var path = '/api/v1/videos' - - request(url) - .get(path + '/search/' + search) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end(end) + flushTests(function () { + for (var j = 1; j <= total_servers; j++) { + (function (k) { // TODO: ES6 with let + // For the virtual buffer + setTimeout(function () { + runServer(k, function (app, url) { + anotherServerDone(k, app, url) + }) + }, 1000 * k) + })(j) + } + }) +} + +function runServer (number, callback) { + var port = 9000 + number + var server_run_string = { + 'Connected to mongodb': false, + 'Server listening on port': false } - function uploadVideo (url, name, description, fixture, end) { - var path = '/api/v1/videos' - - request(url) - .post(path) - .set('Accept', 'application/json') - .field('name', name) - .field('description', description) - .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture)) - .expect(201) - .end(end) + // Share the environment + var env = Object.create(process.env) + env.NODE_ENV = 'test' + env.NODE_APP_INSTANCE = number + var options = { + silent: true, + env: env, + detached: true } - // --------------------------------------------------------------------------- + var app = fork(pathUtils.join(__dirname, '../../server.js'), [], options) + app.stdout.on('data', function onStdout (data) { + var dont_continue = false + // Check if all required sentences are here + for (var key of Object.keys(server_run_string)) { + if (data.toString().indexOf(key) !== -1) server_run_string[key] = true + if (server_run_string[key] === false) dont_continue = true + } - module.exports = testUtils -})() + // If no, there is maybe one thing not already initialized (mongodb...) + if (dont_continue === true) return + + app.stdout.removeListener('data', onStdout) + callback(app, 'http://localhost:' + port) + }) +} + +function searchVideo (url, search, end) { + var path = '/api/v1/videos' + + request(url) + .get(path + '/search/' + search) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +function uploadVideo (url, name, description, fixture, end) { + var path = '/api/v1/videos' + + request(url) + .post(path) + .set('Accept', 'application/json') + .field('name', name) + .field('description', description) + .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture)) + .expect(201) + .end(end) +} + +// --------------------------------------------------------------------------- + +module.exports = testUtils -- cgit v1.2.3