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