]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-imports.ts
Fix print transcode command test
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-imports.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5d08a6a7 2
5d08a6a7 3import 'mocha'
b488ba1e 4import { omit } from 'lodash'
c55e3d72
C
5import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, FIXTURE_URLS } from '@server/tests/shared'
6import { buildAbsoluteFixturePath } from '@shared/core-utils'
7import { HttpStatusCode, VideoPrivacy } from '@shared/models'
5d08a6a7 8import {
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
20describe('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 = {}
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',
f33e5159
C
123 'http://192.168.1.42',
124 'http://127.0.0.1.cpy.re'
7b54a81c
C
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 })
590fb506 139
5d08a6a7 140 it('Should fail with a long name', async function () {
6c5065a0 141 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
5d08a6a7
C
142
143 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
144 })
145
146 it('Should fail with a bad category', async function () {
6c5065a0 147 const fields = { ...baseCorrectParams, category: 125 }
5d08a6a7
C
148
149 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
150 })
151
152 it('Should fail with a bad licence', async function () {
6c5065a0 153 const fields = { ...baseCorrectParams, licence: 125 }
5d08a6a7
C
154
155 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
156 })
157
158 it('Should fail with a bad language', async function () {
6c5065a0 159 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
5d08a6a7
C
160
161 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
162 })
163
164 it('Should fail with a long description', async function () {
6c5065a0 165 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
5d08a6a7
C
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 () {
6c5065a0 171 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
5d08a6a7
C
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 () {
6c5065a0 183 const fields = { ...baseCorrectParams, channelId: 545454 }
5d08a6a7
C
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 }
89d241a7 193 await server.users.create({ username: user.username, password: user.password })
5d08a6a7 194
89d241a7
C
195 const accessTokenUser = await server.login.getAccessToken(user)
196 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
7926c5f9 197 const customChannelId = videoChannels[0].id
5d08a6a7 198
6c5065a0 199 const fields = { ...baseCorrectParams, channelId: customChannelId }
5d08a6a7
C
200
201 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
202 })
203
204 it('Should fail with too many tags', async function () {
6c5065a0 205 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
5d08a6a7
C
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 () {
6c5065a0 211 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
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 big', async function () {
6c5065a0 217 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
5d08a6a7
C
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 = {
3d470a53 225 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
5d08a6a7
C
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 = {
3d470a53 234 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
5d08a6a7
C
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 = {
3d470a53 243 previewfile: buildAbsoluteFixturePath('video_short.mp4')
5d08a6a7
C
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 = {
3d470a53 252 previewfile: buildAbsoluteFixturePath('preview-big.png')
5d08a6a7
C
253 }
254
255 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
256 })
257
187501f8
C
258 it('Should fail with an invalid torrent file', async function () {
259 const fields = omit(baseCorrectParams, 'targetUrl')
260 const attaches = {
3d470a53 261 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
187501f8
C
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')
6c5065a0 269 fields = { ...fields, magnetUri: 'blabla' }
187501f8
C
270
271 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
272 })
273
5d08a6a7 274 it('Should succeed with the correct parameters', async function () {
8d57e728 275 this.timeout(120000)
5d08a6a7 276
b488ba1e
C
277 await makePostBodyRequest({
278 url: server.url,
279 path,
280 token: server.accessToken,
281 fields: baseCorrectParams,
c0e8b12e 282 expectedStatus: HttpStatusCode.OK_200
b488ba1e 283 })
5d08a6a7
C
284 })
285
187501f8 286 it('Should forbid to import http videos', async function () {
89d241a7 287 await server.config.updateCustomSubConfig({
65e6e260
C
288 newConfig: {
289 import: {
290 videos: {
291 http: {
292 enabled: false
293 },
294 torrent: {
295 enabled: true
296 }
590fb506
C
297 }
298 }
299 }
300 })
301
302 await makePostBodyRequest({
303 url: server.url,
304 path,
305 token: server.accessToken,
306 fields: baseCorrectParams,
c0e8b12e 307 expectedStatus: HttpStatusCode.CONFLICT_409
590fb506
C
308 })
309 })
187501f8
C
310
311 it('Should forbid to import torrent videos', async function () {
89d241a7 312 await server.config.updateCustomSubConfig({
65e6e260
C
313 newConfig: {
314 import: {
315 videos: {
316 http: {
317 enabled: true
318 },
319 torrent: {
320 enabled: false
321 }
187501f8
C
322 }
323 }
324 }
325 })
326
327 let fields = omit(baseCorrectParams, 'targetUrl')
59bbcced 328 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
187501f8 329
2d53be02
RK
330 await makePostBodyRequest({
331 url: server.url,
332 path,
333 token: server.accessToken,
334 fields,
c0e8b12e 335 expectedStatus: HttpStatusCode.CONFLICT_409
2d53be02 336 })
187501f8
C
337
338 fields = omit(fields, 'magnetUri')
339 const attaches = {
3d470a53 340 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
187501f8
C
341 }
342
2d53be02
RK
343 await makeUploadRequest({
344 url: server.url,
345 path,
346 token: server.accessToken,
347 fields,
348 attaches,
c0e8b12e 349 expectedStatus: HttpStatusCode.CONFLICT_409
2d53be02 350 })
187501f8 351 })
5d08a6a7
C
352 })
353
419b520c
C
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
7c3b7976
C
415 after(async function () {
416 await cleanupTests([ server ])
5d08a6a7
C
417 })
418})