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