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