1 /* tslint:disable:no-unused-expression */
4 import * as chai from 'chai'
5 const expect = chai.expect
12 setAccessTokensToServers,
13 flushAndRunMultipleServers,
21 describe('Test basic friends', function () {
24 function makeFriendsWrapper (podNumber: number) {
25 const server = servers[podNumber - 1]
26 return makeFriends(server.url, server.accessToken)
29 async function testMadeFriends (servers: ServerInfo[], serverToTest: ServerInfo) {
31 for (let i = 0; i < servers.length; i++) {
32 if (servers[i].url === serverToTest.url) continue
33 friends.push(servers[i].host)
36 const res = await getFriendsList(serverToTest.url)
38 const result = res.body.data
39 expect(result).to.be.an('array')
40 expect(result.length).to.equal(2)
42 const resultHosts = [ result[0].host, result[1].host ]
43 expect(resultHosts[0]).to.not.equal(resultHosts[1])
45 const errorString = 'Friends host do not correspond for ' + serverToTest.host
46 expect(friends).to.contain(resultHosts[0], errorString)
47 expect(friends).to.contain(resultHosts[1], errorString)
50 // ---------------------------------------------------------------
52 before(async function () {
55 servers = await flushAndRunMultipleServers(3)
57 await setAccessTokensToServers(servers)
60 it('Should not have friends', async function () {
61 for (const server of servers) {
62 const res = await getFriendsList(server.url)
64 const result = res.body.data
65 expect(result).to.be.an('array')
66 expect(result.length).to.equal(0)
70 it('Should make friends', async function () {
73 // The second pod make friend with the third
74 await makeFriendsWrapper(2)
76 // Wait for the request between pods
79 // The second pod should have the third as a friend
80 const res1 = await getFriendsList(servers[1].url)
82 const friends = res1.body.data
83 expect(friends).to.be.an('array')
84 expect(friends.length).to.equal(1)
86 const pod1 = friends[0]
87 expect(pod1.host).to.equal(servers[2].host)
88 expect(pod1.email).to.equal('admin3@example.com')
89 expect(pod1.score).to.equal(20)
90 expect(dateIsValid(pod1.createdAt)).to.be.true
92 // Same here, the third pod should have the second pod as a friend
93 const res2 = await getFriendsList(servers[2].url)
94 const result = res2.body.data
95 expect(result).to.be.an('array')
96 expect(result.length).to.equal(1)
98 const pod2 = result[0]
99 expect(pod2.host).to.equal(servers[1].host)
100 expect(pod2.email).to.equal('admin2@example.com')
101 expect(pod2.score).to.equal(20)
102 expect(dateIsValid(pod2.createdAt)).to.be.true
104 // Finally the first pod make friend with the second pod
105 await makeFriendsWrapper(1)
107 // Wait for the request between pods
110 // Now each pod should be friend with the other ones
111 for (const server of servers) {
112 await testMadeFriends(servers, server)
116 it('Should not be allowed to make friend again', async function () {
119 const server = servers[1]
120 await makeFriends(server.url, server.accessToken, 409)
123 it('Should quit friends of pod 2', async function () {
126 // Pod 1 quit friends
127 await quitFriends(servers[1].url, servers[1].accessToken)
129 // Pod 1 should not have friends anymore
130 const res = await getFriendsList(servers[1].url)
131 const friends = res.body.data
132 expect(friends).to.be.an('array')
133 expect(friends).to.have.lengthOf(0)
135 // Other pods shouldn't have pod 1 too
136 const serversToTest = [ servers[0].url, servers[2].url ]
137 for (const url of serversToTest) {
138 const res = await getFriendsList(url)
139 const friends = res.body.data
141 expect(friends).to.be.an('array')
142 expect(friends.length).to.equal(1)
143 expect(friends[0].host).not.to.be.equal(servers[1].host)
147 it('Should allow pod 2 to make friend again', async function () {
150 const server = servers[1]
151 await makeFriends(server.url, server.accessToken)
154 for (const server of servers) {
155 await testMadeFriends(servers, server)
159 it('Should allow pod 1 to quit only pod 2', async function () {
161 const server = servers[0]
163 // Get pod 2 id so we can query it
164 const res1 = await getFriendsList(server.url)
165 const friends1 = res1.body.data
166 let pod1 = friends1.find(friend => (friend.host === servers[1].host))
168 // Remove it from the friends list
169 await quitOneFriend(server.url, server.accessToken, pod1.id)
171 // Pod 1 should have only pod 3 in its friends list
172 const res2 = await getFriendsList(servers[0].url)
173 const friends2 = res2.body.data
174 expect(friends2).to.be.an('array')
175 expect(friends2.length).to.equal(1)
177 const pod2 = friends2[0]
178 expect(pod2.host).to.equal(servers[2].host)
180 // Pod 2 should have only pod 3 in its friends list
181 const res3 = await getFriendsList(servers[1].url)
182 const friends3 = res3.body.data
183 expect(friends3).to.be.an('array')
184 expect(friends3.length).to.equal(1)
186 const pod = friends3[0]
187 expect(pod.host).to.equal(servers[2].host)
189 // Pod 3 should have both pods in its friends list
190 const res4 = await getFriendsList(servers[2].url)
191 const friends4 = res4.body.data
192 expect(friends4).to.be.an('array')
193 expect(friends4.length).to.equal(2)
196 after(async function () {
197 killallServers(servers)