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