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 { extname, join } from 'path'
8 import * as request from 'supertest'
9 import { v4 as uuidv4 } from 'uuid'
10 import validator from 'validator'
11 import { HttpStatusCode } from '@shared/core-utils'
12 import { VideosCommonQuery } from '@shared/models'
13 import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
14 import { VideoDetails, VideoPrivacy } from '../../models/videos'
16 buildAbsoluteFixturePath,
23 } from '../miscs/miscs'
24 import { makeGetRequest, makePutBodyRequest, makeRawRequest, makeUploadRequest } from '../requests/requests'
25 import { waitJobs } from '../server/jobs'
26 import { ServerInfo } from '../server/servers'
27 import { getMyUserInformation } from '../users/users'
31 type VideoAttributes = {
37 commentsEnabled?: boolean
38 downloadEnabled?: boolean
39 waitTranscoding?: boolean
41 originallyPublishedAt?: string
44 privacy?: VideoPrivacy
47 thumbnailfile?: string
51 privacy?: VideoPrivacy
55 function getVideoCategories (url: string) {
56 const path = '/api/v1/videos/categories'
58 return makeGetRequest({
61 statusCodeExpected: HttpStatusCode.OK_200
65 function getVideoLicences (url: string) {
66 const path = '/api/v1/videos/licences'
68 return makeGetRequest({
71 statusCodeExpected: HttpStatusCode.OK_200
75 function getVideoLanguages (url: string) {
76 const path = '/api/v1/videos/languages'
78 return makeGetRequest({
81 statusCodeExpected: HttpStatusCode.OK_200
85 function getVideoPrivacies (url: string) {
86 const path = '/api/v1/videos/privacies'
88 return makeGetRequest({
91 statusCodeExpected: HttpStatusCode.OK_200
95 function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
96 const path = '/api/v1/videos/' + id
100 .set('Accept', 'application/json')
101 .expect(expectedStatus)
104 async function getVideoIdFromUUID (url: string, uuid: string) {
105 const res = await getVideo(url, uuid)
110 function getVideoFileMetadataUrl (url: string) {
113 .set('Accept', 'application/json')
114 .expect(HttpStatusCode.OK_200)
115 .expect('Content-Type', /json/)
118 function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) {
119 const path = '/api/v1/videos/' + id + '/views'
121 const req = request(url)
123 .set('Accept', 'application/json')
126 req.set('X-Forwarded-For', xForwardedFor)
129 return req.expect(expectedStatus)
132 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
133 const path = '/api/v1/videos/' + id
137 .set('Authorization', 'Bearer ' + token)
138 .set('Accept', 'application/json')
139 .expect(expectedStatus)
142 function getVideoDescription (url: string, descriptionPath: string) {
144 .get(descriptionPath)
145 .set('Accept', 'application/json')
146 .expect(HttpStatusCode.OK_200)
147 .expect('Content-Type', /json/)
150 function getVideosList (url: string) {
151 const path = '/api/v1/videos'
155 .query({ sort: 'name' })
156 .set('Accept', 'application/json')
157 .expect(HttpStatusCode.OK_200)
158 .expect('Content-Type', /json/)
161 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
162 const path = '/api/v1/videos'
166 .set('Authorization', 'Bearer ' + token)
167 .query(immutableAssign(query, { sort: 'name' }))
168 .set('Accept', 'application/json')
169 .expect(HttpStatusCode.OK_200)
170 .expect('Content-Type', /json/)
173 function getLocalVideos (url: string) {
174 const path = '/api/v1/videos'
178 .query({ sort: 'name', filter: 'local' })
179 .set('Accept', 'application/json')
180 .expect(HttpStatusCode.OK_200)
181 .expect('Content-Type', /json/)
184 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string, search?: string) {
185 const path = '/api/v1/users/me/videos'
187 const req = request(url)
189 .query({ start: start })
190 .query({ count: count })
191 .query({ search: search })
193 if (sort) req.query({ sort })
195 return req.set('Accept', 'application/json')
196 .set('Authorization', 'Bearer ' + accessToken)
197 .expect(HttpStatusCode.OK_200)
198 .expect('Content-Type', /json/)
201 function getMyVideosWithFilter (url: string, accessToken: string, query: { isLive?: boolean }) {
202 const path = '/api/v1/users/me/videos'
204 return makeGetRequest({
209 statusCodeExpected: HttpStatusCode.OK_200
213 function getAccountVideos (
225 const path = '/api/v1/accounts/' + accountName + '/videos'
227 return makeGetRequest({
230 query: immutableAssign(query, {
236 statusCodeExpected: HttpStatusCode.OK_200
240 function getVideoChannelVideos (
243 videoChannelName: string,
247 query: { nsfw?: boolean } = {}
249 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
251 return makeGetRequest({
254 query: immutableAssign(query, {
260 statusCodeExpected: HttpStatusCode.OK_200
264 function getPlaylistVideos (
267 playlistId: number | string,
270 query: { nsfw?: boolean } = {}
272 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
274 return makeGetRequest({
277 query: immutableAssign(query, {
282 statusCodeExpected: HttpStatusCode.OK_200
286 function getVideosListPagination (url: string, start: number, count: number, sort?: string, skipCount?: boolean) {
287 const path = '/api/v1/videos'
289 const req = request(url)
291 .query({ start: start })
292 .query({ count: count })
294 if (sort) req.query({ sort })
295 if (skipCount) req.query({ skipCount })
297 return req.set('Accept', 'application/json')
298 .expect(HttpStatusCode.OK_200)
299 .expect('Content-Type', /json/)
302 function getVideosListSort (url: string, sort: string) {
303 const path = '/api/v1/videos'
307 .query({ sort: sort })
308 .set('Accept', 'application/json')
309 .expect(HttpStatusCode.OK_200)
310 .expect('Content-Type', /json/)
313 function getVideosWithFilters (url: string, query: VideosCommonQuery) {
314 const path = '/api/v1/videos'
319 .set('Accept', 'application/json')
320 .expect(HttpStatusCode.OK_200)
321 .expect('Content-Type', /json/)
324 function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
325 const path = '/api/v1/videos'
328 .delete(path + '/' + id)
329 .set('Accept', 'application/json')
330 .set('Authorization', 'Bearer ' + token)
331 .expect(expectedStatus)
334 async function removeAllVideos (server: ServerInfo) {
335 const resVideos = await getVideosList(server.url)
337 for (const v of resVideos.body.data) {
338 await removeVideo(server.url, server.accessToken, v.id)
342 async function checkVideoFilesWereRemoved (
344 serverNumber: number,
352 join('playlists', 'hls'),
353 join('redundancy', 'hls')
356 for (const directory of directories) {
357 const directoryPath = buildServerDirectory({ internalServerNumber: serverNumber }, directory)
359 const directoryExists = await pathExists(directoryPath)
360 if (directoryExists === false) continue
362 const files = await readdir(directoryPath)
363 for (const file of files) {
364 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
369 async function uploadVideo (
372 videoAttributesArg: VideoAttributes,
373 specialStatus = HttpStatusCode.OK_200,
374 mode: 'legacy' | 'resumable' = 'legacy'
376 let defaultChannelId = '1'
379 const res = await getMyUserInformation(url, accessToken)
380 defaultChannelId = res.body.videoChannels[0].id
381 } catch (e) { /* empty */ }
383 // Override default attributes
384 const attributes = Object.assign({
385 name: 'my super video',
389 channelId: defaultChannelId,
391 waitTranscoding: false,
392 description: 'my super description',
393 support: 'my super support text',
395 privacy: VideoPrivacy.PUBLIC,
396 commentsEnabled: true,
397 downloadEnabled: true,
398 fixture: 'video_short.webm'
399 }, videoAttributesArg)
401 const res = mode === 'legacy'
402 ? await buildLegacyUpload(url, accessToken, attributes, specialStatus)
403 : await buildResumeUpload(url, accessToken, attributes, specialStatus)
405 // Wait torrent generation
406 if (specialStatus === HttpStatusCode.OK_200) {
407 let video: VideoDetails
409 const resVideo = await getVideoWithToken(url, accessToken, res.body.video.uuid)
410 video = resVideo.body
413 } while (!video.files[0].torrentUrl)
419 function checkUploadVideoParam (
422 attributes: Partial<VideoAttributes>,
423 specialStatus = HttpStatusCode.OK_200,
424 mode: 'legacy' | 'resumable' = 'legacy'
426 return mode === 'legacy'
427 ? buildLegacyUpload(url, token, attributes, specialStatus)
428 : buildResumeUpload(url, token, attributes, specialStatus)
431 async function buildLegacyUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
432 const path = '/api/v1/videos/upload'
433 const req = request(url)
435 .set('Accept', 'application/json')
436 .set('Authorization', 'Bearer ' + token)
438 buildUploadReq(req, attributes)
440 if (attributes.fixture !== undefined) {
441 req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
444 return req.expect(specialStatus)
447 async function buildResumeUpload (url: string, token: string, attributes: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
449 let videoFilePath: string
450 let mimetype = 'video/mp4'
452 if (attributes.fixture) {
453 videoFilePath = buildAbsoluteFixturePath(attributes.fixture)
454 size = (await stat(videoFilePath)).size
456 if (videoFilePath.endsWith('.mkv')) {
457 mimetype = 'video/x-matroska'
458 } else if (videoFilePath.endsWith('.webm')) {
459 mimetype = 'video/webm'
463 const initializeSessionRes = await prepareResumableUpload({ url, token, attributes, size, mimetype })
464 const initStatus = initializeSessionRes.status
466 if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) {
467 const locationHeader = initializeSessionRes.header['location']
468 expect(locationHeader).to.not.be.undefined
470 const pathUploadId = locationHeader.split('?')[1]
472 return sendResumableChunks({ url, token, pathUploadId, videoFilePath, size, specialStatus })
475 const expectedInitStatus = specialStatus === HttpStatusCode.OK_200
476 ? HttpStatusCode.CREATED_201
479 expect(initStatus).to.equal(expectedInitStatus)
481 return initializeSessionRes
484 async function prepareResumableUpload (options: {
487 attributes: VideoAttributes
491 const { url, token, attributes, size, mimetype } = options
493 const path = '/api/v1/videos/upload-resumable'
495 const req = request(url)
497 .set('Authorization', 'Bearer ' + token)
498 .set('X-Upload-Content-Type', mimetype)
499 .set('X-Upload-Content-Length', size.toString())
501 buildUploadReq(req, attributes)
503 if (attributes.fixture) {
504 req.field('filename', attributes.fixture)
510 function sendResumableChunks (options: {
514 videoFilePath: string
516 specialStatus?: HttpStatusCode
517 contentLength?: number
518 contentRangeBuilder?: (start: number, chunk: any) => string
520 const { url, token, pathUploadId, videoFilePath, size, specialStatus, contentLength, contentRangeBuilder } = options
522 const expectedStatus = specialStatus || HttpStatusCode.OK_200
524 const path = '/api/v1/videos/upload-resumable'
527 const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 })
528 return new Promise<GotResponse>((resolve, reject) => {
529 readable.on('data', async function onData (chunk) {
533 'Authorization': 'Bearer ' + token,
534 'Content-Type': 'application/octet-stream',
535 'Content-Range': contentRangeBuilder
536 ? contentRangeBuilder(start, chunk)
537 : `bytes ${start}-${start + chunk.length - 1}/${size}`,
538 'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
541 const res = await got({
545 path: path + '?' + pathUploadId,
547 responseType: 'json',
548 throwHttpErrors: false
551 start += chunk.length
553 if (res.statusCode === expectedStatus) {
557 if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) {
558 readable.off('data', onData)
559 return reject(new Error('Incorrect transient behaviour sending intermediary chunks'))
567 function updateVideo (
571 attributes: VideoAttributes,
572 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
574 const path = '/api/v1/videos/' + id
577 if (attributes.name) body['name'] = attributes.name
578 if (attributes.category) body['category'] = attributes.category
579 if (attributes.licence) body['licence'] = attributes.licence
580 if (attributes.language) body['language'] = attributes.language
581 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
582 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
583 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
584 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
585 if (attributes.description) body['description'] = attributes.description
586 if (attributes.tags) body['tags'] = attributes.tags
587 if (attributes.privacy) body['privacy'] = attributes.privacy
588 if (attributes.channelId) body['channelId'] = attributes.channelId
589 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
592 if (attributes.thumbnailfile || attributes.previewfile) {
593 const attaches: any = {}
594 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
595 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
597 return makeUploadRequest({
608 return makePutBodyRequest({
617 function rateVideo (url: string, accessToken: string, id: number | string, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
618 const path = '/api/v1/videos/' + id + '/rate'
622 .set('Accept', 'application/json')
623 .set('Authorization', 'Bearer ' + accessToken)
625 .expect(specialStatus)
628 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
629 return new Promise<any>((res, rej) => {
630 const torrentName = videoUUID + '-' + resolution + '.torrent'
631 const torrentPath = buildServerDirectory(server, join('torrents', torrentName))
633 readFile(torrentPath, (err, data) => {
634 if (err) return rej(err)
636 return res(parseTorrent(data))
641 async function completeVideoCheck (
650 commentsEnabled: boolean
651 downloadEnabled: boolean
655 originallyPublishedAt?: string
677 thumbnailfile?: string
681 if (!attributes.likes) attributes.likes = 0
682 if (!attributes.dislikes) attributes.dislikes = 0
684 const host = new URL(url).host
685 const originHost = attributes.account.host
687 expect(video.name).to.equal(attributes.name)
688 expect(video.category.id).to.equal(attributes.category)
689 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
690 expect(video.licence.id).to.equal(attributes.licence)
691 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
692 expect(video.language.id).to.equal(attributes.language)
693 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
694 expect(video.privacy.id).to.deep.equal(attributes.privacy)
695 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
696 expect(video.nsfw).to.equal(attributes.nsfw)
697 expect(video.description).to.equal(attributes.description)
698 expect(video.account.id).to.be.a('number')
699 expect(video.account.host).to.equal(attributes.account.host)
700 expect(video.account.name).to.equal(attributes.account.name)
701 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
702 expect(video.channel.name).to.equal(attributes.channel.name)
703 expect(video.likes).to.equal(attributes.likes)
704 expect(video.dislikes).to.equal(attributes.dislikes)
705 expect(video.isLocal).to.equal(attributes.isLocal)
706 expect(video.duration).to.equal(attributes.duration)
707 expect(dateIsValid(video.createdAt)).to.be.true
708 expect(dateIsValid(video.publishedAt)).to.be.true
709 expect(dateIsValid(video.updatedAt)).to.be.true
711 if (attributes.publishedAt) {
712 expect(video.publishedAt).to.equal(attributes.publishedAt)
715 if (attributes.originallyPublishedAt) {
716 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
718 expect(video.originallyPublishedAt).to.be.null
721 const res = await getVideo(url, video.uuid)
722 const videoDetails: VideoDetails = res.body
724 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
725 expect(videoDetails.tags).to.deep.equal(attributes.tags)
726 expect(videoDetails.account.name).to.equal(attributes.account.name)
727 expect(videoDetails.account.host).to.equal(attributes.account.host)
728 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
729 expect(video.channel.name).to.equal(attributes.channel.name)
730 expect(videoDetails.channel.host).to.equal(attributes.account.host)
731 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
732 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
733 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
734 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
735 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
737 for (const attributeFile of attributes.files) {
738 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
739 expect(file).not.to.be.undefined
741 let extension = extname(attributes.fixture)
742 // Transcoding enabled: extension will always be .mp4
743 if (attributes.files.length > 1) extension = '.mp4'
745 expect(file.magnetUri).to.have.lengthOf.above(2)
747 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
748 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
750 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
751 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
754 makeRawRequest(file.torrentUrl, 200),
755 makeRawRequest(file.torrentDownloadUrl, 200),
756 makeRawRequest(file.metadataUrl, 200),
757 // Backward compatibility
758 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
761 expect(file.resolution.id).to.equal(attributeFile.resolution)
762 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
764 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
765 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
768 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
769 ).to.be.above(minSize).and.below(maxSize)
771 const torrent = await webtorrentAdd(file.magnetUri, true)
772 expect(torrent.files).to.be.an('array')
773 expect(torrent.files.length).to.equal(1)
774 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
777 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
779 if (attributes.previewfile) {
780 await testImage(url, attributes.previewfile, videoDetails.previewPath)
784 async function videoUUIDToId (url: string, id: number | string) {
785 if (validator.isUUID('' + id) === false) return id
787 const res = await getVideo(url, id)
791 async function uploadVideoAndGetId (options: {
795 privacy?: VideoPrivacy
799 const videoAttrs: any = { name: options.videoName }
800 if (options.nsfw) videoAttrs.nsfw = options.nsfw
801 if (options.privacy) videoAttrs.privacy = options.privacy
802 if (options.fixture) videoAttrs.fixture = options.fixture
804 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
806 return { id: res.body.video.id, uuid: res.body.video.uuid }
809 async function getLocalIdByUUID (url: string, uuid: string) {
810 const res = await getVideo(url, uuid)
815 // serverNumber starts from 1
816 async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) {
817 const server = servers.find(s => s.serverNumber === serverNumber)
818 const res = await uploadRandomVideo(server, false, additionalParams)
820 await waitJobs(servers)
825 async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) {
826 const prefixName = additionalParams.prefixName || ''
827 const name = prefixName + uuidv4()
829 const data = Object.assign({ name }, additionalParams)
830 const res = await uploadVideo(server.url, server.accessToken, data)
832 if (wait) await waitJobs([ server ])
834 return { uuid: res.body.video.uuid, name }
837 // ---------------------------------------------------------------------------
849 getVideoChannelVideos,
851 getVideoFileMetadataUrl,
855 checkUploadVideoParam,
856 getVideosListPagination,
859 getVideosListWithToken,
862 getVideosWithFilters,
863 uploadRandomVideoOnServers,
870 checkVideoFilesWereRemoved,
872 getMyVideosWithFilter,
876 prepareResumableUpload
879 // ---------------------------------------------------------------------------
881 function buildUploadReq (req: request.Test, attributes: VideoAttributes) {
883 for (const key of [ 'name', 'support', 'channelId', 'description', 'originallyPublishedAt' ]) {
884 if (attributes[key] !== undefined) {
885 req.field(key, attributes[key])
889 for (const key of [ 'nsfw', 'commentsEnabled', 'downloadEnabled', 'waitTranscoding' ]) {
890 if (attributes[key] !== undefined) {
891 req.field(key, JSON.stringify(attributes[key]))
895 for (const key of [ 'language', 'privacy', 'category', 'licence' ]) {
896 if (attributes[key] !== undefined) {
897 req.field(key, attributes[key].toString())
901 const tags = attributes.tags || []
902 for (let i = 0; i < tags.length; i++) {
903 req.field('tags[' + i + ']', attributes.tags[i])
906 for (const key of [ 'thumbnailfile', 'previewfile' ]) {
907 if (attributes[key] !== undefined) {
908 req.attach(key, buildAbsoluteFixturePath(attributes[key]))
912 if (attributes.scheduleUpdate) {
913 if (attributes.scheduleUpdate.updateAt) {
914 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
917 if (attributes.scheduleUpdate.privacy) {
918 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)