From 8c308c2bf7f658945d80be9d5880361238635f5b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Jun 2015 17:41:40 +0200 Subject: Spawn --- test/api/friends.js | 149 ++++++++++++++++++++++ test/api/multiplePods.js | 326 +++++++++++++++++++++++++++++++++++++++++++++++ test/api/singlePod.js | 135 ++++++++++++++++++++ 3 files changed, 610 insertions(+) create mode 100644 test/api/friends.js create mode 100644 test/api/multiplePods.js create mode 100644 test/api/singlePod.js (limited to 'test/api') diff --git a/test/api/friends.js b/test/api/friends.js new file mode 100644 index 000000000..033d3799a --- /dev/null +++ b/test/api/friends.js @@ -0,0 +1,149 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var async = require('async') + + var utils = require('../utils') + + function getFriendsList (url, end) { + var path = '/api/pods/' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) + } + + describe('Test friends', function () { + var apps = [] + var urls = [] + + before(function (done) { + this.timeout(20000) + utils.runMultipleServers(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) { + 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() + }) + }, function (err) { + if (err) throw err + + done() + }) + }) + + it('Should make friends', function (done) { + this.timeout(10000) + + 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]) + } + + 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() + }) + } + + var path = '/api/pods/makefriends' + + // The second pod make friend with the third + request(urls[1]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + // Wait for the request between pods + setTimeout(function () { + // The second pod should have the third as a friend + 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]) + + // Same here, the third pod should have the second pod as a friend + 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]) + + // Finally the first pod make friend with the second pod + request(urls[0]) + .get(path) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + setTimeout(function () { + // Now each pod should be friend with the other ones + async.each(urls, function (url, callback) { + testMadeFriends(urls, url, callback) + }, function (err) { + if (err) throw err + done() + }) + }, 1000) + }) + }) + }) + }, 1000) + }) + }) + + // TODO + it('Should not be able to make friends again') + + after(function (done) { + apps.forEach(function (app) { + process.kill(-app.pid) + }) + + if (this.ok) { + utils.flushTests(function () { + done() + }) + } else { + done() + } + }) + }) +})() diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js new file mode 100644 index 000000000..fff179006 --- /dev/null +++ b/test/api/multiplePods.js @@ -0,0 +1,326 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var async = require('async') + + var utils = require('../utils') + var webtorrent = require(__dirname + '/../../src/webTorrentNode') + webtorrent.silent = true + + describe('Test multiple pods', function () { + var path = '/api/videos' + var apps = [] + var urls = [] + var video_id = -1 + + function getVideosList (url, end) { + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) + } + + function uploadVideo (url, name, description, fixture, end) { + request(url) + .post(path) + .set('Accept', 'application/json') + .field('name', name) + .field('description', description) + .attach('input_video', __dirname + '/../fixtures/' + fixture) + .expect(201) + .end(end) + } + + before(function (done) { + this.timeout(20000) + var path_friends = '/api/pods/makefriends' + + utils.runMultipleServers(3, function (apps_run, urls_run) { + apps = apps_run + urls = urls_run + + // The second pod make friend with the third + request(urls[1]) + .get(path_friends) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + // Wait for the request between pods + setTimeout(function () { + request(urls[0]) + .get(path_friends) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + webtorrent.create(function () { + done() + }) + }) + }, 1000) + }) + }) + }) + + it('Should not have videos for all pods', function (done) { + async.each(urls, function (url, callback) { + getVideosList(url, function (err, res) { + if (err) throw err + + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(0) + + callback() + }) + }, function (err) { + if (err) throw err + + 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(5000) + + uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', function (err) { + if (err) throw err + + setTimeout(function () { + // All pods should have this video + async.each(urls, function (url, callback) { + var base_magnet = null + + 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() + }) + }, function (err) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + + it('Should upload the video on pod 2 and propagate on each pod', function (done) { + this.timeout(5000) + + uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', function (err) { + if (err) throw err + + setTimeout(function () { + // All pods should have this video + async.each(urls, function (url, callback) { + var base_magnet = null + + 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() + }) + }, function (err) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + + it('Should upload the video on pod 3 and propagate on each pod', function (done) { + this.timeout(5000) + + uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', function (err) { + if (err) throw err + + setTimeout(function () { + var base_magnet = null + // All pods should have this video + async.each(urls, function (url, callback) { + getVideosList(url, function (err, res) { + if (err) throw err + + var videos = res.body + expect(videos).to.be.an('array') + expect(videos.length).to.equal(3) + 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 + + // All pods should have the same magnet Uri + if (base_magnet === null) { + base_magnet = video.magnetUri + } else { + expect(video.magnetUri).to.equal.magnetUri + } + + callback() + }) + }, function (err) { + if (err) throw err + + done() + }) + }, 1000) + }) + }) + }) + + 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(60000) + + getVideosList(urls[2], function (err, res) { + if (err) throw err + + var video = res.body[0] + 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(60000) + + getVideosList(urls[0], function (err, res) { + if (err) throw err + + var video = res.body[1] + + 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 add the file 3 by asking pod 2', function (done) { + // Yes, this could be long + this.timeout(60000) + + 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 remove the file 2 by asking pod 2', function (done) { + request(urls[1]) + .delete(path + '/' + video_id) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + + // Wait the propagation to the other pods + setTimeout(function () { + done() + }, 1000) + }) + }) + + it('Should have videos 1 and 2 on each pod', function (done) { + async.each(urls, function (url, callback) { + 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(video_id) + expect(videos[1]._id).not.to.equal(video_id) + + callback() + }) + }, function (err) { + if (err) throw err + + 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(function () { + done() + }) + } else { + done() + } + }) + }) +})() diff --git a/test/api/singlePod.js b/test/api/singlePod.js new file mode 100644 index 000000000..59a617970 --- /dev/null +++ b/test/api/singlePod.js @@ -0,0 +1,135 @@ +;(function () { + 'use strict' + + var request = require('supertest') + var chai = require('chai') + var expect = chai.expect + var webtorrent = require(__dirname + '/../../src/webTorrentNode') + webtorrent.silent = true + + var utils = require('../utils') + + describe('Test a single pod', function () { + var path = '/api/videos' + var app = null + var url = '' + var video_id = -1 + + before(function (done) { + this.timeout(10000) + + utils.flushTests(function () { + utils.runServer(1, function (app1, url1) { + app = app1 + url = url1 + + webtorrent.create(function () { + done() + }) + }) + }) + }) + + it('Should not have videos', 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 upload the video', function (done) { + this.timeout(5000) + + request(url) + .post(path) + .set('Accept', 'application/json') + .field('name', 'my super name') + .field('description', 'my super description') + .attach('input_video', __dirname + '/../fixtures/video_short.webm') + .expect(201, done) + }) + + it('Should seed the uploaded video', function (done) { + // Yes, this could be long + this.timeout(60000) + + 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(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 remove the video', function (done) { + request(url) + .delete(path + '/' + video_id) + .set('Accept', 'application/json') + .expect(204) + .end(function (err, res) { + if (err) throw err + done() + }) + }) + + it('Should not have videos', 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() + }) + }) + + after(function (done) { + process.kill(-app.pid) + process.kill(-webtorrent.app.pid) + + // Keep the logs if the test failed + if (this.ok) { + utils.flushTests(function () { + done() + }) + } else { + done() + } + }) + }) +})() -- cgit v1.2.3