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