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