]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
f0f5567b | 3 | const chai = require('chai') |
1a42c9e2 | 4 | const each = require('async/each') |
f0f5567b | 5 | const expect = chai.expect |
1a42c9e2 | 6 | const series = require('async/series') |
8c308c2b | 7 | |
8d309058 | 8 | const loginUtils = require('../utils/login') |
53572423 | 9 | const miscsUtils = require('../utils/miscs') |
8d309058 C |
10 | const podsUtils = require('../utils/pods') |
11 | const serversUtils = require('../utils/servers') | |
8c308c2b | 12 | |
9f10b292 | 13 | describe('Test basic friends', function () { |
0c1cbbfe | 14 | let servers = [] |
ee66c593 | 15 | |
b3b92647 C |
16 | function makeFriends (podNumber, callback) { |
17 | const server = servers[podNumber - 1] | |
8d309058 | 18 | return podsUtils.makeFriends(server.url, server.accessToken, callback) |
b3b92647 C |
19 | } |
20 | ||
bc503c2a | 21 | function testMadeFriends (servers, serverToTest, callback) { |
f0f5567b | 22 | const friends = [] |
0c1cbbfe | 23 | for (let i = 0; i < servers.length; i++) { |
bc503c2a | 24 | if (servers[i].url === serverToTest.url) continue |
a4254ea1 | 25 | friends.push(servers[i].host) |
9f10b292 C |
26 | } |
27 | ||
8d309058 | 28 | podsUtils.getFriendsList(serverToTest.url, function (err, res) { |
9f10b292 C |
29 | if (err) throw err |
30 | ||
55fa55a9 | 31 | const result = res.body.data |
9f10b292 C |
32 | expect(result).to.be.an('array') |
33 | expect(result.length).to.equal(2) | |
528a9efa | 34 | |
a4254ea1 C |
35 | const resultHosts = [ result[0].host, result[1].host ] |
36 | expect(resultHosts[0]).to.not.equal(resultHosts[1]) | |
45239549 | 37 | |
a4254ea1 C |
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) | |
9f10b292 C |
41 | callback() |
42 | }) | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------- | |
46 | ||
47 | before(function (done) { | |
48 | this.timeout(20000) | |
8d309058 | 49 | serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { |
bc503c2a | 50 | servers = serversRun |
b3b92647 | 51 | |
1a42c9e2 | 52 | each(servers, function (server, callbackEach) { |
8d309058 | 53 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { |
b3b92647 C |
54 | if (err) return callbackEach(err) |
55 | ||
56 | server.accessToken = accessToken | |
57 | callbackEach() | |
58 | }) | |
59 | }, done) | |
9f10b292 C |
60 | }) |
61 | }) | |
62 | ||
63 | it('Should not have friends', function (done) { | |
1a42c9e2 | 64 | each(servers, function (server, callback) { |
8d309058 | 65 | podsUtils.getFriendsList(server.url, function (err, res) { |
45239549 C |
66 | if (err) throw err |
67 | ||
55fa55a9 | 68 | const result = res.body.data |
45239549 | 69 | expect(result).to.be.an('array') |
9f10b292 | 70 | expect(result.length).to.equal(0) |
45239549 C |
71 | callback() |
72 | }) | |
9f10b292 C |
73 | }, done) |
74 | }) | |
45239549 | 75 | |
9f10b292 | 76 | it('Should make friends', function (done) { |
ccc64aa6 | 77 | this.timeout(40000) |
9f10b292 | 78 | |
1a42c9e2 | 79 | series([ |
9f10b292 C |
80 | // The second pod make friend with the third |
81 | function (next) { | |
b3b92647 | 82 | makeFriends(2, next) |
9f10b292 C |
83 | }, |
84 | // Wait for the request between pods | |
85 | function (next) { | |
ccc64aa6 | 86 | setTimeout(next, 11000) |
9f10b292 C |
87 | }, |
88 | // The second pod should have the third as a friend | |
89 | function (next) { | |
8d309058 | 90 | podsUtils.getFriendsList(servers[1].url, function (err, res) { |
9f10b292 | 91 | if (err) throw err |
8c308c2b | 92 | |
55fa55a9 | 93 | const result = res.body.data |
9f10b292 C |
94 | expect(result).to.be.an('array') |
95 | expect(result.length).to.equal(1) | |
53572423 C |
96 | |
97 | const pod = result[0] | |
a4254ea1 | 98 | expect(pod.host).to.equal(servers[2].host) |
4793c343 | 99 | expect(pod.email).to.equal('admin3@example.com') |
53572423 | 100 | expect(pod.score).to.equal(20) |
feb4bdfd | 101 | expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true |
8c308c2b | 102 | |
9f10b292 C |
103 | next() |
104 | }) | |
105 | }, | |
106 | // Same here, the third pod should have the second pod as a friend | |
107 | function (next) { | |
8d309058 | 108 | podsUtils.getFriendsList(servers[2].url, function (err, res) { |
8c308c2b C |
109 | if (err) throw err |
110 | ||
55fa55a9 | 111 | const result = res.body.data |
8c308c2b | 112 | expect(result).to.be.an('array') |
9f10b292 | 113 | expect(result.length).to.equal(1) |
53572423 C |
114 | |
115 | const pod = result[0] | |
a4254ea1 | 116 | expect(pod.host).to.equal(servers[1].host) |
4793c343 | 117 | expect(pod.email).to.equal('admin2@example.com') |
53572423 | 118 | expect(pod.score).to.equal(20) |
feb4bdfd | 119 | expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true |
9f10b292 C |
120 | |
121 | next() | |
8c308c2b | 122 | }) |
9f10b292 C |
123 | }, |
124 | // Finally the first pod make friend with the second pod | |
125 | function (next) { | |
b3b92647 | 126 | makeFriends(1, next) |
9f10b292 C |
127 | }, |
128 | // Wait for the request between pods | |
129 | function (next) { | |
0fb99fb4 | 130 | setTimeout(next, 11000) |
9f10b292 C |
131 | } |
132 | ], | |
133 | // Now each pod should be friend with the other ones | |
134 | function (err) { | |
135 | if (err) throw err | |
1a42c9e2 | 136 | each(servers, function (server, callback) { |
0c1cbbfe | 137 | testMadeFriends(servers, server, callback) |
ee66c593 | 138 | }, done) |
8c308c2b | 139 | }) |
9f10b292 | 140 | }) |
8c308c2b | 141 | |
9f10b292 | 142 | it('Should not be allowed to make friend again', function (done) { |
b3b92647 | 143 | const server = servers[1] |
8d309058 | 144 | podsUtils.makeFriends(server.url, server.accessToken, 409, done) |
9f10b292 | 145 | }) |
8c308c2b | 146 | |
9f10b292 | 147 | it('Should quit friends of pod 2', function (done) { |
1a42c9e2 | 148 | series([ |
9f10b292 C |
149 | // Pod 1 quit friends |
150 | function (next) { | |
b3b92647 | 151 | const server = servers[1] |
8d309058 | 152 | podsUtils.quitFriends(server.url, server.accessToken, next) |
9f10b292 C |
153 | }, |
154 | // Pod 1 should not have friends anymore | |
155 | function (next) { | |
8d309058 | 156 | podsUtils.getFriendsList(servers[1].url, function (err, res) { |
9f10b292 | 157 | if (err) throw err |
8c308c2b | 158 | |
55fa55a9 | 159 | const result = res.body.data |
9f10b292 C |
160 | expect(result).to.be.an('array') |
161 | expect(result.length).to.equal(0) | |
162 | ||
163 | next() | |
164 | }) | |
165 | }, | |
166 | // Other pods shouldn't have pod 1 too | |
167 | function (next) { | |
1a42c9e2 | 168 | each([ servers[0].url, servers[2].url ], function (url, callback) { |
8d309058 | 169 | podsUtils.getFriendsList(url, function (err, res) { |
ee66c593 C |
170 | if (err) throw err |
171 | ||
55fa55a9 | 172 | const result = res.body.data |
ee66c593 C |
173 | expect(result).to.be.an('array') |
174 | expect(result.length).to.equal(1) | |
a4254ea1 | 175 | expect(result[0].host).not.to.be.equal(servers[1].host) |
9f10b292 | 176 | callback() |
ee66c593 | 177 | }) |
9f10b292 C |
178 | }, next) |
179 | } | |
180 | ], done) | |
181 | }) | |
45239549 | 182 | |
9f10b292 | 183 | it('Should allow pod 2 to make friend again', function (done) { |
ccc64aa6 C |
184 | this.timeout(20000) |
185 | ||
b3b92647 | 186 | const server = servers[1] |
8d309058 | 187 | podsUtils.makeFriends(server.url, server.accessToken, function () { |
0fb99fb4 C |
188 | setTimeout(function () { |
189 | each(servers, function (server, callback) { | |
190 | testMadeFriends(servers, server, callback) | |
191 | }, done) | |
192 | }, 11000) | |
45239549 | 193 | }) |
9f10b292 | 194 | }) |
45239549 | 195 | |
9f10b292 | 196 | after(function (done) { |
0c1cbbfe C |
197 | servers.forEach(function (server) { |
198 | process.kill(-server.app.pid) | |
45239549 | 199 | }) |
8c308c2b | 200 | |
9f10b292 | 201 | if (this.ok) { |
8d309058 | 202 | serversUtils.flushTests(done) |
9f10b292 C |
203 | } else { |
204 | done() | |
205 | } | |
8c308c2b | 206 | }) |
9f10b292 | 207 | }) |