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