]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friends-basic.js
1 /* eslint-disable no-unused-expressions */
5 const chai
= require('chai')
6 const each
= require('async/each')
7 const expect
= chai
.expect
8 const series
= require('async/series')
10 const loginUtils
= require('../utils/login')
11 const miscsUtils
= require('../utils/miscs')
12 const podsUtils
= require('../utils/pods')
13 const serversUtils
= require('../utils/servers')
15 describe('Test basic friends', function () {
18 function makeFriends (podNumber
, callback
) {
19 const server
= servers
[podNumber
- 1]
20 return podsUtils
.makeFriends(server
.url
, server
.accessToken
, callback
)
23 function testMadeFriends (servers
, serverToTest
, callback
) {
25 for (let i
= 0; i
< servers
.length
; i
++) {
26 if (servers
[i
].url
=== serverToTest
.url
) continue
27 friends
.push(servers
[i
].host
)
30 podsUtils
.getFriendsList(serverToTest
.url
, function (err
, res
) {
33 const result
= res
.body
.data
34 expect(result
).to
.be
.an('array')
35 expect(result
.length
).to
.equal(2)
37 const resultHosts
= [ result
[0].host
, result
[1].host
]
38 expect(resultHosts
[0]).to
.not
.equal(resultHosts
[1])
40 const errorString
= 'Friends host do not correspond for ' + serverToTest
.host
41 expect(friends
).to
.contain(resultHosts
[0], errorString
)
42 expect(friends
).to
.contain(resultHosts
[1], errorString
)
47 // ---------------------------------------------------------------
49 before(function (done
) {
51 serversUtils
.flushAndRunMultipleServers(3, function (serversRun
, urlsRun
) {
54 each(servers
, function (server
, callbackEach
) {
55 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
56 if (err
) return callbackEach(err
)
58 server
.accessToken
= accessToken
65 it('Should not have friends', function (done
) {
66 each(servers
, function (server
, callback
) {
67 podsUtils
.getFriendsList(server
.url
, function (err
, res
) {
70 const result
= res
.body
.data
71 expect(result
).to
.be
.an('array')
72 expect(result
.length
).to
.equal(0)
78 it('Should make friends', function (done
) {
82 // The second pod make friend with the third
86 // Wait for the request between pods
88 setTimeout(next
, 11000)
90 // The second pod should have the third as a friend
92 podsUtils
.getFriendsList(servers
[1].url
, function (err
, res
) {
95 const result
= res
.body
.data
96 expect(result
).to
.be
.an('array')
97 expect(result
.length
).to
.equal(1)
100 expect(pod
.host
).to
.equal(servers
[2].host
)
101 expect(pod
.email
).to
.equal('admin3@example.com')
102 expect(pod
.score
).to
.equal(20)
103 expect(miscsUtils
.dateIsValid(pod
.createdAt
)).to
.be
.true
108 // Same here, the third pod should have the second pod as a friend
110 podsUtils
.getFriendsList(servers
[2].url
, function (err
, res
) {
113 const result
= res
.body
.data
114 expect(result
).to
.be
.an('array')
115 expect(result
.length
).to
.equal(1)
117 const pod
= result
[0]
118 expect(pod
.host
).to
.equal(servers
[1].host
)
119 expect(pod
.email
).to
.equal('admin2@example.com')
120 expect(pod
.score
).to
.equal(20)
121 expect(miscsUtils
.dateIsValid(pod
.createdAt
)).to
.be
.true
126 // Finally the first pod make friend with the second pod
130 // Wait for the request between pods
132 setTimeout(next
, 11000)
135 // Now each pod should be friend with the other ones
138 each(servers
, function (server
, callback
) {
139 testMadeFriends(servers
, server
, callback
)
144 it('Should not be allowed to make friend again', function (done
) {
146 const server
= servers
[1]
147 podsUtils
.makeFriends(server
.url
, server
.accessToken
, 409, done
)
150 it('Should quit friends of pod 2', function (done
) {
154 // Pod 1 quit friends
156 const server
= servers
[1]
157 podsUtils
.quitFriends(server
.url
, server
.accessToken
, next
)
159 // Pod 1 should not have friends anymore
161 podsUtils
.getFriendsList(servers
[1].url
, function (err
, res
) {
164 const result
= res
.body
.data
165 expect(result
).to
.be
.an('array')
166 expect(result
.length
).to
.equal(0)
171 // Other pods shouldn't have pod 1 too
173 each([ servers
[0].url
, servers
[2].url
], function (url
, callback
) {
174 podsUtils
.getFriendsList(url
, function (err
, res
) {
177 const result
= res
.body
.data
178 expect(result
).to
.be
.an('array')
179 expect(result
.length
).to
.equal(1)
180 expect(result
[0].host
).not
.to
.be
.equal(servers
[1].host
)
188 it('Should allow pod 2 to make friend again', function (done
) {
191 const server
= servers
[1]
192 podsUtils
.makeFriends(server
.url
, server
.accessToken
, function () {
193 setTimeout(function () {
194 each(servers
, function (server
, callback
) {
195 testMadeFriends(servers
, server
, callback
)
201 it('Should allow pod 1 to quit only pod 2', function (done
) {
205 const server
= servers
[0]
207 // Get pod 2 id so we can query it
208 podsUtils
.getFriendsList(server
.url
, function (err
, res
) {
211 const result
= res
.body
.data
212 let pod
= result
.find((friend
) => (friend
.host
=== servers
[1].host
))
214 // Remove it from the friends list
215 podsUtils
.quitOneFriend(server
.url
, server
.accessToken
, pod
.id
, next
)
219 // Pod 1 should have only pod 3 in its friends list
221 podsUtils
.getFriendsList(servers
[0].url
, function (err
, res
) {
224 const result
= res
.body
.data
225 expect(result
).to
.be
.an('array')
226 expect(result
.length
).to
.equal(1)
228 const pod
= result
[0]
229 expect(pod
.host
).to
.equal(servers
[2].host
)
235 // Pod 2 should have only pod 3 in its friends list
237 podsUtils
.getFriendsList(servers
[1].url
, function (err
, res
) {
240 const result
= res
.body
.data
241 expect(result
).to
.be
.an('array')
242 expect(result
.length
).to
.equal(1)
244 const pod
= result
[0]
245 expect(pod
.host
).to
.equal(servers
[2].host
)
251 // Pod 3 should have both pods in its friends list
253 podsUtils
.getFriendsList(servers
[2].url
, function (err
, res
) {
256 const result
= res
.body
.data
257 expect(result
).to
.be
.an('array')
258 expect(result
.length
).to
.equal(2)
266 after(function (done
) {
267 servers
.forEach(function (server
) {
268 process
.kill(-server
.app
.pid
)
272 serversUtils
.flushTests(done
)