]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-imports.ts
51260affab3de43f7be9d24af2f225a777968585
[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 { HttpStatusCode } from '@shared/core-utils'
6 import {
7 buildAbsoluteFixturePath,
8 checkBadCountPagination,
9 checkBadSortPagination,
10 checkBadStartPagination,
11 cleanupTests,
12 createUser,
13 flushAndRunServer,
14 getMyUserInformation,
15 ImportsCommand,
16 makeGetRequest,
17 makePostBodyRequest,
18 makeUploadRequest,
19 ServerInfo,
20 setAccessTokensToServers
21 } from '@shared/extra-utils'
22 import { VideoPrivacy } from '@shared/models'
23
24 describe('Test video imports API validator', function () {
25 const path = '/api/v1/videos/imports'
26 let server: ServerInfo
27 let userAccessToken = ''
28 let channelId: number
29
30 // ---------------------------------------------------------------
31
32 before(async function () {
33 this.timeout(30000)
34
35 server = await flushAndRunServer(1)
36
37 await setAccessTokensToServers([ server ])
38
39 const username = 'user1'
40 const password = 'my super password'
41 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
42 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
43
44 {
45 const res = await getMyUserInformation(server.url, server.accessToken)
46 channelId = res.body.videoChannels[0].id
47 }
48 })
49
50 describe('When listing my video imports', function () {
51 const myPath = '/api/v1/users/me/videos/imports'
52
53 it('Should fail with a bad start pagination', async function () {
54 await checkBadStartPagination(server.url, myPath, server.accessToken)
55 })
56
57 it('Should fail with a bad count pagination', async function () {
58 await checkBadCountPagination(server.url, myPath, server.accessToken)
59 })
60
61 it('Should fail with an incorrect sort', async function () {
62 await checkBadSortPagination(server.url, myPath, server.accessToken)
63 })
64
65 it('Should success with the correct parameters', async function () {
66 await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken })
67 })
68 })
69
70 describe('When adding a video import', function () {
71 let baseCorrectParams
72
73 before(function () {
74 baseCorrectParams = {
75 targetUrl: ImportsCommand.getGoodVideoUrl(),
76 name: 'my super name',
77 category: 5,
78 licence: 1,
79 language: 'pt',
80 nsfw: false,
81 commentsEnabled: true,
82 downloadEnabled: true,
83 waitTranscoding: true,
84 description: 'my super description',
85 support: 'my super support text',
86 tags: [ 'tag1', 'tag2' ],
87 privacy: VideoPrivacy.PUBLIC,
88 channelId
89 }
90 })
91
92 it('Should fail with nothing', async function () {
93 const fields = {}
94 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
95 })
96
97 it('Should fail without a target url', async function () {
98 const fields = omit(baseCorrectParams, 'targetUrl')
99 await makePostBodyRequest({
100 url: server.url,
101 path,
102 token: server.accessToken,
103 fields,
104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
105 })
106 })
107
108 it('Should fail with a bad target url', async function () {
109 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
110
111 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
112 })
113
114 it('Should fail with a long name', async function () {
115 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
116
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
118 })
119
120 it('Should fail with a bad category', async function () {
121 const fields = { ...baseCorrectParams, category: 125 }
122
123 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
124 })
125
126 it('Should fail with a bad licence', async function () {
127 const fields = { ...baseCorrectParams, licence: 125 }
128
129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
130 })
131
132 it('Should fail with a bad language', async function () {
133 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
134
135 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
136 })
137
138 it('Should fail with a long description', async function () {
139 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
140
141 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
142 })
143
144 it('Should fail with a long support text', async function () {
145 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
146
147 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
148 })
149
150 it('Should fail without a channel', async function () {
151 const fields = omit(baseCorrectParams, 'channelId')
152
153 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
154 })
155
156 it('Should fail with a bad channel', async function () {
157 const fields = { ...baseCorrectParams, channelId: 545454 }
158
159 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
160 })
161
162 it('Should fail with another user channel', async function () {
163 const user = {
164 username: 'fake',
165 password: 'fake_password'
166 }
167 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
168
169 const accessTokenUser = await server.loginCommand.getAccessToken(user)
170 const res = await getMyUserInformation(server.url, accessTokenUser)
171 const customChannelId = res.body.videoChannels[0].id
172
173 const fields = { ...baseCorrectParams, channelId: customChannelId }
174
175 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
176 })
177
178 it('Should fail with too many tags', async function () {
179 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
180
181 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
182 })
183
184 it('Should fail with a tag length too low', async function () {
185 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
186
187 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
188 })
189
190 it('Should fail with a tag length too big', async function () {
191 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
192
193 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
194 })
195
196 it('Should fail with an incorrect thumbnail file', async function () {
197 const fields = baseCorrectParams
198 const attaches = {
199 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
200 }
201
202 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
203 })
204
205 it('Should fail with a big thumbnail file', async function () {
206 const fields = baseCorrectParams
207 const attaches = {
208 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
209 }
210
211 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
212 })
213
214 it('Should fail with an incorrect preview file', async function () {
215 const fields = baseCorrectParams
216 const attaches = {
217 previewfile: buildAbsoluteFixturePath('video_short.mp4')
218 }
219
220 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
221 })
222
223 it('Should fail with a big preview file', async function () {
224 const fields = baseCorrectParams
225 const attaches = {
226 previewfile: buildAbsoluteFixturePath('preview-big.png')
227 }
228
229 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
230 })
231
232 it('Should fail with an invalid torrent file', async function () {
233 const fields = omit(baseCorrectParams, 'targetUrl')
234 const attaches = {
235 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
236 }
237
238 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
239 })
240
241 it('Should fail with an invalid magnet URI', async function () {
242 let fields = omit(baseCorrectParams, 'targetUrl')
243 fields = { ...fields, magnetUri: 'blabla' }
244
245 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
246 })
247
248 it('Should succeed with the correct parameters', async function () {
249 this.timeout(30000)
250
251 await makePostBodyRequest({
252 url: server.url,
253 path,
254 token: server.accessToken,
255 fields: baseCorrectParams,
256 statusCodeExpected: HttpStatusCode.OK_200
257 })
258 })
259
260 it('Should forbid to import http videos', async function () {
261 await server.configCommand.updateCustomSubConfig({
262 newConfig: {
263 import: {
264 videos: {
265 http: {
266 enabled: false
267 },
268 torrent: {
269 enabled: true
270 }
271 }
272 }
273 }
274 })
275
276 await makePostBodyRequest({
277 url: server.url,
278 path,
279 token: server.accessToken,
280 fields: baseCorrectParams,
281 statusCodeExpected: HttpStatusCode.CONFLICT_409
282 })
283 })
284
285 it('Should forbid to import torrent videos', async function () {
286 await server.configCommand.updateCustomSubConfig({
287 newConfig: {
288 import: {
289 videos: {
290 http: {
291 enabled: true
292 },
293 torrent: {
294 enabled: false
295 }
296 }
297 }
298 }
299 })
300
301 let fields = omit(baseCorrectParams, 'targetUrl')
302 fields = { ...fields, magnetUri: ImportsCommand.getMagnetURI() }
303
304 await makePostBodyRequest({
305 url: server.url,
306 path,
307 token: server.accessToken,
308 fields,
309 statusCodeExpected: HttpStatusCode.CONFLICT_409
310 })
311
312 fields = omit(fields, 'magnetUri')
313 const attaches = {
314 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
315 }
316
317 await makeUploadRequest({
318 url: server.url,
319 path,
320 token: server.accessToken,
321 fields,
322 attaches,
323 statusCodeExpected: HttpStatusCode.CONFLICT_409
324 })
325 })
326 })
327
328 after(async function () {
329 await cleanupTests([ server ])
330 })
331 })