]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friends-advanced.js
Update bittorrent-tracker and standard to v9
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-advanced.js
1 /* eslint-disable no-unused-expressions */
2
3 'use strict'
4
5 const chai = require('chai')
6 const each = require('async/each')
7 const expect = chai.expect
8 const series = require('async/series')
9
10 const loginUtils = require('../utils/login')
11 const podsUtils = require('../utils/pods')
12 const serversUtils = require('../utils/servers')
13 const videosUtils = require('../utils/videos')
14
15 describe('Test advanced friends', function () {
16 let servers = []
17
18 function makeFriends (podNumber, callback) {
19 const server = servers[podNumber - 1]
20 return podsUtils.makeFriends(server.url, server.accessToken, callback)
21 }
22
23 function quitFriends (podNumber, callback) {
24 const server = servers[podNumber - 1]
25 return podsUtils.quitFriends(server.url, server.accessToken, callback)
26 }
27
28 function getFriendsList (podNumber, end) {
29 const server = servers[podNumber - 1]
30 return podsUtils.getFriendsList(server.url, end)
31 }
32
33 function uploadVideo (podNumber, callback) {
34 const name = 'my super video'
35 const description = 'my super description'
36 const tags = [ 'tag1', 'tag2' ]
37 const fixture = 'video_short.webm'
38 const server = servers[podNumber - 1]
39
40 return videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
41 }
42
43 function getVideos (podNumber, callback) {
44 return videosUtils.getVideosList(servers[podNumber - 1].url, callback)
45 }
46
47 // ---------------------------------------------------------------
48
49 before(function (done) {
50 this.timeout(30000)
51 serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
52 servers = serversRun
53
54 each(servers, function (server, callbackEach) {
55 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
56 if (err) return callbackEach(err)
57
58 server.accessToken = accessToken
59 callbackEach()
60 })
61 }, done)
62 })
63 })
64
65 it('Should make friends with two pod each in a different group', function (done) {
66 this.timeout(20000)
67
68 series([
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) {
89 if (err) throw err
90
91 expect(res.body.data.length).to.equal(0)
92
93 done()
94 })
95 }
96 )
97 })
98
99 it('Should quit all friends', function (done) {
100 this.timeout(10000)
101
102 series([
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
112 each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
113 getFriendsList(i, function (err, res) {
114 if (err) throw err
115
116 expect(res.body.data.length).to.equal(0)
117
118 callback()
119 })
120 }, done)
121 }
122 )
123 })
124
125 it('Should make friends with the pods 1, 2, 3', function (done) {
126 this.timeout(150000)
127
128 series([
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 },
139 // Check the pods 1, 2, 3 and 4 are friends
140 function (next) {
141 each([ 1, 2, 3, 4 ], function (i, callback) {
142 getFriendsList(i, function (err, res) {
143 if (err) throw err
144
145 expect(res.body.data.length).to.equal(3)
146
147 callback()
148 })
149 }, next)
150 },
151 // Kill pod 4
152 function (next) {
153 servers[3].app.kill()
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) {
173 setTimeout(next, 11000)
174 },
175 // Rerun server 4
176 function (next) {
177 serversUtils.runServer(4, function (server) {
178 servers[3].app = server.app
179 next()
180 })
181 },
182 function (next) {
183 getFriendsList(4, function (err, res) {
184 if (err) throw err
185
186 // Pod 4 didn't know pod 1 and 2 removed it
187 expect(res.body.data.length).to.equal(3)
188 next()
189 })
190 },
191 // Pod 6 ask pod 1, 2 and 3
192 function (next) {
193 makeFriends(6, next)
194 },
195 function (next) {
196 setTimeout(next, 11000)
197 }],
198 function (err) {
199 if (err) throw err
200
201 getFriendsList(6, function (err, res) {
202 if (err) throw err
203
204 // Pod 4 should not be our friend
205 const result = res.body.data
206 expect(result.length).to.equal(3)
207 for (const pod of result) {
208 expect(pod.host).not.equal(servers[3].host)
209 }
210
211 done()
212 })
213 }
214 )
215 })
216
217 it('Should pod 1 quit friends', function (done) {
218 this.timeout(25000)
219
220 series([
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
235
236 const videos = res.body.data
237 expect(videos).to.be.an('array')
238 expect(videos.length).to.equal(2)
239
240 next()
241 })
242 }],
243 function (err) {
244 if (err) throw err
245
246 getVideos(2, function (err, res) {
247 if (err) throw err
248
249 const videos = res.body.data
250 expect(videos).to.be.an('array')
251 expect(videos.length).to.equal(3)
252 done()
253 })
254 }
255 )
256 })
257
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
264
265 const videos = res.body.data
266 expect(videos).to.be.an('array')
267 expect(videos.length).to.equal(5)
268
269 done()
270 })
271 }, 11000)
272 })
273 })
274
275 after(function (done) {
276 servers.forEach(function (server) {
277 process.kill(-server.app.pid)
278 })
279
280 if (this.ok) {
281 serversUtils.flushTests(done)
282 } else {
283 done()
284 }
285 })
286 })