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