3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
12 getVideosListPagination,
16 searchVideoWithPagination,
24 // ---------------------- Export functions --------------------
26 function getVideoCategories (url, end) {
27 const path = '/api/v1/videos/categories'
31 .set('Accept', 'application/json')
33 .expect('Content-Type', /json/)
37 function getAllVideosListBy (url, end) {
38 const path = '/api/v1/videos'
42 .query({ sort: 'createdAt' })
44 .query({ count: 10000 })
45 .set('Accept', 'application/json')
47 .expect('Content-Type', /json/)
51 function getVideo (url, id, end) {
52 const path = '/api/v1/videos/' + id
56 .set('Accept', 'application/json')
58 .expect('Content-Type', /json/)
62 function getVideosList (url, end) {
63 const path = '/api/v1/videos'
67 .query({ sort: 'name' })
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
74 function getVideosListPagination (url, start, count, sort, end) {
80 const path = '/api/v1/videos'
82 const req = request(url)
84 .query({ start: start })
85 .query({ count: count })
87 if (sort) req.query({ sort })
89 req.set('Accept', 'application/json')
91 .expect('Content-Type', /json/)
95 function getVideosListSort (url, sort, end) {
96 const path = '/api/v1/videos'
100 .query({ sort: sort })
101 .set('Accept', 'application/json')
103 .expect('Content-Type', /json/)
107 function removeVideo (url, token, id, expectedStatus, end) {
113 const path = '/api/v1/videos'
116 .delete(path + '/' + id)
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + token)
119 .expect(expectedStatus)
123 function searchVideo (url, search, field, end) {
129 const path = '/api/v1/videos'
130 const req = request(url)
131 .get(path + '/search/' + search)
132 .set('Accept', 'application/json')
134 if (field) req.query({ field: field })
136 .expect('Content-Type', /json/)
140 function searchVideoWithPagination (url, search, field, start, count, sort, end) {
146 const path = '/api/v1/videos'
148 const req = request(url)
149 .get(path + '/search/' + search)
150 .query({ start: start })
151 .query({ count: count })
152 .query({ field: field })
154 if (sort) req.query({ sort })
156 req.set('Accept', 'application/json')
158 .expect('Content-Type', /json/)
162 function searchVideoWithSort (url, search, sort, end) {
163 const path = '/api/v1/videos'
166 .get(path + '/search/' + search)
167 .query({ sort: sort })
168 .set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
174 function testVideoImage (url, videoName, imagePath, callback) {
175 // Don't test images if the node env is not set
176 // Because we need a special ffmpeg version for this test
177 if (process.env.NODE_TEST_IMAGE) {
181 .end(function (err, res) {
182 if (err) return callback(err)
184 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
185 if (err) return callback(err)
187 callback(null, data.equals(res.body))
191 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
196 function uploadVideo (url, accessToken, name, category, description, tags, fixture, specialStatus, end) {
202 const path = '/api/v1/videos'
204 const req = request(url)
206 .set('Accept', 'application/json')
207 .set('Authorization', 'Bearer ' + accessToken)
209 .field('category', category)
210 .field('description', description)
212 for (let i = 0; i < tags.length; i++) {
213 req.field('tags[' + i + ']', tags[i])
217 if (pathUtils.isAbsolute(fixture)) {
220 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', fixture)
223 req.attach('videofile', filepath)
224 .expect(specialStatus)
228 function updateVideo (url, accessToken, id, name, category, description, tags, specialStatus, end) {
234 const path = '/api/v1/videos/' + id
236 const req = request(url)
238 .set('Accept', 'application/json')
239 .set('Authorization', 'Bearer ' + accessToken)
241 if (name) req.field('name', name)
242 if (category) req.field('category', category)
243 if (description) req.field('description', description)
246 for (let i = 0; i < tags.length; i++) {
247 req.field('tags[' + i + ']', tags[i])
251 req.expect(specialStatus).end(end)
254 function rateVideo (url, accessToken, id, rating, specialStatus, end) {
260 const path = '/api/v1/videos/' + id + '/rate'
264 .set('Accept', 'application/json')
265 .set('Authorization', 'Bearer ' + accessToken)
267 .expect(specialStatus)
271 // ---------------------------------------------------------------------------
273 module.exports = videosUtils