e62f0918e9f35c767693d54f77a18eab6bb1178d
[oweals/peertube.git] / server / tests / api / check-params / video-imports.ts
1 /* tslint:disable:no-unused-expression */
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   createUser,
9   flushTests,
10   getMyUserInformation,
11   immutableAssign,
12   killallServers,
13   makeGetRequest,
14   makePostBodyRequest,
15   makeUploadRequest,
16   runServer,
17   ServerInfo,
18   setAccessTokensToServers,
19   updateCustomSubConfig,
20   userLogin
21 } from '../../utils'
22 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
23 import { getMagnetURI, getYoutubeVideoUrl } from '../../utils/videos/video-imports'
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 accountName: string
30   let channelId: number
31   let channelUUID: string
32
33   // ---------------------------------------------------------------
34
35   before(async function () {
36     this.timeout(30000)
37
38     await flushTests()
39
40     server = await runServer(1)
41
42     await setAccessTokensToServers([ server ])
43
44     const username = 'user1'
45     const password = 'my super password'
46     await createUser(server.url, server.accessToken, username, password)
47     userAccessToken = await userLogin(server, { username, password })
48
49     {
50       const res = await getMyUserInformation(server.url, server.accessToken)
51       channelId = res.body.videoChannels[ 0 ].id
52       channelUUID = res.body.videoChannels[ 0 ].uuid
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         waitTranscoding: true,
90         description: 'my super description',
91         support: 'my super support text',
92         tags: [ 'tag1', 'tag2' ],
93         privacy: VideoPrivacy.PUBLIC,
94         channelId: channelId
95       }
96     })
97
98     it('Should fail with nothing', async function () {
99       const fields = {}
100       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
101     })
102
103     it('Should fail without a target url', async function () {
104       const fields = omit(baseCorrectParams, 'targetUrl')
105       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 400 })
106     })
107
108     it('Should fail with a bad target url', async function () {
109       const fields = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(baseCorrectParams, { support: 'super'.repeat(150) })
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 = immutableAssign(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(server.url, server.accessToken, user.username, user.password)
168
169       const accessTokenUser = await userLogin(server, user)
170       const res = await getMyUserInformation(server.url, accessTokenUser)
171       const customChannelId = res.body.videoChannels[0].id
172
173       const fields = immutableAssign(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 = immutableAssign(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 = immutableAssign(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 = immutableAssign(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': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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': join(__dirname, '..', '..', 'fixtures', 'avatar-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': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
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': join(__dirname, '..', '..', 'fixtures', 'avatar-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': join(__dirname, '..', '..', 'fixtures', '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 = immutableAssign(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(10000)
250
251       {
252         await makePostBodyRequest({
253           url: server.url,
254           path,
255           token: server.accessToken,
256           fields: baseCorrectParams,
257           statusCodeExpected: 200
258         })
259       }
260     })
261
262     it('Should forbid to import http videos', async function () {
263       await updateCustomSubConfig(server.url, server.accessToken, {
264         import: {
265           videos: {
266             http: {
267               enabled: false
268             },
269             torrent: {
270               enabled: true
271             }
272           }
273         }
274       })
275
276       await makePostBodyRequest({
277         url: server.url,
278         path,
279         token: server.accessToken,
280         fields: baseCorrectParams,
281         statusCodeExpected: 409
282       })
283     })
284
285     it('Should forbid to import torrent videos', async function () {
286       await updateCustomSubConfig(server.url, server.accessToken, {
287         import: {
288           videos: {
289             http: {
290               enabled: true
291             },
292             torrent: {
293               enabled: false
294             }
295           }
296         }
297       })
298
299       let fields = omit(baseCorrectParams, 'targetUrl')
300       fields = immutableAssign(fields, { magnetUri: getMagnetURI() })
301
302       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
303
304       fields = omit(fields, 'magnetUri')
305       const attaches = {
306         'torrentfile': join(__dirname, '..', '..', 'fixtures', '60fps_small-240p.torrent')
307       }
308
309       await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches, statusCodeExpected: 409 })
310     })
311   })
312
313   after(async function () {
314     killallServers([ server ])
315
316     // Keep the logs if the test failed
317     if (this['ok']) {
318       await flushTests()
319     }
320   })
321 })