e2999530eab334b5f5918ccc404ea5502467ac23
[oweals/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       video_id = video.id
80
81       webtorrent.add(video.magnetUri, function (torrent) {
82         expect(torrent.files).to.exist
83         expect(torrent.files.length).to.equal(1)
84         expect(torrent.files[0].path).to.exist.and.to.not.equal('')
85
86         done()
87       })
88     })
89   })
90
91   it('Should get the video', function (done) {
92     // Yes, this could be long
93     this.timeout(60000)
94
95     utils.getVideo(server.url, video_id, function (err, res) {
96       if (err) throw err
97
98       const video = res.body
99       expect(video.name).to.equal('my super name')
100       expect(video.description).to.equal('my super description')
101       expect(video.podUrl).to.equal('http://localhost:9001')
102       expect(video.magnetUri).to.exist
103       expect(video.author).to.equal('root')
104       expect(video.isLocal).to.be.true
105
106       webtorrent.add(video.magnetUri, function (torrent) {
107         expect(torrent.files).to.exist
108         expect(torrent.files.length).to.equal(1)
109         expect(torrent.files[0].path).to.exist.and.to.not.equal('')
110
111         done()
112       })
113     })
114   })
115
116   it('Should search the video', function (done) {
117     utils.searchVideo(server.url, 'my', function (err, res) {
118       if (err) throw err
119
120       expect(res.body).to.be.an('array')
121       expect(res.body.length).to.equal(1)
122
123       const video = res.body[0]
124       expect(video.name).to.equal('my super name')
125       expect(video.description).to.equal('my super description')
126       expect(video.podUrl).to.equal('http://localhost:9001')
127       expect(video.author).to.equal('root')
128       expect(video.isLocal).to.be.true
129
130       done()
131     })
132   })
133
134   it('Should not find a search', function (done) {
135     utils.searchVideo(server.url, 'hello', function (err, res) {
136       if (err) throw err
137
138       expect(res.body).to.be.an('array')
139       expect(res.body.length).to.equal(0)
140
141       done()
142     })
143   })
144
145   it('Should remove the video', function (done) {
146     utils.removeVideo(server.url, server.access_token, video_id, function (err) {
147       if (err) throw err
148
149       fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
150         if (err) throw err
151
152         expect(files.length).to.equal(0)
153         done()
154       })
155     })
156   })
157
158   it('Should not have videos', function (done) {
159     utils.getVideosList(server.url, function (err, res) {
160       if (err) throw err
161
162       expect(res.body).to.be.an('array')
163       expect(res.body.length).to.equal(0)
164
165       done()
166     })
167   })
168
169   it('Should upload 6 videos', function (done) {
170     this.timeout(25000)
171     const videos = [
172       'video_short.mp4', 'video_short.ogv', 'video_short.webm',
173       'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
174     ]
175     async.each(videos, function (video, callback_each) {
176       utils.uploadVideo(server.url, server.access_token, video + ' name', video + ' description', video, callback_each)
177     }, done)
178   })
179
180   it('Should have the correct durations', function (done) {
181     utils.getVideosList(server.url, function (err, res) {
182       if (err) throw err
183
184       const videos = res.body
185       expect(videos).to.be.an('array')
186       expect(videos.length).to.equal(6)
187
188       const videos_by_name = keyBy(videos, 'name')
189       expect(videos_by_name['video_short.mp4 name'].duration).to.equal(5)
190       expect(videos_by_name['video_short.ogv name'].duration).to.equal(5)
191       expect(videos_by_name['video_short.webm name'].duration).to.equal(5)
192       expect(videos_by_name['video_short1.webm name'].duration).to.equal(10)
193       expect(videos_by_name['video_short2.webm name'].duration).to.equal(5)
194       expect(videos_by_name['video_short3.webm name'].duration).to.equal(5)
195
196       done()
197     })
198   })
199
200   after(function (done) {
201     process.kill(-server.app.pid)
202     process.kill(-webtorrent.app.pid)
203
204     // Keep the logs if the test failed
205     if (this.ok) {
206       utils.flushTests(done)
207     } else {
208       done()
209     }
210   })
211 })