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