]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-imports.ts
Adapt CLI to new commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-imports.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5d08a6a7 2
5d08a6a7 3import 'mocha'
b488ba1e 4import { omit } from 'lodash'
65e6e260 5import { HttpStatusCode } from '@shared/core-utils'
5d08a6a7 6import {
3d470a53 7 buildAbsoluteFixturePath,
65e6e260
C
8 checkBadCountPagination,
9 checkBadSortPagination,
10 checkBadStartPagination,
7c3b7976 11 cleanupTests,
5d08a6a7 12 createUser,
7c3b7976 13 flushAndRunServer,
5d08a6a7 14 getMyUserInformation,
6910f20f 15 ImportsCommand,
5d08a6a7
C
16 makeGetRequest,
17 makePostBodyRequest,
18 makeUploadRequest,
5d08a6a7 19 ServerInfo,
41d1d075 20 setAccessTokensToServers
65e6e260 21} from '@shared/extra-utils'
65e6e260 22import { VideoPrivacy } from '@shared/models'
5d08a6a7
C
23
24describe('Test video imports API validator', function () {
25 const path = '/api/v1/videos/imports'
26 let server: ServerInfo
27 let userAccessToken = ''
5d08a6a7 28 let channelId: number
5d08a6a7
C
29
30 // ---------------------------------------------------------------
31
32 before(async function () {
33 this.timeout(30000)
34
210feb6c 35 server = await flushAndRunServer(1)
5d08a6a7
C
36
37 await setAccessTokensToServers([ server ])
38
39 const username = 'user1'
40 const password = 'my super password'
1eddc9a7 41 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
41d1d075 42 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
5d08a6a7
C
43
44 {
45 const res = await getMyUserInformation(server.url, server.accessToken)
a1587156 46 channelId = res.body.videoChannels[0].id
5d08a6a7
C
47 }
48 })
49
50 describe('When listing my video imports', function () {
51 const myPath = '/api/v1/users/me/videos/imports'
52
53 it('Should fail with a bad start pagination', async function () {
54 await checkBadStartPagination(server.url, myPath, server.accessToken)
55 })
56
57 it('Should fail with a bad count pagination', async function () {
58 await checkBadCountPagination(server.url, myPath, server.accessToken)
59 })
60
61 it('Should fail with an incorrect sort', async function () {
62 await checkBadSortPagination(server.url, myPath, server.accessToken)
63 })
64
65 it('Should success with the correct parameters', async function () {
2d53be02 66 await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken })
5d08a6a7
C
67 })
68 })
69
70 describe('When adding a video import', function () {
71 let baseCorrectParams
72
73 before(function () {
74 baseCorrectParams = {
6910f20f 75 targetUrl: ImportsCommand.getGoodVideoUrl(),
5d08a6a7
C
76 name: 'my super name',
77 category: 5,
78 licence: 1,
79 language: 'pt',
80 nsfw: false,
81 commentsEnabled: true,
7f2cfe3a 82 downloadEnabled: true,
5d08a6a7
C
83 waitTranscoding: true,
84 description: 'my super description',
85 support: 'my super support text',
86 tags: [ 'tag1', 'tag2' ],
87 privacy: VideoPrivacy.PUBLIC,
c8861d5d 88 channelId
5d08a6a7
C
89 }
90 })
91
92 it('Should fail with nothing', async function () {
93 const fields = {}
94 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
95 })
96
590fb506
C
97 it('Should fail without a target url', async function () {
98 const fields = omit(baseCorrectParams, 'targetUrl')
2d53be02
RK
99 await makePostBodyRequest({
100 url: server.url,
101 path,
102 token: server.accessToken,
103 fields,
104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
105 })
590fb506
C
106 })
107
108 it('Should fail with a bad target url', async function () {
6c5065a0 109 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
590fb506
C
110
111 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
112 })
113
5d08a6a7 114 it('Should fail with a long name', async function () {
6c5065a0 115 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
5d08a6a7
C
116
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
118 })
119
120 it('Should fail with a bad category', async function () {
6c5065a0 121 const fields = { ...baseCorrectParams, category: 125 }
5d08a6a7
C
122
123 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
124 })
125
126 it('Should fail with a bad licence', async function () {
6c5065a0 127 const fields = { ...baseCorrectParams, licence: 125 }
5d08a6a7
C
128
129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
130 })
131
132 it('Should fail with a bad language', async function () {
6c5065a0 133 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
5d08a6a7
C
134
135 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
136 })
137
138 it('Should fail with a long description', async function () {
6c5065a0 139 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
5d08a6a7
C
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 () {
6c5065a0 145 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
5d08a6a7
C
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 () {
6c5065a0 157 const fields = { ...baseCorrectParams, channelId: 545454 }
5d08a6a7
C
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 }
1eddc9a7 167 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
5d08a6a7 168
41d1d075 169 const accessTokenUser = await server.loginCommand.getAccessToken(user)
5d08a6a7
C
170 const res = await getMyUserInformation(server.url, accessTokenUser)
171 const customChannelId = res.body.videoChannels[0].id
172
6c5065a0 173 const fields = { ...baseCorrectParams, channelId: customChannelId }
5d08a6a7
C
174
175 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
176 })
177
178 it('Should fail with too many tags', async function () {
6c5065a0 179 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
5d08a6a7
C
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 () {
6c5065a0 185 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
5d08a6a7
C
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 () {
6c5065a0 191 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
5d08a6a7
C
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 = {
3d470a53 199 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
5d08a6a7
C
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 = {
3d470a53 208 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
5d08a6a7
C
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 = {
3d470a53 217 previewfile: buildAbsoluteFixturePath('video_short.mp4')
5d08a6a7
C
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 = {
3d470a53 226 previewfile: buildAbsoluteFixturePath('preview-big.png')
5d08a6a7
C
227 }
228
229 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
230 })
231
187501f8
C
232 it('Should fail with an invalid torrent file', async function () {
233 const fields = omit(baseCorrectParams, 'targetUrl')
234 const attaches = {
3d470a53 235 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
187501f8
C
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')
6c5065a0 243 fields = { ...fields, magnetUri: 'blabla' }
187501f8
C
244
245 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
246 })
247
5d08a6a7 248 it('Should succeed with the correct parameters', async function () {
13b6dc1f 249 this.timeout(30000)
5d08a6a7 250
b488ba1e
C
251 await makePostBodyRequest({
252 url: server.url,
253 path,
254 token: server.accessToken,
255 fields: baseCorrectParams,
2d53be02 256 statusCodeExpected: HttpStatusCode.OK_200
b488ba1e 257 })
5d08a6a7
C
258 })
259
187501f8 260 it('Should forbid to import http videos', async function () {
65e6e260
C
261 await server.configCommand.updateCustomSubConfig({
262 newConfig: {
263 import: {
264 videos: {
265 http: {
266 enabled: false
267 },
268 torrent: {
269 enabled: true
270 }
590fb506
C
271 }
272 }
273 }
274 })
275
276 await makePostBodyRequest({
277 url: server.url,
278 path,
279 token: server.accessToken,
280 fields: baseCorrectParams,
2d53be02 281 statusCodeExpected: HttpStatusCode.CONFLICT_409
590fb506
C
282 })
283 })
187501f8
C
284
285 it('Should forbid to import torrent videos', async function () {
65e6e260
C
286 await server.configCommand.updateCustomSubConfig({
287 newConfig: {
288 import: {
289 videos: {
290 http: {
291 enabled: true
292 },
293 torrent: {
294 enabled: false
295 }
187501f8
C
296 }
297 }
298 }
299 })
300
301 let fields = omit(baseCorrectParams, 'targetUrl')
6c5065a0 302 fields = { ...fields, magnetUri: ImportsCommand.getMagnetURI() }
187501f8 303
2d53be02
RK
304 await makePostBodyRequest({
305 url: server.url,
306 path,
307 token: server.accessToken,
308 fields,
309 statusCodeExpected: HttpStatusCode.CONFLICT_409
310 })
187501f8
C
311
312 fields = omit(fields, 'magnetUri')
313 const attaches = {
3d470a53 314 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
187501f8
C
315 }
316
2d53be02
RK
317 await makeUploadRequest({
318 url: server.url,
319 path,
320 token: server.accessToken,
321 fields,
322 attaches,
323 statusCodeExpected: HttpStatusCode.CONFLICT_409
324 })
187501f8 325 })
5d08a6a7
C
326 })
327
7c3b7976
C
328 after(async function () {
329 await cleanupTests([ server ])
5d08a6a7
C
330 })
331})