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