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