3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
13 getVideosListPagination,
17 searchVideoWithPagination,
25 // ---------------------- Export functions --------------------
27 function getVideoCategories (url, end) {
28 const path = '/api/v1/videos/categories'
32 .set('Accept', 'application/json')
34 .expect('Content-Type', /json/)
38 function getVideoLicences (url, end) {
39 const path = '/api/v1/videos/licences'
43 .set('Accept', 'application/json')
45 .expect('Content-Type', /json/)
49 function getAllVideosListBy (url, end) {
50 const path = '/api/v1/videos'
54 .query({ sort: 'createdAt' })
56 .query({ count: 10000 })
57 .set('Accept', 'application/json')
59 .expect('Content-Type', /json/)
63 function getVideo (url, id, end) {
64 const path = '/api/v1/videos/' + id
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
74 function getVideosList (url, end) {
75 const path = '/api/v1/videos'
79 .query({ sort: 'name' })
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function getVideosListPagination (url, start, count, sort, end) {
92 const path = '/api/v1/videos'
94 const req = request(url)
96 .query({ start: start })
97 .query({ count: count })
99 if (sort) req.query({ sort })
101 req.set('Accept', 'application/json')
103 .expect('Content-Type', /json/)
107 function getVideosListSort (url, sort, end) {
108 const path = '/api/v1/videos'
112 .query({ sort: sort })
113 .set('Accept', 'application/json')
115 .expect('Content-Type', /json/)
119 function removeVideo (url, token, id, expectedStatus, end) {
125 const path = '/api/v1/videos'
128 .delete(path + '/' + id)
129 .set('Accept', 'application/json')
130 .set('Authorization', 'Bearer ' + token)
131 .expect(expectedStatus)
135 function searchVideo (url, search, field, end) {
141 const path = '/api/v1/videos'
142 const req = request(url)
143 .get(path + '/search/' + search)
144 .set('Accept', 'application/json')
146 if (field) req.query({ field: field })
148 .expect('Content-Type', /json/)
152 function searchVideoWithPagination (url, search, field, start, count, sort, end) {
158 const path = '/api/v1/videos'
160 const req = request(url)
161 .get(path + '/search/' + search)
162 .query({ start: start })
163 .query({ count: count })
164 .query({ field: field })
166 if (sort) req.query({ sort })
168 req.set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
174 function searchVideoWithSort (url, search, sort, end) {
175 const path = '/api/v1/videos'
178 .get(path + '/search/' + search)
179 .query({ sort: sort })
180 .set('Accept', 'application/json')
182 .expect('Content-Type', /json/)
186 function testVideoImage (url, videoName, imagePath, callback) {
187 // Don't test images if the node env is not set
188 // Because we need a special ffmpeg version for this test
189 if (process.env.NODE_TEST_IMAGE) {
193 .end(function (err, res) {
194 if (err) return callback(err)
196 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
197 if (err) return callback(err)
199 callback(null, data.equals(res.body))
203 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
208 function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
214 const path = '/api/v1/videos'
216 // Default attributes
218 name: 'my super video',
221 description: 'my super description',
223 fixture: 'video_short.webm'
225 attributes = Object.assign(attributes, videoAttributesArg)
227 const req = request(url)
229 .set('Accept', 'application/json')
230 .set('Authorization', 'Bearer ' + accessToken)
231 .field('name', attributes.name)
232 .field('category', attributes.category)
233 .field('licence', attributes.licence)
234 .field('description', attributes.description)
236 for (let i = 0; i < attributes.tags.length; i++) {
237 req.field('tags[' + i + ']', attributes.tags[i])
241 if (pathUtils.isAbsolute(attributes.fixture)) {
242 filepath = attributes.fixture
244 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
247 req.attach('videofile', filepath)
248 .expect(specialStatus)
252 function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
258 const path = '/api/v1/videos/' + id
260 const req = request(url)
262 .set('Accept', 'application/json')
263 .set('Authorization', 'Bearer ' + accessToken)
265 if (attributes.name) req.field('name', attributes.name)
266 if (attributes.category) req.field('category', attributes.category)
267 if (attributes.licence) req.field('licence', attributes.licence)
268 if (attributes.description) req.field('description', attributes.description)
270 if (attributes.tags) {
271 for (let i = 0; i < attributes.tags.length; i++) {
272 req.field('tags[' + i + ']', attributes.tags[i])
276 req.expect(specialStatus).end(end)
279 function rateVideo (url, accessToken, id, rating, specialStatus, end) {
285 const path = '/api/v1/videos/' + id + '/rate'
289 .set('Accept', 'application/json')
290 .set('Authorization', 'Bearer ' + accessToken)
292 .expect(specialStatus)
296 // ---------------------------------------------------------------------------
298 module.exports = videosUtils