]>
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' | |
89d241a7 C |
38 | await server.users.create({ username: username, password: password }) |
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 = {} | |
91 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
92 | }) | |
93 | ||
590fb506 C |
94 | it('Should fail without a target url', async function () { |
95 | const fields = omit(baseCorrectParams, 'targetUrl') | |
2d53be02 RK |
96 | await makePostBodyRequest({ |
97 | url: server.url, | |
98 | path, | |
99 | token: server.accessToken, | |
100 | fields, | |
c0e8b12e | 101 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
2d53be02 | 102 | }) |
590fb506 C |
103 | }) |
104 | ||
105 | it('Should fail with a bad target url', async function () { | |
6c5065a0 | 106 | const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' } |
590fb506 C |
107 | |
108 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
109 | }) | |
7b54a81c C |
110 | |
111 | it('Should fail with localhost', async function () { | |
112 | const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' } | |
113 | ||
114 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
115 | }) | |
116 | ||
117 | it('Should fail with a private IP target urls', async function () { | |
118 | const targetUrls = [ | |
119 | 'http://127.0.0.1:8000', | |
120 | 'http://127.0.0.1', | |
121 | 'http://127.0.0.1/hello', | |
122 | 'https://192.168.1.42', | |
123 | 'http://192.168.1.42' | |
124 | ] | |
125 | ||
126 | for (const targetUrl of targetUrls) { | |
127 | const fields = { ...baseCorrectParams, targetUrl } | |
128 | ||
129 | await makePostBodyRequest({ | |
130 | url: server.url, | |
131 | path, | |
132 | token: server.accessToken, | |
133 | fields, | |
134 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
135 | }) | |
136 | } | |
137 | }) | |
590fb506 | 138 | |
5d08a6a7 | 139 | it('Should fail with a long name', async function () { |
6c5065a0 | 140 | const fields = { ...baseCorrectParams, name: 'super'.repeat(65) } |
5d08a6a7 C |
141 | |
142 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
143 | }) | |
144 | ||
145 | it('Should fail with a bad category', async function () { | |
6c5065a0 | 146 | const fields = { ...baseCorrectParams, category: 125 } |
5d08a6a7 C |
147 | |
148 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
149 | }) | |
150 | ||
151 | it('Should fail with a bad licence', async function () { | |
6c5065a0 | 152 | const fields = { ...baseCorrectParams, licence: 125 } |
5d08a6a7 C |
153 | |
154 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
155 | }) | |
156 | ||
157 | it('Should fail with a bad language', async function () { | |
6c5065a0 | 158 | const fields = { ...baseCorrectParams, language: 'a'.repeat(15) } |
5d08a6a7 C |
159 | |
160 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
161 | }) | |
162 | ||
163 | it('Should fail with a long description', async function () { | |
6c5065a0 | 164 | const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) } |
5d08a6a7 C |
165 | |
166 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
167 | }) | |
168 | ||
169 | it('Should fail with a long support text', async function () { | |
6c5065a0 | 170 | const fields = { ...baseCorrectParams, support: 'super'.repeat(201) } |
5d08a6a7 C |
171 | |
172 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
173 | }) | |
174 | ||
175 | it('Should fail without a channel', async function () { | |
176 | const fields = omit(baseCorrectParams, 'channelId') | |
177 | ||
178 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
179 | }) | |
180 | ||
181 | it('Should fail with a bad channel', async function () { | |
6c5065a0 | 182 | const fields = { ...baseCorrectParams, channelId: 545454 } |
5d08a6a7 C |
183 | |
184 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
185 | }) | |
186 | ||
187 | it('Should fail with another user channel', async function () { | |
188 | const user = { | |
189 | username: 'fake', | |
190 | password: 'fake_password' | |
191 | } | |
89d241a7 | 192 | await server.users.create({ username: user.username, password: user.password }) |
5d08a6a7 | 193 | |
89d241a7 C |
194 | const accessTokenUser = await server.login.getAccessToken(user) |
195 | const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser }) | |
7926c5f9 | 196 | const customChannelId = videoChannels[0].id |
5d08a6a7 | 197 | |
6c5065a0 | 198 | const fields = { ...baseCorrectParams, channelId: customChannelId } |
5d08a6a7 C |
199 | |
200 | await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) | |
201 | }) | |
202 | ||
203 | it('Should fail with too many tags', async function () { | |
6c5065a0 | 204 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] } |
5d08a6a7 C |
205 | |
206 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
207 | }) | |
208 | ||
209 | it('Should fail with a tag length too low', async function () { | |
6c5065a0 | 210 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] } |
5d08a6a7 C |
211 | |
212 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
213 | }) | |
214 | ||
215 | it('Should fail with a tag length too big', async function () { | |
6c5065a0 | 216 | const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] } |
5d08a6a7 C |
217 | |
218 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
219 | }) | |
220 | ||
221 | it('Should fail with an incorrect thumbnail file', async function () { | |
222 | const fields = baseCorrectParams | |
223 | const attaches = { | |
3d470a53 | 224 | thumbnailfile: buildAbsoluteFixturePath('video_short.mp4') |
5d08a6a7 C |
225 | } |
226 | ||
227 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
228 | }) | |
229 | ||
230 | it('Should fail with a big thumbnail file', async function () { | |
231 | const fields = baseCorrectParams | |
232 | const attaches = { | |
3d470a53 | 233 | thumbnailfile: buildAbsoluteFixturePath('preview-big.png') |
5d08a6a7 C |
234 | } |
235 | ||
236 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
237 | }) | |
238 | ||
239 | it('Should fail with an incorrect preview file', async function () { | |
240 | const fields = baseCorrectParams | |
241 | const attaches = { | |
3d470a53 | 242 | previewfile: buildAbsoluteFixturePath('video_short.mp4') |
5d08a6a7 C |
243 | } |
244 | ||
245 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
246 | }) | |
247 | ||
248 | it('Should fail with a big preview file', async function () { | |
249 | const fields = baseCorrectParams | |
250 | const attaches = { | |
3d470a53 | 251 | previewfile: buildAbsoluteFixturePath('preview-big.png') |
5d08a6a7 C |
252 | } |
253 | ||
254 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
255 | }) | |
256 | ||
187501f8 C |
257 | it('Should fail with an invalid torrent file', async function () { |
258 | const fields = omit(baseCorrectParams, 'targetUrl') | |
259 | const attaches = { | |
3d470a53 | 260 | torrentfile: buildAbsoluteFixturePath('avatar-big.png') |
187501f8 C |
261 | } |
262 | ||
263 | await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) | |
264 | }) | |
265 | ||
266 | it('Should fail with an invalid magnet URI', async function () { | |
267 | let fields = omit(baseCorrectParams, 'targetUrl') | |
6c5065a0 | 268 | fields = { ...fields, magnetUri: 'blabla' } |
187501f8 C |
269 | |
270 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | |
271 | }) | |
272 | ||
5d08a6a7 | 273 | it('Should succeed with the correct parameters', async function () { |
13b6dc1f | 274 | this.timeout(30000) |
5d08a6a7 | 275 | |
b488ba1e C |
276 | await makePostBodyRequest({ |
277 | url: server.url, | |
278 | path, | |
279 | token: server.accessToken, | |
280 | fields: baseCorrectParams, | |
c0e8b12e | 281 | expectedStatus: HttpStatusCode.OK_200 |
b488ba1e | 282 | }) |
5d08a6a7 C |
283 | }) |
284 | ||
187501f8 | 285 | it('Should forbid to import http videos', async function () { |
89d241a7 | 286 | await server.config.updateCustomSubConfig({ |
65e6e260 C |
287 | newConfig: { |
288 | import: { | |
289 | videos: { | |
290 | http: { | |
291 | enabled: false | |
292 | }, | |
293 | torrent: { | |
294 | enabled: true | |
295 | } | |
590fb506 C |
296 | } |
297 | } | |
298 | } | |
299 | }) | |
300 | ||
301 | await makePostBodyRequest({ | |
302 | url: server.url, | |
303 | path, | |
304 | token: server.accessToken, | |
305 | fields: baseCorrectParams, | |
c0e8b12e | 306 | expectedStatus: HttpStatusCode.CONFLICT_409 |
590fb506 C |
307 | }) |
308 | }) | |
187501f8 C |
309 | |
310 | it('Should forbid to import torrent videos', async function () { | |
89d241a7 | 311 | await server.config.updateCustomSubConfig({ |
65e6e260 C |
312 | newConfig: { |
313 | import: { | |
314 | videos: { | |
315 | http: { | |
316 | enabled: true | |
317 | }, | |
318 | torrent: { | |
319 | enabled: false | |
320 | } | |
187501f8 C |
321 | } |
322 | } | |
323 | } | |
324 | }) | |
325 | ||
326 | let fields = omit(baseCorrectParams, 'targetUrl') | |
59bbcced | 327 | fields = { ...fields, magnetUri: FIXTURE_URLS.magnet } |
187501f8 | 328 | |
2d53be02 RK |
329 | await makePostBodyRequest({ |
330 | url: server.url, | |
331 | path, | |
332 | token: server.accessToken, | |
333 | fields, | |
c0e8b12e | 334 | expectedStatus: HttpStatusCode.CONFLICT_409 |
2d53be02 | 335 | }) |
187501f8 C |
336 | |
337 | fields = omit(fields, 'magnetUri') | |
338 | const attaches = { | |
3d470a53 | 339 | torrentfile: buildAbsoluteFixturePath('video-720p.torrent') |
187501f8 C |
340 | } |
341 | ||
2d53be02 RK |
342 | await makeUploadRequest({ |
343 | url: server.url, | |
344 | path, | |
345 | token: server.accessToken, | |
346 | fields, | |
347 | attaches, | |
c0e8b12e | 348 | expectedStatus: HttpStatusCode.CONFLICT_409 |
2d53be02 | 349 | }) |
187501f8 | 350 | }) |
5d08a6a7 C |
351 | }) |
352 | ||
419b520c C |
353 | describe('Deleting/cancelling a video import', function () { |
354 | let importId: number | |
355 | ||
356 | async function importVideo () { | |
357 | const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo } | |
358 | const res = await server.imports.importVideo({ attributes }) | |
359 | ||
360 | return res.id | |
361 | } | |
362 | ||
363 | before(async function () { | |
364 | importId = await importVideo() | |
365 | }) | |
366 | ||
367 | it('Should fail with an invalid import id', async function () { | |
368 | await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
369 | await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
370 | }) | |
371 | ||
372 | it('Should fail with an unknown import id', async function () { | |
373 | await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
374 | await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
375 | }) | |
376 | ||
377 | it('Should fail without token', async function () { | |
378 | await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
379 | await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) | |
380 | }) | |
381 | ||
382 | it('Should fail with another user token', async function () { | |
383 | await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
384 | await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) | |
385 | }) | |
386 | ||
387 | it('Should fail to cancel non pending import', async function () { | |
388 | this.timeout(60000) | |
389 | ||
390 | await waitJobs([ server ]) | |
391 | ||
392 | await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
393 | }) | |
394 | ||
395 | it('Should succeed to delete an import', async function () { | |
396 | await server.imports.delete({ importId }) | |
397 | }) | |
398 | ||
399 | it('Should fail to delete a pending import', async function () { | |
400 | await server.jobs.pauseJobQueue() | |
401 | ||
402 | importId = await importVideo() | |
403 | ||
404 | await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
405 | }) | |
406 | ||
407 | it('Should succeed to cancel an import', async function () { | |
408 | importId = await importVideo() | |
409 | ||
410 | await server.imports.cancel({ importId }) | |
411 | }) | |
412 | }) | |
413 | ||
7c3b7976 C |
414 | after(async function () { |
415 | await cleanupTests([ server ]) | |
5d08a6a7 C |
416 | }) |
417 | }) |