]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const fs = require('fs') | |
4 | const pathUtils = require('path') | |
5 | const request = require('supertest') | |
6 | ||
7 | const videosUtils = { | |
8 | getVideoCategories, | |
9 | getVideoLicences, | |
10 | getVideoLanguages, | |
11 | getAllVideosListBy, | |
12 | getVideo, | |
13 | getVideosList, | |
14 | getVideosListPagination, | |
15 | getVideosListSort, | |
16 | removeVideo, | |
17 | searchVideo, | |
18 | searchVideoWithPagination, | |
19 | searchVideoWithSort, | |
20 | testVideoImage, | |
21 | uploadVideo, | |
22 | updateVideo, | |
23 | rateVideo | |
24 | } | |
25 | ||
26 | // ---------------------- Export functions -------------------- | |
27 | ||
28 | function getVideoCategories (url, end) { | |
29 | const path = '/api/v1/videos/categories' | |
30 | ||
31 | request(url) | |
32 | .get(path) | |
33 | .set('Accept', 'application/json') | |
34 | .expect(200) | |
35 | .expect('Content-Type', /json/) | |
36 | .end(end) | |
37 | } | |
38 | ||
39 | function getVideoLicences (url, end) { | |
40 | const path = '/api/v1/videos/licences' | |
41 | ||
42 | request(url) | |
43 | .get(path) | |
44 | .set('Accept', 'application/json') | |
45 | .expect(200) | |
46 | .expect('Content-Type', /json/) | |
47 | .end(end) | |
48 | } | |
49 | ||
50 | function getVideoLanguages (url, end) { | |
51 | const path = '/api/v1/videos/languages' | |
52 | ||
53 | request(url) | |
54 | .get(path) | |
55 | .set('Accept', 'application/json') | |
56 | .expect(200) | |
57 | .expect('Content-Type', /json/) | |
58 | .end(end) | |
59 | } | |
60 | ||
61 | function getAllVideosListBy (url, end) { | |
62 | const path = '/api/v1/videos' | |
63 | ||
64 | request(url) | |
65 | .get(path) | |
66 | .query({ sort: 'createdAt' }) | |
67 | .query({ start: 0 }) | |
68 | .query({ count: 10000 }) | |
69 | .set('Accept', 'application/json') | |
70 | .expect(200) | |
71 | .expect('Content-Type', /json/) | |
72 | .end(end) | |
73 | } | |
74 | ||
75 | function getVideo (url, id, end) { | |
76 | const path = '/api/v1/videos/' + id | |
77 | ||
78 | request(url) | |
79 | .get(path) | |
80 | .set('Accept', 'application/json') | |
81 | .expect(200) | |
82 | .expect('Content-Type', /json/) | |
83 | .end(end) | |
84 | } | |
85 | ||
86 | function getVideosList (url, end) { | |
87 | const path = '/api/v1/videos' | |
88 | ||
89 | request(url) | |
90 | .get(path) | |
91 | .query({ sort: 'name' }) | |
92 | .set('Accept', 'application/json') | |
93 | .expect(200) | |
94 | .expect('Content-Type', /json/) | |
95 | .end(end) | |
96 | } | |
97 | ||
98 | function getVideosListPagination (url, start, count, sort, end) { | |
99 | if (!end) { | |
100 | end = sort | |
101 | sort = null | |
102 | } | |
103 | ||
104 | const path = '/api/v1/videos' | |
105 | ||
106 | const req = request(url) | |
107 | .get(path) | |
108 | .query({ start: start }) | |
109 | .query({ count: count }) | |
110 | ||
111 | if (sort) req.query({ sort }) | |
112 | ||
113 | req.set('Accept', 'application/json') | |
114 | .expect(200) | |
115 | .expect('Content-Type', /json/) | |
116 | .end(end) | |
117 | } | |
118 | ||
119 | function getVideosListSort (url, sort, end) { | |
120 | const path = '/api/v1/videos' | |
121 | ||
122 | request(url) | |
123 | .get(path) | |
124 | .query({ sort: sort }) | |
125 | .set('Accept', 'application/json') | |
126 | .expect(200) | |
127 | .expect('Content-Type', /json/) | |
128 | .end(end) | |
129 | } | |
130 | ||
131 | function removeVideo (url, token, id, expectedStatus, end) { | |
132 | if (!end) { | |
133 | end = expectedStatus | |
134 | expectedStatus = 204 | |
135 | } | |
136 | ||
137 | const path = '/api/v1/videos' | |
138 | ||
139 | request(url) | |
140 | .delete(path + '/' + id) | |
141 | .set('Accept', 'application/json') | |
142 | .set('Authorization', 'Bearer ' + token) | |
143 | .expect(expectedStatus) | |
144 | .end(end) | |
145 | } | |
146 | ||
147 | function searchVideo (url, search, field, end) { | |
148 | if (!end) { | |
149 | end = field | |
150 | field = null | |
151 | } | |
152 | ||
153 | const path = '/api/v1/videos' | |
154 | const req = request(url) | |
155 | .get(path + '/search/' + search) | |
156 | .set('Accept', 'application/json') | |
157 | ||
158 | if (field) req.query({ field: field }) | |
159 | req.expect(200) | |
160 | .expect('Content-Type', /json/) | |
161 | .end(end) | |
162 | } | |
163 | ||
164 | function searchVideoWithPagination (url, search, field, start, count, sort, end) { | |
165 | if (!end) { | |
166 | end = sort | |
167 | sort = null | |
168 | } | |
169 | ||
170 | const path = '/api/v1/videos' | |
171 | ||
172 | const req = request(url) | |
173 | .get(path + '/search/' + search) | |
174 | .query({ start: start }) | |
175 | .query({ count: count }) | |
176 | .query({ field: field }) | |
177 | ||
178 | if (sort) req.query({ sort }) | |
179 | ||
180 | req.set('Accept', 'application/json') | |
181 | .expect(200) | |
182 | .expect('Content-Type', /json/) | |
183 | .end(end) | |
184 | } | |
185 | ||
186 | function searchVideoWithSort (url, search, sort, end) { | |
187 | const path = '/api/v1/videos' | |
188 | ||
189 | request(url) | |
190 | .get(path + '/search/' + search) | |
191 | .query({ sort: sort }) | |
192 | .set('Accept', 'application/json') | |
193 | .expect(200) | |
194 | .expect('Content-Type', /json/) | |
195 | .end(end) | |
196 | } | |
197 | ||
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) { | |
202 | request(url) | |
203 | .get(imagePath) | |
204 | .expect(200) | |
205 | .end(function (err, res) { | |
206 | if (err) return callback(err) | |
207 | ||
208 | fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'), function (err, data) { | |
209 | if (err) return callback(err) | |
210 | ||
211 | callback(null, data.equals(res.body)) | |
212 | }) | |
213 | }) | |
214 | } else { | |
215 | console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') | |
216 | callback(null, true) | |
217 | } | |
218 | } | |
219 | ||
220 | function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) { | |
221 | if (!end) { | |
222 | end = specialStatus | |
223 | specialStatus = 204 | |
224 | } | |
225 | ||
226 | const path = '/api/v1/videos' | |
227 | ||
228 | // Default attributes | |
229 | let attributes = { | |
230 | name: 'my super video', | |
231 | category: 5, | |
232 | licence: 4, | |
233 | language: 3, | |
234 | nsfw: true, | |
235 | description: 'my super description', | |
236 | tags: [ 'tag' ], | |
237 | fixture: 'video_short.webm' | |
238 | } | |
239 | attributes = Object.assign(attributes, videoAttributesArg) | |
240 | ||
241 | const req = request(url) | |
242 | .post(path) | |
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) | |
251 | ||
252 | for (let i = 0; i < attributes.tags.length; i++) { | |
253 | req.field('tags[' + i + ']', attributes.tags[i]) | |
254 | } | |
255 | ||
256 | let filepath = '' | |
257 | if (pathUtils.isAbsolute(attributes.fixture)) { | |
258 | filepath = attributes.fixture | |
259 | } else { | |
260 | filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture) | |
261 | } | |
262 | ||
263 | req.attach('videofile', filepath) | |
264 | .expect(specialStatus) | |
265 | .end(end) | |
266 | } | |
267 | ||
268 | function updateVideo (url, accessToken, id, attributes, specialStatus, end) { | |
269 | if (!end) { | |
270 | end = specialStatus | |
271 | specialStatus = 204 | |
272 | } | |
273 | ||
274 | const path = '/api/v1/videos/' + id | |
275 | const body = {} | |
276 | ||
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 | |
284 | ||
285 | request(url) | |
286 | .put(path) | |
287 | .send(body) | |
288 | .set('Accept', 'application/json') | |
289 | .set('Authorization', 'Bearer ' + accessToken) | |
290 | .expect(specialStatus) | |
291 | .end(end) | |
292 | } | |
293 | ||
294 | function rateVideo (url, accessToken, id, rating, specialStatus, end) { | |
295 | if (!end) { | |
296 | end = specialStatus | |
297 | specialStatus = 204 | |
298 | } | |
299 | ||
300 | const path = '/api/v1/videos/' + id + '/rate' | |
301 | ||
302 | request(url) | |
303 | .put(path) | |
304 | .set('Accept', 'application/json') | |
305 | .set('Authorization', 'Bearer ' + accessToken) | |
306 | .send({ rating }) | |
307 | .expect(specialStatus) | |
308 | .end(end) | |
309 | } | |
310 | ||
311 | // --------------------------------------------------------------------------- | |
312 | ||
313 | module.exports = videosUtils |