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