1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { createReadStream, pathExists, readdir, readFile, stat } from 'fs-extra'
5 import got, { Response as GotResponse } from 'got/dist/source'
6 import * as parseTorrent from 'parse-torrent'
7 import { join } from 'path'
8 import * as request from 'supertest'
9 import validator from 'validator'
10 import { getLowercaseExtension } from '@server/helpers/core-utils'
11 import { buildUUID } from '@server/helpers/uuid'
12 import { HttpStatusCode } from '@shared/core-utils'
13 import { BooleanBothQuery, VideosCommonQuery } from '@shared/models'
14 import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
15 import { VideoDetails, VideoPrivacy } from '../../models/videos'
17 buildAbsoluteFixturePath,
24 } from '../miscs/miscs'
25 import { makeGetRequest, makePutBodyRequest, makeRawRequest, makeUploadRequest } from '../requests/requests'
26 import { waitJobs } from '../server/jobs'
27 import { ServerInfo } from '../server/servers'
28 import { getMyUserInformation } from '../users/users'
32 type VideoAttributes = {
38 commentsEnabled?: boolean
39 downloadEnabled?: boolean
40 waitTranscoding?: boolean
42 originallyPublishedAt?: string
45 privacy?: VideoPrivacy
48 thumbnailfile?: string
52 privacy?: VideoPrivacy
56 function getVideoCategories (url: string) {
57 const path = '/api/v1/videos/categories'
59 return makeGetRequest({
62 statusCodeExpected: HttpStatusCode.OK_200
66 function getVideoLicences (url: string) {
67 const path = '/api/v1/videos/licences'
69 return makeGetRequest({
72 statusCodeExpected: HttpStatusCode.OK_200
76 function getVideoLanguages (url: string) {
77 const path = '/api/v1/videos/languages'
79 return makeGetRequest({
82 statusCodeExpected: HttpStatusCode.OK_200
86 function getVideoPrivacies (url: string) {
87 const path = '/api/v1/videos/privacies'
89 return makeGetRequest({
92 statusCodeExpected: HttpStatusCode.OK_200
96 function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
97 const path = '/api/v1/videos/' + id
101 .set('Accept', 'application/json')
102 .expect(expectedStatus)
105 async function getVideoIdFromUUID (url: string, uuid: string) {
106 const res = await getVideo(url, uuid)
111 function getVideoFileMetadataUrl (url: string) {
114 .set('Accept', 'application/json')
115 .expect(HttpStatusCode.OK_200)
116 .expect('Content-Type', /json/)
119 function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) {
120 const path = '/api/v1/videos/' + id + '/views'
122 const req = request(url)
124 .set('Accept', 'application/json')
127 req.set('X-Forwarded-For', xForwardedFor)
130 return req.expect(expectedStatus)
133 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
134 const path = '/api/v1/videos/' + id
138 .set('Authorization', 'Bearer ' + token)
139 .set('Accept', 'application/json')
140 .expect(expectedStatus)
143 function getVideoDescription (url: string, descriptionPath: string) {
145 .get(descriptionPath)
146 .set('Accept', 'application/json')
147 .expect(HttpStatusCode.OK_200)
148 .expect('Content-Type', /json/)
151 function getVideosList (url: string) {
152 const path = '/api/v1/videos'
156 .query({ sort: 'name' })
157 .set('Accept', 'application/json')
158 .expect(HttpStatusCode.OK_200)
159 .expect('Content-Type', /json/)
162 function getVideosListWithToken (url: string, token: string, query: { nsfw?: BooleanBothQuery } = {}) {
163 const path = '/api/v1/videos'
167 .set('Authorization', 'Bearer ' + token)
168 .query(immutableAssign(query, { sort: 'name' }))
169 .set('Accept', 'application/json')
170 .expect(HttpStatusCode.OK_200)
171 .expect('Content-Type', /json/)
174 function getLocalVideos (url: string) {
175 const path = '/api/v1/videos'
179 .query({ sort: 'name', filter: 'local' })
180 .set('Accept', 'application/json')
181 .expect(HttpStatusCode.OK_200)
182 .expect('Content-Type', /json/)
185 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string, search?: string) {
186 const path = '/api/v1/users/me/videos'
188 const req = request(url)
190 .query({ start: start })
191 .query({ count: count })
192 .query({ search: search })
194 if (sort) req.query({ sort })
196 return req.set('Accept', 'application/json')
197 .set('Authorization', 'Bearer ' + accessToken)
198 .expect(HttpStatusCode.OK_200)
199 .expect('Content-Type', /json/)
202 function getMyVideosWithFilter (url: string, accessToken: string, query: { isLive?: boolean }) {
203 const path = '/api/v1/users/me/videos'
205 return makeGetRequest({
210 statusCodeExpected: HttpStatusCode.OK_200
214 function getAccountVideos (
222 nsfw?: BooleanBothQuery
226 const path = '/api/v1/accounts/' + accountName + '/videos'
228 return makeGetRequest({
231 query: immutableAssign(query, {
237 statusCodeExpected: HttpStatusCode.OK_200
241 function getVideoChannelVideos (
244 videoChannelName: string,
248 query: { nsfw?: BooleanBothQuery } = {}
250 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
252 return makeGetRequest({
255 query: immutableAssign(query, {
261 statusCodeExpected: HttpStatusCode.OK_200
265 function getVideosListPagination (url: string, start: number, count: number, sort?: string, skipCount?: boolean) {
266 const path = '/api/v1/videos'
268 const req = request(url)
270 .query({ start: start })
271 .query({ count: count })
273 if (sort) req.query({ sort })
274 if (skipCount) req.query({ skipCount })
276 return req.set('Accept', 'application/json')
277 .expect(HttpStatusCode.OK_200)
278 .expect('Content-Type', /json/)
281 function getVideosListSort (url: string, sort: string) {
282 const path = '/api/v1/videos'
286 .query({ sort: sort })
287 .set('Accept', 'application/json')
288 .expect(HttpStatusCode.OK_200)
289 .expect('Content-Type', /json/)
292 function getVideosWithFilters (url: string, query: VideosCommonQuery) {
293 const path = '/api/v1/videos'
298 .set('Accept', 'application/json')
299 .expect(HttpStatusCode.OK_200)
300 .expect('Content-Type', /json/)
303 function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
304 const path = '/api/v1/videos'
307 .delete(path + '/' + id)
308 .set('Accept', 'application/json')
309 .set('Authorization', 'Bearer ' + token)
310 .expect(expectedStatus)
313 async function removeAllVideos (server: ServerInfo) {
314 const resVideos = await getVideosList(server.url)
316 for (const v of resVideos.body.data) {
317 await removeVideo(server.url, server.accessToken, v.id)
321 async function checkVideoFilesWereRemoved (
323 serverNumber: number,
331 join('playlists', 'hls'),
332 join('redundancy', 'hls')
335 for (const directory of directories) {
336 const directoryPath = buildServerDirectory({ internalServerNumber: serverNumber }, directory)
338 const directoryExists = await pathExists(directoryPath)
339 if (directoryExists === false) continue
341 const files = await readdir(directoryPath)
342 for (const file of files) {
343 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
348 async function uploadVideo (
351 videoAttributesArg: VideoAttributes,
352 specialStatus = HttpStatusCode.OK_200,
353 mode: 'legacy' | 'resumable' = 'legacy'
355 let defaultChannelId = '1'
358 const res = await getMyUserInformation(url, accessToken)
359 defaultChannelId = res.body.videoChannels[0].id
360 } catch (e) { /* empty */ }
362 // Override default attributes
363 const attributes = Object.assign({
364 name: 'my super video',
368 channelId: defaultChannelId,
370 waitTranscoding: false,
371 description: 'my super description',
372 support: 'my super support text',
374 privacy: VideoPrivacy.PUBLIC,
375 commentsEnabled: true,
376 downloadEnabled: true,
377 fixture: 'video_short.webm'
378 }, videoAttributesArg)
380 const res = mode === 'legacy'
381 ? await buildLegacyUpload(url, accessToken, attributes, specialStatus)
382 : await buildResumeUpload(url, accessToken, attributes, specialStatus)
384 // Wait torrent generation
385 if (specialStatus === HttpStatusCode.OK_200) {
386 let video: VideoDetails
388 const resVideo = await getVideoWithToken(url, accessToken, res.body.video.uuid)
389 video = resVideo.body
392 } while (!video.files[0].torrentUrl)
398 function checkUploadVideoParam (
401 attributes: Partial<VideoAttributes>,
402 specialStatus = HttpStatusCode.OK_200,
403 mode: 'legacy' | 'resumable' = 'legacy'
405 return mode === 'legacy'
406 ? buildLegacyUpload(url, token, attributes, specialStatus)
407 : buildResumeUpload(url, token, attributes, specialStatus)
410 async function buildLegacyUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
411 const path = '/api/v1/videos/upload'
412 const req = request(url)
414 .set('Accept', 'application/json')
415 .set('Authorization', 'Bearer ' + token)
417 buildUploadReq(req, attributes)
419 if (attributes.fixture !== undefined) {
420 req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
423 return req.expect(specialStatus)
426 async function buildResumeUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
428 let videoFilePath: string
429 let mimetype = 'video/mp4'
431 if (attributes.fixture) {
432 videoFilePath = buildAbsoluteFixturePath(attributes.fixture)
433 size = (await stat(videoFilePath)).size
435 if (videoFilePath.endsWith('.mkv')) {
436 mimetype = 'video/x-matroska'
437 } else if (videoFilePath.endsWith('.webm')) {
438 mimetype = 'video/webm'
442 const initializeSessionRes = await prepareResumableUpload({ url, token, attributes, size, mimetype })
443 const initStatus = initializeSessionRes.status
445 if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) {
446 const locationHeader = initializeSessionRes.header['location']
447 expect(locationHeader).to.not.be.undefined
449 const pathUploadId = locationHeader.split('?')[1]
451 return sendResumableChunks({ url, token, pathUploadId, videoFilePath, size, specialStatus })
454 const expectedInitStatus = specialStatus === HttpStatusCode.OK_200
455 ? HttpStatusCode.CREATED_201
458 expect(initStatus).to.equal(expectedInitStatus)
460 return initializeSessionRes
463 async function prepareResumableUpload (options: {
466 attributes: VideoAttributes
470 const { url, token, attributes, size, mimetype } = options
472 const path = '/api/v1/videos/upload-resumable'
474 const req = request(url)
476 .set('Authorization', 'Bearer ' + token)
477 .set('X-Upload-Content-Type', mimetype)
478 .set('X-Upload-Content-Length', size.toString())
480 buildUploadReq(req, attributes)
482 if (attributes.fixture) {
483 req.field('filename', attributes.fixture)
489 function sendResumableChunks (options: {
493 videoFilePath: string
495 specialStatus?: HttpStatusCode
496 contentLength?: number
497 contentRangeBuilder?: (start: number, chunk: any) => string
499 const { url, token, pathUploadId, videoFilePath, size, specialStatus, contentLength, contentRangeBuilder } = options
501 const expectedStatus = specialStatus || HttpStatusCode.OK_200
503 const path = '/api/v1/videos/upload-resumable'
506 const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 })
507 return new Promise<GotResponse>((resolve, reject) => {
508 readable.on('data', async function onData (chunk) {
512 'Authorization': 'Bearer ' + token,
513 'Content-Type': 'application/octet-stream',
514 'Content-Range': contentRangeBuilder
515 ? contentRangeBuilder(start, chunk)
516 : `bytes ${start}-${start + chunk.length - 1}/${size}`,
517 'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
520 const res = await got({
524 path: path + '?' + pathUploadId,
526 responseType: 'json',
527 throwHttpErrors: false
530 start += chunk.length
532 if (res.statusCode === expectedStatus) {
536 if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) {
537 readable.off('data', onData)
538 return reject(new Error('Incorrect transient behaviour sending intermediary chunks'))
546 function updateVideo (
550 attributes: VideoAttributes,
551 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
553 const path = '/api/v1/videos/' + id
556 if (attributes.name) body['name'] = attributes.name
557 if (attributes.category) body['category'] = attributes.category
558 if (attributes.licence) body['licence'] = attributes.licence
559 if (attributes.language) body['language'] = attributes.language
560 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
561 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
562 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
563 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
564 if (attributes.description) body['description'] = attributes.description
565 if (attributes.tags) body['tags'] = attributes.tags
566 if (attributes.privacy) body['privacy'] = attributes.privacy
567 if (attributes.channelId) body['channelId'] = attributes.channelId
568 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
571 if (attributes.thumbnailfile || attributes.previewfile) {
572 const attaches: any = {}
573 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
574 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
576 return makeUploadRequest({
587 return makePutBodyRequest({
596 function rateVideo (url: string, accessToken: string, id: number | string, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
597 const path = '/api/v1/videos/' + id + '/rate'
601 .set('Accept', 'application/json')
602 .set('Authorization', 'Bearer ' + accessToken)
604 .expect(specialStatus)
607 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
608 return new Promise<any>((res, rej) => {
609 const torrentName = videoUUID + '-' + resolution + '.torrent'
610 const torrentPath = buildServerDirectory(server, join('torrents', torrentName))
612 readFile(torrentPath, (err, data) => {
613 if (err) return rej(err)
615 return res(parseTorrent(data))
620 async function completeVideoCheck (
629 commentsEnabled: boolean
630 downloadEnabled: boolean
634 originallyPublishedAt?: string
656 thumbnailfile?: string
660 if (!attributes.likes) attributes.likes = 0
661 if (!attributes.dislikes) attributes.dislikes = 0
663 const host = new URL(url).host
664 const originHost = attributes.account.host
666 expect(video.name).to.equal(attributes.name)
667 expect(video.category.id).to.equal(attributes.category)
668 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
669 expect(video.licence.id).to.equal(attributes.licence)
670 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
671 expect(video.language.id).to.equal(attributes.language)
672 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
673 expect(video.privacy.id).to.deep.equal(attributes.privacy)
674 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
675 expect(video.nsfw).to.equal(attributes.nsfw)
676 expect(video.description).to.equal(attributes.description)
677 expect(video.account.id).to.be.a('number')
678 expect(video.account.host).to.equal(attributes.account.host)
679 expect(video.account.name).to.equal(attributes.account.name)
680 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
681 expect(video.channel.name).to.equal(attributes.channel.name)
682 expect(video.likes).to.equal(attributes.likes)
683 expect(video.dislikes).to.equal(attributes.dislikes)
684 expect(video.isLocal).to.equal(attributes.isLocal)
685 expect(video.duration).to.equal(attributes.duration)
686 expect(dateIsValid(video.createdAt)).to.be.true
687 expect(dateIsValid(video.publishedAt)).to.be.true
688 expect(dateIsValid(video.updatedAt)).to.be.true
690 if (attributes.publishedAt) {
691 expect(video.publishedAt).to.equal(attributes.publishedAt)
694 if (attributes.originallyPublishedAt) {
695 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
697 expect(video.originallyPublishedAt).to.be.null
700 const res = await getVideo(url, video.uuid)
701 const videoDetails: VideoDetails = res.body
703 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
704 expect(videoDetails.tags).to.deep.equal(attributes.tags)
705 expect(videoDetails.account.name).to.equal(attributes.account.name)
706 expect(videoDetails.account.host).to.equal(attributes.account.host)
707 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
708 expect(video.channel.name).to.equal(attributes.channel.name)
709 expect(videoDetails.channel.host).to.equal(attributes.account.host)
710 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
711 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
712 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
713 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
714 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
716 for (const attributeFile of attributes.files) {
717 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
718 expect(file).not.to.be.undefined
720 let extension = getLowercaseExtension(attributes.fixture)
721 // Transcoding enabled: extension will always be .mp4
722 if (attributes.files.length > 1) extension = '.mp4'
724 expect(file.magnetUri).to.have.lengthOf.above(2)
726 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
727 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
729 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
730 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
733 makeRawRequest(file.torrentUrl, 200),
734 makeRawRequest(file.torrentDownloadUrl, 200),
735 makeRawRequest(file.metadataUrl, 200),
736 // Backward compatibility
737 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
740 expect(file.resolution.id).to.equal(attributeFile.resolution)
741 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
743 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
744 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
747 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
748 ).to.be.above(minSize).and.below(maxSize)
750 const torrent = await webtorrentAdd(file.magnetUri, true)
751 expect(torrent.files).to.be.an('array')
752 expect(torrent.files.length).to.equal(1)
753 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
756 expect(videoDetails.thumbnailPath).to.exist
757 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
759 if (attributes.previewfile) {
760 expect(videoDetails.previewPath).to.exist
761 await testImage(url, attributes.previewfile, videoDetails.previewPath)
765 async function videoUUIDToId (url: string, id: number | string) {
766 if (validator.isUUID('' + id) === false) return id
768 const res = await getVideo(url, id)
772 async function uploadVideoAndGetId (options: {
776 privacy?: VideoPrivacy
780 const videoAttrs: any = { name: options.videoName }
781 if (options.nsfw) videoAttrs.nsfw = options.nsfw
782 if (options.privacy) videoAttrs.privacy = options.privacy
783 if (options.fixture) videoAttrs.fixture = options.fixture
785 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
787 return res.body.video as { id: number, uuid: string, shortUUID: string }
790 async function getLocalIdByUUID (url: string, uuid: string) {
791 const res = await getVideo(url, uuid)
796 // serverNumber starts from 1
797 async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) {
798 const server = servers.find(s => s.serverNumber === serverNumber)
799 const res = await uploadRandomVideo(server, false, additionalParams)
801 await waitJobs(servers)
806 async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) {
807 const prefixName = additionalParams.prefixName || ''
808 const name = prefixName + buildUUID()
810 const data = Object.assign({ name }, additionalParams)
811 const res = await uploadVideo(server.url, server.accessToken, data)
813 if (wait) await waitJobs([ server ])
815 return { uuid: res.body.video.uuid, name }
818 // ---------------------------------------------------------------------------
830 getVideoChannelVideos,
832 getVideoFileMetadataUrl,
836 checkUploadVideoParam,
837 getVideosListPagination,
840 getVideosListWithToken,
843 getVideosWithFilters,
844 uploadRandomVideoOnServers,
851 checkVideoFilesWereRemoved,
852 getMyVideosWithFilter,
856 prepareResumableUpload
859 // ---------------------------------------------------------------------------
861 function buildUploadReq (req: request.Test, attributes: VideoAttributes) {
863 for (const key of [ 'name', 'support', 'channelId', 'description', 'originallyPublishedAt' ]) {
864 if (attributes[key] !== undefined) {
865 req.field(key, attributes[key])
869 for (const key of [ 'nsfw', 'commentsEnabled', 'downloadEnabled', 'waitTranscoding' ]) {
870 if (attributes[key] !== undefined) {
871 req.field(key, JSON.stringify(attributes[key]))
875 for (const key of [ 'language', 'privacy', 'category', 'licence' ]) {
876 if (attributes[key] !== undefined) {
877 req.field(key, attributes[key].toString())
881 const tags = attributes.tags || []
882 for (let i = 0; i < tags.length; i++) {
883 req.field('tags[' + i + ']', attributes.tags[i])
886 for (const key of [ 'thumbnailfile', 'previewfile' ]) {
887 if (attributes[key] !== undefined) {
888 req.attach(key, buildAbsoluteFixturePath(attributes[key]))
892 if (attributes.scheduleUpdate) {
893 if (attributes.scheduleUpdate.updateAt) {
894 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
897 if (attributes.scheduleUpdate.privacy) {
898 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)