1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { HttpStatusCode } from '@shared/core-utils'
4 import { expect } from 'chai'
5 import { pathExists, readdir, readFile } from 'fs-extra'
6 import * as parseTorrent from 'parse-torrent'
7 import { extname, join } from 'path'
8 import * as request from 'supertest'
9 import { v4 as uuidv4 } from 'uuid'
10 import validator from 'validator'
11 import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
12 import { VideoDetails, VideoPrivacy } from '../../models/videos'
13 import { buildAbsoluteFixturePath, buildServerDirectory, dateIsValid, immutableAssign, testImage, webtorrentAdd } from '../miscs/miscs'
14 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
15 import { waitJobs } from '../server/jobs'
16 import { ServerInfo } from '../server/servers'
17 import { getMyUserInformation } from '../users/users'
21 type VideoAttributes = {
27 commentsEnabled?: boolean
28 downloadEnabled?: boolean
29 waitTranscoding?: boolean
31 originallyPublishedAt?: string
34 privacy?: VideoPrivacy
36 thumbnailfile?: string
40 privacy?: VideoPrivacy
44 function getVideoCategories (url: string) {
45 const path = '/api/v1/videos/categories'
47 return makeGetRequest({
50 statusCodeExpected: HttpStatusCode.OK_200
54 function getVideoLicences (url: string) {
55 const path = '/api/v1/videos/licences'
57 return makeGetRequest({
60 statusCodeExpected: HttpStatusCode.OK_200
64 function getVideoLanguages (url: string) {
65 const path = '/api/v1/videos/languages'
67 return makeGetRequest({
70 statusCodeExpected: HttpStatusCode.OK_200
74 function getVideoPrivacies (url: string) {
75 const path = '/api/v1/videos/privacies'
77 return makeGetRequest({
80 statusCodeExpected: HttpStatusCode.OK_200
84 function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
85 const path = '/api/v1/videos/' + id
89 .set('Accept', 'application/json')
90 .expect(expectedStatus)
93 async function getVideoIdFromUUID (url: string, uuid: string) {
94 const res = await getVideo(url, uuid)
99 function getVideoFileMetadataUrl (url: string) {
102 .set('Accept', 'application/json')
103 .expect(HttpStatusCode.OK_200)
104 .expect('Content-Type', /json/)
107 function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) {
108 const path = '/api/v1/videos/' + id + '/views'
110 const req = request(url)
112 .set('Accept', 'application/json')
115 req.set('X-Forwarded-For', xForwardedFor)
118 return req.expect(expectedStatus)
121 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
122 const path = '/api/v1/videos/' + id
126 .set('Authorization', 'Bearer ' + token)
127 .set('Accept', 'application/json')
128 .expect(expectedStatus)
131 function getVideoDescription (url: string, descriptionPath: string) {
133 .get(descriptionPath)
134 .set('Accept', 'application/json')
135 .expect(HttpStatusCode.OK_200)
136 .expect('Content-Type', /json/)
139 function getVideosList (url: string) {
140 const path = '/api/v1/videos'
144 .query({ sort: 'name' })
145 .set('Accept', 'application/json')
146 .expect(HttpStatusCode.OK_200)
147 .expect('Content-Type', /json/)
150 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
151 const path = '/api/v1/videos'
155 .set('Authorization', 'Bearer ' + token)
156 .query(immutableAssign(query, { sort: 'name' }))
157 .set('Accept', 'application/json')
159 .expect('Content-Type', /json/)
162 function getLocalVideos (url: string) {
163 const path = '/api/v1/videos'
167 .query({ sort: 'name', filter: 'local' })
168 .set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
173 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string, search?: string) {
174 const path = '/api/v1/users/me/videos'
176 const req = request(url)
178 .query({ start: start })
179 .query({ count: count })
180 .query({ search: search })
182 if (sort) req.query({ sort })
184 return req.set('Accept', 'application/json')
185 .set('Authorization', 'Bearer ' + accessToken)
186 .expect(HttpStatusCode.OK_200)
187 .expect('Content-Type', /json/)
190 function getAccountVideos (
197 query: { nsfw?: boolean } = {}
199 const path = '/api/v1/accounts/' + accountName + '/videos'
201 return makeGetRequest({
204 query: immutableAssign(query, {
210 statusCodeExpected: HttpStatusCode.OK_200
214 function getVideoChannelVideos (
217 videoChannelName: string,
221 query: { nsfw?: boolean } = {}
223 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
225 return makeGetRequest({
228 query: immutableAssign(query, {
234 statusCodeExpected: HttpStatusCode.OK_200
238 function getPlaylistVideos (
241 playlistId: number | string,
244 query: { nsfw?: boolean } = {}
246 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
248 return makeGetRequest({
251 query: immutableAssign(query, {
256 statusCodeExpected: HttpStatusCode.OK_200
260 function getVideosListPagination (url: string, start: number, count: number, sort?: string, skipCount?: boolean) {
261 const path = '/api/v1/videos'
263 const req = request(url)
265 .query({ start: start })
266 .query({ count: count })
268 if (sort) req.query({ sort })
269 if (skipCount) req.query({ skipCount })
271 return req.set('Accept', 'application/json')
272 .expect(HttpStatusCode.OK_200)
273 .expect('Content-Type', /json/)
276 function getVideosListSort (url: string, sort: string) {
277 const path = '/api/v1/videos'
281 .query({ sort: sort })
282 .set('Accept', 'application/json')
283 .expect(HttpStatusCode.OK_200)
284 .expect('Content-Type', /json/)
287 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
288 const path = '/api/v1/videos'
293 .set('Accept', 'application/json')
294 .expect(HttpStatusCode.OK_200)
295 .expect('Content-Type', /json/)
298 function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
299 const path = '/api/v1/videos'
302 .delete(path + '/' + id)
303 .set('Accept', 'application/json')
304 .set('Authorization', 'Bearer ' + token)
305 .expect(expectedStatus)
308 async function removeAllVideos (server: ServerInfo) {
309 const resVideos = await getVideosList(server.url)
311 for (const v of resVideos.body.data) {
312 await removeVideo(server.url, server.accessToken, v.id)
316 async function checkVideoFilesWereRemoved (
318 serverNumber: number,
326 join('playlists', 'hls'),
327 join('redundancy', 'hls')
330 for (const directory of directories) {
331 const directoryPath = buildServerDirectory({ internalServerNumber: serverNumber }, directory)
333 const directoryExists = await pathExists(directoryPath)
334 if (directoryExists === false) continue
336 const files = await readdir(directoryPath)
337 for (const file of files) {
338 expect(file).to.not.contain(videoUUID)
343 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
344 const path = '/api/v1/videos/upload'
345 let defaultChannelId = '1'
348 const res = await getMyUserInformation(url, accessToken)
349 defaultChannelId = res.body.videoChannels[0].id
350 } catch (e) { /* empty */ }
352 // Override default attributes
353 const attributes = Object.assign({
354 name: 'my super video',
358 channelId: defaultChannelId,
360 waitTranscoding: false,
361 description: 'my super description',
362 support: 'my super support text',
364 privacy: VideoPrivacy.PUBLIC,
365 commentsEnabled: true,
366 downloadEnabled: true,
367 fixture: 'video_short.webm'
368 }, videoAttributesArg)
370 const req = request(url)
372 .set('Accept', 'application/json')
373 .set('Authorization', 'Bearer ' + accessToken)
374 .field('name', attributes.name)
375 .field('nsfw', JSON.stringify(attributes.nsfw))
376 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
377 .field('downloadEnabled', JSON.stringify(attributes.downloadEnabled))
378 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
379 .field('privacy', attributes.privacy.toString())
380 .field('channelId', attributes.channelId)
382 if (attributes.support !== undefined) {
383 req.field('support', attributes.support)
386 if (attributes.description !== undefined) {
387 req.field('description', attributes.description)
389 if (attributes.language !== undefined) {
390 req.field('language', attributes.language.toString())
392 if (attributes.category !== undefined) {
393 req.field('category', attributes.category.toString())
395 if (attributes.licence !== undefined) {
396 req.field('licence', attributes.licence.toString())
399 const tags = attributes.tags || []
400 for (let i = 0; i < tags.length; i++) {
401 req.field('tags[' + i + ']', attributes.tags[i])
404 if (attributes.thumbnailfile !== undefined) {
405 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
407 if (attributes.previewfile !== undefined) {
408 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
411 if (attributes.scheduleUpdate) {
412 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
414 if (attributes.scheduleUpdate.privacy) {
415 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
419 if (attributes.originallyPublishedAt !== undefined) {
420 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
423 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
424 .expect(specialStatus)
427 function updateVideo (
431 attributes: VideoAttributes,
432 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
434 const path = '/api/v1/videos/' + id
437 if (attributes.name) body['name'] = attributes.name
438 if (attributes.category) body['category'] = attributes.category
439 if (attributes.licence) body['licence'] = attributes.licence
440 if (attributes.language) body['language'] = attributes.language
441 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
442 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
443 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
444 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
445 if (attributes.description) body['description'] = attributes.description
446 if (attributes.tags) body['tags'] = attributes.tags
447 if (attributes.privacy) body['privacy'] = attributes.privacy
448 if (attributes.channelId) body['channelId'] = attributes.channelId
449 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
452 if (attributes.thumbnailfile || attributes.previewfile) {
453 const attaches: any = {}
454 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
455 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
457 return makeUploadRequest({
468 return makePutBodyRequest({
477 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
478 const path = '/api/v1/videos/' + id + '/rate'
482 .set('Accept', 'application/json')
483 .set('Authorization', 'Bearer ' + accessToken)
485 .expect(specialStatus)
488 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
489 return new Promise<any>((res, rej) => {
490 const torrentName = videoUUID + '-' + resolution + '.torrent'
491 const torrentPath = buildServerDirectory(server, join('torrents', torrentName))
493 readFile(torrentPath, (err, data) => {
494 if (err) return rej(err)
496 return res(parseTorrent(data))
501 async function completeVideoCheck (
510 commentsEnabled: boolean
511 downloadEnabled: boolean
515 originallyPublishedAt?: string
537 thumbnailfile?: string
541 if (!attributes.likes) attributes.likes = 0
542 if (!attributes.dislikes) attributes.dislikes = 0
544 expect(video.name).to.equal(attributes.name)
545 expect(video.category.id).to.equal(attributes.category)
546 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
547 expect(video.licence.id).to.equal(attributes.licence)
548 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
549 expect(video.language.id).to.equal(attributes.language)
550 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
551 expect(video.privacy.id).to.deep.equal(attributes.privacy)
552 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
553 expect(video.nsfw).to.equal(attributes.nsfw)
554 expect(video.description).to.equal(attributes.description)
555 expect(video.account.id).to.be.a('number')
556 expect(video.account.host).to.equal(attributes.account.host)
557 expect(video.account.name).to.equal(attributes.account.name)
558 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
559 expect(video.channel.name).to.equal(attributes.channel.name)
560 expect(video.likes).to.equal(attributes.likes)
561 expect(video.dislikes).to.equal(attributes.dislikes)
562 expect(video.isLocal).to.equal(attributes.isLocal)
563 expect(video.duration).to.equal(attributes.duration)
564 expect(dateIsValid(video.createdAt)).to.be.true
565 expect(dateIsValid(video.publishedAt)).to.be.true
566 expect(dateIsValid(video.updatedAt)).to.be.true
568 if (attributes.publishedAt) {
569 expect(video.publishedAt).to.equal(attributes.publishedAt)
572 if (attributes.originallyPublishedAt) {
573 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
575 expect(video.originallyPublishedAt).to.be.null
578 const res = await getVideo(url, video.uuid)
579 const videoDetails: VideoDetails = res.body
581 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
582 expect(videoDetails.tags).to.deep.equal(attributes.tags)
583 expect(videoDetails.account.name).to.equal(attributes.account.name)
584 expect(videoDetails.account.host).to.equal(attributes.account.host)
585 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
586 expect(video.channel.name).to.equal(attributes.channel.name)
587 expect(videoDetails.channel.host).to.equal(attributes.account.host)
588 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
589 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
590 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
591 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
592 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
594 for (const attributeFile of attributes.files) {
595 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
596 expect(file).not.to.be.undefined
598 let extension = extname(attributes.fixture)
599 // Transcoding enabled: extension will always be .mp4
600 if (attributes.files.length > 1) extension = '.mp4'
602 expect(file.magnetUri).to.have.lengthOf.above(2)
603 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
604 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
605 expect(file.resolution.id).to.equal(attributeFile.resolution)
606 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
608 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
609 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
612 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
613 ).to.be.above(minSize).and.below(maxSize)
615 const torrent = await webtorrentAdd(file.magnetUri, true)
616 expect(torrent.files).to.be.an('array')
617 expect(torrent.files.length).to.equal(1)
618 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
621 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
623 if (attributes.previewfile) {
624 await testImage(url, attributes.previewfile, videoDetails.previewPath)
628 async function videoUUIDToId (url: string, id: number | string) {
629 if (validator.isUUID('' + id) === false) return id
631 const res = await getVideo(url, id)
635 async function uploadVideoAndGetId (options: {
639 privacy?: VideoPrivacy
642 const videoAttrs: any = { name: options.videoName }
643 if (options.nsfw) videoAttrs.nsfw = options.nsfw
644 if (options.privacy) videoAttrs.privacy = options.privacy
646 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
648 return { id: res.body.video.id, uuid: res.body.video.uuid }
651 async function getLocalIdByUUID (url: string, uuid: string) {
652 const res = await getVideo(url, uuid)
657 // serverNumber starts from 1
658 async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) {
659 const server = servers.find(s => s.serverNumber === serverNumber)
660 const res = await uploadRandomVideo(server, false, additionalParams)
662 await waitJobs(servers)
667 async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) {
668 const prefixName = additionalParams.prefixName || ''
669 const name = prefixName + uuidv4()
671 const data = Object.assign({ name }, additionalParams)
672 const res = await uploadVideo(server.url, server.accessToken, data)
674 if (wait) await waitJobs([ server ])
676 return { uuid: res.body.video.uuid, name }
679 // ---------------------------------------------------------------------------
691 getVideoChannelVideos,
693 getVideoFileMetadataUrl,
697 getVideosListPagination,
700 getVideosListWithToken,
702 getVideosWithFilters,
703 uploadRandomVideoOnServers,
710 checkVideoFilesWereRemoved,