]>
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
) {
145 const server
= servers
[1]
146 podsUtils
.makeFriends(server
.url
, server
.accessToken
, 409, done
)
149 it('Should quit friends of pod 2', function (done
) {
151 // Pod 1 quit friends
153 const server
= servers
[1]
154 podsUtils
.quitFriends(server
.url
, server
.accessToken
, next
)
156 // Pod 1 should not have friends anymore
158 podsUtils
.getFriendsList(servers
[1].url
, function (err
, res
) {
161 const result
= res
.body
.data
162 expect(result
).to
.be
.an('array')
163 expect(result
.length
).to
.equal(0)
168 // Other pods shouldn't have pod 1 too
170 each([ servers
[0].url
, servers
[2].url
], function (url
, callback
) {
171 podsUtils
.getFriendsList(url
, function (err
, res
) {
174 const result
= res
.body
.data
175 expect(result
).to
.be
.an('array')
176 expect(result
.length
).to
.equal(1)
177 expect(result
[0].host
).not
.to
.be
.equal(servers
[1].host
)
185 it('Should allow pod 2 to make friend again', function (done
) {
188 const server
= servers
[1]
189 podsUtils
.makeFriends(server
.url
, server
.accessToken
, function () {
190 setTimeout(function () {
191 each(servers
, function (server
, callback
) {
192 testMadeFriends(servers
, server
, callback
)
198 after(function (done
) {
199 servers
.forEach(function (server
) {
200 process
.kill(-server
.app
.pid
)
204 serversUtils
.flushTests(done
)