]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/single-pod.js
Server: forbid to remove the root user
[github/Chocobozzz/PeerTube.git] / server / tests / api / single-pod.js
1 'use strict'
2
3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const fs = require('fs')
7 const keyBy = require('lodash/keyBy')
8 const pathUtils = require('path')
9 const series = require('async/series')
10 const webtorrent = new (require('webtorrent'))()
11
12 const loginUtils = require('../utils/login')
13 const miscsUtils = require('../utils/miscs')
14 const serversUtils = require('../utils/servers')
15 const videosUtils = require('../utils/videos')
16
17 describe('Test a single pod', function () {
18 let server = null
19 let videoId = -1
20 let videosListBase = null
21
22 before(function (done) {
23 this.timeout(20000)
24
25 series([
26 function (next) {
27 serversUtils.flushTests(next)
28 },
29 function (next) {
30 serversUtils.runServer(1, function (server1) {
31 server = server1
32 next()
33 })
34 },
35 function (next) {
36 loginUtils.loginAndGetAccessToken(server, function (err, token) {
37 if (err) throw err
38 server.accessToken = token
39 next()
40 })
41 }
42 ], done)
43 })
44
45 it('Should not have videos', function (done) {
46 videosUtils.getVideosList(server.url, function (err, res) {
47 if (err) throw err
48
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)
52
53 done()
54 })
55 })
56
57 it('Should upload the video', function (done) {
58 this.timeout(5000)
59 const name = 'my super name'
60 const description = 'my super description'
61 const tags = [ 'tag1', 'tag2', 'tag3' ]
62 const file = 'video_short.webm'
63 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
64 })
65
66 it('Should seed the uploaded video', function (done) {
67 // Yes, this could be long
68 this.timeout(60000)
69
70 videosUtils.getVideosList(server.url, function (err, res) {
71 if (err) throw err
72
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)
76
77 const video = res.body.data[0]
78 expect(video.name).to.equal('my super name')
79 expect(video.description).to.equal('my super description')
80 expect(video.podUrl).to.equal('localhost:9001')
81 expect(video.magnetUri).to.exist
82 expect(video.author).to.equal('root')
83 expect(video.isLocal).to.be.true
84 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
85 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
86
87 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
88 if (err) throw err
89 expect(test).to.equal(true)
90
91 videoId = video.id
92
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
98 done()
99 })
100 })
101 })
102 })
103
104 it('Should get the video', function (done) {
105 // Yes, this could be long
106 this.timeout(60000)
107
108 videosUtils.getVideo(server.url, videoId, function (err, res) {
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')
114 expect(video.podUrl).to.equal('localhost:9001')
115 expect(video.magnetUri).to.exist
116 expect(video.author).to.equal('root')
117 expect(video.isLocal).to.be.true
118 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
119 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
120
121 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
122 if (err) throw err
123 expect(test).to.equal(true)
124
125 done()
126 })
127 })
128 })
129
130 it('Should search the video by name by default', function (done) {
131 videosUtils.searchVideo(server.url, 'my', function (err, res) {
132 if (err) throw err
133
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)
137
138 const video = res.body.data[0]
139 expect(video.name).to.equal('my super name')
140 expect(video.description).to.equal('my super description')
141 expect(video.podUrl).to.equal('localhost:9001')
142 expect(video.author).to.equal('root')
143 expect(video.isLocal).to.be.true
144 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
145 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
146
147 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
148 if (err) throw err
149 expect(test).to.equal(true)
150
151 done()
152 })
153 })
154 })
155
156 it('Should search the video by podUrl', function (done) {
157 videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
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')
167 expect(video.podUrl).to.equal('localhost:9001')
168 expect(video.author).to.equal('root')
169 expect(video.isLocal).to.be.true
170 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
171 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
172
173 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
174 if (err) throw err
175 expect(test).to.equal(true)
176
177 done()
178 })
179 })
180 })
181
182 it('Should search the video by tag', function (done) {
183 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
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')
193 expect(video.podUrl).to.equal('localhost:9001')
194 expect(video.author).to.equal('root')
195 expect(video.isLocal).to.be.true
196 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
197 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
198
199 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
200 if (err) throw err
201 expect(test).to.equal(true)
202
203 done()
204 })
205 })
206 })
207
208 it('Should not find a search by name by default', function (done) {
209 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
210 if (err) throw err
211
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)
215
216 done()
217 })
218 })
219
220 it('Should not find a search by author', function (done) {
221 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
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
232 it('Should not find a search by tag', function (done) {
233 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
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
244 it('Should remove the video', function (done) {
245 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
246 if (err) throw err
247
248 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
249 if (err) throw err
250
251 expect(files.length).to.equal(0)
252 done()
253 })
254 })
255 })
256
257 it('Should not have videos', function (done) {
258 videosUtils.getVideosList(server.url, function (err, res) {
259 if (err) throw err
260
261 expect(res.body.total).to.equal(0)
262 expect(res.body.data).to.be.an('array')
263 expect(res.body.data.length).to.equal(0)
264
265 done()
266 })
267 })
268
269 it('Should upload 6 videos', function (done) {
270 this.timeout(25000)
271 const videos = [
272 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
273 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
274 ]
275 each(videos, function (video, callbackEach) {
276 const name = video + ' name'
277 const description = video + ' description'
278 const tags = [ 'tag1', 'tag2', 'tag3' ]
279
280 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
281 }, done)
282 })
283
284 it('Should have the correct durations', function (done) {
285 videosUtils.getVideosList(server.url, function (err, res) {
286 if (err) throw err
287
288 expect(res.body.total).to.equal(6)
289 const videos = res.body.data
290 expect(videos).to.be.an('array')
291 expect(videos.length).to.equal(6)
292
293 const videosByName = keyBy(videos, 'name')
294 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
295 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
296 expect(videosByName['video_short.webm name'].duration).to.equal(5)
297 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
298 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
299 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
300
301 done()
302 })
303 })
304
305 it('Should have the correct thumbnails', function (done) {
306 videosUtils.getVideosList(server.url, function (err, res) {
307 if (err) throw err
308
309 const videos = res.body.data
310 // For the next test
311 videosListBase = videos
312
313 each(videos, function (video, callbackEach) {
314 if (err) throw err
315 const videoName = video.name.replace(' name', '')
316
317 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
318 if (err) throw err
319
320 expect(test).to.equal(true)
321 callbackEach()
322 })
323 }, done)
324 })
325 })
326
327 it('Should list only the two first videos', function (done) {
328 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
329 if (err) throw err
330
331 const videos = res.body.data
332 expect(res.body.total).to.equal(6)
333 expect(videos.length).to.equal(2)
334 expect(videos[0].name === videosListBase[0].name)
335 expect(videos[1].name === videosListBase[1].name)
336
337 done()
338 })
339 })
340
341 it('Should list only the next three videos', function (done) {
342 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
343 if (err) throw err
344
345 const videos = res.body.data
346 expect(res.body.total).to.equal(6)
347 expect(videos.length).to.equal(3)
348 expect(videos[0].name === videosListBase[2].name)
349 expect(videos[1].name === videosListBase[3].name)
350 expect(videos[2].name === videosListBase[4].name)
351
352 done()
353 })
354 })
355
356 it('Should list the last video', function (done) {
357 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
358 if (err) throw err
359
360 const videos = res.body.data
361 expect(res.body.total).to.equal(6)
362 expect(videos.length).to.equal(1)
363 expect(videos[0].name === videosListBase[5].name)
364
365 done()
366 })
367 })
368
369 it('Should search the first video', function (done) {
370 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
371 if (err) throw err
372
373 const videos = res.body.data
374 expect(res.body.total).to.equal(4)
375 expect(videos.length).to.equal(1)
376 expect(videos[0].name === 'video_short.webm name')
377
378 done()
379 })
380 })
381
382 it('Should search the last two videos', function (done) {
383 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
384 if (err) throw err
385
386 const videos = res.body.data
387 expect(res.body.total).to.equal(4)
388 expect(videos.length).to.equal(2)
389 expect(videos[0].name === 'video_short2.webm name')
390 expect(videos[1].name === 'video_short3.webm name')
391
392 done()
393 })
394 })
395
396 it('Should search all the webm videos', function (done) {
397 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
398 if (err) throw err
399
400 const videos = res.body.data
401 expect(res.body.total).to.equal(4)
402 expect(videos.length).to.equal(4)
403
404 done()
405 })
406 })
407
408 it('Should search all the root author videos', function (done) {
409 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
410 if (err) throw err
411
412 const videos = res.body.data
413 expect(res.body.total).to.equal(6)
414 expect(videos.length).to.equal(6)
415
416 done()
417 })
418 })
419
420 it('Should search all the 9001 port videos', function (done) {
421 videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
422 if (err) throw err
423
424 const videos = res.body.data
425 expect(res.body.total).to.equal(6)
426 expect(videos.length).to.equal(6)
427
428 done()
429 })
430 })
431
432 it('Should search all the localhost videos', function (done) {
433 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
434 if (err) throw err
435
436 const videos = res.body.data
437 expect(res.body.total).to.equal(6)
438 expect(videos.length).to.equal(6)
439
440 done()
441 })
442 })
443
444 it('Should search the good magnetUri video', function (done) {
445 const video = videosListBase[0]
446 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
447 if (err) throw err
448
449 const videos = res.body.data
450 expect(res.body.total).to.equal(1)
451 expect(videos.length).to.equal(1)
452 expect(videos[0].name).to.equal(video.name)
453
454 done()
455 })
456 })
457
458 it('Should list and sort by name in descending order', function (done) {
459 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
460 if (err) throw err
461
462 const videos = res.body.data
463 expect(res.body.total).to.equal(6)
464 expect(videos.length).to.equal(6)
465 expect(videos[5].name === 'video_short.mp4 name')
466 expect(videos[4].name === 'video_short.ogv name')
467 expect(videos[3].name === 'video_short.webm name')
468 expect(videos[2].name === 'video_short1.webm name')
469 expect(videos[1].name === 'video_short2.webm name')
470 expect(videos[0].name === 'video_short3.webm name')
471
472 done()
473 })
474 })
475
476 it('Should search and sort by name in ascending order', function (done) {
477 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
478 if (err) throw err
479
480 const videos = res.body.data
481 expect(res.body.total).to.equal(4)
482 expect(videos.length).to.equal(4)
483
484 expect(videos[0].name === 'video_short.webm name')
485 expect(videos[1].name === 'video_short1.webm name')
486 expect(videos[2].name === 'video_short2.webm name')
487 expect(videos[3].name === 'video_short3.webm name')
488
489 done()
490 })
491 })
492
493 after(function (done) {
494 process.kill(-server.app.pid)
495
496 // Keep the logs if the test failed
497 if (this.ok) {
498 serversUtils.flushTests(done)
499 } else {
500 done()
501 }
502 })
503 })