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