3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
11 getVideosListPagination,
15 searchVideoWithPagination,
23 // ---------------------- Export functions --------------------
25 function getAllVideosListBy (url, end) {
26 const path = '/api/v1/videos'
30 .query({ sort: 'createdAt' })
32 .query({ count: 10000 })
33 .set('Accept', 'application/json')
35 .expect('Content-Type', /json/)
39 function getVideo (url, id, end) {
40 const path = '/api/v1/videos/' + id
44 .set('Accept', 'application/json')
46 .expect('Content-Type', /json/)
50 function getVideosList (url, end) {
51 const path = '/api/v1/videos'
55 .query({ sort: 'name' })
56 .set('Accept', 'application/json')
58 .expect('Content-Type', /json/)
62 function getVideosListPagination (url, start, count, sort, end) {
68 const path = '/api/v1/videos'
70 const req = request(url)
72 .query({ start: start })
73 .query({ count: count })
75 if (sort) req.query({ sort })
77 req.set('Accept', 'application/json')
79 .expect('Content-Type', /json/)
83 function getVideosListSort (url, sort, end) {
84 const path = '/api/v1/videos'
88 .query({ sort: sort })
89 .set('Accept', 'application/json')
91 .expect('Content-Type', /json/)
95 function removeVideo (url, token, id, expectedStatus, end) {
101 const path = '/api/v1/videos'
104 .delete(path + '/' + id)
105 .set('Accept', 'application/json')
106 .set('Authorization', 'Bearer ' + token)
107 .expect(expectedStatus)
111 function searchVideo (url, search, field, end) {
117 const path = '/api/v1/videos'
118 const req = request(url)
119 .get(path + '/search/' + search)
120 .set('Accept', 'application/json')
122 if (field) req.query({ field: field })
124 .expect('Content-Type', /json/)
128 function searchVideoWithPagination (url, search, field, start, count, sort, end) {
134 const path = '/api/v1/videos'
136 const req = request(url)
137 .get(path + '/search/' + search)
138 .query({ start: start })
139 .query({ count: count })
140 .query({ field: field })
142 if (sort) req.query({ sort })
144 req.set('Accept', 'application/json')
146 .expect('Content-Type', /json/)
150 function searchVideoWithSort (url, search, sort, end) {
151 const path = '/api/v1/videos'
154 .get(path + '/search/' + search)
155 .query({ sort: sort })
156 .set('Accept', 'application/json')
158 .expect('Content-Type', /json/)
162 function testVideoImage (url, videoName, imagePath, callback) {
163 // Don't test images if the node env is not set
164 // Because we need a special ffmpeg version for this test
165 if (process.env.NODE_TEST_IMAGE) {
169 .end(function (err, res) {
170 if (err) return callback(err)
172 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
173 if (err) return callback(err)
175 callback(null, data.equals(res.body))
179 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
184 function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
190 const path = '/api/v1/videos'
192 const req = request(url)
194 .set('Accept', 'application/json')
195 .set('Authorization', 'Bearer ' + accessToken)
197 .field('description', description)
199 for (let i = 0; i < tags.length; i++) {
200 req.field('tags[' + i + ']', tags[i])
204 if (pathUtils.isAbsolute(fixture)) {
207 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', fixture)
210 req.attach('videofile', filepath)
211 .expect(specialStatus)
215 function updateVideo (url, accessToken, id, name, description, tags, specialStatus, end) {
221 const path = '/api/v1/videos/' + id
223 const req = request(url)
225 .set('Accept', 'application/json')
226 .set('Authorization', 'Bearer ' + accessToken)
228 if (name) req.field('name', name)
229 if (description) req.field('description', description)
232 for (let i = 0; i < tags.length; i++) {
233 req.field('tags[' + i + ']', tags[i])
237 req.expect(specialStatus).end(end)
240 function rateVideo (url, accessToken, id, rating, specialStatus, end) {
246 const path = '/api/v1/videos/' + id + '/rate'
250 .set('Accept', 'application/json')
251 .set('Authorization', 'Bearer ' + accessToken)
253 .expect(specialStatus)
257 // ---------------------------------------------------------------------------
259 module.exports = videosUtils