aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/friends-advanced.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/friends-advanced.ts')
-rw-r--r--server/tests/api/friends-advanced.ts261
1 files changed, 261 insertions, 0 deletions
diff --git a/server/tests/api/friends-advanced.ts b/server/tests/api/friends-advanced.ts
new file mode 100644
index 000000000..dc5c83c5e
--- /dev/null
+++ b/server/tests/api/friends-advanced.ts
@@ -0,0 +1,261 @@
1/* tslint:disable:no-unused-expression */
2
3import 'mocha'
4import * as chai from 'chai'
5const expect = chai.expect
6
7import {
8 ServerInfo,
9 flushTests,
10 runServer,
11 uploadVideo,
12 quitFriends,
13 getVideosList,
14 wait,
15 setAccessTokensToServers,
16 flushAndRunMultipleServers,
17 killallServers,
18 makeFriends,
19 getFriendsList,
20 quitOneFriend
21} from '../utils'
22
23describe('Test advanced friends', function () {
24 let servers: ServerInfo[] = []
25
26 async function makeFriendsWrapper (podNumber: number) {
27 const server = servers[podNumber - 1]
28 return await makeFriends(server.url, server.accessToken)
29 }
30
31 async function quitFriendsWrapper (podNumber: number) {
32 const server = servers[podNumber - 1]
33 return await quitFriends(server.url, server.accessToken)
34 }
35
36 async function removeFriendWrapper (podNumber: number, podNumberToRemove: number) {
37 const server = servers[podNumber - 1]
38 const serverToRemove = servers[podNumberToRemove - 1]
39
40 const res = await getFriendsList(server.url)
41
42 let friendsList = res.body.data
43 let podToRemove = friendsList.find(friend => (friend.host === serverToRemove.host))
44
45 return await quitOneFriend(server.url, server.accessToken, podToRemove.id)
46 }
47
48 async function getFriendsListWrapper (podNumber: number) {
49 const server = servers[podNumber - 1]
50 return await getFriendsList(server.url)
51 }
52
53 async function uploadVideoWrapper (podNumber: number) {
54 const videoAttributes = {
55 tags: [ 'tag1', 'tag2' ]
56 }
57 const server = servers[podNumber - 1]
58
59 return await uploadVideo(server.url, server.accessToken, videoAttributes)
60 }
61
62 async function getVideosWrapper (podNumber: number) {
63 return await getVideosList(servers[podNumber - 1].url)
64 }
65
66 // ---------------------------------------------------------------
67
68 before(async function () {
69 this.timeout(120000)
70
71 servers = await flushAndRunMultipleServers(6)
72 await setAccessTokensToServers(servers)
73 })
74
75 it('Should make friends with two pod each in a different group', async function () {
76 this.timeout(20000)
77
78 // Pod 3 makes friend with the first one
79 await makeFriendsWrapper(3)
80
81 // Pod 4 makes friend with the second one
82 await makeFriendsWrapper(4)
83
84 // Now if the fifth wants to make friends with the third et the first
85 await makeFriendsWrapper(5)
86
87 await wait(11000)
88
89 // It should have 0 friends
90 const res = await getFriendsListWrapper(5)
91 expect(res.body.data.length).to.equal(0)
92 })
93
94 it('Should quit all friends', async function () {
95 this.timeout(10000)
96
97 await quitFriendsWrapper(1)
98 await quitFriendsWrapper(2)
99
100 const serverNumbersToTest = [ 1, 2, 3, 4, 5, 6 ]
101 for (const i of serverNumbersToTest) {
102 const res = await getFriendsListWrapper(i)
103 expect(res.body.data.length).to.equal(0)
104 }
105 })
106
107 it('Should make friends with the pods 1, 2, 3', async function () {
108 this.timeout(150000)
109
110 // Pods 1, 2, 3 and 4 become friends
111 await makeFriendsWrapper(2)
112 await makeFriendsWrapper(1)
113 await makeFriendsWrapper(4)
114
115 // Check the pods 1, 2, 3 and 4 are friends
116 let serverNumbersToTest = [ 1, 2, 3, 4 ]
117 for (const i of serverNumbersToTest) {
118 const res = await getFriendsListWrapper(i)
119 expect(res.body.data.length).to.equal(3)
120 }
121
122 // Kill pod 4
123 servers[3].app.kill()
124
125 // Remove pod 4 from pod 1 and 2
126 await uploadVideoWrapper(1)
127 await uploadVideoWrapper(2)
128
129 await wait(11000)
130
131 await uploadVideoWrapper(1)
132 await uploadVideoWrapper(2)
133
134 await wait(11000)
135
136 serverNumbersToTest = [ 1, 2 ]
137
138 for (const i of serverNumbersToTest) {
139 const res = await getFriendsListWrapper(i)
140
141 // Pod 4 should not be our friend
142 const friends = res.body.data
143 expect(friends.length).to.equal(2)
144
145 for (const pod of friends) {
146 expect(pod.host).not.equal(servers[3].host)
147 }
148 }
149
150 // Rerun server 4
151 const newServer = await runServer(4)
152 servers[3].app = newServer.app
153 servers[3].app
154
155 const res1 = await getFriendsListWrapper(4)
156
157 // Pod 4 didn't know pod 1 and 2 removed it
158 expect(res1.body.data.length).to.equal(3)
159
160 // Pod 6 asks pod 1, 2 and 3
161 await makeFriendsWrapper(6)
162
163 await wait(11000)
164
165 const res2 = await getFriendsListWrapper(6)
166
167 // Pod 4 should not be our friend
168 const friends = res2.body.data
169 expect(friends.length).to.equal(3)
170 for (const pod of friends) {
171 expect(pod.host).not.equal(servers[3].host)
172 }
173 })
174
175 it('Should pod 1 quit friends', async function () {
176 this.timeout(25000)
177
178 // Upload a video on server 3 for additional tests
179 await uploadVideoWrapper(3)
180
181 await wait(15000)
182
183 await quitFriendsWrapper(1)
184
185 // Remove pod 1 from pod 2
186 const res1 = await getVideosWrapper(1)
187 const videos1 = res1.body.data
188 expect(videos1).to.be.an('array')
189 expect(videos1.length).to.equal(2)
190
191 const res2 = await getVideosWrapper(2)
192 const videos2 = res2.body.data
193 expect(videos2).to.be.an('array')
194 expect(videos2.length).to.equal(3)
195 })
196
197 it('Should make friends between pod 1 and 2 and exchange their videos', async function () {
198 this.timeout(20000)
199
200 await makeFriendsWrapper(1)
201
202 await wait(11000)
203
204 const res = await getVideosWrapper(1)
205 const videos = res.body.data
206 expect(videos).to.be.an('array')
207 expect(videos.length).to.equal(5)
208 })
209
210 it('Should allow pod 6 to quit pod 1 & 2 and be friend with pod 3', async function () {
211 this.timeout(30000)
212
213 // Pod 3 should have 4 friends
214 const res1 = await getFriendsListWrapper(3)
215 const friendsList1 = res1.body.data
216 expect(friendsList1).to.be.an('array')
217 expect(friendsList1.length).to.equal(4)
218
219 // Pod 1, 2, 6 should have 3 friends each
220 let serverNumbersToTest = [ 1, 2, 6 ]
221 for (const i of serverNumbersToTest) {
222 const res = await getFriendsListWrapper(i)
223 const friendsList = res.body.data
224 expect(friendsList).to.be.an('array')
225 expect(friendsList.length).to.equal(3)
226 }
227
228 await removeFriendWrapper(6, 1)
229 await removeFriendWrapper(6, 2)
230
231 // Pod 6 should now have only 1 friend (and it should be Pod 3)
232 const res2 = await getFriendsListWrapper(6)
233 const friendsList2 = res2.body.data
234 expect(friendsList2).to.be.an('array')
235 expect(friendsList2.length).to.equal(1)
236 expect(friendsList2[0].host).to.equal(servers[2].host)
237
238 // Pod 1 & 2 should not know friend 6 anymore
239 serverNumbersToTest = [ 1, 2 ]
240 for (const i of serverNumbersToTest) {
241 const res = await getFriendsListWrapper(i)
242 const friendsList = res.body.data
243 expect(friendsList).to.be.an('array')
244 expect(friendsList.length).to.equal(2)
245 }
246
247 // Pod 3 should know every pod
248 const res3 = await getFriendsListWrapper(3)
249 const friendsList3 = res3.body.data
250 expect(friendsList3).to.be.an('array')
251 expect(friendsList3.length).to.equal(4)
252 })
253
254 after(async function () {
255 killallServers(servers)
256
257 if (this['ok']) {
258 await flushTests()
259 }
260 })
261})