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