]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-imports.ts
Prevent video import on non unicast ips
[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'
5d08a6a7 5import {
3d470a53 6 buildAbsoluteFixturePath,
65e6e260
C
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
7c3b7976 10 cleanupTests,
254d3579 11 createSingleServer,
59bbcced 12 FIXTURE_URLS,
5d08a6a7
C
13 makeGetRequest,
14 makePostBodyRequest,
15 makeUploadRequest,
254d3579 16 PeerTubeServer,
41d1d075 17 setAccessTokensToServers
65e6e260 18} from '@shared/extra-utils'
4c7e60bc 19import { HttpStatusCode, VideoPrivacy } from '@shared/models'
5d08a6a7
C
20
21describe('Test video imports API validator', function () {
22 const path = '/api/v1/videos/imports'
254d3579 23 let server: PeerTubeServer
5d08a6a7 24 let userAccessToken = ''
5d08a6a7 25 let channelId: number
5d08a6a7
C
26
27 // ---------------------------------------------------------------
28
29 before(async function () {
30 this.timeout(30000)
31
254d3579 32 server = await createSingleServer(1)
5d08a6a7
C
33
34 await setAccessTokensToServers([ server ])
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
7c3b7976
C
353 after(async function () {
354 await cleanupTests([ server ])
5d08a6a7
C
355 })
356})