diff options
Diffstat (limited to 'server/tests/api/friends-basic.ts')
-rw-r--r-- | server/tests/api/friends-basic.ts | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/server/tests/api/friends-basic.ts b/server/tests/api/friends-basic.ts new file mode 100644 index 000000000..13edf6273 --- /dev/null +++ b/server/tests/api/friends-basic.ts | |||
@@ -0,0 +1,203 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | ||
2 | |||
3 | import 'mocha' | ||
4 | import * as chai from 'chai' | ||
5 | const expect = chai.expect | ||
6 | |||
7 | import { | ||
8 | ServerInfo, | ||
9 | flushTests, | ||
10 | quitFriends, | ||
11 | wait, | ||
12 | setAccessTokensToServers, | ||
13 | flushAndRunMultipleServers, | ||
14 | killallServers, | ||
15 | makeFriends, | ||
16 | getFriendsList, | ||
17 | dateIsValid, | ||
18 | quitOneFriend | ||
19 | } from '../utils' | ||
20 | |||
21 | describe('Test basic friends', function () { | ||
22 | let servers = [] | ||
23 | |||
24 | function makeFriendsWrapper (podNumber: number) { | ||
25 | const server = servers[podNumber - 1] | ||
26 | return makeFriends(server.url, server.accessToken) | ||
27 | } | ||
28 | |||
29 | async function testMadeFriends (servers: ServerInfo[], serverToTest: ServerInfo) { | ||
30 | const friends = [] | ||
31 | for (let i = 0; i < servers.length; i++) { | ||
32 | if (servers[i].url === serverToTest.url) continue | ||
33 | friends.push(servers[i].host) | ||
34 | } | ||
35 | |||
36 | const res = await getFriendsList(serverToTest.url) | ||
37 | |||
38 | const result = res.body.data | ||
39 | expect(result).to.be.an('array') | ||
40 | expect(result.length).to.equal(2) | ||
41 | |||
42 | const resultHosts = [ result[0].host, result[1].host ] | ||
43 | expect(resultHosts[0]).to.not.equal(resultHosts[1]) | ||
44 | |||
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) | ||
48 | } | ||
49 | |||
50 | // --------------------------------------------------------------- | ||
51 | |||
52 | before(async function () { | ||
53 | this.timeout(120000) | ||
54 | |||
55 | servers = await flushAndRunMultipleServers(3) | ||
56 | |||
57 | await setAccessTokensToServers(servers) | ||
58 | }) | ||
59 | |||
60 | it('Should not have friends', async function () { | ||
61 | for (const server of servers) { | ||
62 | const res = await getFriendsList(server.url) | ||
63 | |||
64 | const result = res.body.data | ||
65 | expect(result).to.be.an('array') | ||
66 | expect(result.length).to.equal(0) | ||
67 | } | ||
68 | }) | ||
69 | |||
70 | it('Should make friends', async function () { | ||
71 | this.timeout(120000) | ||
72 | |||
73 | // The second pod make friend with the third | ||
74 | await makeFriendsWrapper(2) | ||
75 | |||
76 | // Wait for the request between pods | ||
77 | await wait(11000) | ||
78 | |||
79 | // The second pod should have the third as a friend | ||
80 | const res1 = await getFriendsList(servers[1].url) | ||
81 | |||
82 | const friends = res1.body.data | ||
83 | expect(friends).to.be.an('array') | ||
84 | expect(friends.length).to.equal(1) | ||
85 | |||
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 | ||
91 | |||
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) | ||
97 | |||
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 | ||
103 | |||
104 | // Finally the first pod make friend with the second pod | ||
105 | await makeFriendsWrapper(1) | ||
106 | |||
107 | // Wait for the request between pods | ||
108 | await wait(11000) | ||
109 | |||
110 | // Now each pod should be friend with the other ones | ||
111 | for (const server of servers) { | ||
112 | await testMadeFriends(servers, server) | ||
113 | } | ||
114 | }) | ||
115 | |||
116 | it('Should not be allowed to make friend again', async function () { | ||
117 | this.timeout(10000) | ||
118 | |||
119 | const server = servers[1] | ||
120 | await makeFriends(server.url, server.accessToken, 409) | ||
121 | }) | ||
122 | |||
123 | it('Should quit friends of pod 2', async function () { | ||
124 | this.timeout(10000) | ||
125 | |||
126 | // Pod 1 quit friends | ||
127 | await quitFriends(servers[1].url, servers[1].accessToken) | ||
128 | |||
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) | ||
134 | |||
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 | ||
140 | |||
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) | ||
144 | } | ||
145 | }) | ||
146 | |||
147 | it('Should allow pod 2 to make friend again', async function () { | ||
148 | this.timeout(120000) | ||
149 | |||
150 | const server = servers[1] | ||
151 | await makeFriends(server.url, server.accessToken) | ||
152 | await wait(11000) | ||
153 | |||
154 | for (const server of servers) { | ||
155 | await testMadeFriends(servers, server) | ||
156 | } | ||
157 | }) | ||
158 | |||
159 | it('Should allow pod 1 to quit only pod 2', async function () { | ||
160 | // Pod 1 quits pod 2 | ||
161 | const server = servers[0] | ||
162 | |||
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)) | ||
167 | |||
168 | // Remove it from the friends list | ||
169 | await quitOneFriend(server.url, server.accessToken, pod1.id) | ||
170 | |||
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) | ||
176 | |||
177 | const pod2 = friends2[0] | ||
178 | expect(pod2.host).to.equal(servers[2].host) | ||
179 | |||
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) | ||
185 | |||
186 | const pod = friends3[0] | ||
187 | expect(pod.host).to.equal(servers[2].host) | ||
188 | |||
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) | ||
194 | }) | ||
195 | |||
196 | after(async function () { | ||
197 | killallServers(servers) | ||
198 | |||
199 | if (this['ok']) { | ||
200 | await flushTests() | ||
201 | } | ||
202 | }) | ||
203 | }) | ||