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