]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/tests/api/friends-basic.js
Fix request schedulers stats
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-basic.js
... / ...
CommitLineData
1'use strict'
2
3const chai = require('chai')
4const each = require('async/each')
5const expect = chai.expect
6const series = require('async/series')
7
8const loginUtils = require('../utils/login')
9const miscsUtils = require('../utils/miscs')
10const podsUtils = require('../utils/pods')
11const serversUtils = require('../utils/servers')
12
13describe('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.data
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.data
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.data
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.email).to.equal('admin3@example.com')
100 expect(pod.score).to.equal(20)
101 expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
102
103 next()
104 })
105 },
106 // Same here, the third pod should have the second pod as a friend
107 function (next) {
108 podsUtils.getFriendsList(servers[2].url, function (err, res) {
109 if (err) throw err
110
111 const result = res.body.data
112 expect(result).to.be.an('array')
113 expect(result.length).to.equal(1)
114
115 const pod = result[0]
116 expect(pod.host).to.equal(servers[1].host)
117 expect(pod.email).to.equal('admin2@example.com')
118 expect(pod.score).to.equal(20)
119 expect(miscsUtils.dateIsValid(pod.createdAt)).to.be.true
120
121 next()
122 })
123 },
124 // Finally the first pod make friend with the second pod
125 function (next) {
126 makeFriends(1, next)
127 },
128 // Wait for the request between pods
129 function (next) {
130 setTimeout(next, 11000)
131 }
132 ],
133 // Now each pod should be friend with the other ones
134 function (err) {
135 if (err) throw err
136 each(servers, function (server, callback) {
137 testMadeFriends(servers, server, callback)
138 }, done)
139 })
140 })
141
142 it('Should not be allowed to make friend again', function (done) {
143 const server = servers[1]
144 podsUtils.makeFriends(server.url, server.accessToken, 409, done)
145 })
146
147 it('Should quit friends of pod 2', function (done) {
148 series([
149 // Pod 1 quit friends
150 function (next) {
151 const server = servers[1]
152 podsUtils.quitFriends(server.url, server.accessToken, next)
153 },
154 // Pod 1 should not have friends anymore
155 function (next) {
156 podsUtils.getFriendsList(servers[1].url, function (err, res) {
157 if (err) throw err
158
159 const result = res.body.data
160 expect(result).to.be.an('array')
161 expect(result.length).to.equal(0)
162
163 next()
164 })
165 },
166 // Other pods shouldn't have pod 1 too
167 function (next) {
168 each([ servers[0].url, servers[2].url ], function (url, callback) {
169 podsUtils.getFriendsList(url, function (err, res) {
170 if (err) throw err
171
172 const result = res.body.data
173 expect(result).to.be.an('array')
174 expect(result.length).to.equal(1)
175 expect(result[0].host).not.to.be.equal(servers[1].host)
176 callback()
177 })
178 }, next)
179 }
180 ], done)
181 })
182
183 it('Should allow pod 2 to make friend again', function (done) {
184 this.timeout(20000)
185
186 const server = servers[1]
187 podsUtils.makeFriends(server.url, server.accessToken, function () {
188 setTimeout(function () {
189 each(servers, function (server, callback) {
190 testMadeFriends(servers, server, callback)
191 }, done)
192 }, 11000)
193 })
194 })
195
196 after(function (done) {
197 servers.forEach(function (server) {
198 process.kill(-server.app.pid)
199 })
200
201 if (this.ok) {
202 serversUtils.flushTests(done)
203 } else {
204 done()
205 }
206 })
207})