]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/single-pod.js
Server: adapt tests to host
[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' ])
8d309058 85 expect(miscsUtils.dateIsValid(video.createdDate)).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' ])
8d309058 119 expect(miscsUtils.dateIsValid(video.createdDate)).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' ])
8d309058 145 expect(miscsUtils.dateIsValid(video.createdDate)).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
a4254ea1
C
156 it('Should search the video by podHost', function (done) {
157 videosUtils.searchVideo(server.url, '9001', 'podHost', function (err, res) {
46246b5f
C
158 if (err) throw err
159
160 expect(res.body.total).to.equal(1)
161 expect(res.body.data).to.be.an('array')
162 expect(res.body.data.length).to.equal(1)
163
164 const video = res.body.data[0]
165 expect(video.name).to.equal('my super name')
166 expect(video.description).to.equal('my super description')
a4254ea1 167 expect(video.podHost).to.equal('localhost:9001')
46246b5f
C
168 expect(video.author).to.equal('root')
169 expect(video.isLocal).to.be.true
be587647 170 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 171 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
46246b5f 172
8d309058 173 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
46246b5f
C
174 if (err) throw err
175 expect(test).to.equal(true)
176
177 done()
178 })
179 })
180 })
181
8d199cb8 182 it('Should search the video by tag', function (done) {
8d309058 183 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
8d199cb8
C
184 if (err) throw err
185
186 expect(res.body.total).to.equal(1)
187 expect(res.body.data).to.be.an('array')
188 expect(res.body.data.length).to.equal(1)
189
190 const video = res.body.data[0]
191 expect(video.name).to.equal('my super name')
192 expect(video.description).to.equal('my super description')
a4254ea1 193 expect(video.podHost).to.equal('localhost:9001')
8d199cb8
C
194 expect(video.author).to.equal('root')
195 expect(video.isLocal).to.be.true
196 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 197 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8d199cb8 198
8d309058 199 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
8d199cb8
C
200 if (err) throw err
201 expect(test).to.equal(true)
202
203 done()
204 })
205 })
206 })
207
46246b5f 208 it('Should not find a search by name by default', function (done) {
8d309058 209 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
9f10b292 210 if (err) throw err
4d5f8138 211
68ce3ae0
C
212 expect(res.body.total).to.equal(0)
213 expect(res.body.data).to.be.an('array')
214 expect(res.body.data.length).to.equal(0)
4d5f8138 215
9f10b292 216 done()
4d5f8138 217 })
9f10b292 218 })
4d5f8138 219
46246b5f 220 it('Should not find a search by author', function (done) {
8d309058 221 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
46246b5f
C
222 if (err) throw err
223
224 expect(res.body.total).to.equal(0)
225 expect(res.body.data).to.be.an('array')
226 expect(res.body.data.length).to.equal(0)
227
228 done()
229 })
230 })
231
8d199cb8 232 it('Should not find a search by tag', function (done) {
8d309058 233 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
8d199cb8
C
234 if (err) throw err
235
236 expect(res.body.total).to.equal(0)
237 expect(res.body.data).to.be.an('array')
238 expect(res.body.data.length).to.equal(0)
239
240 done()
241 })
242 })
243
9f10b292 244 it('Should remove the video', function (done) {
8d309058 245 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
9f10b292 246 if (err) throw err
f5a60a51 247
b3d92510 248 fs.readdir(pathUtils.join(__dirname, '../../../test1/videos/'), function (err, files) {
9f10b292 249 if (err) throw err
f5a60a51 250
9f10b292 251 expect(files.length).to.equal(0)
b3d92510
C
252
253 fs.readdir(pathUtils.join(__dirname, '../../../test1/thumbnails/'), function (err, files) {
254 if (err) throw err
255
256 expect(files.length).to.equal(0)
257
258 done()
259 })
876d1bcf 260 })
8c308c2b 261 })
9f10b292 262 })
8c308c2b 263
9f10b292 264 it('Should not have videos', function (done) {
8d309058 265 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 266 if (err) throw err
8c308c2b 267
68ce3ae0
C
268 expect(res.body.total).to.equal(0)
269 expect(res.body.data).to.be.an('array')
270 expect(res.body.data.length).to.equal(0)
8c308c2b 271
9f10b292 272 done()
8c308c2b 273 })
9f10b292 274 })
8c308c2b 275
3a8a8b51
C
276 it('Should upload 6 videos', function (done) {
277 this.timeout(25000)
278 const videos = [
279 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
280 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
281 ]
1a42c9e2 282 each(videos, function (video, callbackEach) {
be587647
C
283 const name = video + ' name'
284 const description = video + ' description'
285 const tags = [ 'tag1', 'tag2', 'tag3' ]
286
8d309058 287 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
3a8a8b51
C
288 }, done)
289 })
290
291 it('Should have the correct durations', function (done) {
8d309058 292 videosUtils.getVideosList(server.url, function (err, res) {
3a8a8b51
C
293 if (err) throw err
294
68ce3ae0
C
295 expect(res.body.total).to.equal(6)
296 const videos = res.body.data
3a8a8b51
C
297 expect(videos).to.be.an('array')
298 expect(videos.length).to.equal(6)
299
bc503c2a
C
300 const videosByName = keyBy(videos, 'name')
301 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
302 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
303 expect(videosByName['video_short.webm name'].duration).to.equal(5)
304 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
305 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
306 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
3a8a8b51
C
307
308 done()
309 })
310 })
311
9e5f3740 312 it('Should have the correct thumbnails', function (done) {
8d309058 313 videosUtils.getVideosList(server.url, function (err, res) {
fbf1134e
C
314 if (err) throw err
315
68ce3ae0 316 const videos = res.body.data
fbf1134e
C
317 // For the next test
318 videosListBase = videos
9e5f3740 319
1a42c9e2 320 each(videos, function (video, callbackEach) {
9e5f3740 321 if (err) throw err
bc503c2a 322 const videoName = video.name.replace(' name', '')
9e5f3740 323
8d309058 324 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
9e5f3740
C
325 if (err) throw err
326
327 expect(test).to.equal(true)
bc503c2a 328 callbackEach()
9e5f3740
C
329 })
330 }, done)
331 })
332 })
333
fbf1134e 334 it('Should list only the two first videos', function (done) {
8d309058 335 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
fbf1134e
C
336 if (err) throw err
337
68ce3ae0
C
338 const videos = res.body.data
339 expect(res.body.total).to.equal(6)
fbf1134e
C
340 expect(videos.length).to.equal(2)
341 expect(videos[0].name === videosListBase[0].name)
342 expect(videos[1].name === videosListBase[1].name)
343
344 done()
345 })
346 })
347
348 it('Should list only the next three videos', function (done) {
8d309058 349 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
fbf1134e
C
350 if (err) throw err
351
68ce3ae0
C
352 const videos = res.body.data
353 expect(res.body.total).to.equal(6)
022856f8 354 expect(videos.length).to.equal(3)
fbf1134e
C
355 expect(videos[0].name === videosListBase[2].name)
356 expect(videos[1].name === videosListBase[3].name)
357 expect(videos[2].name === videosListBase[4].name)
358
359 done()
360 })
361 })
362
363 it('Should list the last video', function (done) {
8d309058 364 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
fbf1134e
C
365 if (err) throw err
366
68ce3ae0
C
367 const videos = res.body.data
368 expect(res.body.total).to.equal(6)
fbf1134e
C
369 expect(videos.length).to.equal(1)
370 expect(videos[0].name === videosListBase[5].name)
371
372 done()
373 })
374 })
375
376 it('Should search the first video', function (done) {
8d309058 377 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
fbf1134e
C
378 if (err) throw err
379
68ce3ae0
C
380 const videos = res.body.data
381 expect(res.body.total).to.equal(4)
fbf1134e
C
382 expect(videos.length).to.equal(1)
383 expect(videos[0].name === 'video_short.webm name')
384
385 done()
386 })
387 })
388
389 it('Should search the last two videos', function (done) {
8d309058 390 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
fbf1134e
C
391 if (err) throw err
392
68ce3ae0
C
393 const videos = res.body.data
394 expect(res.body.total).to.equal(4)
fbf1134e
C
395 expect(videos.length).to.equal(2)
396 expect(videos[0].name === 'video_short2.webm name')
397 expect(videos[1].name === 'video_short3.webm name')
398
399 done()
400 })
401 })
402
46246b5f 403 it('Should search all the webm videos', function (done) {
8d309058 404 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
fbf1134e
C
405 if (err) throw err
406
68ce3ae0
C
407 const videos = res.body.data
408 expect(res.body.total).to.equal(4)
fbf1134e
C
409 expect(videos.length).to.equal(4)
410
411 done()
412 })
413 })
414
46246b5f 415 it('Should search all the root author videos', function (done) {
8d309058 416 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
46246b5f
C
417 if (err) throw err
418
419 const videos = res.body.data
420 expect(res.body.total).to.equal(6)
421 expect(videos.length).to.equal(6)
422
423 done()
424 })
425 })
426
427 it('Should search all the 9001 port videos', function (done) {
a4254ea1 428 videosUtils.searchVideoWithPagination(server.url, '9001', 'podHost', 0, 15, function (err, res) {
46246b5f
C
429 if (err) throw err
430
431 const videos = res.body.data
432 expect(res.body.total).to.equal(6)
433 expect(videos.length).to.equal(6)
434
435 done()
436 })
437 })
438
439 it('Should search all the localhost videos', function (done) {
a4254ea1 440 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podHost', 0, 15, function (err, res) {
46246b5f
C
441 if (err) throw err
442
443 const videos = res.body.data
444 expect(res.body.total).to.equal(6)
445 expect(videos.length).to.equal(6)
446
447 done()
448 })
449 })
450
451 it('Should search the good magnetUri video', function (done) {
452 const video = videosListBase[0]
8d309058 453 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
46246b5f
C
454 if (err) throw err
455
456 const videos = res.body.data
457 expect(res.body.total).to.equal(1)
458 expect(videos.length).to.equal(1)
459 expect(videos[0].name).to.equal(video.name)
460
461 done()
462 })
463 })
464
a877d5ac 465 it('Should list and sort by name in descending order', function (done) {
8d309058 466 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
a877d5ac
C
467 if (err) throw err
468
68ce3ae0
C
469 const videos = res.body.data
470 expect(res.body.total).to.equal(6)
a877d5ac
C
471 expect(videos.length).to.equal(6)
472 expect(videos[5].name === 'video_short.mp4 name')
473 expect(videos[4].name === 'video_short.ogv name')
474 expect(videos[3].name === 'video_short.webm name')
475 expect(videos[2].name === 'video_short1.webm name')
476 expect(videos[1].name === 'video_short2.webm name')
477 expect(videos[0].name === 'video_short3.webm name')
478
479 done()
480 })
481 })
482
483 it('Should search and sort by name in ascending order', function (done) {
8d309058 484 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
a877d5ac
C
485 if (err) throw err
486
68ce3ae0
C
487 const videos = res.body.data
488 expect(res.body.total).to.equal(4)
a877d5ac
C
489 expect(videos.length).to.equal(4)
490
491 expect(videos[0].name === 'video_short.webm name')
492 expect(videos[1].name === 'video_short1.webm name')
493 expect(videos[2].name === 'video_short2.webm name')
494 expect(videos[3].name === 'video_short3.webm name')
495
496 done()
497 })
498 })
499
9f10b292 500 after(function (done) {
0c1cbbfe 501 process.kill(-server.app.pid)
8c308c2b 502
9f10b292
C
503 // Keep the logs if the test failed
504 if (this.ok) {
8d309058 505 serversUtils.flushTests(done)
9f10b292
C
506 } else {
507 done()
508 }
8c308c2b 509 })
9f10b292 510})