]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/friendsAdvanced.js
Try to make a better communication (between pods) module
[github/Chocobozzz/PeerTube.git] / server / tests / api / friendsAdvanced.js
CommitLineData
9f10b292 1'use strict'
3bcb78b3 2
f0f5567b
C
3const async = require('async')
4const chai = require('chai')
5const expect = chai.expect
3bcb78b3 6
f0f5567b 7const utils = require('./utils')
3bcb78b3 8
9f10b292 9describe('Test advanced friends', function () {
0c1cbbfe 10 let servers = []
3bcb78b3 11
bc503c2a 12 function makeFriends (podNumber, callback) {
b3b92647
C
13 const server = servers[podNumber - 1]
14 return utils.makeFriends(server.url, server.accessToken, callback)
9f10b292 15 }
45239549 16
bc503c2a 17 function quitFriends (podNumber, callback) {
b3b92647
C
18 const server = servers[podNumber - 1]
19 return utils.quitFriends(server.url, server.accessToken, callback)
9f10b292 20 }
3bcb78b3 21
bc503c2a 22 function getFriendsList (podNumber, end) {
b3b92647
C
23 const server = servers[podNumber - 1]
24 return utils.getFriendsList(server.url, end)
9f10b292 25 }
3bcb78b3 26
bc503c2a 27 function uploadVideo (podNumber, callback) {
f0f5567b
C
28 const name = 'my super video'
29 const description = 'my super description'
be587647 30 const tags = [ 'tag1', 'tag2' ]
f0f5567b 31 const fixture = 'video_short.webm'
bc503c2a 32 const server = servers[podNumber - 1]
3bcb78b3 33
be587647 34 return utils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
9f10b292 35 }
3bcb78b3 36
bc503c2a
C
37 function getVideos (podNumber, callback) {
38 return utils.getVideosList(servers[podNumber - 1].url, callback)
9f10b292 39 }
45239549 40
9f10b292 41 // ---------------------------------------------------------------
ee66c593 42
9f10b292
C
43 before(function (done) {
44 this.timeout(30000)
bc503c2a
C
45 utils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
46 servers = serversRun
0c1cbbfe 47
bc503c2a
C
48 async.each(servers, function (server, callbackEach) {
49 utils.loginAndGetAccessToken(server, function (err, accessToken) {
50 if (err) return callbackEach(err)
0c1cbbfe 51
bc503c2a
C
52 server.accessToken = accessToken
53 callbackEach()
0c1cbbfe
C
54 })
55 }, done)
3bcb78b3 56 })
9f10b292 57 })
3bcb78b3 58
9f10b292
C
59 it('Should make friends with two pod each in a different group', function (done) {
60 this.timeout(20000)
61
62 async.series([
63 // Pod 3 makes friend with the first one
64 function (next) {
65 makeFriends(3, next)
66 },
67 // Pod 4 makes friend with the second one
68 function (next) {
69 makeFriends(4, next)
70 },
71 // Now if the fifth wants to make friends with the third et the first
72 function (next) {
73 makeFriends(5, next)
74 },
75 function (next) {
76 setTimeout(next, 11000)
77 }],
78 function (err) {
79 if (err) throw err
80
81 // It should have 0 friends
82 getFriendsList(5, function (err, res) {
ee66c593
C
83 if (err) throw err
84
9f10b292
C
85 expect(res.body.length).to.equal(0)
86
87 done()
88 })
89 }
90 )
91 })
92
93 it('Should quit all friends', function (done) {
94 this.timeout(10000)
95
96 async.series([
97 function (next) {
98 quitFriends(1, next)
99 },
100 function (next) {
101 quitFriends(2, next)
102 }],
103 function (err) {
104 if (err) throw err
105
106 async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
107 getFriendsList(i, function (err, res) {
ee66c593
C
108 if (err) throw err
109
110 expect(res.body.length).to.equal(0)
111
9f10b292 112 callback()
3bcb78b3 113 })
9f10b292
C
114 }, done)
115 }
116 )
117 })
3bcb78b3 118
9f10b292
C
119 it('Should make friends with the pods 1, 2, 3', function (done) {
120 this.timeout(150000)
121
122 async.series([
123 // Pods 1, 2, 3 and 4 become friends
124 function (next) {
125 makeFriends(2, next)
126 },
127 function (next) {
128 makeFriends(1, next)
129 },
130 function (next) {
131 makeFriends(4, next)
132 },
528a9efa
C
133 // Check the pods 1, 2, 3 and 4 are friends
134 function (next) {
135 async.each([ 1, 2, 3, 4 ], function (i, callback) {
136 getFriendsList(i, function (err, res) {
137 if (err) throw err
138
139 expect(res.body.length).to.equal(3)
140
141 callback()
142 })
143 }, next)
144 },
9f10b292
C
145 // Kill pod 4
146 function (next) {
0c1cbbfe 147 servers[3].app.kill()
9f10b292
C
148 next()
149 },
150 // Expulse pod 4 from pod 1 and 2
151 function (next) {
152 uploadVideo(1, next)
153 },
154 function (next) {
155 uploadVideo(2, next)
156 },
157 function (next) {
158 setTimeout(next, 11000)
159 },
160 function (next) {
161 uploadVideo(1, next)
162 },
163 function (next) {
164 uploadVideo(2, next)
165 },
166 function (next) {
528a9efa 167 setTimeout(next, 11000)
9f10b292
C
168 },
169 // Rerun server 4
170 function (next) {
0c1cbbfe
C
171 utils.runServer(4, function (server) {
172 servers[3].app = server.app
9f10b292
C
173 next()
174 })
175 },
176 function (next) {
177 getFriendsList(4, function (err, res) {
ee66c593
C
178 if (err) throw err
179
9f10b292
C
180 // Pod 4 didn't know pod 1 and 2 removed it
181 expect(res.body.length).to.equal(3)
ee66c593 182 next()
9f10b292
C
183 })
184 },
185 // Pod 6 ask pod 1, 2 and 3
186 function (next) {
187 makeFriends(6, next)
528a9efa
C
188 },
189 function (next) {
190 setTimeout(next, 11000)
9f10b292
C
191 }],
192 function (err) {
193 if (err) throw err
194
195 getFriendsList(6, function (err, res) {
ee66c593
C
196 if (err) throw err
197
9f10b292 198 // Pod 4 should not be our friend
f0f5567b 199 const result = res.body
9f10b292 200 expect(result.length).to.equal(3)
f0f5567b 201 for (const pod of result) {
0c1cbbfe 202 expect(pod.url).not.equal(servers[3].url)
9f10b292 203 }
ee66c593 204
9f10b292
C
205 done()
206 })
207 }
208 )
209 })
ee66c593 210
9f10b292
C
211 it('Should pod 1 quit friends', function (done) {
212 this.timeout(25000)
213
214 async.series([
215 // Upload a video on server 3 for aditionnal tests
216 function (next) {
217 uploadVideo(3, next)
218 },
219 function (next) {
220 setTimeout(next, 15000)
221 },
222 function (next) {
223 quitFriends(1, next)
224 },
225 // Remove pod 1 from pod 2
226 function (next) {
227 getVideos(1, function (err, res) {
228 if (err) throw err
68ce3ae0
C
229
230 const videos = res.body.data
231 expect(videos).to.be.an('array')
232 expect(videos.length).to.equal(2)
45239549 233
9f10b292
C
234 next()
235 })
236 }],
237 function (err) {
238 if (err) throw err
ee66c593 239
9f10b292 240 getVideos(2, function (err, res) {
ee66c593 241 if (err) throw err
68ce3ae0
C
242
243 const videos = res.body.data
244 expect(videos).to.be.an('array')
245 expect(videos.length).to.equal(3)
9f10b292
C
246 done()
247 })
248 }
249 )
250 })
ee66c593 251
9f10b292
C
252 it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
253 this.timeout(20000)
254 makeFriends(1, function () {
255 setTimeout(function () {
256 getVideos(1, function (err, res) {
257 if (err) throw err
45239549 258
68ce3ae0
C
259 const videos = res.body.data
260 expect(videos).to.be.an('array')
261 expect(videos.length).to.equal(5)
45239549 262
9f10b292
C
263 done()
264 })
528a9efa 265 }, 11000)
45239549 266 })
9f10b292 267 })
ee66c593 268
9f10b292 269 after(function (done) {
0c1cbbfe
C
270 servers.forEach(function (server) {
271 process.kill(-server.app.pid)
ee66c593 272 })
9f10b292
C
273
274 if (this.ok) {
275 utils.flushTests(done)
276 } else {
277 done()
278 }
3bcb78b3 279 })
9f10b292 280})