dbea39c48cb3340142e061b2e9ccf7c1e66dc073
[oweals/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 { omit } from 'lodash'
4 import 'mocha'
5 import { join } from 'path'
6 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
7 import {
8   cleanupTests,
9   createUser,
10   flushAndRunServer,
11   getMyUserInformation,
12   immutableAssign,
13   makeGetRequest,
14   makePostBodyRequest,
15   makeUploadRequest,
16   ServerInfo,
17   setAccessTokensToServers,
18   updateCustomSubConfig,
19   userLogin
20 } from '../../../../shared/extra-utils'
21 import {
22   checkBadCountPagination,
23   checkBadSortPagination,
24   checkBadStartPagination
25 } from '../../../../shared/extra-utils/requests/check-api-params'
26 import { getMagnetURI, getYoutubeVideoUrl } from '../../../../shared/extra-utils/videos/video-imports'
27
28 describe('Test video imports API validator', function () {
29   const path = '/api/v1/videos/imports'
30   let server: ServerInfo
31   let userAccessToken = ''
32   // eslint-disable-next-line @typescript-eslint/no-unused-vars
33   let accountName: string
34   let channelId: number
35
36   // ---------------------------------------------------------------
37
38   before(async function () {
39     this.timeout(30000)
40
41     server = await flushAndRunServer(1)
42
43     await setAccessTokensToServers([ server ])
44
45     const username = 'user1'
46     const password = 'my super password'
47     await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
48     userAccessToken = await userLogin(server, { username, password })
49
50     {
51       const res = await getMyUserInformation(server.url, server.accessToken)
52       channelId = res.body.videoChannels[0].id
53       accountName = res.body.account.name + '@' + res.body.account.host
54     }
55   })
56
57   describe('When listing my video imports', function () {
58     const myPath = '/api/v1/users/me/videos/imports'
59
60     it('Should fail with a bad start pagination', async function () {
61       await checkBadStartPagination(server.url, myPath, server.accessToken)
62     })
63
64     it('Should fail with a bad count pagination', async function () {
65       await checkBadCountPagination(server.url, myPath, server.accessToken)
66     })
67
68     it('Should fail with an incorrect sort', async function () {
69       await checkBadSortPagination(server.url, myPath, server.accessToken)
70     })
71
72     it('Should success with the correct parameters', async function () {
73       await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: 200, token: server.accessToken })
74     })
75   })
76
77   describe('When adding a video import', function () {
78     let baseCorrectParams
79
80     before(function () {
81       baseCorrectParams = {
82         targetUrl: getYoutubeVideoUrl(),
83         name: 'my super name',
84         category: 5,
85         licence: 1,
86         language: 'pt',
87         nsfw: false,
88         commentsEnabled: true,
89         downloadEnabled: true,
90         waitTranscoding: true,
91         description: 'my super description',
92         support: 'my super support text',
93         tags: [ 'tag1', 'tag2' ],
94         privacy: VideoPrivacy.PUBLIC,
95         channelId
96       }
97     })
98
99     it('Should fail with nothing', async function () {
100       const fields = {}
101       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
102     })
103
104     it('Should fail without a target url', async function () {
105       const fields = omit(baseCorrectParams, 'targetUrl')
106       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 400 })
107     })
108
109     it('Should fail with a bad target url', async function () {
110       const fields = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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: join(__dirname, '..', '..', 'fixtures', 'avatar-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: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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: join(__dirname, '..', '..', 'fixtures', 'avatar-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: join(__dirname, '..', '..', 'fixtures', '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 = immutableAssign(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       {
253         await makePostBodyRequest({
254           url: server.url,
255           path,
256           token: server.accessToken,
257           fields: baseCorrectParams,
258           statusCodeExpected: 200
259         })
260       }
261     })
262
263     it('Should forbid to import http videos', async function () {
264       await updateCustomSubConfig(server.url, server.accessToken, {
265         import: {
266           videos: {
267             http: {
268               enabled: false
269             },
270             torrent: {
271               enabled: true
272             }
273           }
274         }
275       })
276
277       await makePostBodyRequest({
278         url: server.url,
279         path,
280         token: server.accessToken,
281         fields: baseCorrectParams,
282         statusCodeExpected: 409
283       })
284     })
285
286     it('Should forbid to import torrent videos', async function () {
287       await updateCustomSubConfig(server.url, server.accessToken, {
288         import: {
289           videos: {
290             http: {
291               enabled: true
292             },
293             torrent: {
294               enabled: false
295             }
296           }
297         }
298       })
299
300       let fields = omit(baseCorrectParams, 'targetUrl')
301       fields = immutableAssign(fields, { magnetUri: getMagnetURI() })
302
303       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
304
305       fields = omit(fields, 'magnetUri')
306       const attaches = {
307         torrentfile: join(__dirname, '..', '..', 'fixtures', 'video-720p.torrent')
308       }
309
310       await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches, statusCodeExpected: 409 })
311     })
312   })
313
314   after(async function () {
315     await cleanupTests([ server ])
316   })
317 })