]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friendsAdvanced.js
Don't forget to test the client stuffs
[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 (pod_number, callback) {
13 return utils.makeFriends(servers[pod_number - 1].url, callback)
14 }
15
16 function quitFriends (pod_number, callback) {
17 return utils.quitFriends(servers[pod_number - 1].url, callback)
18 }
19
20 function getFriendsList (pod_number, end) {
21 return utils.getFriendsList(servers[pod_number - 1].url, end)
22 }
23
24 function uploadVideo (pod_number, callback) {
25 const name = 'my super video'
26 const description = 'my super description'
27 const fixture = 'video_short.webm'
28 const server = servers[pod_number - 1]
29
30 return utils.uploadVideo(server.url, server.access_token, name, description, fixture, callback)
31 }
32
33 function getVideos (pod_number, callback) {
34 return utils.getVideosList(servers[pod_number - 1].url, callback)
35 }
36
37 // ---------------------------------------------------------------
38
39 before(function (done) {
40 this.timeout(30000)
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)
52 })
53 })
54
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) {
79 if (err) throw err
80
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) {
104 if (err) throw err
105
106 expect(res.body.length).to.equal(0)
107
108 callback()
109 })
110 }, done)
111 }
112 )
113 })
114
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) {
131 servers[3].app.kill()
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) {
155 utils.runServer(4, function (server) {
156 servers[3].app = server.app
157 next()
158 })
159 },
160 function (next) {
161 getFriendsList(4, function (err, res) {
162 if (err) throw err
163
164 // Pod 4 didn't know pod 1 and 2 removed it
165 expect(res.body.length).to.equal(3)
166 next()
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) {
177 if (err) throw err
178
179 // Pod 4 should not be our friend
180 const result = res.body
181 expect(result.length).to.equal(3)
182 for (const pod of result) {
183 expect(pod.url).not.equal(servers[3].url)
184 }
185
186 done()
187 })
188 }
189 )
190 })
191
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)
212
213 next()
214 })
215 }],
216 function (err) {
217 if (err) throw err
218
219 getVideos(2, function (err, res) {
220 if (err) throw err
221 expect(res.body).to.be.an('array')
222 expect(res.body.length).to.equal(3)
223 done()
224 })
225 }
226 )
227 })
228
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
235
236 expect(res.body).to.be.an('array')
237 expect(res.body.length).to.equal(5)
238
239 done()
240 })
241 }, 5000)
242 })
243 })
244
245 after(function (done) {
246 servers.forEach(function (server) {
247 process.kill(-server.app.pid)
248 })
249
250 if (this.ok) {
251 utils.flushTests(done)
252 } else {
253 done()
254 }
255 })
256 })