]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/multiplePods.js
Server: do not allow a user to remove a video of another user
[github/Chocobozzz/PeerTube.git] / server / tests / api / multiplePods.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const chai = require('chai')
1a42c9e2 4const each = require('async/each')
f0f5567b
C
5const expect = chai.expect
6const pathUtils = require('path')
1a42c9e2 7const series = require('async/series')
9f10b292 8
f0f5567b
C
9const utils = require('./utils')
10const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9f10b292
C
11webtorrent.silent = true
12
13describe('Test multiple pods', function () {
0c1cbbfe 14 let servers = []
bc503c2a 15 const toRemove = []
9f10b292
C
16
17 before(function (done) {
18 this.timeout(30000)
19
1a42c9e2 20 series([
9f10b292
C
21 // Run servers
22 function (next) {
bc503c2a
C
23 utils.flushAndRunMultipleServers(3, function (serversRun) {
24 servers = serversRun
9f10b292
C
25 next()
26 })
27 },
0c1cbbfe
C
28 // Get the access tokens
29 function (next) {
1a42c9e2 30 each(servers, function (server, callbackEach) {
bc503c2a
C
31 utils.loginAndGetAccessToken(server, function (err, accessToken) {
32 if (err) return callbackEach(err)
0c1cbbfe 33
bc503c2a
C
34 server.accessToken = accessToken
35 callbackEach()
0c1cbbfe
C
36 })
37 }, next)
38 },
9f10b292
C
39 // The second pod make friend with the third
40 function (next) {
b3b92647
C
41 const server = servers[1]
42 utils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
43 },
44 // Wait for the request between pods
45 function (next) {
46 setTimeout(next, 10000)
47 },
48 // Pod 1 make friends too
49 function (next) {
b3b92647
C
50 const server = servers[0]
51 utils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
52 },
53 function (next) {
54 webtorrent.create({ host: 'client', port: '1' }, next)
55 }
56 ], done)
57 })
8c308c2b 58
9f10b292 59 it('Should not have videos for all pods', function (done) {
1a42c9e2 60 each(servers, function (server, callback) {
0c1cbbfe 61 utils.getVideosList(server.url, function (err, res) {
9f10b292 62 if (err) throw err
8c308c2b 63
68ce3ae0
C
64 const videos = res.body.data
65 expect(videos).to.be.an('array')
66 expect(videos.length).to.equal(0)
8c308c2b 67
9f10b292
C
68 callback()
69 })
70 }, done)
71 })
8c308c2b 72
9f10b292
C
73 describe('Should upload the video and propagate on each pod', function () {
74 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
75 this.timeout(15000)
8c308c2b 76
1a42c9e2 77 series([
ee66c593 78 function (next) {
be587647
C
79 const name = 'my super name for pod 1'
80 const description = 'my super description for pod 1'
81 const tags = [ 'tag1p1', 'tag2p1' ]
82 const file = 'video_short1.webm'
83 utils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next)
ee66c593 84 },
ee66c593 85 function (next) {
9f10b292
C
86 setTimeout(next, 11000)
87 }],
88 // All pods should have this video
89 function (err) {
90 if (err) throw err
91
1a42c9e2 92 each(servers, function (server, callback) {
bc503c2a 93 let baseMagnet = null
9f10b292 94
0c1cbbfe 95 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
96 if (err) throw err
97
68ce3ae0 98 const videos = res.body.data
9f10b292
C
99 expect(videos).to.be.an('array')
100 expect(videos.length).to.equal(1)
f0f5567b 101 const video = videos[0]
9f10b292
C
102 expect(video.name).to.equal('my super name for pod 1')
103 expect(video.description).to.equal('my super description for pod 1')
68ce3ae0 104 expect(video.podUrl).to.equal('localhost:9001')
9f10b292 105 expect(video.magnetUri).to.exist
3a8a8b51 106 expect(video.duration).to.equal(10)
be587647 107 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
bb10240e 108 expect(utils.dateIsValid(video.createdDate)).to.be.true
528a9efa 109 expect(video.author).to.equal('root')
9f10b292 110
6d8ada5f
C
111 if (server.url !== 'http://localhost:9001') {
112 expect(video.isLocal).to.be.false
113 } else {
114 expect(video.isLocal).to.be.true
115 }
116
9f10b292 117 // All pods should have the same magnet Uri
bc503c2a
C
118 if (baseMagnet === null) {
119 baseMagnet = video.magnetUri
9f10b292
C
120 } else {
121 expect(video.magnetUri).to.equal.magnetUri
122 }
123
36d56024 124 utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
125 if (err) throw err
126 expect(test).to.equal(true)
127
128 callback()
129 })
9f10b292
C
130 })
131 }, done)
132 }
133 )
134 })
135
136 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
137 this.timeout(15000)
138
1a42c9e2 139 series([
ee66c593 140 function (next) {
be587647
C
141 const name = 'my super name for pod 2'
142 const description = 'my super description for pod 2'
143 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ]
144 const file = 'video_short2.webm'
145 utils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
ee66c593
C
146 },
147 function (next) {
9f10b292
C
148 setTimeout(next, 11000)
149 }],
150 // All pods should have this video
151 function (err) {
152 if (err) throw err
153
1a42c9e2 154 each(servers, function (server, callback) {
bc503c2a 155 let baseMagnet = null
9f10b292 156
0c1cbbfe 157 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
158 if (err) throw err
159
68ce3ae0 160 const videos = res.body.data
9f10b292
C
161 expect(videos).to.be.an('array')
162 expect(videos.length).to.equal(2)
f0f5567b 163 const video = videos[1]
9f10b292
C
164 expect(video.name).to.equal('my super name for pod 2')
165 expect(video.description).to.equal('my super description for pod 2')
68ce3ae0 166 expect(video.podUrl).to.equal('localhost:9002')
9f10b292 167 expect(video.magnetUri).to.exist
3a8a8b51 168 expect(video.duration).to.equal(5)
be587647 169 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
bb10240e 170 expect(utils.dateIsValid(video.createdDate)).to.be.true
528a9efa 171 expect(video.author).to.equal('root')
9f10b292 172
6d8ada5f
C
173 if (server.url !== 'http://localhost:9002') {
174 expect(video.isLocal).to.be.false
175 } else {
176 expect(video.isLocal).to.be.true
177 }
178
9f10b292 179 // All pods should have the same magnet Uri
bc503c2a
C
180 if (baseMagnet === null) {
181 baseMagnet = video.magnetUri
9f10b292
C
182 } else {
183 expect(video.magnetUri).to.equal.magnetUri
184 }
185
36d56024 186 utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
187 if (err) throw err
188 expect(test).to.equal(true)
189
190 callback()
191 })
9f10b292
C
192 })
193 }, done)
ee66c593 194 }
9f10b292 195 )
8c308c2b
C
196 })
197
9f10b292
C
198 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
199 this.timeout(30000)
8c308c2b 200
1a42c9e2 201 series([
9f10b292 202 function (next) {
be587647
C
203 const name = 'my super name for pod 3'
204 const description = 'my super description for pod 3'
205 const tags = [ 'tag1p3' ]
206 const file = 'video_short3.webm'
207 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
208 },
209 function (next) {
be587647
C
210 const name = 'my super name for pod 3-2'
211 const description = 'my super description for pod 3-2'
212 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ]
213 const file = 'video_short.webm'
214 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
215 },
216 function (next) {
217 setTimeout(next, 22000)
218 }],
219 function (err) {
220 if (err) throw err
8c308c2b 221
bc503c2a 222 let baseMagnet = null
9f10b292 223 // All pods should have this video
1a42c9e2 224 each(servers, function (server, callback) {
0c1cbbfe 225 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
226 if (err) throw err
227
68ce3ae0 228 const videos = res.body.data
9f10b292
C
229 expect(videos).to.be.an('array')
230 expect(videos.length).to.equal(4)
3a8a8b51 231
cbe2f7c3
C
232 // We not sure about the order of the two last uploads
233 let video1 = null
234 let video2 = null
235 if (videos[2].name === 'my super name for pod 3') {
236 video1 = videos[2]
237 video2 = videos[3]
238 } else {
239 video1 = videos[3]
240 video2 = videos[2]
241 }
242
6d8ada5f
C
243 expect(video1.name).to.equal('my super name for pod 3')
244 expect(video1.description).to.equal('my super description for pod 3')
68ce3ae0 245 expect(video1.podUrl).to.equal('localhost:9003')
6d8ada5f 246 expect(video1.magnetUri).to.exist
3a8a8b51 247 expect(video1.duration).to.equal(5)
be587647 248 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
528a9efa 249 expect(video1.author).to.equal('root')
bb10240e 250 expect(utils.dateIsValid(video1.createdDate)).to.be.true
6d8ada5f 251
6d8ada5f
C
252 expect(video2.name).to.equal('my super name for pod 3-2')
253 expect(video2.description).to.equal('my super description for pod 3-2')
68ce3ae0 254 expect(video2.podUrl).to.equal('localhost:9003')
6d8ada5f 255 expect(video2.magnetUri).to.exist
3a8a8b51 256 expect(video2.duration).to.equal(5)
be587647 257 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
528a9efa 258 expect(video2.author).to.equal('root')
bb10240e 259 expect(utils.dateIsValid(video2.createdDate)).to.be.true
6d8ada5f
C
260
261 if (server.url !== 'http://localhost:9003') {
262 expect(video1.isLocal).to.be.false
263 expect(video2.isLocal).to.be.false
264 } else {
265 expect(video1.isLocal).to.be.true
266 expect(video2.isLocal).to.be.true
267 }
9f10b292
C
268
269 // All pods should have the same magnet Uri
bc503c2a
C
270 if (baseMagnet === null) {
271 baseMagnet = video2.magnetUri
9f10b292 272 } else {
6d8ada5f 273 expect(video2.magnetUri).to.equal.magnetUri
9f10b292
C
274 }
275
36d56024 276 utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
9e5f3740
C
277 if (err) throw err
278 expect(test).to.equal(true)
279
36d56024 280 utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
9e5f3740
C
281 if (err) throw err
282 expect(test).to.equal(true)
283
284 callback()
285 })
286 })
9f10b292
C
287 })
288 }, done)
289 }
290 )
8c308c2b 291 })
9f10b292 292 })
8c308c2b 293
9f10b292
C
294 describe('Should seed the uploaded video', function () {
295 it('Should add the file 1 by asking pod 3', function (done) {
296 // Yes, this could be long
297 this.timeout(200000)
8c308c2b 298
0c1cbbfe 299 utils.getVideosList(servers[2].url, function (err, res) {
9f10b292 300 if (err) throw err
8c308c2b 301
68ce3ae0
C
302 const video = res.body.data[0]
303 toRemove.push(res.body.data[2].id)
304 toRemove.push(res.body.data[3].id)
8c308c2b 305
9f10b292
C
306 webtorrent.add(video.magnetUri, function (torrent) {
307 expect(torrent.files).to.exist
308 expect(torrent.files.length).to.equal(1)
309 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 310
9f10b292
C
311 done()
312 })
8c308c2b
C
313 })
314 })
315
9f10b292
C
316 it('Should add the file 2 by asking pod 1', function (done) {
317 // Yes, this could be long
318 this.timeout(200000)
8c308c2b 319
0c1cbbfe 320 utils.getVideosList(servers[0].url, function (err, res) {
9f10b292 321 if (err) throw err
8c308c2b 322
68ce3ae0 323 const video = res.body.data[1]
0b697522 324
9f10b292
C
325 webtorrent.add(video.magnetUri, function (torrent) {
326 expect(torrent.files).to.exist
327 expect(torrent.files.length).to.equal(1)
328 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 329
9f10b292 330 done()
8c308c2b
C
331 })
332 })
9f10b292 333 })
8c308c2b 334
9f10b292
C
335 it('Should add the file 3 by asking pod 2', function (done) {
336 // Yes, this could be long
337 this.timeout(200000)
8c308c2b 338
0c1cbbfe 339 utils.getVideosList(servers[1].url, function (err, res) {
9f10b292 340 if (err) throw err
8c308c2b 341
68ce3ae0 342 const video = res.body.data[2]
8c308c2b 343
9f10b292
C
344 webtorrent.add(video.magnetUri, function (torrent) {
345 expect(torrent.files).to.exist
346 expect(torrent.files.length).to.equal(1)
347 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 348
bc59d9e0 349 webtorrent.remove(video.magnetUri, done)
8c308c2b
C
350 })
351 })
9f10b292 352 })
8c308c2b 353
9f10b292
C
354 it('Should add the file 3-2 by asking pod 1', function (done) {
355 // Yes, this could be long
356 this.timeout(200000)
8c308c2b 357
0c1cbbfe 358 utils.getVideosList(servers[0].url, function (err, res) {
9f10b292 359 if (err) throw err
8c308c2b 360
68ce3ae0 361 const video = res.body.data[3]
8c308c2b 362
9f10b292
C
363 webtorrent.add(video.magnetUri, function (torrent) {
364 expect(torrent.files).to.exist
365 expect(torrent.files.length).to.equal(1)
366 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 367
9f10b292 368 done()
8c308c2b
C
369 })
370 })
9f10b292 371 })
8c308c2b 372
9f10b292
C
373 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
374 this.timeout(15000)
0b697522 375
1a42c9e2 376 series([
9f10b292 377 function (next) {
bc503c2a 378 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
9f10b292
C
379 },
380 function (next) {
bc503c2a 381 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
9f10b292
C
382 }],
383 function (err) {
0b697522 384 if (err) throw err
9f10b292
C
385 setTimeout(done, 11000)
386 }
387 )
388 })
0b697522 389
9f10b292 390 it('Should have videos 1 and 3 on each pod', function (done) {
1a42c9e2 391 each(servers, function (server, callback) {
0c1cbbfe 392 utils.getVideosList(server.url, function (err, res) {
9f10b292 393 if (err) throw err
0b697522 394
68ce3ae0 395 const videos = res.body.data
9f10b292
C
396 expect(videos).to.be.an('array')
397 expect(videos.length).to.equal(2)
2df82d42 398 expect(videos[0].id).not.to.equal(videos[1].id)
bc503c2a
C
399 expect(videos[0].id).not.to.equal(toRemove[0])
400 expect(videos[1].id).not.to.equal(toRemove[0])
401 expect(videos[0].id).not.to.equal(toRemove[1])
402 expect(videos[1].id).not.to.equal(toRemove[1])
0b697522 403
9f10b292 404 callback()
0b697522 405 })
9f10b292 406 }, done)
8c308c2b 407 })
9f10b292 408 })
8c308c2b 409
9f10b292 410 after(function (done) {
0c1cbbfe
C
411 servers.forEach(function (server) {
412 process.kill(-server.app.pid)
8c308c2b 413 })
9f10b292
C
414 process.kill(-webtorrent.app.pid)
415
416 // Keep the logs if the test failed
417 if (this.ok) {
00057e85 418 utils.flushTests(done)
9f10b292
C
419 } else {
420 done()
421 }
8c308c2b 422 })
9f10b292 423})