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