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