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