3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const series = require('async/series')
8 const loginUtils = require('../utils/login')
9 const podsUtils = require('../utils/pods')
10 const serversUtils = require('../utils/servers')
11 const videosUtils = require('../utils/videos')
13 describe('Test advanced friends', function () {
16 function makeFriends (podNumber, callback) {
17 const server = servers[podNumber - 1]
18 return podsUtils.makeFriends(server.url, server.accessToken, callback)
21 function quitFriends (podNumber, callback) {
22 const server = servers[podNumber - 1]
23 return podsUtils.quitFriends(server.url, server.accessToken, callback)
26 function getFriendsList (podNumber, end) {
27 const server = servers[podNumber - 1]
28 return podsUtils.getFriendsList(server.url, end)
31 function uploadVideo (podNumber, callback) {
32 const name = 'my super video'
33 const description = 'my super description'
34 const tags = [ 'tag1', 'tag2' ]
35 const fixture = 'video_short.webm'
36 const server = servers[podNumber - 1]
38 return videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
41 function getVideos (podNumber, callback) {
42 return videosUtils.getVideosList(servers[podNumber - 1].url, callback)
45 // ---------------------------------------------------------------
47 before(function (done) {
49 serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
52 each(servers, function (server, callbackEach) {
53 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
54 if (err) return callbackEach(err)
56 server.accessToken = accessToken
63 it('Should make friends with two pod each in a different group', function (done) {
67 // Pod 3 makes friend with the first one
71 // Pod 4 makes friend with the second one
75 // Now if the fifth wants to make friends with the third et the first
80 setTimeout(next, 11000)
85 // It should have 0 friends
86 getFriendsList(5, function (err, res) {
89 expect(res.body.length).to.equal(0)
97 it('Should quit all friends', function (done) {
110 each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
111 getFriendsList(i, function (err, res) {
114 expect(res.body.length).to.equal(0)
123 it('Should make friends with the pods 1, 2, 3', function (done) {
127 // Pods 1, 2, 3 and 4 become friends
137 // Check the pods 1, 2, 3 and 4 are friends
139 each([ 1, 2, 3, 4 ], function (i, callback) {
140 getFriendsList(i, function (err, res) {
143 expect(res.body.length).to.equal(3)
151 servers[3].app.kill()
154 // Expulse pod 4 from pod 1 and 2
162 setTimeout(next, 11000)
171 setTimeout(next, 11000)
175 serversUtils.runServer(4, function (server) {
176 servers[3].app = server.app
181 getFriendsList(4, function (err, res) {
184 // Pod 4 didn't know pod 1 and 2 removed it
185 expect(res.body.length).to.equal(3)
189 // Pod 6 ask pod 1, 2 and 3
194 setTimeout(next, 11000)
199 getFriendsList(6, function (err, res) {
202 // Pod 4 should not be our friend
203 const result = res.body
204 expect(result.length).to.equal(3)
205 for (const pod of result) {
206 expect(pod.url).not.equal(servers[3].url)
215 it('Should pod 1 quit friends', function (done) {
219 // Upload a video on server 3 for aditionnal tests
224 setTimeout(next, 15000)
229 // Remove pod 1 from pod 2
231 getVideos(1, function (err, res) {
234 const videos = res.body.data
235 expect(videos).to.be.an('array')
236 expect(videos.length).to.equal(2)
244 getVideos(2, function (err, res) {
247 const videos = res.body.data
248 expect(videos).to.be.an('array')
249 expect(videos.length).to.equal(3)
256 it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
258 makeFriends(1, function () {
259 setTimeout(function () {
260 getVideos(1, function (err, res) {
263 const videos = res.body.data
264 expect(videos).to.be.an('array')
265 expect(videos.length).to.equal(5)
273 after(function (done) {
274 servers.forEach(function (server) {
275 process.kill(-server.app.pid)
279 serversUtils.flushTests(done)