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