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
, videoAttributesArg
, specialStatus
, end
) {
202 const path
= '/api/v1/videos'
204 // Default attributes
206 name: 'my super video',
208 description: 'my super description',
210 fixture: 'video_short.webm'
212 attributes
= Object
.assign(attributes
, videoAttributesArg
)
214 const req
= request(url
)
216 .set('Accept', 'application/json')
217 .set('Authorization', 'Bearer ' + accessToken
)
218 .field('name', attributes
.name
)
219 .field('category', attributes
.category
)
220 .field('description', attributes
.description
)
222 for (let i
= 0; i
< attributes
.tags
.length
; i
++) {
223 req
.field('tags[' + i
+ ']', attributes
.tags
[i
])
227 if (pathUtils
.isAbsolute(attributes
.fixture
)) {
228 filepath
= attributes
.fixture
230 filepath
= pathUtils
.join(__dirname
, '..', 'api', 'fixtures', attributes
.fixture
)
233 req
.attach('videofile', filepath
)
234 .expect(specialStatus
)
238 function updateVideo (url
, accessToken
, id
, attributes
, specialStatus
, end
) {
244 const path
= '/api/v1/videos/' + id
246 const req
= request(url
)
248 .set('Accept', 'application/json')
249 .set('Authorization', 'Bearer ' + accessToken
)
251 if (attributes
.name
) req
.field('name', attributes
.name
)
252 if (attributes
.category
) req
.field('category', attributes
.category
)
253 if (attributes
.description
) req
.field('description', attributes
.description
)
255 if (attributes
.tags
) {
256 for (let i
= 0; i
< attributes
.tags
.length
; i
++) {
257 req
.field('tags[' + i
+ ']', attributes
.tags
[i
])
261 req
.expect(specialStatus
).end(end
)
264 function rateVideo (url
, accessToken
, id
, rating
, specialStatus
, end
) {
270 const path
= '/api/v1/videos/' + id
+ '/rate'
274 .set('Accept', 'application/json')
275 .set('Authorization', 'Bearer ' + accessToken
)
277 .expect(specialStatus
)
281 // ---------------------------------------------------------------------------
283 module
.exports
= videosUtils