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