First version with PostgreSQL
[oweals/peertube.git] / server / tests / api / friends-basic.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 miscsUtils = require('../utils/miscs')
10 const podsUtils = require('../utils/pods')
11 const serversUtils = require('../utils/servers')
12
13 describe('Test basic 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 testMadeFriends (servers, serverToTest, callback) {
22     const friends = []
23     for (let i = 0; i < servers.length; i++) {
24       if (servers[i].url === serverToTest.url) continue
25       friends.push(servers[i].host)
26     }
27
28     podsUtils.getFriendsList(serverToTest.url, function (err, res) {
29       if (err) throw err
30
31       const result = res.body
32       expect(result).to.be.an('array')
33       expect(result.length).to.equal(2)
34
35       const resultHosts = [ result[0].host, result[1].host ]
36       expect(resultHosts[0]).to.not.equal(resultHosts[1])
37
38       const errorString = 'Friends host do not correspond for ' + serverToTest.host
39       expect(friends).to.contain(resultHosts[0], errorString)
40       expect(friends).to.contain(resultHosts[1], errorString)
41       callback()
42     })
43   }
44
45   // ---------------------------------------------------------------
46
47   before(function (done) {
48     this.timeout(20000)
49     serversUtils.flushAndRunMultipleServers(3, 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 not have friends', function (done) {
64     each(servers, function (server, callback) {
65       podsUtils.getFriendsList(server.url, function (err, res) {
66         if (err) throw err
67
68         const result = res.body
69         expect(result).to.be.an('array')
70         expect(result.length).to.equal(0)
71         callback()
72       })
73     }, done)
74   })
75
76   it('Should make friends', function (done) {
77     this.timeout(40000)
78
79     series([
80       // The second pod make friend with the third
81       function (next) {
82         makeFriends(2, next)
83       },
84       // Wait for the request between pods
85       function (next) {
86         setTimeout(next, 11000)
87       },
88       // The second pod should have the third as a friend
89       function (next) {
90         podsUtils.getFriendsList(servers[1].url, function (err, res) {
91           if (err) throw err
92
93           const result = res.body
94           expect(result).to.be.an('array')
95           expect(result.length).to.equal(1)
96
97           const pod = result[0]
98           expect(pod.host).to.equal(servers[2].host)
99           expect(pod.score).to.equal(20)
100           expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
101
102           next()
103         })
104       },
105       // Same here, the third pod should have the second pod as a friend
106       function (next) {
107         podsUtils.getFriendsList(servers[2].url, function (err, res) {
108           if (err) throw err
109
110           const result = res.body
111           expect(result).to.be.an('array')
112           expect(result.length).to.equal(1)
113
114           const pod = result[0]
115           expect(pod.host).to.equal(servers[1].host)
116           expect(pod.score).to.equal(20)
117           expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
118
119           next()
120         })
121       },
122       // Finally the first pod make friend with the second pod
123       function (next) {
124         makeFriends(1, next)
125       },
126       // Wait for the request between pods
127       function (next) {
128         setTimeout(next, 11000)
129       }
130     ],
131     // Now each pod should be friend with the other ones
132     function (err) {
133       if (err) throw err
134       each(servers, function (server, callback) {
135         testMadeFriends(servers, server, callback)
136       }, done)
137     })
138   })
139
140   it('Should not be allowed to make friend again', function (done) {
141     const server = servers[1]
142     podsUtils.makeFriends(server.url, server.accessToken, 409, done)
143   })
144
145   it('Should quit friends of pod 2', function (done) {
146     series([
147       // Pod 1 quit friends
148       function (next) {
149         const server = servers[1]
150         podsUtils.quitFriends(server.url, server.accessToken, next)
151       },
152       // Pod 1 should not have friends anymore
153       function (next) {
154         podsUtils.getFriendsList(servers[1].url, function (err, res) {
155           if (err) throw err
156
157           const result = res.body
158           expect(result).to.be.an('array')
159           expect(result.length).to.equal(0)
160
161           next()
162         })
163       },
164       // Other pods shouldn't have pod 1 too
165       function (next) {
166         each([ servers[0].url, servers[2].url ], function (url, callback) {
167           podsUtils.getFriendsList(url, function (err, res) {
168             if (err) throw err
169
170             const result = res.body
171             expect(result).to.be.an('array')
172             expect(result.length).to.equal(1)
173             expect(result[0].host).not.to.be.equal(servers[1].host)
174             callback()
175           })
176         }, next)
177       }
178     ], done)
179   })
180
181   it('Should allow pod 2 to make friend again', function (done) {
182     this.timeout(20000)
183
184     const server = servers[1]
185     podsUtils.makeFriends(server.url, server.accessToken, function () {
186       setTimeout(function () {
187         each(servers, function (server, callback) {
188           testMadeFriends(servers, server, callback)
189         }, done)
190       }, 11000)
191     })
192   })
193
194   after(function (done) {
195     servers.forEach(function (server) {
196       process.kill(-server.app.pid)
197     })
198
199     if (this.ok) {
200       serversUtils.flushTests(done)
201     } else {
202       done()
203     }
204   })
205 })