]>
Commit | Line | Data |
---|---|---|
fdbda9e3 | 1 | import { readFile } from 'fs' |
fdbda9e3 | 2 | import * as parseTorrent from 'parse-torrent' |
c5d31dba C |
3 | import { isAbsolute, join } from 'path' |
4 | import * as request from 'supertest' | |
5 | import { getMyUserInformation, makeGetRequest, readFilePromise, ServerInfo } from '../' | |
6 | import { VideoPrivacy } from '../../../../shared/models/videos' | |
0e1dc3e7 C |
7 | |
8 | type VideoAttributes = { | |
9 | name?: string | |
10 | category?: number | |
11 | licence?: number | |
12 | language?: number | |
13 | nsfw?: boolean | |
14 | description?: string | |
15 | tags?: string[] | |
5f04dd2f | 16 | channelId?: number |
11474c3c | 17 | privacy?: VideoPrivacy |
0e1dc3e7 C |
18 | fixture?: string |
19 | } | |
20 | ||
21 | function getVideoCategories (url: string) { | |
22 | const path = '/api/v1/videos/categories' | |
23 | ||
eec63bbc C |
24 | return makeGetRequest({ |
25 | url, | |
26 | path | |
27 | }) | |
0e1dc3e7 C |
28 | } |
29 | ||
30 | function getVideoLicences (url: string) { | |
31 | const path = '/api/v1/videos/licences' | |
32 | ||
eec63bbc C |
33 | return makeGetRequest({ |
34 | url, | |
35 | path | |
36 | }) | |
0e1dc3e7 C |
37 | } |
38 | ||
39 | function getVideoLanguages (url: string) { | |
40 | const path = '/api/v1/videos/languages' | |
41 | ||
eec63bbc C |
42 | return makeGetRequest({ |
43 | url, | |
44 | path | |
45 | }) | |
0e1dc3e7 C |
46 | } |
47 | ||
11474c3c C |
48 | function getVideoPrivacies (url: string) { |
49 | const path = '/api/v1/videos/privacies' | |
50 | ||
eec63bbc C |
51 | return makeGetRequest({ |
52 | url, | |
53 | path | |
54 | }) | |
11474c3c C |
55 | } |
56 | ||
11474c3c | 57 | function getVideo (url: string, id: number | string, expectedStatus = 200) { |
0e1dc3e7 C |
58 | const path = '/api/v1/videos/' + id |
59 | ||
60 | return request(url) | |
61 | .get(path) | |
62 | .set('Accept', 'application/json') | |
11474c3c C |
63 | .expect(expectedStatus) |
64 | } | |
65 | ||
1f3e9fec C |
66 | function viewVideo (url: string, id: number | string, expectedStatus = 204) { |
67 | const path = '/api/v1/videos/' + id + '/views' | |
68 | ||
69 | return request(url) | |
70 | .post(path) | |
71 | .set('Accept', 'application/json') | |
72 | .expect(expectedStatus) | |
73 | } | |
74 | ||
11474c3c C |
75 | function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) { |
76 | const path = '/api/v1/videos/' + id | |
77 | ||
78 | return request(url) | |
79 | .get(path) | |
80 | .set('Authorization', 'Bearer ' + token) | |
81 | .set('Accept', 'application/json') | |
82 | .expect(expectedStatus) | |
0e1dc3e7 C |
83 | } |
84 | ||
9567011b C |
85 | function getVideoDescription (url: string, descriptionPath: string) { |
86 | return request(url) | |
87 | .get(descriptionPath) | |
88 | .set('Accept', 'application/json') | |
89 | .expect(200) | |
90 | .expect('Content-Type', /json/) | |
91 | } | |
92 | ||
0e1dc3e7 C |
93 | function getVideosList (url: string) { |
94 | const path = '/api/v1/videos' | |
95 | ||
96 | return request(url) | |
97 | .get(path) | |
98 | .query({ sort: 'name' }) | |
99 | .set('Accept', 'application/json') | |
100 | .expect(200) | |
101 | .expect('Content-Type', /json/) | |
102 | } | |
103 | ||
11474c3c C |
104 | function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) { |
105 | const path = '/api/v1/users/me/videos' | |
106 | ||
107 | const req = request(url) | |
108 | .get(path) | |
109 | .query({ start: start }) | |
110 | .query({ count: count }) | |
111 | ||
112 | if (sort) req.query({ sort }) | |
113 | ||
114 | return req.set('Accept', 'application/json') | |
115 | .set('Authorization', 'Bearer ' + accessToken) | |
116 | .expect(200) | |
117 | .expect('Content-Type', /json/) | |
118 | } | |
119 | ||
0e1dc3e7 C |
120 | function getVideosListPagination (url: string, start: number, count: number, sort?: string) { |
121 | const path = '/api/v1/videos' | |
122 | ||
123 | const req = request(url) | |
124 | .get(path) | |
125 | .query({ start: start }) | |
126 | .query({ count: count }) | |
127 | ||
128 | if (sort) req.query({ sort }) | |
129 | ||
130 | return req.set('Accept', 'application/json') | |
131 | .expect(200) | |
132 | .expect('Content-Type', /json/) | |
133 | } | |
134 | ||
135 | function getVideosListSort (url: string, sort: string) { | |
136 | const path = '/api/v1/videos' | |
137 | ||
138 | return request(url) | |
139 | .get(path) | |
140 | .query({ sort: sort }) | |
141 | .set('Accept', 'application/json') | |
142 | .expect(200) | |
143 | .expect('Content-Type', /json/) | |
144 | } | |
145 | ||
146 | function removeVideo (url: string, token: string, id: number, expectedStatus = 204) { | |
147 | const path = '/api/v1/videos' | |
148 | ||
149 | return request(url) | |
150 | .delete(path + '/' + id) | |
151 | .set('Accept', 'application/json') | |
152 | .set('Authorization', 'Bearer ' + token) | |
153 | .expect(expectedStatus) | |
154 | } | |
155 | ||
f3aaa9a9 | 156 | function searchVideo (url: string, search: string) { |
0e1dc3e7 C |
157 | const path = '/api/v1/videos' |
158 | const req = request(url) | |
f3aaa9a9 C |
159 | .get(path + '/search') |
160 | .query({ search }) | |
161 | .set('Accept', 'application/json') | |
0e1dc3e7 C |
162 | |
163 | return req.expect(200) | |
f3aaa9a9 | 164 | .expect('Content-Type', /json/) |
0e1dc3e7 C |
165 | } |
166 | ||
f3aaa9a9 | 167 | function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) { |
0e1dc3e7 C |
168 | const path = '/api/v1/videos' |
169 | ||
170 | const req = request(url) | |
f3aaa9a9 | 171 | .get(path + '/search') |
0e1dc3e7 | 172 | .query({ start }) |
f3aaa9a9 | 173 | .query({ search }) |
0e1dc3e7 | 174 | .query({ count }) |
0e1dc3e7 C |
175 | |
176 | if (sort) req.query({ sort }) | |
177 | ||
178 | return req.set('Accept', 'application/json') | |
179 | .expect(200) | |
180 | .expect('Content-Type', /json/) | |
181 | } | |
182 | ||
183 | function searchVideoWithSort (url: string, search: string, sort: string) { | |
184 | const path = '/api/v1/videos' | |
185 | ||
186 | return request(url) | |
f3aaa9a9 C |
187 | .get(path + '/search') |
188 | .query({ search }) | |
0e1dc3e7 C |
189 | .query({ sort }) |
190 | .set('Accept', 'application/json') | |
191 | .expect(200) | |
192 | .expect('Content-Type', /json/) | |
193 | } | |
194 | ||
195 | async function testVideoImage (url: string, imageName: string, imagePath: string) { | |
196 | // Don't test images if the node env is not set | |
197 | // Because we need a special ffmpeg version for this test | |
198 | if (process.env['NODE_TEST_IMAGE']) { | |
199 | const res = await request(url) | |
200 | .get(imagePath) | |
201 | .expect(200) | |
202 | ||
203 | const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg')) | |
204 | ||
205 | return data.equals(res.body) | |
206 | } else { | |
207 | console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') | |
208 | return true | |
209 | } | |
210 | } | |
211 | ||
e11f68a3 | 212 | async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) { |
e95561cd | 213 | const path = '/api/v1/videos/upload' |
5f04dd2f C |
214 | let defaultChannelId = '1' |
215 | ||
216 | try { | |
217 | const res = await getMyUserInformation(url, accessToken) | |
218 | defaultChannelId = res.body.videoChannels[0].id | |
219 | } catch (e) { /* empty */ } | |
0e1dc3e7 C |
220 | |
221 | // Default attributes | |
222 | let attributes = { | |
223 | name: 'my super video', | |
224 | category: 5, | |
225 | licence: 4, | |
226 | language: 3, | |
5f04dd2f | 227 | channelId: defaultChannelId, |
0e1dc3e7 C |
228 | nsfw: true, |
229 | description: 'my super description', | |
230 | tags: [ 'tag' ], | |
11474c3c | 231 | privacy: VideoPrivacy.PUBLIC, |
0e1dc3e7 C |
232 | fixture: 'video_short.webm' |
233 | } | |
234 | attributes = Object.assign(attributes, videoAttributesArg) | |
235 | ||
236 | const req = request(url) | |
237 | .post(path) | |
238 | .set('Accept', 'application/json') | |
239 | .set('Authorization', 'Bearer ' + accessToken) | |
240 | .field('name', attributes.name) | |
241 | .field('category', attributes.category.toString()) | |
242 | .field('licence', attributes.licence.toString()) | |
0e1dc3e7 C |
243 | .field('nsfw', JSON.stringify(attributes.nsfw)) |
244 | .field('description', attributes.description) | |
11474c3c | 245 | .field('privacy', attributes.privacy.toString()) |
5f04dd2f | 246 | .field('channelId', attributes.channelId) |
0e1dc3e7 | 247 | |
8df87ce7 C |
248 | if (attributes.language !== undefined) { |
249 | req.field('language', attributes.language.toString()) | |
250 | } | |
251 | ||
0e1dc3e7 C |
252 | for (let i = 0; i < attributes.tags.length; i++) { |
253 | req.field('tags[' + i + ']', attributes.tags[i]) | |
254 | } | |
255 | ||
14d3270f | 256 | let filePath = '' |
0e1dc3e7 | 257 | if (isAbsolute(attributes.fixture)) { |
14d3270f | 258 | filePath = attributes.fixture |
0e1dc3e7 | 259 | } else { |
14d3270f | 260 | filePath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture) |
0e1dc3e7 C |
261 | } |
262 | ||
14d3270f | 263 | return req.attach('videofile', filePath) |
0e1dc3e7 C |
264 | .expect(specialStatus) |
265 | } | |
266 | ||
267 | function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) { | |
268 | const path = '/api/v1/videos/' + id | |
269 | const body = {} | |
270 | ||
271 | if (attributes.name) body['name'] = attributes.name | |
272 | if (attributes.category) body['category'] = attributes.category | |
273 | if (attributes.licence) body['licence'] = attributes.licence | |
274 | if (attributes.language) body['language'] = attributes.language | |
275 | if (attributes.nsfw) body['nsfw'] = attributes.nsfw | |
276 | if (attributes.description) body['description'] = attributes.description | |
277 | if (attributes.tags) body['tags'] = attributes.tags | |
11474c3c | 278 | if (attributes.privacy) body['privacy'] = attributes.privacy |
0e1dc3e7 C |
279 | |
280 | return request(url) | |
281 | .put(path) | |
282 | .send(body) | |
283 | .set('Accept', 'application/json') | |
284 | .set('Authorization', 'Bearer ' + accessToken) | |
285 | .expect(specialStatus) | |
286 | } | |
287 | ||
288 | function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { | |
289 | const path = '/api/v1/videos/' + id + '/rate' | |
290 | ||
291 | return request(url) | |
292 | .put(path) | |
293 | .set('Accept', 'application/json') | |
294 | .set('Authorization', 'Bearer ' + accessToken) | |
295 | .send({ rating }) | |
296 | .expect(specialStatus) | |
297 | } | |
298 | ||
14d3270f | 299 | function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) { |
fdbda9e3 | 300 | return new Promise<any>((res, rej) => { |
14d3270f | 301 | const torrentName = videoUUID + '-' + resolution + '.torrent' |
40298b02 | 302 | const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName) |
fdbda9e3 C |
303 | readFile(torrentPath, (err, data) => { |
304 | if (err) return rej(err) | |
305 | ||
306 | return res(parseTorrent(data)) | |
307 | }) | |
308 | }) | |
309 | } | |
310 | ||
0e1dc3e7 C |
311 | // --------------------------------------------------------------------------- |
312 | ||
313 | export { | |
9567011b | 314 | getVideoDescription, |
0e1dc3e7 C |
315 | getVideoCategories, |
316 | getVideoLicences, | |
11474c3c | 317 | getVideoPrivacies, |
0e1dc3e7 | 318 | getVideoLanguages, |
11474c3c | 319 | getMyVideos, |
0e1dc3e7 | 320 | getVideo, |
11474c3c | 321 | getVideoWithToken, |
0e1dc3e7 C |
322 | getVideosList, |
323 | getVideosListPagination, | |
324 | getVideosListSort, | |
325 | removeVideo, | |
326 | searchVideo, | |
327 | searchVideoWithPagination, | |
328 | searchVideoWithSort, | |
329 | testVideoImage, | |
330 | uploadVideo, | |
331 | updateVideo, | |
fdbda9e3 | 332 | rateVideo, |
1f3e9fec | 333 | viewVideo, |
fdbda9e3 | 334 | parseTorrentVideo |
0e1dc3e7 | 335 | } |