]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/singlePod.js
Server: do not allow a user to remove a video of another user
[github/Chocobozzz/PeerTube.git] / server / tests / api / singlePod.js
1 'use strict'
2
3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const fs = require('fs')
7 const keyBy = require('lodash/keyBy')
8 const pathUtils = require('path')
9 const series = require('async/series')
10
11 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
12 webtorrent.silent = true
13
14 const utils = require('./utils')
15
16 describe('Test a single pod', function () {
17 let server = null
18 let videoId = -1
19 let videosListBase = null
20
21 before(function (done) {
22 this.timeout(20000)
23
24 series([
25 function (next) {
26 utils.flushTests(next)
27 },
28 function (next) {
29 utils.runServer(1, function (server1) {
30 server = server1
31 next()
32 })
33 },
34 function (next) {
35 utils.loginAndGetAccessToken(server, function (err, token) {
36 if (err) throw err
37 server.accessToken = token
38 next()
39 })
40 },
41 function (next) {
42 webtorrent.create({ host: 'client', port: '1' }, next)
43 }
44 ], done)
45 })
46
47 it('Should not have videos', function (done) {
48 utils.getVideosList(server.url, function (err, res) {
49 if (err) throw err
50
51 expect(res.body.total).to.equal(0)
52 expect(res.body.data).to.be.an('array')
53 expect(res.body.data.length).to.equal(0)
54
55 done()
56 })
57 })
58
59 it('Should upload the video', function (done) {
60 this.timeout(5000)
61 const name = 'my super name'
62 const description = 'my super description'
63 const tags = [ 'tag1', 'tag2', 'tag3' ]
64 const file = 'video_short.webm'
65 utils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
66 })
67
68 it('Should seed the uploaded video', function (done) {
69 // Yes, this could be long
70 this.timeout(60000)
71
72 utils.getVideosList(server.url, function (err, res) {
73 if (err) throw err
74
75 expect(res.body.total).to.equal(1)
76 expect(res.body.data).to.be.an('array')
77 expect(res.body.data.length).to.equal(1)
78
79 const video = res.body.data[0]
80 expect(video.name).to.equal('my super name')
81 expect(video.description).to.equal('my super description')
82 expect(video.podUrl).to.equal('localhost:9001')
83 expect(video.magnetUri).to.exist
84 expect(video.author).to.equal('root')
85 expect(video.isLocal).to.be.true
86 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
87 expect(utils.dateIsValid(video.createdDate)).to.be.true
88
89 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
90 if (err) throw err
91 expect(test).to.equal(true)
92
93 videoId = video.id
94
95 webtorrent.add(video.magnetUri, function (torrent) {
96 expect(torrent.files).to.exist
97 expect(torrent.files.length).to.equal(1)
98 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
99
100 // We remove it because we'll add it again
101 webtorrent.remove(video.magnetUri, done)
102 })
103 })
104 })
105 })
106
107 it('Should get the video', function (done) {
108 // Yes, this could be long
109 this.timeout(60000)
110
111 utils.getVideo(server.url, videoId, function (err, res) {
112 if (err) throw err
113
114 const video = res.body
115 expect(video.name).to.equal('my super name')
116 expect(video.description).to.equal('my super description')
117 expect(video.podUrl).to.equal('localhost:9001')
118 expect(video.magnetUri).to.exist
119 expect(video.author).to.equal('root')
120 expect(video.isLocal).to.be.true
121 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
122 expect(utils.dateIsValid(video.createdDate)).to.be.true
123
124 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
125 if (err) throw err
126 expect(test).to.equal(true)
127
128 webtorrent.add(video.magnetUri, function (torrent) {
129 expect(torrent.files).to.exist
130 expect(torrent.files.length).to.equal(1)
131 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
132
133 done()
134 })
135 })
136 })
137 })
138
139 it('Should search the video by name by default', function (done) {
140 utils.searchVideo(server.url, 'my', function (err, res) {
141 if (err) throw err
142
143 expect(res.body.total).to.equal(1)
144 expect(res.body.data).to.be.an('array')
145 expect(res.body.data.length).to.equal(1)
146
147 const video = res.body.data[0]
148 expect(video.name).to.equal('my super name')
149 expect(video.description).to.equal('my super description')
150 expect(video.podUrl).to.equal('localhost:9001')
151 expect(video.author).to.equal('root')
152 expect(video.isLocal).to.be.true
153 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
154 expect(utils.dateIsValid(video.createdDate)).to.be.true
155
156 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
157 if (err) throw err
158 expect(test).to.equal(true)
159
160 done()
161 })
162 })
163 })
164
165 it('Should search the video by podUrl', function (done) {
166 utils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
167 if (err) throw err
168
169 expect(res.body.total).to.equal(1)
170 expect(res.body.data).to.be.an('array')
171 expect(res.body.data.length).to.equal(1)
172
173 const video = res.body.data[0]
174 expect(video.name).to.equal('my super name')
175 expect(video.description).to.equal('my super description')
176 expect(video.podUrl).to.equal('localhost:9001')
177 expect(video.author).to.equal('root')
178 expect(video.isLocal).to.be.true
179 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
180 expect(utils.dateIsValid(video.createdDate)).to.be.true
181
182 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
183 if (err) throw err
184 expect(test).to.equal(true)
185
186 done()
187 })
188 })
189 })
190
191 it('Should search the video by tag', function (done) {
192 utils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
193 if (err) throw err
194
195 expect(res.body.total).to.equal(1)
196 expect(res.body.data).to.be.an('array')
197 expect(res.body.data.length).to.equal(1)
198
199 const video = res.body.data[0]
200 expect(video.name).to.equal('my super name')
201 expect(video.description).to.equal('my super description')
202 expect(video.podUrl).to.equal('localhost:9001')
203 expect(video.author).to.equal('root')
204 expect(video.isLocal).to.be.true
205 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
206 expect(utils.dateIsValid(video.createdDate)).to.be.true
207
208 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
209 if (err) throw err
210 expect(test).to.equal(true)
211
212 done()
213 })
214 })
215 })
216
217 it('Should not find a search by name by default', function (done) {
218 utils.searchVideo(server.url, 'hello', function (err, res) {
219 if (err) throw err
220
221 expect(res.body.total).to.equal(0)
222 expect(res.body.data).to.be.an('array')
223 expect(res.body.data.length).to.equal(0)
224
225 done()
226 })
227 })
228
229 it('Should not find a search by author', function (done) {
230 utils.searchVideo(server.url, 'hello', 'author', function (err, res) {
231 if (err) throw err
232
233 expect(res.body.total).to.equal(0)
234 expect(res.body.data).to.be.an('array')
235 expect(res.body.data.length).to.equal(0)
236
237 done()
238 })
239 })
240
241 it('Should not find a search by tag', function (done) {
242 utils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
243 if (err) throw err
244
245 expect(res.body.total).to.equal(0)
246 expect(res.body.data).to.be.an('array')
247 expect(res.body.data.length).to.equal(0)
248
249 done()
250 })
251 })
252
253 it('Should remove the video', function (done) {
254 utils.removeVideo(server.url, server.accessToken, videoId, function (err) {
255 if (err) throw err
256
257 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
258 if (err) throw err
259
260 expect(files.length).to.equal(0)
261 done()
262 })
263 })
264 })
265
266 it('Should not have videos', function (done) {
267 utils.getVideosList(server.url, function (err, res) {
268 if (err) throw err
269
270 expect(res.body.total).to.equal(0)
271 expect(res.body.data).to.be.an('array')
272 expect(res.body.data.length).to.equal(0)
273
274 done()
275 })
276 })
277
278 it('Should upload 6 videos', function (done) {
279 this.timeout(25000)
280 const videos = [
281 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
282 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
283 ]
284 each(videos, function (video, callbackEach) {
285 const name = video + ' name'
286 const description = video + ' description'
287 const tags = [ 'tag1', 'tag2', 'tag3' ]
288
289 utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
290 }, done)
291 })
292
293 it('Should have the correct durations', function (done) {
294 utils.getVideosList(server.url, function (err, res) {
295 if (err) throw err
296
297 expect(res.body.total).to.equal(6)
298 const videos = res.body.data
299 expect(videos).to.be.an('array')
300 expect(videos.length).to.equal(6)
301
302 const videosByName = keyBy(videos, 'name')
303 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
304 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
305 expect(videosByName['video_short.webm name'].duration).to.equal(5)
306 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
307 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
308 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
309
310 done()
311 })
312 })
313
314 it('Should have the correct thumbnails', function (done) {
315 utils.getVideosList(server.url, function (err, res) {
316 if (err) throw err
317
318 const videos = res.body.data
319 // For the next test
320 videosListBase = videos
321
322 each(videos, function (video, callbackEach) {
323 if (err) throw err
324 const videoName = video.name.replace(' name', '')
325
326 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) {
327 if (err) throw err
328
329 expect(test).to.equal(true)
330 callbackEach()
331 })
332 }, done)
333 })
334 })
335
336 it('Should list only the two first videos', function (done) {
337 utils.getVideosListPagination(server.url, 0, 2, function (err, res) {
338 if (err) throw err
339
340 const videos = res.body.data
341 expect(res.body.total).to.equal(6)
342 expect(videos.length).to.equal(2)
343 expect(videos[0].name === videosListBase[0].name)
344 expect(videos[1].name === videosListBase[1].name)
345
346 done()
347 })
348 })
349
350 it('Should list only the next three videos', function (done) {
351 utils.getVideosListPagination(server.url, 2, 3, function (err, res) {
352 if (err) throw err
353
354 const videos = res.body.data
355 expect(res.body.total).to.equal(6)
356 expect(videos.length).to.equal(3)
357 expect(videos[0].name === videosListBase[2].name)
358 expect(videos[1].name === videosListBase[3].name)
359 expect(videos[2].name === videosListBase[4].name)
360
361 done()
362 })
363 })
364
365 it('Should list the last video', function (done) {
366 utils.getVideosListPagination(server.url, 5, 6, function (err, res) {
367 if (err) throw err
368
369 const videos = res.body.data
370 expect(res.body.total).to.equal(6)
371 expect(videos.length).to.equal(1)
372 expect(videos[0].name === videosListBase[5].name)
373
374 done()
375 })
376 })
377
378 it('Should search the first video', function (done) {
379 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
380 if (err) throw err
381
382 const videos = res.body.data
383 expect(res.body.total).to.equal(4)
384 expect(videos.length).to.equal(1)
385 expect(videos[0].name === 'video_short.webm name')
386
387 done()
388 })
389 })
390
391 it('Should search the last two videos', function (done) {
392 utils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
393 if (err) throw err
394
395 const videos = res.body.data
396 expect(res.body.total).to.equal(4)
397 expect(videos.length).to.equal(2)
398 expect(videos[0].name === 'video_short2.webm name')
399 expect(videos[1].name === 'video_short3.webm name')
400
401 done()
402 })
403 })
404
405 it('Should search all the webm videos', function (done) {
406 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
407 if (err) throw err
408
409 const videos = res.body.data
410 expect(res.body.total).to.equal(4)
411 expect(videos.length).to.equal(4)
412
413 done()
414 })
415 })
416
417 it('Should search all the root author videos', function (done) {
418 utils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
419 if (err) throw err
420
421 const videos = res.body.data
422 expect(res.body.total).to.equal(6)
423 expect(videos.length).to.equal(6)
424
425 done()
426 })
427 })
428
429 it('Should search all the 9001 port videos', function (done) {
430 utils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
431 if (err) throw err
432
433 const videos = res.body.data
434 expect(res.body.total).to.equal(6)
435 expect(videos.length).to.equal(6)
436
437 done()
438 })
439 })
440
441 it('Should search all the localhost videos', function (done) {
442 utils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
443 if (err) throw err
444
445 const videos = res.body.data
446 expect(res.body.total).to.equal(6)
447 expect(videos.length).to.equal(6)
448
449 done()
450 })
451 })
452
453 it('Should search the good magnetUri video', function (done) {
454 const video = videosListBase[0]
455 utils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
456 if (err) throw err
457
458 const videos = res.body.data
459 expect(res.body.total).to.equal(1)
460 expect(videos.length).to.equal(1)
461 expect(videos[0].name).to.equal(video.name)
462
463 done()
464 })
465 })
466
467 it('Should list and sort by name in descending order', function (done) {
468 utils.getVideosListSort(server.url, '-name', function (err, res) {
469 if (err) throw err
470
471 const videos = res.body.data
472 expect(res.body.total).to.equal(6)
473 expect(videos.length).to.equal(6)
474 expect(videos[5].name === 'video_short.mp4 name')
475 expect(videos[4].name === 'video_short.ogv name')
476 expect(videos[3].name === 'video_short.webm name')
477 expect(videos[2].name === 'video_short1.webm name')
478 expect(videos[1].name === 'video_short2.webm name')
479 expect(videos[0].name === 'video_short3.webm name')
480
481 done()
482 })
483 })
484
485 it('Should search and sort by name in ascending order', function (done) {
486 utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
487 if (err) throw err
488
489 const videos = res.body.data
490 expect(res.body.total).to.equal(4)
491 expect(videos.length).to.equal(4)
492
493 expect(videos[0].name === 'video_short.webm name')
494 expect(videos[1].name === 'video_short1.webm name')
495 expect(videos[2].name === 'video_short2.webm name')
496 expect(videos[3].name === 'video_short3.webm name')
497
498 done()
499 })
500 })
501
502 after(function (done) {
503 process.kill(-server.app.pid)
504 process.kill(-webtorrent.app.pid)
505
506 // Keep the logs if the test failed
507 if (this.ok) {
508 utils.flushTests(done)
509 } else {
510 done()
511 }
512 })
513 })