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'
16 import { buildAbsoluteFixturePath, dateIsValid, testImage, wait, webtorrentAdd } from '../miscs'
17 import { makeGetRequest, makePutBodyRequest, makeRawRequest, makeUploadRequest } from '../requests/requests'
18 import { waitJobs } from '../server/jobs'
19 import { ServerInfo } from '../server/servers'
20 import { getMyUserInformation } from '../users/users'
24 type VideoAttributes = {
30 commentsEnabled?: boolean
31 downloadEnabled?: boolean
32 waitTranscoding?: boolean
34 originallyPublishedAt?: string
37 privacy?: VideoPrivacy
40 thumbnailfile?: string
44 privacy?: VideoPrivacy
48 function getVideoCategories (url: string) {
49 const path = '/api/v1/videos/categories'
51 return makeGetRequest({
54 statusCodeExpected: HttpStatusCode.OK_200
58 function getVideoLicences (url: string) {
59 const path = '/api/v1/videos/licences'
61 return makeGetRequest({
64 statusCodeExpected: HttpStatusCode.OK_200
68 function getVideoLanguages (url: string) {
69 const path = '/api/v1/videos/languages'
71 return makeGetRequest({
74 statusCodeExpected: HttpStatusCode.OK_200
78 function getVideoPrivacies (url: string) {
79 const path = '/api/v1/videos/privacies'
81 return makeGetRequest({
84 statusCodeExpected: HttpStatusCode.OK_200
88 function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
89 const path = '/api/v1/videos/' + id
93 .set('Accept', 'application/json')
94 .expect(expectedStatus)
97 async function getVideoIdFromUUID (url: string, uuid: string) {
98 const res = await getVideo(url, uuid)
103 function getVideoFileMetadataUrl (url: string) {
106 .set('Accept', 'application/json')
107 .expect(HttpStatusCode.OK_200)
108 .expect('Content-Type', /json/)
111 function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) {
112 const path = '/api/v1/videos/' + id + '/views'
114 const req = request(url)
116 .set('Accept', 'application/json')
119 req.set('X-Forwarded-For', xForwardedFor)
122 return req.expect(expectedStatus)
125 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
126 const path = '/api/v1/videos/' + id
130 .set('Authorization', 'Bearer ' + token)
131 .set('Accept', 'application/json')
132 .expect(expectedStatus)
135 function getVideoDescription (url: string, descriptionPath: string) {
137 .get(descriptionPath)
138 .set('Accept', 'application/json')
139 .expect(HttpStatusCode.OK_200)
140 .expect('Content-Type', /json/)
143 function getVideosList (url: string) {
144 const path = '/api/v1/videos'
148 .query({ sort: 'name' })
149 .set('Accept', 'application/json')
150 .expect(HttpStatusCode.OK_200)
151 .expect('Content-Type', /json/)
154 function getVideosListWithToken (url: string, token: string, query: { nsfw?: BooleanBothQuery } = {}) {
155 const path = '/api/v1/videos'
159 .set('Authorization', 'Bearer ' + token)
160 .query({ sort: 'name', ...query })
161 .set('Accept', 'application/json')
162 .expect(HttpStatusCode.OK_200)
163 .expect('Content-Type', /json/)
166 function getLocalVideos (url: string) {
167 const path = '/api/v1/videos'
171 .query({ sort: 'name', filter: 'local' })
172 .set('Accept', 'application/json')
173 .expect(HttpStatusCode.OK_200)
174 .expect('Content-Type', /json/)
177 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string, search?: string) {
178 const path = '/api/v1/users/me/videos'
180 const req = request(url)
182 .query({ start: start })
183 .query({ count: count })
184 .query({ search: search })
186 if (sort) req.query({ sort })
188 return req.set('Accept', 'application/json')
189 .set('Authorization', 'Bearer ' + accessToken)
190 .expect(HttpStatusCode.OK_200)
191 .expect('Content-Type', /json/)
194 function getMyVideosWithFilter (url: string, accessToken: string, query: { isLive?: boolean }) {
195 const path = '/api/v1/users/me/videos'
197 return makeGetRequest({
202 statusCodeExpected: HttpStatusCode.OK_200
206 function getAccountVideos (
214 nsfw?: BooleanBothQuery
218 const path = '/api/v1/accounts/' + accountName + '/videos'
220 return makeGetRequest({
223 query: { ...query, start, count, sort },
225 statusCodeExpected: HttpStatusCode.OK_200
229 function getVideoChannelVideos (
232 videoChannelName: string,
236 query: { nsfw?: BooleanBothQuery } = {}
238 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
240 return makeGetRequest({
243 query: { ...query, start, count, sort },
245 statusCodeExpected: HttpStatusCode.OK_200
249 function getVideosListPagination (url: string, start: number, count: number, sort?: string, skipCount?: boolean) {
250 const path = '/api/v1/videos'
252 const req = request(url)
254 .query({ start: start })
255 .query({ count: count })
257 if (sort) req.query({ sort })
258 if (skipCount) req.query({ skipCount })
260 return req.set('Accept', 'application/json')
261 .expect(HttpStatusCode.OK_200)
262 .expect('Content-Type', /json/)
265 function getVideosListSort (url: string, sort: string) {
266 const path = '/api/v1/videos'
270 .query({ sort: sort })
271 .set('Accept', 'application/json')
272 .expect(HttpStatusCode.OK_200)
273 .expect('Content-Type', /json/)
276 function getVideosWithFilters (url: string, query: VideosCommonQuery) {
277 const path = '/api/v1/videos'
282 .set('Accept', 'application/json')
283 .expect(HttpStatusCode.OK_200)
284 .expect('Content-Type', /json/)
287 function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
288 const path = '/api/v1/videos'
291 .delete(path + '/' + id)
292 .set('Accept', 'application/json')
293 .set('Authorization', 'Bearer ' + token)
294 .expect(expectedStatus)
297 async function removeAllVideos (server: ServerInfo) {
298 const resVideos = await getVideosList(server.url)
300 for (const v of resVideos.body.data) {
301 await removeVideo(server.url, server.accessToken, v.id)
305 async function checkVideoFilesWereRemoved (
315 join('playlists', 'hls'),
316 join('redundancy', 'hls')
319 for (const directory of directories) {
320 const directoryPath = server.serversCommand.buildDirectory(directory)
322 const directoryExists = await pathExists(directoryPath)
323 if (directoryExists === false) continue
325 const files = await readdir(directoryPath)
326 for (const file of files) {
327 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
332 async function uploadVideo (
335 videoAttributesArg: VideoAttributes,
336 specialStatus = HttpStatusCode.OK_200,
337 mode: 'legacy' | 'resumable' = 'legacy'
339 let defaultChannelId = '1'
342 const res = await getMyUserInformation(url, accessToken)
343 defaultChannelId = res.body.videoChannels[0].id
344 } catch (e) { /* empty */ }
346 // Override default attributes
347 const attributes = Object.assign({
348 name: 'my super video',
352 channelId: defaultChannelId,
354 waitTranscoding: false,
355 description: 'my super description',
356 support: 'my super support text',
358 privacy: VideoPrivacy.PUBLIC,
359 commentsEnabled: true,
360 downloadEnabled: true,
361 fixture: 'video_short.webm'
362 }, videoAttributesArg)
364 const res = mode === 'legacy'
365 ? await buildLegacyUpload(url, accessToken, attributes, specialStatus)
366 : await buildResumeUpload(url, accessToken, attributes, specialStatus)
368 // Wait torrent generation
369 if (specialStatus === HttpStatusCode.OK_200) {
370 let video: VideoDetails
372 const resVideo = await getVideoWithToken(url, accessToken, res.body.video.uuid)
373 video = resVideo.body
376 } while (!video.files[0].torrentUrl)
382 function checkUploadVideoParam (
385 attributes: Partial<VideoAttributes>,
386 specialStatus = HttpStatusCode.OK_200,
387 mode: 'legacy' | 'resumable' = 'legacy'
389 return mode === 'legacy'
390 ? buildLegacyUpload(url, token, attributes, specialStatus)
391 : buildResumeUpload(url, token, attributes, specialStatus)
394 async function buildLegacyUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
395 const path = '/api/v1/videos/upload'
396 const req = request(url)
398 .set('Accept', 'application/json')
399 .set('Authorization', 'Bearer ' + token)
401 buildUploadReq(req, attributes)
403 if (attributes.fixture !== undefined) {
404 req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
407 return req.expect(specialStatus)
410 async function buildResumeUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
412 let videoFilePath: string
413 let mimetype = 'video/mp4'
415 if (attributes.fixture) {
416 videoFilePath = buildAbsoluteFixturePath(attributes.fixture)
417 size = (await stat(videoFilePath)).size
419 if (videoFilePath.endsWith('.mkv')) {
420 mimetype = 'video/x-matroska'
421 } else if (videoFilePath.endsWith('.webm')) {
422 mimetype = 'video/webm'
426 const initializeSessionRes = await prepareResumableUpload({ url, token, attributes, size, mimetype })
427 const initStatus = initializeSessionRes.status
429 if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) {
430 const locationHeader = initializeSessionRes.header['location']
431 expect(locationHeader).to.not.be.undefined
433 const pathUploadId = locationHeader.split('?')[1]
435 return sendResumableChunks({ url, token, pathUploadId, videoFilePath, size, specialStatus })
438 const expectedInitStatus = specialStatus === HttpStatusCode.OK_200
439 ? HttpStatusCode.CREATED_201
442 expect(initStatus).to.equal(expectedInitStatus)
444 return initializeSessionRes
447 async function prepareResumableUpload (options: {
450 attributes: VideoAttributes
454 const { url, token, attributes, size, mimetype } = options
456 const path = '/api/v1/videos/upload-resumable'
458 const req = request(url)
460 .set('Authorization', 'Bearer ' + token)
461 .set('X-Upload-Content-Type', mimetype)
462 .set('X-Upload-Content-Length', size.toString())
464 buildUploadReq(req, attributes)
466 if (attributes.fixture) {
467 req.field('filename', attributes.fixture)
473 function sendResumableChunks (options: {
477 videoFilePath: string
479 specialStatus?: HttpStatusCode
480 contentLength?: number
481 contentRangeBuilder?: (start: number, chunk: any) => string
483 const { url, token, pathUploadId, videoFilePath, size, specialStatus, contentLength, contentRangeBuilder } = options
485 const expectedStatus = specialStatus || HttpStatusCode.OK_200
487 const path = '/api/v1/videos/upload-resumable'
490 const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 })
491 return new Promise<GotResponse>((resolve, reject) => {
492 readable.on('data', async function onData (chunk) {
496 'Authorization': 'Bearer ' + token,
497 'Content-Type': 'application/octet-stream',
498 'Content-Range': contentRangeBuilder
499 ? contentRangeBuilder(start, chunk)
500 : `bytes ${start}-${start + chunk.length - 1}/${size}`,
501 'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
504 const res = await got({
508 path: path + '?' + pathUploadId,
510 responseType: 'json',
511 throwHttpErrors: false
514 start += chunk.length
516 if (res.statusCode === expectedStatus) {
520 if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) {
521 readable.off('data', onData)
522 return reject(new Error('Incorrect transient behaviour sending intermediary chunks'))
530 function updateVideo (
534 attributes: VideoAttributes,
535 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
537 const path = '/api/v1/videos/' + id
540 if (attributes.name) body['name'] = attributes.name
541 if (attributes.category) body['category'] = attributes.category
542 if (attributes.licence) body['licence'] = attributes.licence
543 if (attributes.language) body['language'] = attributes.language
544 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
545 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
546 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
547 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
548 if (attributes.description) body['description'] = attributes.description
549 if (attributes.tags) body['tags'] = attributes.tags
550 if (attributes.privacy) body['privacy'] = attributes.privacy
551 if (attributes.channelId) body['channelId'] = attributes.channelId
552 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
555 if (attributes.thumbnailfile || attributes.previewfile) {
556 const attaches: any = {}
557 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
558 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
560 return makeUploadRequest({
571 return makePutBodyRequest({
580 function rateVideo (url: string, accessToken: string, id: number | string, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
581 const path = '/api/v1/videos/' + id + '/rate'
585 .set('Accept', 'application/json')
586 .set('Authorization', 'Bearer ' + accessToken)
588 .expect(specialStatus)
591 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
592 return new Promise<any>((res, rej) => {
593 const torrentName = videoUUID + '-' + resolution + '.torrent'
594 const torrentPath = server.serversCommand.buildDirectory(join('torrents', torrentName))
596 readFile(torrentPath, (err, data) => {
597 if (err) return rej(err)
599 return res(parseTorrent(data))
604 async function completeVideoCheck (
613 commentsEnabled: boolean
614 downloadEnabled: boolean
618 originallyPublishedAt?: string
640 thumbnailfile?: string
644 if (!attributes.likes) attributes.likes = 0
645 if (!attributes.dislikes) attributes.dislikes = 0
647 const host = new URL(url).host
648 const originHost = attributes.account.host
650 expect(video.name).to.equal(attributes.name)
651 expect(video.category.id).to.equal(attributes.category)
652 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
653 expect(video.licence.id).to.equal(attributes.licence)
654 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
655 expect(video.language.id).to.equal(attributes.language)
656 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
657 expect(video.privacy.id).to.deep.equal(attributes.privacy)
658 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
659 expect(video.nsfw).to.equal(attributes.nsfw)
660 expect(video.description).to.equal(attributes.description)
661 expect(video.account.id).to.be.a('number')
662 expect(video.account.host).to.equal(attributes.account.host)
663 expect(video.account.name).to.equal(attributes.account.name)
664 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
665 expect(video.channel.name).to.equal(attributes.channel.name)
666 expect(video.likes).to.equal(attributes.likes)
667 expect(video.dislikes).to.equal(attributes.dislikes)
668 expect(video.isLocal).to.equal(attributes.isLocal)
669 expect(video.duration).to.equal(attributes.duration)
670 expect(dateIsValid(video.createdAt)).to.be.true
671 expect(dateIsValid(video.publishedAt)).to.be.true
672 expect(dateIsValid(video.updatedAt)).to.be.true
674 if (attributes.publishedAt) {
675 expect(video.publishedAt).to.equal(attributes.publishedAt)
678 if (attributes.originallyPublishedAt) {
679 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
681 expect(video.originallyPublishedAt).to.be.null
684 const res = await getVideo(url, video.uuid)
685 const videoDetails: VideoDetails = res.body
687 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
688 expect(videoDetails.tags).to.deep.equal(attributes.tags)
689 expect(videoDetails.account.name).to.equal(attributes.account.name)
690 expect(videoDetails.account.host).to.equal(attributes.account.host)
691 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
692 expect(video.channel.name).to.equal(attributes.channel.name)
693 expect(videoDetails.channel.host).to.equal(attributes.account.host)
694 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
695 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
696 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
697 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
698 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
700 for (const attributeFile of attributes.files) {
701 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
702 expect(file).not.to.be.undefined
704 let extension = getLowercaseExtension(attributes.fixture)
705 // Transcoding enabled: extension will always be .mp4
706 if (attributes.files.length > 1) extension = '.mp4'
708 expect(file.magnetUri).to.have.lengthOf.above(2)
710 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
711 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
713 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
714 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
717 makeRawRequest(file.torrentUrl, 200),
718 makeRawRequest(file.torrentDownloadUrl, 200),
719 makeRawRequest(file.metadataUrl, 200),
720 // Backward compatibility
721 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
724 expect(file.resolution.id).to.equal(attributeFile.resolution)
725 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
727 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
728 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
731 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
732 ).to.be.above(minSize).and.below(maxSize)
734 const torrent = await webtorrentAdd(file.magnetUri, true)
735 expect(torrent.files).to.be.an('array')
736 expect(torrent.files.length).to.equal(1)
737 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
740 expect(videoDetails.thumbnailPath).to.exist
741 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
743 if (attributes.previewfile) {
744 expect(videoDetails.previewPath).to.exist
745 await testImage(url, attributes.previewfile, videoDetails.previewPath)
749 async function videoUUIDToId (url: string, id: number | string) {
750 if (validator.isUUID('' + id) === false) return id
752 const res = await getVideo(url, id)
756 async function uploadVideoAndGetId (options: {
760 privacy?: VideoPrivacy
764 const videoAttrs: any = { name: options.videoName }
765 if (options.nsfw) videoAttrs.nsfw = options.nsfw
766 if (options.privacy) videoAttrs.privacy = options.privacy
767 if (options.fixture) videoAttrs.fixture = options.fixture
769 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
771 return res.body.video as { id: number, uuid: string, shortUUID: string }
774 async function getLocalIdByUUID (url: string, uuid: string) {
775 const res = await getVideo(url, uuid)
780 // serverNumber starts from 1
781 async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) {
782 const server = servers.find(s => s.serverNumber === serverNumber)
783 const res = await uploadRandomVideo(server, false, additionalParams)
785 await waitJobs(servers)
790 async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) {
791 const prefixName = additionalParams.prefixName || ''
792 const name = prefixName + buildUUID()
794 const data = Object.assign({ name }, additionalParams)
795 const res = await uploadVideo(server.url, server.accessToken, data)
797 if (wait) await waitJobs([ server ])
799 return { uuid: res.body.video.uuid, name }
802 // ---------------------------------------------------------------------------
814 getVideoChannelVideos,
816 getVideoFileMetadataUrl,
820 checkUploadVideoParam,
821 getVideosListPagination,
824 getVideosListWithToken,
827 getVideosWithFilters,
828 uploadRandomVideoOnServers,
835 checkVideoFilesWereRemoved,
836 getMyVideosWithFilter,
840 prepareResumableUpload
843 // ---------------------------------------------------------------------------
845 function buildUploadReq (req: request.Test, attributes: VideoAttributes) {
847 for (const key of [ 'name', 'support', 'channelId', 'description', 'originallyPublishedAt' ]) {
848 if (attributes[key] !== undefined) {
849 req.field(key, attributes[key])
853 for (const key of [ 'nsfw', 'commentsEnabled', 'downloadEnabled', 'waitTranscoding' ]) {
854 if (attributes[key] !== undefined) {
855 req.field(key, JSON.stringify(attributes[key]))
859 for (const key of [ 'language', 'privacy', 'category', 'licence' ]) {
860 if (attributes[key] !== undefined) {
861 req.field(key, attributes[key].toString())
865 const tags = attributes.tags || []
866 for (let i = 0; i < tags.length; i++) {
867 req.field('tags[' + i + ']', attributes.tags[i])
870 for (const key of [ 'thumbnailfile', 'previewfile' ]) {
871 if (attributes[key] !== undefined) {
872 req.attach(key, buildAbsoluteFixturePath(attributes[key]))
876 if (attributes.scheduleUpdate) {
877 if (attributes.scheduleUpdate.updateAt) {
878 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
881 if (attributes.scheduleUpdate.privacy) {
882 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)