]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/singlePod.js
Authenticate before make/quit friends (server + tests)
[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
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.accessToken = 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.accessToken, '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 videoId = 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 // We remove it because we'll add it again
91 webtorrent.remove(video.magnetUri, done)
92 })
93 })
94 })
95 })
96
97 it('Should get the video', function (done) {
98 // Yes, this could be long
99 this.timeout(60000)
100
101 utils.getVideo(server.url, videoId, function (err, res) {
102 if (err) throw err
103
104 const video = res.body
105 expect(video.name).to.equal('my super name')
106 expect(video.description).to.equal('my super description')
107 expect(video.podUrl).to.equal('http://localhost:9001')
108 expect(video.magnetUri).to.exist
109 expect(video.author).to.equal('root')
110 expect(video.isLocal).to.be.true
111
112 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
113 if (err) throw err
114 expect(test).to.equal(true)
115
116 webtorrent.add(video.magnetUri, function (torrent) {
117 expect(torrent.files).to.exist
118 expect(torrent.files.length).to.equal(1)
119 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
120
121 done()
122 })
123 })
124 })
125 })
126
127 it('Should search the video', function (done) {
128 utils.searchVideo(server.url, 'my', function (err, res) {
129 if (err) throw err
130
131 expect(res.body).to.be.an('array')
132 expect(res.body.length).to.equal(1)
133
134 const video = res.body[0]
135 expect(video.name).to.equal('my super name')
136 expect(video.description).to.equal('my super description')
137 expect(video.podUrl).to.equal('http://localhost:9001')
138 expect(video.author).to.equal('root')
139 expect(video.isLocal).to.be.true
140
141 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
142 if (err) throw err
143 expect(test).to.equal(true)
144
145 done()
146 })
147 })
148 })
149
150 it('Should not find a search', function (done) {
151 utils.searchVideo(server.url, 'hello', function (err, res) {
152 if (err) throw err
153
154 expect(res.body).to.be.an('array')
155 expect(res.body.length).to.equal(0)
156
157 done()
158 })
159 })
160
161 it('Should remove the video', function (done) {
162 utils.removeVideo(server.url, server.accessToken, videoId, function (err) {
163 if (err) throw err
164
165 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
166 if (err) throw err
167
168 expect(files.length).to.equal(0)
169 done()
170 })
171 })
172 })
173
174 it('Should not have videos', function (done) {
175 utils.getVideosList(server.url, function (err, res) {
176 if (err) throw err
177
178 expect(res.body).to.be.an('array')
179 expect(res.body.length).to.equal(0)
180
181 done()
182 })
183 })
184
185 it('Should upload 6 videos', function (done) {
186 this.timeout(25000)
187 const videos = [
188 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
189 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
190 ]
191 async.each(videos, function (video, callbackEach) {
192 utils.uploadVideo(server.url, server.accessToken, video + ' name', video + ' description', video, callbackEach)
193 }, done)
194 })
195
196 it('Should have the correct durations', function (done) {
197 utils.getVideosList(server.url, function (err, res) {
198 if (err) throw err
199
200 const videos = res.body
201 expect(videos).to.be.an('array')
202 expect(videos.length).to.equal(6)
203
204 const videosByName = keyBy(videos, 'name')
205 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
206 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
207 expect(videosByName['video_short.webm name'].duration).to.equal(5)
208 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
209 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
210 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
211
212 done()
213 })
214 })
215
216 it('Should have the correct thumbnails', function (done) {
217 utils.getVideosList(server.url, function (err, res) {
218 const videos = res.body
219
220 async.each(videos, function (video, callbackEach) {
221 if (err) throw err
222 const videoName = video.name.replace(' name', '')
223
224 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) {
225 if (err) throw err
226
227 expect(test).to.equal(true)
228 callbackEach()
229 })
230 }, done)
231 })
232 })
233
234 after(function (done) {
235 process.kill(-server.app.pid)
236 process.kill(-webtorrent.app.pid)
237
238 // Keep the logs if the test failed
239 if (this.ok) {
240 utils.flushTests(done)
241 } else {
242 done()
243 }
244 })
245 })