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