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