]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/singlePod.js
e8b578770128f1b5355ee3e57ec151030ddcd4bd
[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 video_id = -1
18
19 before(function (done) {
20 this.timeout(20000)
21
22 async.series([
23 function (next) {
24 utils.flushTests(next)
25 },
26 function (next) {
27 utils.runServer(1, function (server1) {
28 server = server1
29 next()
30 })
31 },
32 function (next) {
33 utils.loginAndGetAccessToken(server, function (err, token) {
34 if (err) throw err
35 server.access_token = token
36 next()
37 })
38 },
39 function (next) {
40 webtorrent.create({ host: 'client', port: '1' }, next)
41 }
42 ], done)
43 })
44
45 it('Should not have videos', function (done) {
46 utils.getVideosList(server.url, function (err, res) {
47 if (err) throw err
48
49 expect(res.body).to.be.an('array')
50 expect(res.body.length).to.equal(0)
51
52 done()
53 })
54 })
55
56 it('Should upload the video', function (done) {
57 this.timeout(5000)
58 utils.uploadVideo(server.url, server.access_token, 'my super name', 'my super description', 'video_short.webm', done)
59 })
60
61 it('Should seed the uploaded video', function (done) {
62 // Yes, this could be long
63 this.timeout(60000)
64
65 utils.getVideosList(server.url, function (err, res) {
66 if (err) throw err
67
68 expect(res.body).to.be.an('array')
69 expect(res.body.length).to.equal(1)
70
71 const video = res.body[0]
72 expect(video.name).to.equal('my super name')
73 expect(video.description).to.equal('my super description')
74 expect(video.podUrl).to.equal('http://localhost:9001')
75 expect(video.magnetUri).to.exist
76 expect(video.author).to.equal('root')
77 expect(video.isLocal).to.be.true
78
79 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
80 if (err) throw err
81 expect(test).to.equal(true)
82
83 video_id = video.id
84
85 webtorrent.add(video.magnetUri, function (torrent) {
86 expect(torrent.files).to.exist
87 expect(torrent.files.length).to.equal(1)
88 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
89
90 done()
91 })
92 })
93 })
94 })
95
96 it('Should get the video', function (done) {
97 // Yes, this could be long
98 this.timeout(60000)
99
100 utils.getVideo(server.url, video_id, function (err, res) {
101 if (err) throw err
102
103 const video = res.body
104 expect(video.name).to.equal('my super name')
105 expect(video.description).to.equal('my super description')
106 expect(video.podUrl).to.equal('http://localhost:9001')
107 expect(video.magnetUri).to.exist
108 expect(video.author).to.equal('root')
109 expect(video.isLocal).to.be.true
110
111 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
112 if (err) throw err
113 expect(test).to.equal(true)
114
115 webtorrent.add(video.magnetUri, function (torrent) {
116 expect(torrent.files).to.exist
117 expect(torrent.files.length).to.equal(1)
118 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
119
120 done()
121 })
122 })
123 })
124 })
125
126 it('Should search the video', function (done) {
127 utils.searchVideo(server.url, 'my', function (err, res) {
128 if (err) throw err
129
130 expect(res.body).to.be.an('array')
131 expect(res.body.length).to.equal(1)
132
133 const video = res.body[0]
134 expect(video.name).to.equal('my super name')
135 expect(video.description).to.equal('my super description')
136 expect(video.podUrl).to.equal('http://localhost:9001')
137 expect(video.author).to.equal('root')
138 expect(video.isLocal).to.be.true
139
140 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
141 if (err) throw err
142 expect(test).to.equal(true)
143
144 done()
145 })
146 })
147 })
148
149 it('Should not find a search', function (done) {
150 utils.searchVideo(server.url, 'hello', function (err, res) {
151 if (err) throw err
152
153 expect(res.body).to.be.an('array')
154 expect(res.body.length).to.equal(0)
155
156 done()
157 })
158 })
159
160 it('Should remove the video', function (done) {
161 utils.removeVideo(server.url, server.access_token, video_id, function (err) {
162 if (err) throw err
163
164 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
165 if (err) throw err
166
167 expect(files.length).to.equal(0)
168 done()
169 })
170 })
171 })
172
173 it('Should not have videos', function (done) {
174 utils.getVideosList(server.url, function (err, res) {
175 if (err) throw err
176
177 expect(res.body).to.be.an('array')
178 expect(res.body.length).to.equal(0)
179
180 done()
181 })
182 })
183
184 it('Should upload 6 videos', function (done) {
185 this.timeout(25000)
186 const videos = [
187 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
188 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
189 ]
190 async.each(videos, function (video, callback_each) {
191 utils.uploadVideo(server.url, server.access_token, video + ' name', video + ' description', video, callback_each)
192 }, done)
193 })
194
195 it('Should have the correct durations', function (done) {
196 utils.getVideosList(server.url, function (err, res) {
197 if (err) throw err
198
199 const videos = res.body
200 expect(videos).to.be.an('array')
201 expect(videos.length).to.equal(6)
202
203 const videos_by_name = keyBy(videos, 'name')
204 expect(videos_by_name['video_short.mp4 name'].duration).to.equal(5)
205 expect(videos_by_name['video_short.ogv name'].duration).to.equal(5)
206 expect(videos_by_name['video_short.webm name'].duration).to.equal(5)
207 expect(videos_by_name['video_short1.webm name'].duration).to.equal(10)
208 expect(videos_by_name['video_short2.webm name'].duration).to.equal(5)
209 expect(videos_by_name['video_short3.webm name'].duration).to.equal(5)
210
211 done()
212 })
213 })
214
215 it('Should have the correct thumbnails', function (done) {
216 utils.getVideosList(server.url, function (err, res) {
217 const videos = res.body
218
219 async.each(videos, function (video, callback_each) {
220 if (err) throw err
221 const video_name = video.name.replace(' name', '')
222
223 utils.testImage(server.url, video_name, video.thumbnailPath, function (err, test) {
224 if (err) throw err
225
226 expect(test).to.equal(true)
227 callback_each()
228 })
229 }, done)
230 })
231 })
232
233 after(function (done) {
234 process.kill(-server.app.pid)
235 process.kill(-webtorrent.app.pid)
236
237 // Keep the logs if the test failed
238 if (this.ok) {
239 utils.flushTests(done)
240 } else {
241 done()
242 }
243 })
244 })