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 miscsUtils = require('../utils/miscs')
10 const podsUtils = require('../utils/pods')
11 const serversUtils = require('../utils/servers')
13 describe('Test basic friends', function () {
16 function makeFriends (podNumber, callback) {
17 const server = servers[podNumber - 1]
18 return podsUtils.makeFriends(server.url, server.accessToken, callback)
21 function testMadeFriends (servers, serverToTest, callback) {
23 for (let i = 0; i < servers.length; i++) {
24 if (servers[i].url === serverToTest.url) continue
25 friends.push(servers[i].host)
28 podsUtils.getFriendsList(serverToTest.url, function (err, res) {
31 const result = res.body
32 expect(result).to.be.an('array')
33 expect(result.length).to.equal(2)
35 const resultHosts = [ result[0].host, result[1].host ]
36 expect(resultHosts[0]).to.not.equal(resultHosts[1])
38 const errorString = 'Friends host do not correspond for ' + serverToTest.host
39 expect(friends).to.contain(resultHosts[0], errorString)
40 expect(friends).to.contain(resultHosts[1], errorString)
45 // ---------------------------------------------------------------
47 before(function (done) {
49 serversUtils.flushAndRunMultipleServers(3, 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 not have friends', function (done) {
64 each(servers, function (server, callback) {
65 podsUtils.getFriendsList(server.url, function (err, res) {
68 const result = res.body
69 expect(result).to.be.an('array')
70 expect(result.length).to.equal(0)
76 it('Should make friends', function (done) {
80 // The second pod make friend with the third
84 // Wait for the request between pods
86 setTimeout(next, 11000)
88 // The second pod should have the third as a friend
90 podsUtils.getFriendsList(servers[1].url, function (err, res) {
93 const result = res.body
94 expect(result).to.be.an('array')
95 expect(result.length).to.equal(1)
98 expect(pod.host).to.equal(servers[2].host)
99 expect(pod.score).to.equal(20)
100 expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
105 // Same here, the third pod should have the second pod as a friend
107 podsUtils.getFriendsList(servers[2].url, function (err, res) {
110 const result = res.body
111 expect(result).to.be.an('array')
112 expect(result.length).to.equal(1)
114 const pod = result[0]
115 expect(pod.host).to.equal(servers[1].host)
116 expect(pod.score).to.equal(20)
117 expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
122 // Finally the first pod make friend with the second pod
126 // Wait for the request between pods
128 setTimeout(next, 11000)
131 // Now each pod should be friend with the other ones
134 each(servers, function (server, callback) {
135 testMadeFriends(servers, server, callback)
140 it('Should not be allowed to make friend again', function (done) {
141 const server = servers[1]
142 podsUtils.makeFriends(server.url, server.accessToken, 409, done)
145 it('Should quit friends of pod 2', function (done) {
147 // Pod 1 quit friends
149 const server = servers[1]
150 podsUtils.quitFriends(server.url, server.accessToken, next)
152 // Pod 1 should not have friends anymore
154 podsUtils.getFriendsList(servers[1].url, function (err, res) {
157 const result = res.body
158 expect(result).to.be.an('array')
159 expect(result.length).to.equal(0)
164 // Other pods shouldn't have pod 1 too
166 each([ servers[0].url, servers[2].url ], function (url, callback) {
167 podsUtils.getFriendsList(url, function (err, res) {
170 const result = res.body
171 expect(result).to.be.an('array')
172 expect(result.length).to.equal(1)
173 expect(result[0].host).not.to.be.equal(servers[1].host)
181 it('Should allow pod 2 to make friend again', function (done) {
184 const server = servers[1]
185 podsUtils.makeFriends(server.url, server.accessToken, function () {
186 setTimeout(function () {
187 each(servers, function (server, callback) {
188 testMadeFriends(servers, server, callback)
194 after(function (done) {
195 servers.forEach(function (server) {
196 process.kill(-server.app.pid)
200 serversUtils.flushTests(done)