From 45239549bf2659998dcf9196d86974b0b625912e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 23 Jan 2016 18:31:58 +0100 Subject: Finalise the join in a network and add the ability to quit it --- test/api/friendsAdvanced.js | 88 ++++++++++++++++++++++++++++++++++++++------ test/api/friendsBasic.js | 90 ++++++++++++++++++++++++++++++++------------- test/api/multiplePods.js | 4 +- test/api/utils.js | 28 +++++++++++++- 4 files changed, 170 insertions(+), 40 deletions(-) (limited to 'test/api') diff --git a/test/api/friendsAdvanced.js b/test/api/friendsAdvanced.js index a638eb0d4..b24cd39c3 100644 --- a/test/api/friendsAdvanced.js +++ b/test/api/friendsAdvanced.js @@ -1,6 +1,7 @@ ;(function () { 'use strict' + var async = require('async') var chai = require('chai') var expect = chai.expect @@ -10,8 +11,12 @@ var apps = [] var urls = [] - function makeFriend (pod_number, callback) { - return utils.makeFriend(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 getFriendsList (pod_number, end) { @@ -26,7 +31,11 @@ return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) } - beforeEach(function (done) { + function getVideos (pod_number, callback) { + return utils.getVideosList(urls[pod_number - 1], callback) + } + + before(function (done) { this.timeout(30000) utils.runMultipleServers(6, function (apps_run, urls_run) { apps = apps_run @@ -35,7 +44,7 @@ }) }) - afterEach(function (done) { + after(function (done) { apps.forEach(function (app) { process.kill(-app.pid) }) @@ -53,11 +62,11 @@ this.timeout(20000) // Pod 3 makes friend with the first one - makeFriend(3, function () { + makeFriends(3, function () { // Pod 4 makes friend with the second one - makeFriend(4, function () { + makeFriends(4, function () { // Now if the fifth wants to make friends with the third et the first - makeFriend(5, function () { + makeFriends(5, function () { setTimeout(function () { // It should have 0 friends getFriendsList(5, function (err, res) { @@ -73,13 +82,30 @@ }) }) + it('Should quit all friends', function (done) { + this.timeout(10000) + quitFriends(1, function () { + quitFriends(2, function () { + 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() + }) + }, function () { + done() + }) + }) + }) + }) + it('Should make friends with the pods 1, 2, 3', function (done) { this.timeout(150000) // Pods 1, 2, 3 and 4 become friends (yes this is beautiful) - makeFriend(2, function () { - makeFriend(1, function () { - makeFriend(4, function () { + makeFriends(2, function () { + makeFriends(1, function () { + makeFriends(4, function () { // Kill the server 4 apps[3].kill() @@ -99,7 +125,7 @@ expect(res.body.length).to.equal(3) // Pod 6 ask pod 1, 2 and 3 - makeFriend(6, function () { + makeFriends(6, function () { getFriendsList(6, function (err, res) { if (err) throw err @@ -125,5 +151,45 @@ }) }) }) + + it('Should pod 1 quit friends', function (done) { + this.timeout(25000) + // Upload a video on server 3 for aditionnal tests + uploadVideo(3, function () { + setTimeout(function () { + quitFriends(1, function () { + // Remove pod 1 from pod 2 + getVideos(1, function (err, res) { + if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(2) + + getVideos(2, function (err, res) { + if (err) throw err + expect(res.body).to.be.an('array') + expect(res.body.length).to.equal(3) + done() + }) + }) + }) + }, 15000) + }) + }) + + 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) + }) + }) }) })() diff --git a/test/api/friendsBasic.js b/test/api/friendsBasic.js index 43ec41633..15b83d421 100644 --- a/test/api/friendsBasic.js +++ b/test/api/friendsBasic.js @@ -9,6 +9,29 @@ var utils = require('./utils') describe('Test basic friends', function () { + 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() + }) + } + var apps = [] var urls = [] @@ -41,29 +64,6 @@ 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]) - } - - 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() - }) - } - var path = '/api/v1/pods/makefriends' // The second pod make friend with the third @@ -118,8 +118,48 @@ }) }) - // TODO - it('Should not be able to make friends again') + 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) { + utils.quitFriends(urls[1], function () { + 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) + + // Other pods shouldn't have pod 2 too + 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() + }) + }, function (err) { + if (err) throw err + 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) + }, function (err) { + if (err) throw err + done() + }) + }) + }) after(function (done) { apps.forEach(function (app) { diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js index a831e0fa6..531e1ef33 100644 --- a/test/api/multiplePods.js +++ b/test/api/multiplePods.js @@ -22,12 +22,12 @@ urls = urls_run // The second pod make friend with the third - utils.makeFriend(urls[1], function (err, res) { + utils.makeFriends(urls[1], function (err, res) { if (err) throw err // Wait for the request between pods setTimeout(function () { - utils.makeFriend(urls[0], function (err, res) { + utils.makeFriends(urls[0], function (err, res) { if (err) throw err webtorrent.create({ host: 'client', port: '1' }, function () { diff --git a/test/api/utils.js b/test/api/utils.js index 8d059b01c..b00890539 100644 --- a/test/api/utils.js +++ b/test/api/utils.js @@ -34,9 +34,32 @@ .end(end) } - function makeFriend (url, callback) { + 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(function () { + callback() + }, 1000) + }) + } + + function quitFriends (url, callback) { + var path = '/api/v1/pods/quitfriends' + // The first pod make friend with the third request(url) .get(path) @@ -152,7 +175,8 @@ flushTests: flushTests, getFriendsList: getFriendsList, getVideosList: getVideosList, - makeFriend: makeFriend, + makeFriends: makeFriends, + quitFriends: quitFriends, removeVideo: removeVideo, runMultipleServers: runMultipleServers, runServer: runServer, -- cgit v1.2.3