]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
5d08a6a7 | 2 | |
5d08a6a7 | 3 | import 'mocha' |
b488ba1e | 4 | import { omit } from 'lodash' |
c55e3d72 C |
5 | import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, FIXTURE_URLS } from '@server/tests/shared' |
6 | import { buildAbsoluteFixturePath } from '@shared/core-utils' | |
7 | import { HttpStatusCode, VideoPrivacy } from '@shared/models' | |
5d08a6a7 | 8 | import { |
7c3b7976 | 9 | cleanupTests, |
254d3579 | 10 | createSingleServer, |
5d08a6a7 C |
11 | makeGetRequest, |
12 | makePostBodyRequest, | |
13 | makeUploadRequest, | |
254d3579 | 14 | PeerTubeServer, |
419b520c C |
15 | setAccessTokensToServers, |
16 | setDefaultVideoChannel, | |
17 | waitJobs | |
bf54587a | 18 | } from '@shared/server-commands' |
5d08a6a7 C |
19 | |
20 | describe('Test video imports API validator', function () { | |
21 | const path = '/api/v1/videos/imports' | |
254d3579 | 22 | let server: PeerTubeServer |
5d08a6a7 | 23 | let userAccessToken = '' |
5d08a6a7 | 24 | let channelId: number |
5d08a6a7 C |
25 | |
26 | // --------------------------------------------------------------- | |
27 | ||
28 | before(async function () { | |
29 | this.timeout(30000) | |
30 | ||
254d3579 | 31 | server = await createSingleServer(1) |
5d08a6a7 C |
32 | |
33 | await setAccessTokensToServers([ server ]) | |
419b520c | 34 | await setDefaultVideoChannel([ server ]) |
5d08a6a7 C |
35 | |
36 | const username = 'user1' | |
37 | const password = 'my super password' | |
ba2684ce | 38 | await server.users.create({ username, password }) |
89d241a7 | 39 | userAccessToken = await server.login.getAccessToken({ username, password }) |
5d08a6a7 C |
40 | |
41 | { | |
89d241a7 | 42 | const { videoChannels } = await server.users.getMyInfo() |
7926c5f9 | 43 | channelId = videoChannels[0].id |
5d08a6a7 C |
44 | } |
45 | }) | |
46 | ||
47 | describe('When listing my video imports', function () { | |
48 | const myPath = '/api/v1/users/me/videos/imports' | |
49 | ||
50 | it('Should fail with a bad start pagination', async function () { | |
51 | await checkBadStartPagination(server.url, myPath, server.accessToken) | |
52 | }) | |
53 | ||
54 | it('Should fail with a bad count pagination', async function () { | |
55 | await checkBadCountPagination(server.url, myPath, server.accessToken) | |
56 | }) | |
57 | ||
58 | it('Should fail with an incorrect sort', async function () { | |
59 | await checkBadSortPagination(server.url, myPath, server.accessToken) | |
60 | }) | |
61 | ||
62 | it('Should success with the correct parameters', async function () { | |
c0e8b12e | 63 | await makeGetRequest({ url: server.url, path: myPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken }) |
5d08a6a7 C |
64 | }) |
65 | }) | |
66 | ||
67 | describe('When adding a video import', function () { | |
68 | let baseCorrectParams | |
69 | ||
70 | before(function () { | |
71 | baseCorrectParams = { | |
59bbcced | 72 | targetUrl: FIXTURE_URLS.goodVideo, |
5d08a6a7 C |
73 | name: 'my super name', |
74 | category: 5, | |
75 | licence: 1, | |
76 | language: 'pt', | |
77 | nsfw: false, | |
78 | commentsEnabled: true, | |
7f2cfe3a | 79 | downloadEnabled: true, |
5d08a6a7 C |
80 | waitTranscoding: true, |
81 | description: 'my super description', | |
82 | support: 'my super support text', | |
83 | tags: [ 'tag1', 'tag2' ], | |
84 | privacy: VideoPrivacy.PUBLIC, | |
c8861d5d | 85 | channelId |
5d08a6a7 C |
86 | } |
87 | }) | |
88 | ||
89 | it('Should fail with nothing', async function () { | |
90 | const fields = {} | |
2a491182 F |
91 | await makePostBodyRequest({ |
92 | url: server.url, | |
93 | path, | |
94 | token: server.accessToken, | |
95 | fields, | |
96 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
97 | }) | |
5d08a6a7 C |
98 | }) |
99 | ||
590fb506 C |
100 | it('Should fail without a target url', async function () { |
101 | const fields = omit(baseCorrectParams, 'targetUrl') | |
2d53be02 RK |
102 | await makePostBodyRequest({ |
103 | url: server.url, | |
104 | path, | |
105 | token: server.accessToken, | |
106 | fields, | |
c0e8b12e | 107 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
2d53be02 | 108 | }) |
590fb506 C |
109 | }) |
110 | ||
111 | it('Should fail with a bad target url', async function () { | |
6c5065a0 | 112 | const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' } |
590fb506 C |
113 | |
114 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
115 | }) | |
7b54a81c C |
116 | |
117 | it('Should fail with localhost', async function () { | |
118 | const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' } | |
119 | ||
120 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
121 | }) | |
122 | ||
123 | it('Should fail with a private IP target urls', async function () { | |
124 | const targetUrls = [ | |
125 | 'http://127.0.0.1:8000', | |
126 | 'http://127.0.0.1', | |
127 | 'http://127.0.0.1/hello', | |
128 | 'https://192.168.1.42', | |
f33e5159 C |
129 | 'http://192.168.1.42', |
130 | 'http://127.0.0.1.cpy.re' | |
7b54a81c C |
131 | ] |
132 | ||
133 | for (const targetUrl of targetUrls) { | |
134 | const fields = { ...baseCorrectParams, targetUrl } | |
135 | ||
136 | await makePostBodyRequest({ | |
137 | url: server.url, | |
138 | path, | |
139 | token: server.accessToken, | |
140 | fields, | |
141 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
142 | }) | |
143 | } | |
144 | }) | |
590fb506 | 145 | |
5d08a6a7 | 146 | it('Should fail with a long name', async function () { |
6c5065a0 | 147 | const fields = { ...baseCorrectParams, name: 'super'.repeat(65) } |
5d08a6a7 C |
148 | |
149 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
150 | }) | |
151 | ||
152 | it('Should fail with a bad category', async function () { | |
6c5065a0 | 153 | const fields = { ...baseCorrectParams, category: 125 } |
5d08a6a7 C |
154 | |
155 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
156 | }) | |
157 | ||
158 | it('Should fail with a bad licence', async function () { | |
6c5065a0 | 159 | const fields = { ...baseCorrectParams, licence: 125 } |
5d08a6a7 C |
160 | |
161 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
162 | }) | |
163 | ||
164 | it('Should fail with a bad language', async function () { | |
6c5065a0 | 165 | const fields = { ...baseCorrectParams, language: 'a'.repeat(15) } |
5d08a6a7 C |
166 | |
167 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
168 | }) | |
169 | ||
170 | it('Should fail with a long description', async function () { | |
6c5065a0 | 171 | const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) } |
5d08a6a7 C |
172 | |
173 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
174 | }) | |
175 | ||
176 | it('Should fail with a long support text', async function () { | |
6c5065a0 | 177 | const fields = { ...baseCorrectParams, support: 'super'.repeat(201) } |
5d08a6a7 C |
178 | |
179 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
180 | }) | |
181 | ||
182 | it('Should fail without a channel', async function () { | |
183 | const fields = omit(baseCorrectParams, 'channelId') | |
184 | ||
185 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
186 | }) | |
187 | ||
188 | it('Should fail with a bad channel', async function () { | |
6c5065a0 | 189 | const fields = { ...baseCorrectParams, channelId: 545454 } |
5d08a6a7 C |
190 | |
191 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
192 | }) | |
193 | ||
194 | it('Should fail with another user channel', async function () { | |
195 | const user = { | |
196 | username: 'fake', | |
197 | password: 'fake_password' | |
198 | } | |
89d241a7 | 199 | await server.users.create({ username: user.username, password: user.password }) |
5d08a6a7 | 200 | |
89d241a7 C |
201 | const accessTokenUser = await server.login.getAccessToken(user) |
202 | const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser }) | |
7926c5f9 | 203 | const customChannelId = videoChannels[0].id |
5d08a6a7 | 204 | |
6c5065a0 | 205 | const fields = { ...baseCorrectParams, channelId: customChannelId } |
5d08a6a7 C |
206 | |
207 | await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) | |
208 | }) | |
209 | ||
210 | it('Should fail with too many tags', async function () { | |
6c5065a0 | 211 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] } |
5d08a6a7 C |
212 | |
213 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
214 | }) | |
215 | ||
216 | it('Should fail with a tag length too low', async function () { | |
6c5065a0 | 217 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] } |
5d08a6a7 C |
218 | |
219 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
220 | }) | |
221 | ||
222 | it('Should fail with a tag length too big', async function () { | |
6c5065a0 | 223 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] } |
5d08a6a7 C |
224 | |
225 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
226 | }) | |
227 | ||
228 | it('Should fail with an incorrect thumbnail file', async function () { | |
229 | const fields = baseCorrectParams | |
230 | const attaches = { | |
3d470a53 | 231 | thumbnailfile: buildAbsoluteFixturePath('video_short.mp4') |
5d08a6a7 C |
232 | } |
233 | ||
234 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
235 | }) | |
236 | ||
237 | it('Should fail with a big thumbnail file', async function () { | |
238 | const fields = baseCorrectParams | |
239 | const attaches = { | |
3d470a53 | 240 | thumbnailfile: buildAbsoluteFixturePath('preview-big.png') |
5d08a6a7 C |
241 | } |
242 | ||
243 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
244 | }) | |
245 | ||
246 | it('Should fail with an incorrect preview file', async function () { | |
247 | const fields = baseCorrectParams | |
248 | const attaches = { | |
3d470a53 | 249 | previewfile: buildAbsoluteFixturePath('video_short.mp4') |
5d08a6a7 C |
250 | } |
251 | ||
252 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
253 | }) | |
254 | ||
255 | it('Should fail with a big preview file', async function () { | |
256 | const fields = baseCorrectParams | |
257 | const attaches = { | |
3d470a53 | 258 | previewfile: buildAbsoluteFixturePath('preview-big.png') |
5d08a6a7 C |
259 | } |
260 | ||
261 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
262 | }) | |
263 | ||
187501f8 C |
264 | it('Should fail with an invalid torrent file', async function () { |
265 | const fields = omit(baseCorrectParams, 'targetUrl') | |
266 | const attaches = { | |
3d470a53 | 267 | torrentfile: buildAbsoluteFixturePath('avatar-big.png') |
187501f8 C |
268 | } |
269 | ||
270 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
271 | }) | |
272 | ||
273 | it('Should fail with an invalid magnet URI', async function () { | |
274 | let fields = omit(baseCorrectParams, 'targetUrl') | |
6c5065a0 | 275 | fields = { ...fields, magnetUri: 'blabla' } |
187501f8 C |
276 | |
277 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
278 | }) | |
279 | ||
5d08a6a7 | 280 | it('Should succeed with the correct parameters', async function () { |
8d57e728 | 281 | this.timeout(120000) |
5d08a6a7 | 282 | |
b488ba1e C |
283 | await makePostBodyRequest({ |
284 | url: server.url, | |
285 | path, | |
286 | token: server.accessToken, | |
287 | fields: baseCorrectParams, | |
c0e8b12e | 288 | expectedStatus: HttpStatusCode.OK_200 |
b488ba1e | 289 | }) |
5d08a6a7 C |
290 | }) |
291 | ||
187501f8 | 292 | it('Should forbid to import http videos', async function () { |
89d241a7 | 293 | await server.config.updateCustomSubConfig({ |
65e6e260 C |
294 | newConfig: { |
295 | import: { | |
296 | videos: { | |
297 | http: { | |
298 | enabled: false | |
299 | }, | |
300 | torrent: { | |
301 | enabled: true | |
302 | } | |
590fb506 C |
303 | } |
304 | } | |
305 | } | |
306 | }) | |
307 | ||
308 | await makePostBodyRequest({ | |
309 | url: server.url, | |
310 | path, | |
311 | token: server.accessToken, | |
312 | fields: baseCorrectParams, | |
c0e8b12e | 313 | expectedStatus: HttpStatusCode.CONFLICT_409 |
590fb506 C |
314 | }) |
315 | }) | |
187501f8 C |
316 | |
317 | it('Should forbid to import torrent videos', async function () { | |
89d241a7 | 318 | await server.config.updateCustomSubConfig({ |
65e6e260 C |
319 | newConfig: { |
320 | import: { | |
321 | videos: { | |
322 | http: { | |
323 | enabled: true | |
324 | }, | |
325 | torrent: { | |
326 | enabled: false | |
327 | } | |
187501f8 C |
328 | } |
329 | } | |
330 | } | |
331 | }) | |
332 | ||
333 | let fields = omit(baseCorrectParams, 'targetUrl') | |
59bbcced | 334 | fields = { ...fields, magnetUri: FIXTURE_URLS.magnet } |
187501f8 | 335 | |
2d53be02 RK |
336 | await makePostBodyRequest({ |
337 | url: server.url, | |
338 | path, | |
339 | token: server.accessToken, | |
340 | fields, | |
c0e8b12e | 341 | expectedStatus: HttpStatusCode.CONFLICT_409 |
2d53be02 | 342 | }) |
187501f8 C |
343 | |
344 | fields = omit(fields, 'magnetUri') | |
345 | const attaches = { | |
3d470a53 | 346 | torrentfile: buildAbsoluteFixturePath('video-720p.torrent') |
187501f8 C |
347 | } |
348 | ||
2d53be02 RK |
349 | await makeUploadRequest({ |
350 | url: server.url, | |
351 | path, | |
352 | token: server.accessToken, | |
353 | fields, | |
354 | attaches, | |
c0e8b12e | 355 | expectedStatus: HttpStatusCode.CONFLICT_409 |
2d53be02 | 356 | }) |
187501f8 | 357 | }) |
5d08a6a7 C |
358 | }) |
359 | ||
419b520c C |
360 | describe('Deleting/cancelling a video import', function () { |
361 | let importId: number | |
362 | ||
363 | async function importVideo () { | |
364 | const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo } | |
365 | const res = await server.imports.importVideo({ attributes }) | |
366 | ||
367 | return res.id | |
368 | } | |
369 | ||
370 | before(async function () { | |
371 | importId = await importVideo() | |
372 | }) | |
373 | ||
374 | it('Should fail with an invalid import id', async function () { | |
375 | await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
376 | await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
377 | }) | |
378 | ||
379 | it('Should fail with an unknown import id', async function () { | |
380 | await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
381 | await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
382 | }) | |
383 | ||
384 | it('Should fail without token', async function () { | |
385 | await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
386 | await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
387 | }) | |
388 | ||
389 | it('Should fail with another user token', async function () { | |
390 | await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
391 | await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
392 | }) | |
393 | ||
394 | it('Should fail to cancel non pending import', async function () { | |
395 | this.timeout(60000) | |
396 | ||
397 | await waitJobs([ server ]) | |
398 | ||
399 | await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
400 | }) | |
401 | ||
402 | it('Should succeed to delete an import', async function () { | |
403 | await server.imports.delete({ importId }) | |
404 | }) | |
405 | ||
406 | it('Should fail to delete a pending import', async function () { | |
407 | await server.jobs.pauseJobQueue() | |
408 | ||
409 | importId = await importVideo() | |
410 | ||
411 | await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
412 | }) | |
413 | ||
414 | it('Should succeed to cancel an import', async function () { | |
415 | importId = await importVideo() | |
416 | ||
417 | await server.imports.cancel({ importId }) | |
418 | }) | |
419 | }) | |
420 | ||
7c3b7976 C |
421 | after(async function () { |
422 | await cleanupTests([ server ]) | |
5d08a6a7 C |
423 | }) |
424 | }) |