3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
11 getVideosListPagination,
15 searchVideoWithPagination,
22 // ---------------------- Export functions --------------------
24 function getAllVideosListBy (url, end) {
25 const path = '/api/v1/videos'
29 .query({ sort: 'createdAt' })
31 .query({ count: 10000 })
32 .set('Accept', 'application/json')
34 .expect('Content-Type', /json/)
38 function getVideo (url, id, end) {
39 const path = '/api/v1/videos/' + id
43 .set('Accept', 'application/json')
45 .expect('Content-Type', /json/)
49 function getVideosList (url, end) {
50 const path = '/api/v1/videos'
54 .query({ sort: 'name' })
55 .set('Accept', 'application/json')
57 .expect('Content-Type', /json/)
61 function getVideosListPagination (url, start, count, end) {
62 const path = '/api/v1/videos'
66 .query({ start: start })
67 .query({ count: count })
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
74 function getVideosListSort (url, sort, end) {
75 const path = '/api/v1/videos'
79 .query({ sort: sort })
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function removeVideo (url, token, id, expectedStatus, end) {
92 const path = '/api/v1/videos'
95 .delete(path + '/' + id)
96 .set('Accept', 'application/json')
97 .set('Authorization', 'Bearer ' + token)
98 .expect(expectedStatus)
102 function searchVideo (url, search, field, end) {
108 const path = '/api/v1/videos'
109 const req = request(url)
110 .get(path + '/search/' + search)
111 .set('Accept', 'application/json')
113 if (field) req.query({ field: field })
115 .expect('Content-Type', /json/)
119 function searchVideoWithPagination (url, search, field, start, count, end) {
120 const path = '/api/v1/videos'
123 .get(path + '/search/' + search)
124 .query({ start: start })
125 .query({ count: count })
126 .query({ field: field })
127 .set('Accept', 'application/json')
129 .expect('Content-Type', /json/)
133 function searchVideoWithSort (url, search, sort, end) {
134 const path = '/api/v1/videos'
137 .get(path + '/search/' + search)
138 .query({ sort: sort })
139 .set('Accept', 'application/json')
141 .expect('Content-Type', /json/)
145 function testVideoImage (url, videoName, imagePath, callback) {
146 // Don't test images if the node env is not set
147 // Because we need a special ffmpeg version for this test
148 if (process.env.NODE_TEST_IMAGE) {
152 .end(function (err, res) {
153 if (err) return callback(err)
155 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
156 if (err) return callback(err)
158 callback(null, data.equals(res.body))
162 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
167 function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
173 const path = '/api/v1/videos'
175 const req = request(url)
177 .set('Accept', 'application/json')
178 .set('Authorization', 'Bearer ' + accessToken)
180 .field('description', description)
182 for (let i = 0; i < tags.length; i++) {
183 req.field('tags[' + i + ']', tags[i])
187 if (pathUtils.isAbsolute(fixture)) {
190 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', fixture)
193 req.attach('videofile', filepath)
194 .expect(specialStatus)
198 function updateVideo (url, accessToken, id, name, description, tags, specialStatus, end) {
204 const path = '/api/v1/videos/' + id
206 const req = request(url)
208 .set('Accept', 'application/json')
209 .set('Authorization', 'Bearer ' + accessToken)
211 if (name) req.field('name', name)
212 if (description) req.field('description', description)
215 for (let i = 0; i < tags.length; i++) {
216 req.field('tags[' + i + ']', tags[i])
220 req.expect(specialStatus).end(end)
223 // ---------------------------------------------------------------------------
225 module.exports = videosUtils