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