3 const fs
= require('fs')
4 const pathUtils
= require('path')
5 const request
= require('supertest')
13 getVideosListPagination
,
17 searchVideoWithPagination
,
25 // ---------------------- Export functions --------------------
27 function getVideoCategories (url
, end
) {
28 const path
= '/api/v1/videos/categories'
32 .set('Accept', 'application/json')
34 .expect('Content-Type', /json/)
38 function getVideoLicences (url
, end
) {
39 const path
= '/api/v1/videos/licences'
43 .set('Accept', 'application/json')
45 .expect('Content-Type', /json/)
49 function getAllVideosListBy (url
, end
) {
50 const path
= '/api/v1/videos'
54 .query({ sort: 'createdAt' })
56 .query({ count: 10000 })
57 .set('Accept', 'application/json')
59 .expect('Content-Type', /json/)
63 function getVideo (url
, id
, end
) {
64 const path
= '/api/v1/videos/' + id
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
74 function getVideosList (url
, end
) {
75 const path
= '/api/v1/videos'
79 .query({ sort: 'name' })
80 .set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
86 function getVideosListPagination (url
, start
, count
, sort
, end
) {
92 const path
= '/api/v1/videos'
94 const req
= request(url
)
96 .query({ start: start
})
97 .query({ count: count
})
99 if (sort
) req
.query({ sort
})
101 req
.set('Accept', 'application/json')
103 .expect('Content-Type', /json/)
107 function getVideosListSort (url
, sort
, end
) {
108 const path
= '/api/v1/videos'
112 .query({ sort: sort
})
113 .set('Accept', 'application/json')
115 .expect('Content-Type', /json/)
119 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
125 const path
= '/api/v1/videos'
128 .delete(path
+ '/' + id
)
129 .set('Accept', 'application/json')
130 .set('Authorization', 'Bearer ' + token
)
131 .expect(expectedStatus
)
135 function searchVideo (url
, search
, field
, end
) {
141 const path
= '/api/v1/videos'
142 const req
= request(url
)
143 .get(path
+ '/search/' + search
)
144 .set('Accept', 'application/json')
146 if (field
) req
.query({ field: field
})
148 .expect('Content-Type', /json/)
152 function searchVideoWithPagination (url
, search
, field
, start
, count
, sort
, end
) {
158 const path
= '/api/v1/videos'
160 const req
= request(url
)
161 .get(path
+ '/search/' + search
)
162 .query({ start: start
})
163 .query({ count: count
})
164 .query({ field: field
})
166 if (sort
) req
.query({ sort
})
168 req
.set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
174 function searchVideoWithSort (url
, search
, sort
, end
) {
175 const path
= '/api/v1/videos'
178 .get(path
+ '/search/' + search
)
179 .query({ sort: sort
})
180 .set('Accept', 'application/json')
182 .expect('Content-Type', /json/)
186 function testVideoImage (url
, videoName
, imagePath
, callback
) {
187 // Don't test images if the node env is not set
188 // Because we need a special ffmpeg version for this test
189 if (process
.env
.NODE_TEST_IMAGE
) {
193 .end(function (err
, res
) {
194 if (err
) return callback(err
)
196 fs
.readFile(pathUtils
.join(__dirname
, '..', 'api', 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
197 if (err
) return callback(err
)
199 callback(null, data
.equals(res
.body
))
203 console
.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
208 function uploadVideo (url
, accessToken
, videoAttributesArg
, specialStatus
, end
) {
214 const path
= '/api/v1/videos'
216 // Default attributes
218 name: 'my super video',
222 description: 'my super description',
224 fixture: 'video_short.webm'
226 attributes
= Object
.assign(attributes
, videoAttributesArg
)
228 const req
= request(url
)
230 .set('Accept', 'application/json')
231 .set('Authorization', 'Bearer ' + accessToken
)
232 .field('name', attributes
.name
)
233 .field('category', attributes
.category
)
234 .field('licence', attributes
.licence
)
235 .field('nsfw', attributes
.nsfw
)
236 .field('description', attributes
.description
)
238 for (let i
= 0; i
< attributes
.tags
.length
; i
++) {
239 req
.field('tags[' + i
+ ']', attributes
.tags
[i
])
243 if (pathUtils
.isAbsolute(attributes
.fixture
)) {
244 filepath
= attributes
.fixture
246 filepath
= pathUtils
.join(__dirname
, '..', 'api', 'fixtures', attributes
.fixture
)
249 req
.attach('videofile', filepath
)
250 .expect(specialStatus
)
254 function updateVideo (url
, accessToken
, id
, attributes
, specialStatus
, end
) {
260 const path
= '/api/v1/videos/' + id
262 const req
= request(url
)
264 .set('Accept', 'application/json')
265 .set('Authorization', 'Bearer ' + accessToken
)
267 if (attributes
.name
) req
.field('name', attributes
.name
)
268 if (attributes
.category
) req
.field('category', attributes
.category
)
269 if (attributes
.licence
) req
.field('licence', attributes
.licence
)
270 if (attributes
.nsfw
) req
.field('nsfw', attributes
.nsfw
)
271 if (attributes
.description
) req
.field('description', attributes
.description
)
273 if (attributes
.tags
) {
274 for (let i
= 0; i
< attributes
.tags
.length
; i
++) {
275 req
.field('tags[' + i
+ ']', attributes
.tags
[i
])
279 req
.expect(specialStatus
).end(end
)
282 function rateVideo (url
, accessToken
, id
, rating
, specialStatus
, end
) {
288 const path
= '/api/v1/videos/' + id
+ '/rate'
292 .set('Accept', 'application/json')
293 .set('Authorization', 'Bearer ' + accessToken
)
295 .expect(specialStatus
)
299 // ---------------------------------------------------------------------------
301 module
.exports
= videosUtils