From b9a3e09ad5a7673f64556d1dba122ed4c4fac980 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 7 Mar 2016 11:33:59 +0100 Subject: Prepare folders structure for angular app --- server/tests/api/checkParams.js | 300 ++++++++++++++++++++++ server/tests/api/fixtures/video_short.mp4 | Bin 0 -> 38783 bytes server/tests/api/fixtures/video_short.ogv | Bin 0 -> 140849 bytes server/tests/api/fixtures/video_short.webm | Bin 0 -> 218910 bytes server/tests/api/fixtures/video_short1.webm | Bin 0 -> 572456 bytes server/tests/api/fixtures/video_short2.webm | Bin 0 -> 942961 bytes server/tests/api/fixtures/video_short3.webm | Bin 0 -> 292677 bytes server/tests/api/fixtures/video_short_fake.webm | 1 + server/tests/api/friendsAdvanced.js | 250 ++++++++++++++++++ server/tests/api/friendsBasic.js | 185 +++++++++++++ server/tests/api/index.js | 8 + server/tests/api/multiplePods.js | 328 ++++++++++++++++++++++++ server/tests/api/singlePod.js | 146 +++++++++++ server/tests/api/utils.js | 185 +++++++++++++ server/tests/index.js | 6 + 15 files changed, 1409 insertions(+) create mode 100644 server/tests/api/checkParams.js create mode 100644 server/tests/api/fixtures/video_short.mp4 create mode 100644 server/tests/api/fixtures/video_short.ogv create mode 100644 server/tests/api/fixtures/video_short.webm create mode 100644 server/tests/api/fixtures/video_short1.webm create mode 100644 server/tests/api/fixtures/video_short2.webm create mode 100644 server/tests/api/fixtures/video_short3.webm create mode 100644 server/tests/api/fixtures/video_short_fake.webm create mode 100644 server/tests/api/friendsAdvanced.js create mode 100644 server/tests/api/friendsBasic.js create mode 100644 server/tests/api/index.js create mode 100644 server/tests/api/multiplePods.js create mode 100644 server/tests/api/singlePod.js create mode 100644 server/tests/api/utils.js create mode 100644 server/tests/index.js (limited to 'server/tests') diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js new file mode 100644 index 000000000..1c1ec71b3 --- /dev/null +++ b/server/tests/api/checkParams.js @@ -0,0 +1,300 @@ +'use strict' + +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var pathUtils = require('path') +var request = require('supertest') + +var utils = require('./utils') + +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 + + 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) + }) + + 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' + } + } + makePostBodyRequest(path, data, done) + }) + + it('Should fail without an url', function (done) { + var data = { + data: { + publicKey: 'mysuperpublickey' + } + } + makePostBodyRequest(path, data, done) + }) + + 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 = 'coucou' + makePostBodyRequest(path, data, done) + }) + }) + }) + + it('Should succeed with the correct parameters', function (done) { + var data = { + data: { + url: 'http://coucou.com', + publicKey: 'mysuperpublickey' + } + } + makePostBodyRequest(path, data, done, false) + }) + }) + }) + + 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 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 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 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 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') + } + 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) + }, 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() + }) + }) + + it('Should fail without a mongodb id', function (done) { + request(url) + .get(path + 'coucou') + .set('Accept', 'application/json') + .expect(400, done) + }) + + it('Should return 404 with an incorrect video', function (done) { + request(url) + .get(path + '123456789012345678901234') + .set('Accept', 'application/json') + .expect(404, done) + }) + + it('Should succeed with the correct parameters') + }) + + describe('When removing a video', function () { + it('Should have 404 with nothing', function (done) { + request(url) + .delete(path) + .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') + }) + }) + + describe('Of the remote videos API', function () { + describe('When making a secure request', function () { + it('Should check a secure request') + }) + + 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/server/tests/api/fixtures/video_short.mp4 b/server/tests/api/fixtures/video_short.mp4 new file mode 100644 index 000000000..35678362b Binary files /dev/null and b/server/tests/api/fixtures/video_short.mp4 differ diff --git a/server/tests/api/fixtures/video_short.ogv b/server/tests/api/fixtures/video_short.ogv new file mode 100644 index 000000000..9e253da82 Binary files /dev/null and b/server/tests/api/fixtures/video_short.ogv differ diff --git a/server/tests/api/fixtures/video_short.webm b/server/tests/api/fixtures/video_short.webm new file mode 100644 index 000000000..bf4b0ab6c Binary files /dev/null and b/server/tests/api/fixtures/video_short.webm differ diff --git a/server/tests/api/fixtures/video_short1.webm b/server/tests/api/fixtures/video_short1.webm new file mode 100644 index 000000000..70ac0c644 Binary files /dev/null and b/server/tests/api/fixtures/video_short1.webm differ diff --git a/server/tests/api/fixtures/video_short2.webm b/server/tests/api/fixtures/video_short2.webm new file mode 100644 index 000000000..13d72dff7 Binary files /dev/null and b/server/tests/api/fixtures/video_short2.webm differ diff --git a/server/tests/api/fixtures/video_short3.webm b/server/tests/api/fixtures/video_short3.webm new file mode 100644 index 000000000..cde5dcd58 Binary files /dev/null and b/server/tests/api/fixtures/video_short3.webm differ diff --git a/server/tests/api/fixtures/video_short_fake.webm b/server/tests/api/fixtures/video_short_fake.webm new file mode 100644 index 000000000..d85290ae5 --- /dev/null +++ b/server/tests/api/fixtures/video_short_fake.webm @@ -0,0 +1 @@ +this is a fake video mouahahah diff --git a/server/tests/api/friendsAdvanced.js b/server/tests/api/friendsAdvanced.js new file mode 100644 index 000000000..9838d890f --- /dev/null +++ b/server/tests/api/friendsAdvanced.js @@ -0,0 +1,250 @@ +'use strict' + +var async = require('async') +var chai = require('chai') +var expect = chai.expect + +var utils = require('./utils') + +describe('Test advanced friends', function () { + var apps = [] + var urls = [] + + 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 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' + + return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, 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() + }) + }) + + 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 + + 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) + + 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) { + 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]) + } + + 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) + + next() + }) + }], + function (err) { + if (err) throw 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() + }) + } + ) + }) + + 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) + + done() + }) + }, 5000) + }) + }) + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + + if (this.ok) { + utils.flushTests(done) + } else { + done() + } + }) +}) diff --git a/server/tests/api/friendsBasic.js b/server/tests/api/friendsBasic.js new file mode 100644 index 000000000..328724936 --- /dev/null +++ b/server/tests/api/friendsBasic.js @@ -0,0 +1,185 @@ +'use strict' + +var async = require('async') +var chai = require('chai') +var expect = chai.expect +var request = require('supertest') + +var utils = require('./utils') + +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]) + } + + 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]) + + 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 + expect(result).to.be.an('array') + 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 + + 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]) + + 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(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 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() + }) + }, + // 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) + }) + }) + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + + if (this.ok) { + utils.flushTests(done) + } else { + done() + } + }) +}) diff --git a/server/tests/api/index.js b/server/tests/api/index.js new file mode 100644 index 000000000..9c4fdd48a --- /dev/null +++ b/server/tests/api/index.js @@ -0,0 +1,8 @@ +'use strict' + +// Order of the tests we want to execute +require('./checkParams') +require('./friendsBasic') +require('./singlePod') +require('./multiplePods') +require('./friendsAdvanced') diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js new file mode 100644 index 000000000..9fdd0f308 --- /dev/null +++ b/server/tests/api/multiplePods.js @@ -0,0 +1,328 @@ +'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) + }) + + 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 + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + 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) + } + ) + }) + + it('Should upload the video on pod 2 and propagate on each pod', function (done) { + this.timeout(15000) + + 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) + } + ) + }) + + 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 + + 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 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) + + utils.getVideosList(urls[2], 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) + + 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() + }) + }) + }) + + it('Should add the file 2 by asking pod 1', function (done) { + // Yes, this could be long + this.timeout(200000) + + utils.getVideosList(urls[0], function (err, res) { + if (err) throw err + + 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('') + + done() + }) + }) + }) + + it('Should add the file 3 by asking pod 2', function (done) { + // Yes, this could be long + this.timeout(200000) + + utils.getVideosList(urls[1], function (err, res) { + if (err) throw err + + 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('') + + done() + }) + }) + }) + + it('Should add the file 3-2 by asking pod 1', function (done) { + // Yes, this could be long + this.timeout(200000) + + utils.getVideosList(urls[0], function (err, res) { + if (err) throw err + + 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('') + + done() + }) + }) + }) + + 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) + }) + }) + + 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/server/tests/api/singlePod.js b/server/tests/api/singlePod.js new file mode 100644 index 000000000..3dd72c01b --- /dev/null +++ b/server/tests/api/singlePod.js @@ -0,0 +1,146 @@ +'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 + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + 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) + + utils.getVideosList(url, function (err, res) { + if (err) throw err + + 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 + + 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('') + + done() + }) + }) + }) + + 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) + + 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() + }) + }) + + 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) + + done() + }) + }) + + 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 + + expect(files.length).to.equal(0) + done() + }) + }) + }) + + 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) + + done() + }) + }) + + 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() + } + }) +}) diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js new file mode 100644 index 000000000..47b706294 --- /dev/null +++ b/server/tests/api/utils.js @@ -0,0 +1,185 @@ +'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 + } + + 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(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) + } + }) +} + +function runServer (number, callback) { + var port = 9000 + number + var server_run_string = { + 'Connected to mongodb': false, + 'Server listening on port': false + } + + // 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 + } + + // 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 diff --git a/server/tests/index.js b/server/tests/index.js new file mode 100644 index 000000000..ccebbfe51 --- /dev/null +++ b/server/tests/index.js @@ -0,0 +1,6 @@ +;(function () { + 'use strict' + + // Order of the tests we want to execute + require('./api/') +})() -- cgit v1.2.3