]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/singlePod.js
Add tags support to server
[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 not find a search by name by default', function (done) {
191 utils.searchVideo(server.url, 'hello', function (err, res) {
192 if (err) throw err
193
194 expect(res.body.total).to.equal(0)
195 expect(res.body.data).to.be.an('array')
196 expect(res.body.data.length).to.equal(0)
197
198 done()
199 })
200 })
201
202 it('Should not find a search by author', function (done) {
203 utils.searchVideo(server.url, 'hello', 'author', function (err, res) {
204 if (err) throw err
205
206 expect(res.body.total).to.equal(0)
207 expect(res.body.data).to.be.an('array')
208 expect(res.body.data.length).to.equal(0)
209
210 done()
211 })
212 })
213
214 it('Should remove the video', function (done) {
215 utils.removeVideo(server.url, server.accessToken, videoId, function (err) {
216 if (err) throw err
217
218 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
219 if (err) throw err
220
221 expect(files.length).to.equal(0)
222 done()
223 })
224 })
225 })
226
227 it('Should not have videos', function (done) {
228 utils.getVideosList(server.url, function (err, res) {
229 if (err) throw err
230
231 expect(res.body.total).to.equal(0)
232 expect(res.body.data).to.be.an('array')
233 expect(res.body.data.length).to.equal(0)
234
235 done()
236 })
237 })
238
239 it('Should upload 6 videos', function (done) {
240 this.timeout(25000)
241 const videos = [
242 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
243 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
244 ]
245 async.each(videos, function (video, callbackEach) {
246 const name = video + ' name'
247 const description = video + ' description'
248 const tags = [ 'tag1', 'tag2', 'tag3' ]
249
250 utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
251 }, done)
252 })
253
254 it('Should have the correct durations', function (done) {
255 utils.getVideosList(server.url, function (err, res) {
256 if (err) throw err
257
258 expect(res.body.total).to.equal(6)
259 const videos = res.body.data
260 expect(videos).to.be.an('array')
261 expect(videos.length).to.equal(6)
262
263 const videosByName = keyBy(videos, 'name')
264 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
265 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
266 expect(videosByName['video_short.webm name'].duration).to.equal(5)
267 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
268 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
269 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
270
271 done()
272 })
273 })
274
275 it('Should have the correct thumbnails', function (done) {
276 utils.getVideosList(server.url, function (err, res) {
277 if (err) throw err
278
279 const videos = res.body.data
280 // For the next test
281 videosListBase = videos
282
283 async.each(videos, function (video, callbackEach) {
284 if (err) throw err
285 const videoName = video.name.replace(' name', '')
286
287 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) {
288 if (err) throw err
289
290 expect(test).to.equal(true)
291 callbackEach()
292 })
293 }, done)
294 })
295 })
296
297 it('Should list only the two first videos', function (done) {
298 utils.getVideosListPagination(server.url, 0, 2, function (err, res) {
299 if (err) throw err
300
301 const videos = res.body.data
302 expect(res.body.total).to.equal(6)
303 expect(videos.length).to.equal(2)
304 expect(videos[0].name === videosListBase[0].name)
305 expect(videos[1].name === videosListBase[1].name)
306
307 done()
308 })
309 })
310
311 it('Should list only the next three videos', function (done) {
312 utils.getVideosListPagination(server.url, 2, 3, function (err, res) {
313 if (err) throw err
314
315 const videos = res.body.data
316 expect(res.body.total).to.equal(6)
317 expect(videos.length).to.equal(4)
318 expect(videos[0].name === videosListBase[2].name)
319 expect(videos[1].name === videosListBase[3].name)
320 expect(videos[2].name === videosListBase[4].name)
321
322 done()
323 })
324 })
325
326 it('Should list the last video', function (done) {
327 utils.getVideosListPagination(server.url, 5, 6, function (err, res) {
328 if (err) throw err
329
330 const videos = res.body.data
331 expect(res.body.total).to.equal(6)
332 expect(videos.length).to.equal(1)
333 expect(videos[0].name === videosListBase[5].name)
334
335 done()
336 })
337 })
338
339 it('Should search the first video', function (done) {
340 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
341 if (err) throw err
342
343 const videos = res.body.data
344 expect(res.body.total).to.equal(4)
345 expect(videos.length).to.equal(1)
346 expect(videos[0].name === 'video_short.webm name')
347
348 done()
349 })
350 })
351
352 it('Should search the last two videos', function (done) {
353 utils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
354 if (err) throw err
355
356 const videos = res.body.data
357 expect(res.body.total).to.equal(4)
358 expect(videos.length).to.equal(2)
359 expect(videos[0].name === 'video_short2.webm name')
360 expect(videos[1].name === 'video_short3.webm name')
361
362 done()
363 })
364 })
365
366 it('Should search all the webm videos', function (done) {
367 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
368 if (err) throw err
369
370 const videos = res.body.data
371 expect(res.body.total).to.equal(4)
372 expect(videos.length).to.equal(4)
373
374 done()
375 })
376 })
377
378 it('Should search all the root author videos', function (done) {
379 utils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
380 if (err) throw err
381
382 const videos = res.body.data
383 expect(res.body.total).to.equal(6)
384 expect(videos.length).to.equal(6)
385
386 done()
387 })
388 })
389
390 it('Should search all the 9001 port videos', function (done) {
391 utils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
392 if (err) throw err
393
394 const videos = res.body.data
395 expect(res.body.total).to.equal(6)
396 expect(videos.length).to.equal(6)
397
398 done()
399 })
400 })
401
402 it('Should search all the localhost videos', function (done) {
403 utils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
404 if (err) throw err
405
406 const videos = res.body.data
407 expect(res.body.total).to.equal(6)
408 expect(videos.length).to.equal(6)
409
410 done()
411 })
412 })
413
414 it('Should search the good magnetUri video', function (done) {
415 const video = videosListBase[0]
416 utils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
417 if (err) throw err
418
419 const videos = res.body.data
420 expect(res.body.total).to.equal(1)
421 expect(videos.length).to.equal(1)
422 expect(videos[0].name).to.equal(video.name)
423
424 done()
425 })
426 })
427
428 it('Should list and sort by name in descending order', function (done) {
429 utils.getVideosListSort(server.url, '-name', 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 expect(videos[5].name === 'video_short.mp4 name')
436 expect(videos[4].name === 'video_short.ogv name')
437 expect(videos[3].name === 'video_short.webm name')
438 expect(videos[2].name === 'video_short1.webm name')
439 expect(videos[1].name === 'video_short2.webm name')
440 expect(videos[0].name === 'video_short3.webm name')
441
442 done()
443 })
444 })
445
446 it('Should search and sort by name in ascending order', function (done) {
447 utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
448 if (err) throw err
449
450 const videos = res.body.data
451 expect(res.body.total).to.equal(4)
452 expect(videos.length).to.equal(4)
453
454 expect(videos[0].name === 'video_short.webm name')
455 expect(videos[1].name === 'video_short1.webm name')
456 expect(videos[2].name === 'video_short2.webm name')
457 expect(videos[3].name === 'video_short3.webm name')
458
459 done()
460 })
461 })
462
463 after(function (done) {
464 process.kill(-server.app.pid)
465 process.kill(-webtorrent.app.pid)
466
467 // Keep the logs if the test failed
468 if (this.ok) {
469 utils.flushTests(done)
470 } else {
471 done()
472 }
473 })
474 })