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