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