]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/singlePod.js
1a53ada3ace5d15256793a51f12ba202f5536dc6
[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).to.be.an('array')
51 expect(res.body.length).to.equal(0)
52
53 done()
54 })
55 })
56
57 it('Should upload the video', function (done) {
58 this.timeout(5000)
59 utils.uploadVideo(server.url, server.accessToken, 'my super name', 'my super description', 'video_short.webm', done)
60 })
61
62 it('Should seed the uploaded video', function (done) {
63 // Yes, this could be long
64 this.timeout(60000)
65
66 utils.getVideosList(server.url, function (err, res) {
67 if (err) throw err
68
69 expect(res.body).to.be.an('array')
70 expect(res.body.length).to.equal(1)
71
72 const video = res.body[0]
73 expect(video.name).to.equal('my super name')
74 expect(video.description).to.equal('my super description')
75 expect(video.podUrl).to.equal('http://localhost:9001')
76 expect(video.magnetUri).to.exist
77 expect(video.author).to.equal('root')
78 expect(video.isLocal).to.be.true
79 expect(utils.dateIsValid(video.createdDate)).to.be.true
80
81 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
82 if (err) throw err
83 expect(test).to.equal(true)
84
85 videoId = video.id
86
87 webtorrent.add(video.magnetUri, function (torrent) {
88 expect(torrent.files).to.exist
89 expect(torrent.files.length).to.equal(1)
90 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
91
92 // We remove it because we'll add it again
93 webtorrent.remove(video.magnetUri, done)
94 })
95 })
96 })
97 })
98
99 it('Should get the video', function (done) {
100 // Yes, this could be long
101 this.timeout(60000)
102
103 utils.getVideo(server.url, videoId, function (err, res) {
104 if (err) throw err
105
106 const video = res.body
107 expect(video.name).to.equal('my super name')
108 expect(video.description).to.equal('my super description')
109 expect(video.podUrl).to.equal('http://localhost:9001')
110 expect(video.magnetUri).to.exist
111 expect(video.author).to.equal('root')
112 expect(video.isLocal).to.be.true
113 expect(utils.dateIsValid(video.createdDate)).to.be.true
114
115 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
116 if (err) throw err
117 expect(test).to.equal(true)
118
119 webtorrent.add(video.magnetUri, function (torrent) {
120 expect(torrent.files).to.exist
121 expect(torrent.files.length).to.equal(1)
122 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
123
124 done()
125 })
126 })
127 })
128 })
129
130 it('Should search the video', function (done) {
131 utils.searchVideo(server.url, 'my', function (err, res) {
132 if (err) throw err
133
134 expect(res.body).to.be.an('array')
135 expect(res.body.length).to.equal(1)
136
137 const video = res.body[0]
138 expect(video.name).to.equal('my super name')
139 expect(video.description).to.equal('my super description')
140 expect(video.podUrl).to.equal('http://localhost:9001')
141 expect(video.author).to.equal('root')
142 expect(video.isLocal).to.be.true
143 expect(utils.dateIsValid(video.createdDate)).to.be.true
144
145 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
146 if (err) throw err
147 expect(test).to.equal(true)
148
149 done()
150 })
151 })
152 })
153
154 it('Should not find a search', function (done) {
155 utils.searchVideo(server.url, 'hello', function (err, res) {
156 if (err) throw err
157
158 expect(res.body).to.be.an('array')
159 expect(res.body.length).to.equal(0)
160
161 done()
162 })
163 })
164
165 it('Should remove the video', function (done) {
166 utils.removeVideo(server.url, server.accessToken, videoId, function (err) {
167 if (err) throw err
168
169 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
170 if (err) throw err
171
172 expect(files.length).to.equal(0)
173 done()
174 })
175 })
176 })
177
178 it('Should not have videos', function (done) {
179 utils.getVideosList(server.url, function (err, res) {
180 if (err) throw err
181
182 expect(res.body).to.be.an('array')
183 expect(res.body.length).to.equal(0)
184
185 done()
186 })
187 })
188
189 it('Should upload 6 videos', function (done) {
190 this.timeout(25000)
191 const videos = [
192 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
193 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
194 ]
195 async.each(videos, function (video, callbackEach) {
196 utils.uploadVideo(server.url, server.accessToken, video + ' name', video + ' description', video, callbackEach)
197 }, done)
198 })
199
200 it('Should have the correct durations', function (done) {
201 utils.getVideosList(server.url, function (err, res) {
202 if (err) throw err
203
204 const videos = res.body
205 expect(videos).to.be.an('array')
206 expect(videos.length).to.equal(6)
207
208 const videosByName = keyBy(videos, 'name')
209 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
210 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
211 expect(videosByName['video_short.webm name'].duration).to.equal(5)
212 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
213 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
214 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
215
216 done()
217 })
218 })
219
220 it('Should have the correct thumbnails', function (done) {
221 utils.getVideosList(server.url, function (err, res) {
222 if (err) throw err
223
224 const videos = res.body
225 // For the next test
226 videosListBase = videos
227
228 async.each(videos, function (video, callbackEach) {
229 if (err) throw err
230 const videoName = video.name.replace(' name', '')
231
232 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) {
233 if (err) throw err
234
235 expect(test).to.equal(true)
236 callbackEach()
237 })
238 }, done)
239 })
240 })
241
242 it('Should list only the two first videos', function (done) {
243 utils.getVideosListPagination(server.url, 0, 2, function (err, res) {
244 if (err) throw err
245
246 const videos = res.body
247 expect(videos.length).to.equal(2)
248 expect(videos[0].name === videosListBase[0].name)
249 expect(videos[1].name === videosListBase[1].name)
250
251 done()
252 })
253 })
254
255 it('Should list only the next three videos', function (done) {
256 utils.getVideosListPagination(server.url, 2, 3, function (err, res) {
257 if (err) throw err
258
259 const videos = res.body
260 expect(videos.length).to.equal(4)
261 expect(videos[0].name === videosListBase[2].name)
262 expect(videos[1].name === videosListBase[3].name)
263 expect(videos[2].name === videosListBase[4].name)
264
265 done()
266 })
267 })
268
269 it('Should list the last video', function (done) {
270 utils.getVideosListPagination(server.url, 5, 6, function (err, res) {
271 if (err) throw err
272
273 const videos = res.body
274 expect(videos.length).to.equal(1)
275 expect(videos[0].name === videosListBase[5].name)
276
277 done()
278 })
279 })
280
281 it('Should search the first video', function (done) {
282 utils.searchVideoWithPagination(server.url, 'webm', 0, 1, function (err, res) {
283 if (err) throw err
284
285 const videos = res.body
286 expect(videos.length).to.equal(1)
287 expect(videos[0].name === 'video_short.webm name')
288
289 done()
290 })
291 })
292
293 it('Should search the last two videos', function (done) {
294 utils.searchVideoWithPagination(server.url, 'webm', 2, 2, function (err, res) {
295 if (err) throw err
296
297 const videos = res.body
298 expect(videos.length).to.equal(2)
299 expect(videos[0].name === 'video_short2.webm name')
300 expect(videos[1].name === 'video_short3.webm name')
301
302 done()
303 })
304 })
305
306 it('Should search all the videos', function (done) {
307 utils.searchVideoWithPagination(server.url, 'webm', 0, 15, function (err, res) {
308 if (err) throw err
309
310 const videos = res.body
311 expect(videos.length).to.equal(4)
312
313 done()
314 })
315 })
316
317 it('Should list and sort by name in descending order', function (done) {
318 utils.getVideosListSort(server.url, '-name', function (err, res) {
319 if (err) throw err
320
321 const videos = res.body
322 expect(videos.length).to.equal(6)
323 expect(videos[5].name === 'video_short.mp4 name')
324 expect(videos[4].name === 'video_short.ogv name')
325 expect(videos[3].name === 'video_short.webm name')
326 expect(videos[2].name === 'video_short1.webm name')
327 expect(videos[1].name === 'video_short2.webm name')
328 expect(videos[0].name === 'video_short3.webm name')
329
330 done()
331 })
332 })
333
334 it('Should search and sort by name in ascending order', function (done) {
335 utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
336 if (err) throw err
337
338 const videos = res.body
339 expect(videos.length).to.equal(4)
340
341 expect(videos[0].name === 'video_short.webm name')
342 expect(videos[1].name === 'video_short1.webm name')
343 expect(videos[2].name === 'video_short2.webm name')
344 expect(videos[3].name === 'video_short3.webm name')
345
346 done()
347 })
348 })
349
350 after(function (done) {
351 process.kill(-server.app.pid)
352 process.kill(-webtorrent.app.pid)
353
354 // Keep the logs if the test failed
355 if (this.ok) {
356 utils.flushTests(done)
357 } else {
358 done()
359 }
360 })
361 })