Use const/let now we use node 4.2
[oweals/peertube.git] / server / tests / api / friendsBasic.js
1 'use strict'
2
3 const async = require('async')
4 const chai = require('chai')
5 const expect = chai.expect
6 const request = require('supertest')
7
8 const utils = require('./utils')
9
10 describe('Test basic friends', function () {
11   let apps = []
12   let urls = []
13
14   function testMadeFriends (urls, url_to_test, callback) {
15     const friends = []
16     for (let i = 0; i < urls.length; i++) {
17       if (urls[i] === url_to_test) continue
18       friends.push(urls[i])
19     }
20
21     utils.getFriendsList(url_to_test, function (err, res) {
22       if (err) throw err
23
24       const result = res.body
25       const result_urls = [ result[0].url, result[1].url ]
26       expect(result).to.be.an('array')
27       expect(result.length).to.equal(2)
28       expect(result_urls[0]).to.not.equal(result_urls[1])
29
30       const error_string = 'Friends url do not correspond for ' + url_to_test
31       expect(friends).to.contain(result_urls[0], error_string)
32       expect(friends).to.contain(result_urls[1], error_string)
33       callback()
34     })
35   }
36
37   // ---------------------------------------------------------------
38
39   before(function (done) {
40     this.timeout(20000)
41     utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
42       apps = apps_run
43       urls = urls_run
44       done()
45     })
46   })
47
48   it('Should not have friends', function (done) {
49     async.each(urls, function (url, callback) {
50       utils.getFriendsList(url, function (err, res) {
51         if (err) throw err
52
53         const result = res.body
54         expect(result).to.be.an('array')
55         expect(result.length).to.equal(0)
56         callback()
57       })
58     }, done)
59   })
60
61   it('Should make friends', function (done) {
62     this.timeout(10000)
63
64     const path = '/api/v1/pods/makefriends'
65
66     async.series([
67       // The second pod make friend with the third
68       function (next) {
69         request(urls[1])
70           .get(path)
71           .set('Accept', 'application/json')
72           .expect(204)
73           .end(next)
74       },
75       // Wait for the request between pods
76       function (next) {
77         setTimeout(next, 1000)
78       },
79       // The second pod should have the third as a friend
80       function (next) {
81         utils.getFriendsList(urls[1], function (err, res) {
82           if (err) throw err
83
84           const result = res.body
85           expect(result).to.be.an('array')
86           expect(result.length).to.equal(1)
87           expect(result[0].url).to.be.equal(urls[2])
88
89           next()
90         })
91       },
92       // Same here, the third pod should have the second pod as a friend
93       function (next) {
94         utils.getFriendsList(urls[2], function (err, res) {
95           if (err) throw err
96
97           const result = res.body
98           expect(result).to.be.an('array')
99           expect(result.length).to.equal(1)
100           expect(result[0].url).to.be.equal(urls[1])
101
102           next()
103         })
104       },
105       // Finally the first pod make friend with the second pod
106       function (next) {
107         request(urls[0])
108           .get(path)
109           .set('Accept', 'application/json')
110           .expect(204)
111           .end(next)
112       },
113       // Wait for the request between pods
114       function (next) {
115         setTimeout(next, 1000)
116       }
117     ],
118     // Now each pod should be friend with the other ones
119     function (err) {
120       if (err) throw err
121       async.each(urls, function (url, callback) {
122         testMadeFriends(urls, url, callback)
123       }, done)
124     })
125   })
126
127   it('Should not be allowed to make friend again', function (done) {
128     utils.makeFriends(urls[1], 409, done)
129   })
130
131   it('Should quit friends of pod 2', function (done) {
132     async.series([
133       // Pod 1 quit friends
134       function (next) {
135         utils.quitFriends(urls[1], next)
136       },
137       // Pod 1 should not have friends anymore
138       function (next) {
139         utils.getFriendsList(urls[1], function (err, res) {
140           if (err) throw err
141
142           const result = res.body
143           expect(result).to.be.an('array')
144           expect(result.length).to.equal(0)
145
146           next()
147         })
148       },
149       // Other pods shouldn't have pod 1 too
150       function (next) {
151         async.each([ urls[0], urls[2] ], function (url, callback) {
152           utils.getFriendsList(url, function (err, res) {
153             if (err) throw err
154
155             const result = res.body
156             expect(result).to.be.an('array')
157             expect(result.length).to.equal(1)
158             expect(result[0].url).not.to.be.equal(urls[1])
159             callback()
160           })
161         }, next)
162       }
163     ], done)
164   })
165
166   it('Should allow pod 2 to make friend again', function (done) {
167     utils.makeFriends(urls[1], function () {
168       async.each(urls, function (url, callback) {
169         testMadeFriends(urls, url, callback)
170       }, done)
171     })
172   })
173
174   after(function (done) {
175     apps.forEach(function (app) {
176       process.kill(-app.pid)
177     })
178
179     if (this.ok) {
180       utils.flushTests(done)
181     } else {
182       done()
183     }
184   })
185 })