]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friends-basic.js
require -> import
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-basic.js
1 /* eslint-disable no-unused-expressions */
2
3 'use strict'
4
5 const chai = require('chai')
6 const each = require('async/each')
7 const expect = chai.expect
8 const series = require('async/series')
9
10 const loginUtils = require('../utils/login')
11 const miscsUtils = require('../utils/miscs')
12 const podsUtils = require('../utils/pods')
13 const serversUtils = require('../utils/servers')
14
15 describe('Test basic friends', function () {
16 let servers = []
17
18 function makeFriends (podNumber, callback) {
19 const server = servers[podNumber - 1]
20 return podsUtils.makeFriends(server.url, server.accessToken, callback)
21 }
22
23 function testMadeFriends (servers, serverToTest, callback) {
24 const friends = []
25 for (let i = 0; i < servers.length; i++) {
26 if (servers[i].url === serverToTest.url) continue
27 friends.push(servers[i].host)
28 }
29
30 podsUtils.getFriendsList(serverToTest.url, function (err, res) {
31 if (err) throw err
32
33 const result = res.body.data
34 expect(result).to.be.an('array')
35 expect(result.length).to.equal(2)
36
37 const resultHosts = [ result[0].host, result[1].host ]
38 expect(resultHosts[0]).to.not.equal(resultHosts[1])
39
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)
43 callback()
44 })
45 }
46
47 // ---------------------------------------------------------------
48
49 before(function (done) {
50 this.timeout(20000)
51 serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) {
52 servers = serversRun
53
54 each(servers, function (server, callbackEach) {
55 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
56 if (err) return callbackEach(err)
57
58 server.accessToken = accessToken
59 callbackEach()
60 })
61 }, done)
62 })
63 })
64
65 it('Should not have friends', function (done) {
66 each(servers, function (server, callback) {
67 podsUtils.getFriendsList(server.url, function (err, res) {
68 if (err) throw err
69
70 const result = res.body.data
71 expect(result).to.be.an('array')
72 expect(result.length).to.equal(0)
73 callback()
74 })
75 }, done)
76 })
77
78 it('Should make friends', function (done) {
79 this.timeout(40000)
80
81 series([
82 // The second pod make friend with the third
83 function (next) {
84 makeFriends(2, next)
85 },
86 // Wait for the request between pods
87 function (next) {
88 setTimeout(next, 11000)
89 },
90 // The second pod should have the third as a friend
91 function (next) {
92 podsUtils.getFriendsList(servers[1].url, function (err, res) {
93 if (err) throw err
94
95 const result = res.body.data
96 expect(result).to.be.an('array')
97 expect(result.length).to.equal(1)
98
99 const pod = result[0]
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
104
105 next()
106 })
107 },
108 // Same here, the third pod should have the second pod as a friend
109 function (next) {
110 podsUtils.getFriendsList(servers[2].url, function (err, res) {
111 if (err) throw err
112
113 const result = res.body.data
114 expect(result).to.be.an('array')
115 expect(result.length).to.equal(1)
116
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
122
123 next()
124 })
125 },
126 // Finally the first pod make friend with the second pod
127 function (next) {
128 makeFriends(1, next)
129 },
130 // Wait for the request between pods
131 function (next) {
132 setTimeout(next, 11000)
133 }
134 ],
135 // Now each pod should be friend with the other ones
136 function (err) {
137 if (err) throw err
138 each(servers, function (server, callback) {
139 testMadeFriends(servers, server, callback)
140 }, done)
141 })
142 })
143
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)
147 })
148
149 it('Should quit friends of pod 2', function (done) {
150 series([
151 // Pod 1 quit friends
152 function (next) {
153 const server = servers[1]
154 podsUtils.quitFriends(server.url, server.accessToken, next)
155 },
156 // Pod 1 should not have friends anymore
157 function (next) {
158 podsUtils.getFriendsList(servers[1].url, function (err, res) {
159 if (err) throw err
160
161 const result = res.body.data
162 expect(result).to.be.an('array')
163 expect(result.length).to.equal(0)
164
165 next()
166 })
167 },
168 // Other pods shouldn't have pod 1 too
169 function (next) {
170 each([ servers[0].url, servers[2].url ], function (url, callback) {
171 podsUtils.getFriendsList(url, function (err, res) {
172 if (err) throw err
173
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)
178 callback()
179 })
180 }, next)
181 }
182 ], done)
183 })
184
185 it('Should allow pod 2 to make friend again', function (done) {
186 this.timeout(20000)
187
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)
193 }, done)
194 }, 11000)
195 })
196 })
197
198 after(function (done) {
199 servers.forEach(function (server) {
200 process.kill(-server.app.pid)
201 })
202
203 if (this.ok) {
204 serversUtils.flushTests(done)
205 } else {
206 done()
207 }
208 })
209 })