]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friendsAdvanced.js
b082270ff58615b7862cc87496b1c98a0938ad52
[github/Chocobozzz/PeerTube.git] / server / tests / api / friendsAdvanced.js
1 'use strict'
2
3 const async = require('async')
4 const chai = require('chai')
5 const expect = chai.expect
6
7 const utils = require('./utils')
8
9 describe('Test advanced friends', function () {
10 let servers = []
11
12 function makeFriends (podNumber, callback) {
13 const server = servers[podNumber - 1]
14 return utils.makeFriends(server.url, server.accessToken, callback)
15 }
16
17 function quitFriends (podNumber, callback) {
18 const server = servers[podNumber - 1]
19 return utils.quitFriends(server.url, server.accessToken, callback)
20 }
21
22 function getFriendsList (podNumber, end) {
23 const server = servers[podNumber - 1]
24 return utils.getFriendsList(server.url, end)
25 }
26
27 function uploadVideo (podNumber, callback) {
28 const name = 'my super video'
29 const description = 'my super description'
30 const tags = [ 'tag1', 'tag2' ]
31 const fixture = 'video_short.webm'
32 const server = servers[podNumber - 1]
33
34 return utils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
35 }
36
37 function getVideos (podNumber, callback) {
38 return utils.getVideosList(servers[podNumber - 1].url, callback)
39 }
40
41 // ---------------------------------------------------------------
42
43 before(function (done) {
44 this.timeout(30000)
45 utils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
46 servers = serversRun
47
48 async.each(servers, function (server, callbackEach) {
49 utils.loginAndGetAccessToken(server, function (err, accessToken) {
50 if (err) return callbackEach(err)
51
52 server.accessToken = accessToken
53 callbackEach()
54 })
55 }, done)
56 })
57 })
58
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) {
83 if (err) throw err
84
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) {
108 if (err) throw err
109
110 expect(res.body.length).to.equal(0)
111
112 callback()
113 })
114 }, done)
115 }
116 )
117 })
118
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 },
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 },
145 // Kill pod 4
146 function (next) {
147 servers[3].app.kill()
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) {
167 setTimeout(next, 11000)
168 },
169 // Rerun server 4
170 function (next) {
171 utils.runServer(4, function (server) {
172 servers[3].app = server.app
173 next()
174 })
175 },
176 function (next) {
177 getFriendsList(4, function (err, res) {
178 if (err) throw err
179
180 // Pod 4 didn't know pod 1 and 2 removed it
181 expect(res.body.length).to.equal(3)
182 next()
183 })
184 },
185 // Pod 6 ask pod 1, 2 and 3
186 function (next) {
187 makeFriends(6, next)
188 },
189 function (next) {
190 setTimeout(next, 11000)
191 }],
192 function (err) {
193 if (err) throw err
194
195 getFriendsList(6, function (err, res) {
196 if (err) throw err
197
198 // Pod 4 should not be our friend
199 const result = res.body
200 expect(result.length).to.equal(3)
201 for (const pod of result) {
202 expect(pod.url).not.equal(servers[3].url)
203 }
204
205 done()
206 })
207 }
208 )
209 })
210
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
229
230 const videos = res.body.data
231 expect(videos).to.be.an('array')
232 expect(videos.length).to.equal(2)
233
234 next()
235 })
236 }],
237 function (err) {
238 if (err) throw err
239
240 getVideos(2, function (err, res) {
241 if (err) throw err
242
243 const videos = res.body.data
244 expect(videos).to.be.an('array')
245 expect(videos.length).to.equal(3)
246 done()
247 })
248 }
249 )
250 })
251
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
258
259 const videos = res.body.data
260 expect(videos).to.be.an('array')
261 expect(videos.length).to.equal(5)
262
263 done()
264 })
265 }, 11000)
266 })
267 })
268
269 after(function (done) {
270 servers.forEach(function (server) {
271 process.kill(-server.app.pid)
272 })
273
274 if (this.ok) {
275 utils.flushTests(done)
276 } else {
277 done()
278 }
279 })
280 })