]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/videos/videos.ts
Prevent brute force login attack
[github/Chocobozzz/PeerTube.git] / server / tests / utils / videos / videos.ts
CommitLineData
a20399c9
C
1/* tslint:disable:no-unused-expression */
2
3import { expect } from 'chai'
f05a1c30 4import { existsSync, readFile } from 'fs'
fdbda9e3 5import * as parseTorrent from 'parse-torrent'
1d791a26 6import { extname, join } from 'path'
c5d31dba 7import * as request from 'supertest'
ac81d1a0
C
8import {
9 buildAbsoluteFixturePath,
10 getMyUserInformation,
11 makeGetRequest,
12 makePutBodyRequest,
13 makeUploadRequest,
14 root,
15 ServerInfo,
16 testImage
17} from '../'
c5d31dba 18import { VideoPrivacy } from '../../../../shared/models/videos'
5e1c08eb 19import { readdirPromise } from '../../../helpers/core-utils'
a20399c9
C
20import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers'
21import { dateIsValid, webtorrentAdd } from '../index'
0e1dc3e7
C
22
23type VideoAttributes = {
24 name?: string
25 category?: number
26 licence?: number
27 language?: number
28 nsfw?: boolean
47564bbe 29 commentsEnabled?: boolean
0e1dc3e7
C
30 description?: string
31 tags?: string[]
5f04dd2f 32 channelId?: number
11474c3c 33 privacy?: VideoPrivacy
0e1dc3e7 34 fixture?: string
ac81d1a0
C
35 thumbnailfile?: string
36 previewfile?: string
0e1dc3e7
C
37}
38
39function getVideoCategories (url: string) {
40 const path = '/api/v1/videos/categories'
41
eec63bbc
C
42 return makeGetRequest({
43 url,
59651eee
C
44 path,
45 statusCodeExpected: 200
eec63bbc 46 })
0e1dc3e7
C
47}
48
49function getVideoLicences (url: string) {
50 const path = '/api/v1/videos/licences'
51
eec63bbc
C
52 return makeGetRequest({
53 url,
59651eee
C
54 path,
55 statusCodeExpected: 200
eec63bbc 56 })
0e1dc3e7
C
57}
58
59function getVideoLanguages (url: string) {
60 const path = '/api/v1/videos/languages'
61
eec63bbc
C
62 return makeGetRequest({
63 url,
59651eee
C
64 path,
65 statusCodeExpected: 200
eec63bbc 66 })
0e1dc3e7
C
67}
68
11474c3c
C
69function getVideoPrivacies (url: string) {
70 const path = '/api/v1/videos/privacies'
71
eec63bbc
C
72 return makeGetRequest({
73 url,
59651eee
C
74 path,
75 statusCodeExpected: 200
eec63bbc 76 })
11474c3c
C
77}
78
11474c3c 79function getVideo (url: string, id: number | string, expectedStatus = 200) {
0e1dc3e7
C
80 const path = '/api/v1/videos/' + id
81
82 return request(url)
83 .get(path)
84 .set('Accept', 'application/json')
11474c3c
C
85 .expect(expectedStatus)
86}
87
490b595a 88function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
1f3e9fec
C
89 const path = '/api/v1/videos/' + id + '/views'
90
490b595a 91 const req = request(url)
1f3e9fec
C
92 .post(path)
93 .set('Accept', 'application/json')
490b595a
C
94
95 if (xForwardedFor) {
96 req.set('X-Forwarded-For', xForwardedFor)
97 }
98
99 return req.expect(expectedStatus)
1f3e9fec
C
100}
101
11474c3c
C
102function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
103 const path = '/api/v1/videos/' + id
104
105 return request(url)
106 .get(path)
107 .set('Authorization', 'Bearer ' + token)
108 .set('Accept', 'application/json')
109 .expect(expectedStatus)
0e1dc3e7
C
110}
111
9567011b
C
112function getVideoDescription (url: string, descriptionPath: string) {
113 return request(url)
114 .get(descriptionPath)
115 .set('Accept', 'application/json')
116 .expect(200)
117 .expect('Content-Type', /json/)
118}
119
0e1dc3e7
C
120function getVideosList (url: string) {
121 const path = '/api/v1/videos'
122
123 return request(url)
124 .get(path)
125 .query({ sort: 'name' })
126 .set('Accept', 'application/json')
127 .expect(200)
128 .expect('Content-Type', /json/)
129}
130
066e94c5
C
131function getLocalVideos (url: string) {
132 const path = '/api/v1/videos'
133
134 return request(url)
135 .get(path)
136 .query({ sort: 'name', filter: 'local' })
137 .set('Accept', 'application/json')
138 .expect(200)
139 .expect('Content-Type', /json/)
140}
141
11474c3c
C
142function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
143 const path = '/api/v1/users/me/videos'
144
145 const req = request(url)
146 .get(path)
147 .query({ start: start })
148 .query({ count: count })
149
150 if (sort) req.query({ sort })
151
152 return req.set('Accept', 'application/json')
153 .set('Authorization', 'Bearer ' + accessToken)
154 .expect(200)
155 .expect('Content-Type', /json/)
156}
157
0e1dc3e7
C
158function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
159 const path = '/api/v1/videos'
160
161 const req = request(url)
162 .get(path)
163 .query({ start: start })
164 .query({ count: count })
165
166 if (sort) req.query({ sort })
167
168 return req.set('Accept', 'application/json')
169 .expect(200)
170 .expect('Content-Type', /json/)
171}
172
173function getVideosListSort (url: string, sort: string) {
174 const path = '/api/v1/videos'
175
176 return request(url)
177 .get(path)
178 .query({ sort: sort })
179 .set('Accept', 'application/json')
180 .expect(200)
181 .expect('Content-Type', /json/)
182}
183
11ba2ab3 184function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
0e1dc3e7
C
185 const path = '/api/v1/videos'
186
187 return request(url)
188 .delete(path + '/' + id)
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + token)
191 .expect(expectedStatus)
192}
193
f3aaa9a9 194function searchVideo (url: string, search: string) {
0e1dc3e7
C
195 const path = '/api/v1/videos'
196 const req = request(url)
f3aaa9a9
C
197 .get(path + '/search')
198 .query({ search })
199 .set('Accept', 'application/json')
0e1dc3e7
C
200
201 return req.expect(200)
f3aaa9a9 202 .expect('Content-Type', /json/)
0e1dc3e7
C
203}
204
f3aaa9a9 205function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
0e1dc3e7
C
206 const path = '/api/v1/videos'
207
208 const req = request(url)
f3aaa9a9 209 .get(path + '/search')
0e1dc3e7 210 .query({ start })
f3aaa9a9 211 .query({ search })
0e1dc3e7 212 .query({ count })
0e1dc3e7
C
213
214 if (sort) req.query({ sort })
215
216 return req.set('Accept', 'application/json')
217 .expect(200)
218 .expect('Content-Type', /json/)
219}
220
221function searchVideoWithSort (url: string, search: string, sort: string) {
222 const path = '/api/v1/videos'
223
224 return request(url)
f3aaa9a9
C
225 .get(path + '/search')
226 .query({ search })
0e1dc3e7
C
227 .query({ sort })
228 .set('Accept', 'application/json')
229 .expect(200)
230 .expect('Content-Type', /json/)
231}
232
f05a1c30
C
233async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
234 const testDirectory = 'test' + serverNumber
235
236 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
237 const directoryPath = join(root(), testDirectory, directory)
238
239 const directoryExists = existsSync(directoryPath)
240 expect(directoryExists).to.be.true
241
242 const files = await readdirPromise(directoryPath)
243 for (const file of files) {
244 expect(file).to.not.contain(videoUUID)
245 }
246 }
247}
248
e11f68a3 249async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
e95561cd 250 const path = '/api/v1/videos/upload'
5f04dd2f
C
251 let defaultChannelId = '1'
252
253 try {
254 const res = await getMyUserInformation(url, accessToken)
255 defaultChannelId = res.body.videoChannels[0].id
256 } catch (e) { /* empty */ }
0e1dc3e7 257
ac81d1a0
C
258 // Override default attributes
259 const attributes = Object.assign({
0e1dc3e7
C
260 name: 'my super video',
261 category: 5,
262 licence: 4,
263 language: 3,
5f04dd2f 264 channelId: defaultChannelId,
0e1dc3e7
C
265 nsfw: true,
266 description: 'my super description',
2422c46b 267 support: 'my super support text',
0e1dc3e7 268 tags: [ 'tag' ],
11474c3c 269 privacy: VideoPrivacy.PUBLIC,
47564bbe 270 commentsEnabled: true,
0e1dc3e7 271 fixture: 'video_short.webm'
ac81d1a0 272 }, videoAttributesArg)
0e1dc3e7
C
273
274 const req = request(url)
275 .post(path)
276 .set('Accept', 'application/json')
277 .set('Authorization', 'Bearer ' + accessToken)
278 .field('name', attributes.name)
0e1dc3e7 279 .field('nsfw', JSON.stringify(attributes.nsfw))
47564bbe 280 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
11474c3c 281 .field('privacy', attributes.privacy.toString())
5f04dd2f 282 .field('channelId', attributes.channelId)
0e1dc3e7 283
a87d467a
C
284 if (attributes.description !== undefined) {
285 req.field('description', attributes.description)
286 }
8df87ce7
C
287 if (attributes.language !== undefined) {
288 req.field('language', attributes.language.toString())
289 }
a7fea183
C
290 if (attributes.category !== undefined) {
291 req.field('category', attributes.category.toString())
292 }
293 if (attributes.licence !== undefined) {
294 req.field('licence', attributes.licence.toString())
295 }
8df87ce7 296
2422c46b
C
297 for (let i = 0; i < attributes.tags.length; i++) {
298 req.field('tags[' + i + ']', attributes.tags[i])
299 }
300
ac81d1a0
C
301 if (attributes.thumbnailfile !== undefined) {
302 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
303 }
304 if (attributes.previewfile !== undefined) {
305 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
0e1dc3e7
C
306 }
307
ac81d1a0 308 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
0e1dc3e7
C
309 .expect(specialStatus)
310}
311
ac81d1a0 312function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
0e1dc3e7
C
313 const path = '/api/v1/videos/' + id
314 const body = {}
315
316 if (attributes.name) body['name'] = attributes.name
317 if (attributes.category) body['category'] = attributes.category
318 if (attributes.licence) body['licence'] = attributes.licence
319 if (attributes.language) body['language'] = attributes.language
47564bbe
C
320 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
321 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
0e1dc3e7
C
322 if (attributes.description) body['description'] = attributes.description
323 if (attributes.tags) body['tags'] = attributes.tags
11474c3c 324 if (attributes.privacy) body['privacy'] = attributes.privacy
0e1dc3e7 325
ac81d1a0
C
326 // Upload request
327 if (attributes.thumbnailfile || attributes.previewfile) {
328 const attaches: any = {}
329 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
330 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
331
332 return makeUploadRequest({
333 url,
334 method: 'PUT',
335 path,
336 token: accessToken,
337 fields: body,
338 attaches,
339 statusCodeExpected
340 })
341 }
342
343 return makePutBodyRequest({
344 url,
345 path,
346 fields: body,
347 token: accessToken,
348 statusCodeExpected
349 })
0e1dc3e7
C
350}
351
352function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
353 const path = '/api/v1/videos/' + id + '/rate'
354
355 return request(url)
356 .put(path)
357 .set('Accept', 'application/json')
358 .set('Authorization', 'Bearer ' + accessToken)
359 .send({ rating })
360 .expect(specialStatus)
361}
362
14d3270f 363function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
fdbda9e3 364 return new Promise<any>((res, rej) => {
14d3270f 365 const torrentName = videoUUID + '-' + resolution + '.torrent'
331128ed 366 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
fdbda9e3
C
367 readFile(torrentPath, (err, data) => {
368 if (err) return rej(err)
369
370 return res(parseTorrent(data))
371 })
372 })
373}
374
a20399c9
C
375async function completeVideoCheck (
376 url: string,
377 video: any,
378 attributes: {
379 name: string
380 category: number
381 licence: number
382 language: number
383 nsfw: boolean
47564bbe 384 commentsEnabled: boolean
a20399c9 385 description: string
2422c46b 386 support: string
b64c950a
C
387 account: {
388 name: string
389 host: string
390 }
a20399c9
C
391 isLocal: boolean,
392 tags: string[],
393 privacy: number,
b1f5b93e
C
394 likes?: number,
395 dislikes?: number,
396 duration: number,
a20399c9
C
397 channel: {
398 name: string,
b1f5b93e 399 description
a20399c9
C
400 isLocal: boolean
401 }
402 fixture: string,
403 files: {
404 resolution: number
405 size: number
ac81d1a0
C
406 }[],
407 thumbnailfile?: string
408 previewfile?: string
a20399c9
C
409 }
410) {
b1f5b93e
C
411 if (!attributes.likes) attributes.likes = 0
412 if (!attributes.dislikes) attributes.dislikes = 0
413
a20399c9 414 expect(video.name).to.equal(attributes.name)
09700934
C
415 expect(video.category.id).to.equal(attributes.category)
416 expect(video.category.label).to.equal(VIDEO_CATEGORIES[attributes.category] || 'Misc')
417 expect(video.licence.id).to.equal(attributes.licence)
418 expect(video.licence.label).to.equal(VIDEO_LICENCES[attributes.licence] || 'Unknown')
419 expect(video.language.id).to.equal(attributes.language)
420 expect(video.language.label).to.equal(VIDEO_LANGUAGES[attributes.language] || 'Unknown')
a20399c9
C
421 expect(video.nsfw).to.equal(attributes.nsfw)
422 expect(video.description).to.equal(attributes.description)
b64c950a
C
423 expect(video.account.host).to.equal(attributes.account.host)
424 expect(video.account.name).to.equal(attributes.account.name)
b1f5b93e
C
425 expect(video.likes).to.equal(attributes.likes)
426 expect(video.dislikes).to.equal(attributes.dislikes)
a20399c9 427 expect(video.isLocal).to.equal(attributes.isLocal)
b1f5b93e 428 expect(video.duration).to.equal(attributes.duration)
a20399c9
C
429 expect(dateIsValid(video.createdAt)).to.be.true
430 expect(dateIsValid(video.updatedAt)).to.be.true
431
66b16caf 432 const res = await getVideo(url, video.uuid)
a20399c9
C
433 const videoDetails = res.body
434
435 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
436 expect(videoDetails.tags).to.deep.equal(attributes.tags)
09700934
C
437 expect(videoDetails.privacy.id).to.deep.equal(attributes.privacy)
438 expect(videoDetails.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
19a3b914
C
439 expect(videoDetails.account.name).to.equal(attributes.account.name)
440 expect(videoDetails.account.host).to.equal(attributes.account.host)
47564bbe 441 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
a20399c9 442
7bc29171
C
443 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
444 expect(videoDetails.channel.name).to.have.lengthOf(36)
a20399c9
C
445 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
446 expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
447 expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
448
449 for (const attributeFile of attributes.files) {
5d00a3d7 450 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
a20399c9
C
451 expect(file).not.to.be.undefined
452
b1f5b93e
C
453 let extension = extname(attributes.fixture)
454 // Transcoding enabled on server 2, extension will always be .mp4
b64c950a 455 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
b1f5b93e 456
a20399c9
C
457 const magnetUri = file.magnetUri
458 expect(file.magnetUri).to.have.lengthOf.above(2)
5d00a3d7
C
459 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
460 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
09700934
C
461 expect(file.resolution.id).to.equal(attributeFile.resolution)
462 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
b1f5b93e
C
463
464 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
465 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
466 expect(file.size).to.be.above(minSize).and.below(maxSize)
a20399c9 467
ac81d1a0 468 {
7b0956ec 469 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
ac81d1a0
C
470 }
471
472 if (attributes.previewfile) {
7b0956ec 473 await testImage(url, attributes.previewfile, videoDetails.previewPath)
ac81d1a0 474 }
a20399c9
C
475
476 const torrent = await webtorrentAdd(magnetUri, true)
477 expect(torrent.files).to.be.an('array')
478 expect(torrent.files.length).to.equal(1)
479 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
480 }
481}
482
0e1dc3e7
C
483// ---------------------------------------------------------------------------
484
485export {
9567011b 486 getVideoDescription,
0e1dc3e7
C
487 getVideoCategories,
488 getVideoLicences,
11474c3c 489 getVideoPrivacies,
0e1dc3e7 490 getVideoLanguages,
11474c3c 491 getMyVideos,
0e1dc3e7 492 getVideo,
11474c3c 493 getVideoWithToken,
0e1dc3e7
C
494 getVideosList,
495 getVideosListPagination,
496 getVideosListSort,
497 removeVideo,
498 searchVideo,
499 searchVideoWithPagination,
500 searchVideoWithSort,
0e1dc3e7
C
501 uploadVideo,
502 updateVideo,
fdbda9e3 503 rateVideo,
1f3e9fec 504 viewVideo,
a20399c9 505 parseTorrentVideo,
066e94c5 506 getLocalVideos,
f05a1c30
C
507 completeVideoCheck,
508 checkVideoFilesWereRemoved
0e1dc3e7 509}