]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/singlePod.js
Server: split tests utils in multiple files
[github/Chocobozzz/PeerTube.git] / server / tests / api / singlePod.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')
9f10b292 10
8d309058
C
11const loginUtils = require('../utils/login')
12const miscsUtils = require('../utils/miscs')
13const serversUtils = require('../utils/servers')
14const videosUtils = require('../utils/videos')
f0f5567b 15const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9f10b292
C
16webtorrent.silent = true
17
9f10b292 18describe('Test a single pod', function () {
0c1cbbfe 19 let server = null
bc503c2a 20 let videoId = -1
fbf1134e 21 let videosListBase = null
9f10b292
C
22
23 before(function (done) {
24 this.timeout(20000)
25
1a42c9e2 26 series([
9f10b292 27 function (next) {
8d309058 28 serversUtils.flushTests(next)
9f10b292
C
29 },
30 function (next) {
8d309058 31 serversUtils.runServer(1, function (server1) {
0c1cbbfe
C
32 server = server1
33 next()
34 })
35 },
36 function (next) {
8d309058 37 loginUtils.loginAndGetAccessToken(server, function (err, token) {
0c1cbbfe 38 if (err) throw err
b6c6f935 39 server.accessToken = token
9f10b292
C
40 next()
41 })
42 },
43 function (next) {
44 webtorrent.create({ host: 'client', port: '1' }, next)
45 }
46 ], done)
47 })
8c308c2b 48
9f10b292 49 it('Should not have videos', function (done) {
8d309058 50 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 51 if (err) throw err
8c308c2b 52
68ce3ae0
C
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)
8c308c2b 56
9f10b292 57 done()
8c308c2b 58 })
9f10b292 59 })
8c308c2b 60
9f10b292
C
61 it('Should upload the video', function (done) {
62 this.timeout(5000)
be587647
C
63 const name = 'my super name'
64 const description = 'my super description'
65 const tags = [ 'tag1', 'tag2', 'tag3' ]
66 const file = 'video_short.webm'
8d309058 67 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
9f10b292 68 })
8c308c2b 69
9f10b292
C
70 it('Should seed the uploaded video', function (done) {
71 // Yes, this could be long
72 this.timeout(60000)
8c308c2b 73
8d309058 74 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 75 if (err) throw err
8c308c2b 76
68ce3ae0
C
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)
8c308c2b 80
68ce3ae0 81 const video = res.body.data[0]
9f10b292
C
82 expect(video.name).to.equal('my super name')
83 expect(video.description).to.equal('my super description')
68ce3ae0 84 expect(video.podUrl).to.equal('localhost:9001')
9f10b292 85 expect(video.magnetUri).to.exist
6d8ada5f
C
86 expect(video.author).to.equal('root')
87 expect(video.isLocal).to.be.true
be587647 88 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 89 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 90
8d309058 91 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
92 if (err) throw err
93 expect(test).to.equal(true)
2df82d42 94
bc503c2a 95 videoId = video.id
2df82d42 96
9e5f3740
C
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
b6c6f935
C
102 // We remove it because we'll add it again
103 webtorrent.remove(video.magnetUri, done)
9e5f3740 104 })
2df82d42
C
105 })
106 })
107 })
108
109 it('Should get the video', function (done) {
110 // Yes, this could be long
111 this.timeout(60000)
112
8d309058 113 videosUtils.getVideo(server.url, videoId, function (err, res) {
2df82d42
C
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')
68ce3ae0 119 expect(video.podUrl).to.equal('localhost:9001')
2df82d42 120 expect(video.magnetUri).to.exist
6d8ada5f
C
121 expect(video.author).to.equal('root')
122 expect(video.isLocal).to.be.true
be587647 123 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 124 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 125
8d309058 126 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
127 if (err) throw err
128 expect(test).to.equal(true)
8c308c2b 129
9e5f3740
C
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 })
876d1bcf 137 })
8c308c2b 138 })
9f10b292 139 })
8c308c2b 140
46246b5f 141 it('Should search the video by name by default', function (done) {
8d309058 142 videosUtils.searchVideo(server.url, 'my', function (err, res) {
9f10b292 143 if (err) throw err
4d5f8138 144
68ce3ae0
C
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)
4d5f8138 148
68ce3ae0 149 const video = res.body.data[0]
9f10b292
C
150 expect(video.name).to.equal('my super name')
151 expect(video.description).to.equal('my super description')
68ce3ae0 152 expect(video.podUrl).to.equal('localhost:9001')
6d8ada5f
C
153 expect(video.author).to.equal('root')
154 expect(video.isLocal).to.be.true
be587647 155 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 156 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
4d5f8138 157
8d309058 158 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
159 if (err) throw err
160 expect(test).to.equal(true)
161
162 done()
163 })
4d5f8138 164 })
9f10b292 165 })
4d5f8138 166
46246b5f 167 it('Should search the video by podUrl', function (done) {
8d309058 168 videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
46246b5f
C
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
be587647 181 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 182 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
46246b5f 183
8d309058 184 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
46246b5f
C
185 if (err) throw err
186 expect(test).to.equal(true)
187
188 done()
189 })
190 })
191 })
192
8d199cb8 193 it('Should search the video by tag', function (done) {
8d309058 194 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
8d199cb8
C
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' ])
8d309058 208 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8d199cb8 209
8d309058 210 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
8d199cb8
C
211 if (err) throw err
212 expect(test).to.equal(true)
213
214 done()
215 })
216 })
217 })
218
46246b5f 219 it('Should not find a search by name by default', function (done) {
8d309058 220 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
9f10b292 221 if (err) throw err
4d5f8138 222
68ce3ae0
C
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)
4d5f8138 226
9f10b292 227 done()
4d5f8138 228 })
9f10b292 229 })
4d5f8138 230
46246b5f 231 it('Should not find a search by author', function (done) {
8d309058 232 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
46246b5f
C
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
8d199cb8 243 it('Should not find a search by tag', function (done) {
8d309058 244 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
8d199cb8
C
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
9f10b292 255 it('Should remove the video', function (done) {
8d309058 256 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
9f10b292 257 if (err) throw err
f5a60a51 258
3d446a26 259 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
9f10b292 260 if (err) throw err
f5a60a51 261
9f10b292
C
262 expect(files.length).to.equal(0)
263 done()
876d1bcf 264 })
8c308c2b 265 })
9f10b292 266 })
8c308c2b 267
9f10b292 268 it('Should not have videos', function (done) {
8d309058 269 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 270 if (err) throw err
8c308c2b 271
68ce3ae0
C
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)
8c308c2b 275
9f10b292 276 done()
8c308c2b 277 })
9f10b292 278 })
8c308c2b 279
3a8a8b51
C
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 ]
1a42c9e2 286 each(videos, function (video, callbackEach) {
be587647
C
287 const name = video + ' name'
288 const description = video + ' description'
289 const tags = [ 'tag1', 'tag2', 'tag3' ]
290
8d309058 291 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
3a8a8b51
C
292 }, done)
293 })
294
295 it('Should have the correct durations', function (done) {
8d309058 296 videosUtils.getVideosList(server.url, function (err, res) {
3a8a8b51
C
297 if (err) throw err
298
68ce3ae0
C
299 expect(res.body.total).to.equal(6)
300 const videos = res.body.data
3a8a8b51
C
301 expect(videos).to.be.an('array')
302 expect(videos.length).to.equal(6)
303
bc503c2a
C
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)
3a8a8b51
C
311
312 done()
313 })
314 })
315
9e5f3740 316 it('Should have the correct thumbnails', function (done) {
8d309058 317 videosUtils.getVideosList(server.url, function (err, res) {
fbf1134e
C
318 if (err) throw err
319
68ce3ae0 320 const videos = res.body.data
fbf1134e
C
321 // For the next test
322 videosListBase = videos
9e5f3740 323
1a42c9e2 324 each(videos, function (video, callbackEach) {
9e5f3740 325 if (err) throw err
bc503c2a 326 const videoName = video.name.replace(' name', '')
9e5f3740 327
8d309058 328 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
9e5f3740
C
329 if (err) throw err
330
331 expect(test).to.equal(true)
bc503c2a 332 callbackEach()
9e5f3740
C
333 })
334 }, done)
335 })
336 })
337
fbf1134e 338 it('Should list only the two first videos', function (done) {
8d309058 339 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
fbf1134e
C
340 if (err) throw err
341
68ce3ae0
C
342 const videos = res.body.data
343 expect(res.body.total).to.equal(6)
fbf1134e
C
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) {
8d309058 353 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
fbf1134e
C
354 if (err) throw err
355
68ce3ae0
C
356 const videos = res.body.data
357 expect(res.body.total).to.equal(6)
022856f8 358 expect(videos.length).to.equal(3)
fbf1134e
C
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) {
8d309058 368 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
fbf1134e
C
369 if (err) throw err
370
68ce3ae0
C
371 const videos = res.body.data
372 expect(res.body.total).to.equal(6)
fbf1134e
C
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) {
8d309058 381 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
fbf1134e
C
382 if (err) throw err
383
68ce3ae0
C
384 const videos = res.body.data
385 expect(res.body.total).to.equal(4)
fbf1134e
C
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) {
8d309058 394 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
fbf1134e
C
395 if (err) throw err
396
68ce3ae0
C
397 const videos = res.body.data
398 expect(res.body.total).to.equal(4)
fbf1134e
C
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
46246b5f 407 it('Should search all the webm videos', function (done) {
8d309058 408 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
fbf1134e
C
409 if (err) throw err
410
68ce3ae0
C
411 const videos = res.body.data
412 expect(res.body.total).to.equal(4)
fbf1134e
C
413 expect(videos.length).to.equal(4)
414
415 done()
416 })
417 })
418
46246b5f 419 it('Should search all the root author videos', function (done) {
8d309058 420 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
46246b5f
C
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) {
8d309058 432 videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
46246b5f
C
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) {
8d309058 444 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
46246b5f
C
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]
8d309058 457 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
46246b5f
C
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
a877d5ac 469 it('Should list and sort by name in descending order', function (done) {
8d309058 470 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
a877d5ac
C
471 if (err) throw err
472
68ce3ae0
C
473 const videos = res.body.data
474 expect(res.body.total).to.equal(6)
a877d5ac
C
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) {
8d309058 488 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
a877d5ac
C
489 if (err) throw err
490
68ce3ae0
C
491 const videos = res.body.data
492 expect(res.body.total).to.equal(4)
a877d5ac
C
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
9f10b292 504 after(function (done) {
0c1cbbfe 505 process.kill(-server.app.pid)
9f10b292 506 process.kill(-webtorrent.app.pid)
8c308c2b 507
9f10b292
C
508 // Keep the logs if the test failed
509 if (this.ok) {
8d309058 510 serversUtils.flushTests(done)
9f10b292
C
511 } else {
512 done()
513 }
8c308c2b 514 })
9f10b292 515})