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