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