3 const fs
= require('fs')
4 const pathUtils
= require('path')
5 const request
= require('supertest')
14 getVideosListPagination
,
18 searchVideoWithPagination
,
26 // ---------------------- Export functions --------------------
28 function getVideoCategories (url
, end
) {
29 const path
= '/api/v1/videos/categories'
33 .set('Accept', 'application/json')
35 .expect('Content-Type', /json/)
39 function getVideoLicences (url
, end
) {
40 const path
= '/api/v1/videos/licences'
44 .set('Accept', 'application/json')
46 .expect('Content-Type', /json/)
50 function getVideoLanguages (url
, end
) {
51 const path
= '/api/v1/videos/languages'
55 .set('Accept', 'application/json')
57 .expect('Content-Type', /json/)
61 function getAllVideosListBy (url
, end
) {
62 const path
= '/api/v1/videos'
66 .query({ sort: 'createdAt' })
68 .query({ count: 10000 })
69 .set('Accept', 'application/json')
71 .expect('Content-Type', /json/)
75 function getVideo (url
, id
, end
) {
76 const path
= '/api/v1/videos/' + id
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function getVideosList (url
, end
) {
87 const path
= '/api/v1/videos'
91 .query({ sort: 'name' })
92 .set('Accept', 'application/json')
94 .expect('Content-Type', /json/)
98 function getVideosListPagination (url
, start
, count
, sort
, end
) {
104 const path
= '/api/v1/videos'
106 const req
= request(url
)
108 .query({ start: start
})
109 .query({ count: count
})
111 if (sort
) req
.query({ sort
})
113 req
.set('Accept', 'application/json')
115 .expect('Content-Type', /json/)
119 function getVideosListSort (url
, sort
, end
) {
120 const path
= '/api/v1/videos'
124 .query({ sort: sort
})
125 .set('Accept', 'application/json')
127 .expect('Content-Type', /json/)
131 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
137 const path
= '/api/v1/videos'
140 .delete(path
+ '/' + id
)
141 .set('Accept', 'application/json')
142 .set('Authorization', 'Bearer ' + token
)
143 .expect(expectedStatus
)
147 function searchVideo (url
, search
, field
, end
) {
153 const path
= '/api/v1/videos'
154 const req
= request(url
)
155 .get(path
+ '/search/' + search
)
156 .set('Accept', 'application/json')
158 if (field
) req
.query({ field: field
})
160 .expect('Content-Type', /json/)
164 function searchVideoWithPagination (url
, search
, field
, start
, count
, sort
, end
) {
170 const path
= '/api/v1/videos'
172 const req
= request(url
)
173 .get(path
+ '/search/' + search
)
174 .query({ start: start
})
175 .query({ count: count
})
176 .query({ field: field
})
178 if (sort
) req
.query({ sort
})
180 req
.set('Accept', 'application/json')
182 .expect('Content-Type', /json/)
186 function searchVideoWithSort (url
, search
, sort
, end
) {
187 const path
= '/api/v1/videos'
190 .get(path
+ '/search/' + search
)
191 .query({ sort: sort
})
192 .set('Accept', 'application/json')
194 .expect('Content-Type', /json/)
198 function testVideoImage (url
, imageName
, imagePath
, callback
) {
199 // Don't test images if the node env is not set
200 // Because we need a special ffmpeg version for this test
201 if (process
.env
.NODE_TEST_IMAGE
) {
205 .end(function (err
, res
) {
206 if (err
) return callback(err
)
208 fs
.readFile(pathUtils
.join(__dirname
, '..', 'api', 'fixtures', imageName
+ '.jpg'), function (err
, data
) {
209 if (err
) return callback(err
)
211 callback(null, data
.equals(res
.body
))
215 console
.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
220 function uploadVideo (url
, accessToken
, videoAttributesArg
, specialStatus
, end
) {
226 const path
= '/api/v1/videos'
228 // Default attributes
230 name: 'my super video',
235 description: 'my super description',
237 fixture: 'video_short.webm'
239 attributes
= Object
.assign(attributes
, videoAttributesArg
)
241 const req
= request(url
)
243 .set('Accept', 'application/json')
244 .set('Authorization', 'Bearer ' + accessToken
)
245 .field('name', attributes
.name
)
246 .field('category', attributes
.category
)
247 .field('licence', attributes
.licence
)
248 .field('language', attributes
.language
)
249 .field('nsfw', attributes
.nsfw
)
250 .field('description', attributes
.description
)
252 for (let i
= 0; i
< attributes
.tags
.length
; i
++) {
253 req
.field('tags[' + i
+ ']', attributes
.tags
[i
])
257 if (pathUtils
.isAbsolute(attributes
.fixture
)) {
258 filepath
= attributes
.fixture
260 filepath
= pathUtils
.join(__dirname
, '..', 'api', 'fixtures', attributes
.fixture
)
263 req
.attach('videofile', filepath
)
264 .expect(specialStatus
)
268 function updateVideo (url
, accessToken
, id
, attributes
, specialStatus
, end
) {
274 const path
= '/api/v1/videos/' + id
277 if (attributes
.name
) body
.name
= attributes
.name
278 if (attributes
.category
) body
.category
= attributes
.category
279 if (attributes
.licence
) body
.licence
= attributes
.licence
280 if (attributes
.language
) body
.language
= attributes
.language
281 if (attributes
.nsfw
) body
.nsfw
= attributes
.nsfw
282 if (attributes
.description
) body
.description
= attributes
.description
283 if (attributes
.tags
) body
.tags
= attributes
.tags
288 .set('Accept', 'application/json')
289 .set('Authorization', 'Bearer ' + accessToken
)
290 .expect(specialStatus
)
294 function rateVideo (url
, accessToken
, id
, rating
, specialStatus
, end
) {
300 const path
= '/api/v1/videos/' + id
+ '/rate'
304 .set('Accept', 'application/json')
305 .set('Authorization', 'Bearer ' + accessToken
)
307 .expect(specialStatus
)
311 // ---------------------------------------------------------------------------
313 module
.exports
= videosUtils