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
, sort
, end
) {
67 const path
= '/api/v1/videos'
69 const req
= request(url
)
71 .query({ start: start
})
72 .query({ count: count
})
74 if (sort
) req
.query({ sort
})
76 req
.set('Accept', 'application/json')
78 .expect('Content-Type', /json/)
82 function getVideosListSort (url
, sort
, end
) {
83 const path
= '/api/v1/videos'
87 .query({ sort: sort
})
88 .set('Accept', 'application/json')
90 .expect('Content-Type', /json/)
94 function removeVideo (url
, token
, id
, expectedStatus
, end
) {
100 const path
= '/api/v1/videos'
103 .delete(path
+ '/' + id
)
104 .set('Accept', 'application/json')
105 .set('Authorization', 'Bearer ' + token
)
106 .expect(expectedStatus
)
110 function searchVideo (url
, search
, field
, end
) {
116 const path
= '/api/v1/videos'
117 const req
= request(url
)
118 .get(path
+ '/search/' + search
)
119 .set('Accept', 'application/json')
121 if (field
) req
.query({ field: field
})
123 .expect('Content-Type', /json/)
127 function searchVideoWithPagination (url
, search
, field
, start
, count
, sort
, end
) {
133 const path
= '/api/v1/videos'
135 const req
= request(url
)
136 .get(path
+ '/search/' + search
)
137 .query({ start: start
})
138 .query({ count: count
})
139 .query({ field: field
})
141 if (sort
) req
.query({ sort
})
143 req
.set('Accept', 'application/json')
145 .expect('Content-Type', /json/)
149 function searchVideoWithSort (url
, search
, sort
, end
) {
150 const path
= '/api/v1/videos'
153 .get(path
+ '/search/' + search
)
154 .query({ sort: sort
})
155 .set('Accept', 'application/json')
157 .expect('Content-Type', /json/)
161 function testVideoImage (url
, videoName
, imagePath
, callback
) {
162 // Don't test images if the node env is not set
163 // Because we need a special ffmpeg version for this test
164 if (process
.env
.NODE_TEST_IMAGE
) {
168 .end(function (err
, res
) {
169 if (err
) return callback(err
)
171 fs
.readFile(pathUtils
.join(__dirname
, '..', 'api', 'fixtures', videoName
+ '.jpg'), function (err
, data
) {
172 if (err
) return callback(err
)
174 callback(null, data
.equals(res
.body
))
178 console
.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
183 function uploadVideo (url
, accessToken
, name
, description
, tags
, fixture
, specialStatus
, end
) {
189 const path
= '/api/v1/videos'
191 const req
= request(url
)
193 .set('Accept', 'application/json')
194 .set('Authorization', 'Bearer ' + accessToken
)
196 .field('description', description
)
198 for (let i
= 0; i
< tags
.length
; i
++) {
199 req
.field('tags[' + i
+ ']', tags
[i
])
203 if (pathUtils
.isAbsolute(fixture
)) {
206 filepath
= pathUtils
.join(__dirname
, '..', 'api', 'fixtures', fixture
)
209 req
.attach('videofile', filepath
)
210 .expect(specialStatus
)
214 function updateVideo (url
, accessToken
, id
, name
, description
, tags
, specialStatus
, end
) {
220 const path
= '/api/v1/videos/' + id
222 const req
= request(url
)
224 .set('Accept', 'application/json')
225 .set('Authorization', 'Bearer ' + accessToken
)
227 if (name
) req
.field('name', name
)
228 if (description
) req
.field('description', description
)
231 for (let i
= 0; i
< tags
.length
; i
++) {
232 req
.field('tags[' + i
+ ']', tags
[i
])
236 req
.expect(specialStatus
).end(end
)
239 // ---------------------------------------------------------------------------
241 module
.exports
= videosUtils