1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { pathExists, readdir, readFile } from 'fs-extra'
5 import * as parseTorrent from 'parse-torrent'
6 import { extname, join } from 'path'
7 import * as request from 'supertest'
8 import { v4 as uuidv4 } from 'uuid'
9 import validator from 'validator'
10 import { HttpStatusCode } from '@shared/core-utils'
11 import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
12 import { VideoDetails, VideoPrivacy } from '../../models/videos'
14 buildAbsoluteFixturePath,
21 } from '../miscs/miscs'
22 import { makeGetRequest, makePutBodyRequest, makeRawRequest, makeUploadRequest } from '../requests/requests'
23 import { waitJobs } from '../server/jobs'
24 import { ServerInfo } from '../server/servers'
25 import { getMyUserInformation } from '../users/users'
29 type VideoAttributes = {
35 commentsEnabled?: boolean
36 downloadEnabled?: boolean
37 waitTranscoding?: boolean
39 originallyPublishedAt?: string
42 privacy?: VideoPrivacy
44 thumbnailfile?: string
48 privacy?: VideoPrivacy
52 function getVideoCategories (url: string) {
53 const path = '/api/v1/videos/categories'
55 return makeGetRequest({
58 statusCodeExpected: HttpStatusCode.OK_200
62 function getVideoLicences (url: string) {
63 const path = '/api/v1/videos/licences'
65 return makeGetRequest({
68 statusCodeExpected: HttpStatusCode.OK_200
72 function getVideoLanguages (url: string) {
73 const path = '/api/v1/videos/languages'
75 return makeGetRequest({
78 statusCodeExpected: HttpStatusCode.OK_200
82 function getVideoPrivacies (url: string) {
83 const path = '/api/v1/videos/privacies'
85 return makeGetRequest({
88 statusCodeExpected: HttpStatusCode.OK_200
92 function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
93 const path = '/api/v1/videos/' + id
97 .set('Accept', 'application/json')
98 .expect(expectedStatus)
101 async function getVideoIdFromUUID (url: string, uuid: string) {
102 const res = await getVideo(url, uuid)
107 function getVideoFileMetadataUrl (url: string) {
110 .set('Accept', 'application/json')
111 .expect(HttpStatusCode.OK_200)
112 .expect('Content-Type', /json/)
115 function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) {
116 const path = '/api/v1/videos/' + id + '/views'
118 const req = request(url)
120 .set('Accept', 'application/json')
123 req.set('X-Forwarded-For', xForwardedFor)
126 return req.expect(expectedStatus)
129 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) {
130 const path = '/api/v1/videos/' + id
134 .set('Authorization', 'Bearer ' + token)
135 .set('Accept', 'application/json')
136 .expect(expectedStatus)
139 function getVideoDescription (url: string, descriptionPath: string) {
141 .get(descriptionPath)
142 .set('Accept', 'application/json')
143 .expect(HttpStatusCode.OK_200)
144 .expect('Content-Type', /json/)
147 function getVideosList (url: string) {
148 const path = '/api/v1/videos'
152 .query({ sort: 'name' })
153 .set('Accept', 'application/json')
154 .expect(HttpStatusCode.OK_200)
155 .expect('Content-Type', /json/)
158 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
159 const path = '/api/v1/videos'
163 .set('Authorization', 'Bearer ' + token)
164 .query(immutableAssign(query, { sort: 'name' }))
165 .set('Accept', 'application/json')
166 .expect(HttpStatusCode.OK_200)
167 .expect('Content-Type', /json/)
170 function getLocalVideos (url: string) {
171 const path = '/api/v1/videos'
175 .query({ sort: 'name', filter: 'local' })
176 .set('Accept', 'application/json')
177 .expect(HttpStatusCode.OK_200)
178 .expect('Content-Type', /json/)
181 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string, search?: string) {
182 const path = '/api/v1/users/me/videos'
184 const req = request(url)
186 .query({ start: start })
187 .query({ count: count })
188 .query({ search: search })
190 if (sort) req.query({ sort })
192 return req.set('Accept', 'application/json')
193 .set('Authorization', 'Bearer ' + accessToken)
194 .expect(HttpStatusCode.OK_200)
195 .expect('Content-Type', /json/)
198 function getAccountVideos (
210 const path = '/api/v1/accounts/' + accountName + '/videos'
212 return makeGetRequest({
215 query: immutableAssign(query, {
221 statusCodeExpected: HttpStatusCode.OK_200
225 function getVideoChannelVideos (
228 videoChannelName: string,
232 query: { nsfw?: boolean } = {}
234 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
236 return makeGetRequest({
239 query: immutableAssign(query, {
245 statusCodeExpected: HttpStatusCode.OK_200
249 function getPlaylistVideos (
252 playlistId: number | string,
255 query: { nsfw?: boolean } = {}
257 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
259 return makeGetRequest({
262 query: immutableAssign(query, {
267 statusCodeExpected: HttpStatusCode.OK_200
271 function getVideosListPagination (url: string, start: number, count: number, sort?: string, skipCount?: boolean) {
272 const path = '/api/v1/videos'
274 const req = request(url)
276 .query({ start: start })
277 .query({ count: count })
279 if (sort) req.query({ sort })
280 if (skipCount) req.query({ skipCount })
282 return req.set('Accept', 'application/json')
283 .expect(HttpStatusCode.OK_200)
284 .expect('Content-Type', /json/)
287 function getVideosListSort (url: string, sort: string) {
288 const path = '/api/v1/videos'
292 .query({ sort: sort })
293 .set('Accept', 'application/json')
294 .expect(HttpStatusCode.OK_200)
295 .expect('Content-Type', /json/)
298 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
299 const path = '/api/v1/videos'
304 .set('Accept', 'application/json')
305 .expect(HttpStatusCode.OK_200)
306 .expect('Content-Type', /json/)
309 function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
310 const path = '/api/v1/videos'
313 .delete(path + '/' + id)
314 .set('Accept', 'application/json')
315 .set('Authorization', 'Bearer ' + token)
316 .expect(expectedStatus)
319 async function removeAllVideos (server: ServerInfo) {
320 const resVideos = await getVideosList(server.url)
322 for (const v of resVideos.body.data) {
323 await removeVideo(server.url, server.accessToken, v.id)
327 async function checkVideoFilesWereRemoved (
329 serverNumber: number,
337 join('playlists', 'hls'),
338 join('redundancy', 'hls')
341 for (const directory of directories) {
342 const directoryPath = buildServerDirectory({ internalServerNumber: serverNumber }, directory)
344 const directoryExists = await pathExists(directoryPath)
345 if (directoryExists === false) continue
347 const files = await readdir(directoryPath)
348 for (const file of files) {
349 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
354 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = HttpStatusCode.OK_200) {
355 const path = '/api/v1/videos/upload'
356 let defaultChannelId = '1'
359 const res = await getMyUserInformation(url, accessToken)
360 defaultChannelId = res.body.videoChannels[0].id
361 } catch (e) { /* empty */ }
363 // Override default attributes
364 const attributes = Object.assign({
365 name: 'my super video',
369 channelId: defaultChannelId,
371 waitTranscoding: false,
372 description: 'my super description',
373 support: 'my super support text',
375 privacy: VideoPrivacy.PUBLIC,
376 commentsEnabled: true,
377 downloadEnabled: true,
378 fixture: 'video_short.webm'
379 }, videoAttributesArg)
381 const req = request(url)
383 .set('Accept', 'application/json')
384 .set('Authorization', 'Bearer ' + accessToken)
385 .field('name', attributes.name)
386 .field('nsfw', JSON.stringify(attributes.nsfw))
387 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
388 .field('downloadEnabled', JSON.stringify(attributes.downloadEnabled))
389 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
390 .field('privacy', attributes.privacy.toString())
391 .field('channelId', attributes.channelId)
393 if (attributes.support !== undefined) {
394 req.field('support', attributes.support)
397 if (attributes.description !== undefined) {
398 req.field('description', attributes.description)
400 if (attributes.language !== undefined) {
401 req.field('language', attributes.language.toString())
403 if (attributes.category !== undefined) {
404 req.field('category', attributes.category.toString())
406 if (attributes.licence !== undefined) {
407 req.field('licence', attributes.licence.toString())
410 const tags = attributes.tags || []
411 for (let i = 0; i < tags.length; i++) {
412 req.field('tags[' + i + ']', attributes.tags[i])
415 if (attributes.thumbnailfile !== undefined) {
416 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
418 if (attributes.previewfile !== undefined) {
419 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
422 if (attributes.scheduleUpdate) {
423 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
425 if (attributes.scheduleUpdate.privacy) {
426 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
430 if (attributes.originallyPublishedAt !== undefined) {
431 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
434 const res = await req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
435 .expect(specialStatus)
437 // Wait torrent generation
438 if (specialStatus === HttpStatusCode.OK_200) {
439 let video: VideoDetails
441 const resVideo = await getVideoWithToken(url, accessToken, res.body.video.uuid)
442 video = resVideo.body
445 } while (!video.files[0].torrentUrl)
451 function updateVideo (
455 attributes: VideoAttributes,
456 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
458 const path = '/api/v1/videos/' + id
461 if (attributes.name) body['name'] = attributes.name
462 if (attributes.category) body['category'] = attributes.category
463 if (attributes.licence) body['licence'] = attributes.licence
464 if (attributes.language) body['language'] = attributes.language
465 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
466 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
467 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
468 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
469 if (attributes.description) body['description'] = attributes.description
470 if (attributes.tags) body['tags'] = attributes.tags
471 if (attributes.privacy) body['privacy'] = attributes.privacy
472 if (attributes.channelId) body['channelId'] = attributes.channelId
473 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
476 if (attributes.thumbnailfile || attributes.previewfile) {
477 const attaches: any = {}
478 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
479 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
481 return makeUploadRequest({
492 return makePutBodyRequest({
501 function rateVideo (url: string, accessToken: string, id: number | string, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
502 const path = '/api/v1/videos/' + id + '/rate'
506 .set('Accept', 'application/json')
507 .set('Authorization', 'Bearer ' + accessToken)
509 .expect(specialStatus)
512 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
513 return new Promise<any>((res, rej) => {
514 const torrentName = videoUUID + '-' + resolution + '.torrent'
515 const torrentPath = buildServerDirectory(server, join('torrents', torrentName))
517 readFile(torrentPath, (err, data) => {
518 if (err) return rej(err)
520 return res(parseTorrent(data))
525 async function completeVideoCheck (
534 commentsEnabled: boolean
535 downloadEnabled: boolean
539 originallyPublishedAt?: string
561 thumbnailfile?: string
565 if (!attributes.likes) attributes.likes = 0
566 if (!attributes.dislikes) attributes.dislikes = 0
568 const host = new URL(url).host
569 const originHost = attributes.account.host
571 expect(video.name).to.equal(attributes.name)
572 expect(video.category.id).to.equal(attributes.category)
573 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
574 expect(video.licence.id).to.equal(attributes.licence)
575 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
576 expect(video.language.id).to.equal(attributes.language)
577 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
578 expect(video.privacy.id).to.deep.equal(attributes.privacy)
579 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
580 expect(video.nsfw).to.equal(attributes.nsfw)
581 expect(video.description).to.equal(attributes.description)
582 expect(video.account.id).to.be.a('number')
583 expect(video.account.host).to.equal(attributes.account.host)
584 expect(video.account.name).to.equal(attributes.account.name)
585 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
586 expect(video.channel.name).to.equal(attributes.channel.name)
587 expect(video.likes).to.equal(attributes.likes)
588 expect(video.dislikes).to.equal(attributes.dislikes)
589 expect(video.isLocal).to.equal(attributes.isLocal)
590 expect(video.duration).to.equal(attributes.duration)
591 expect(dateIsValid(video.createdAt)).to.be.true
592 expect(dateIsValid(video.publishedAt)).to.be.true
593 expect(dateIsValid(video.updatedAt)).to.be.true
595 if (attributes.publishedAt) {
596 expect(video.publishedAt).to.equal(attributes.publishedAt)
599 if (attributes.originallyPublishedAt) {
600 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
602 expect(video.originallyPublishedAt).to.be.null
605 const res = await getVideo(url, video.uuid)
606 const videoDetails: VideoDetails = res.body
608 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
609 expect(videoDetails.tags).to.deep.equal(attributes.tags)
610 expect(videoDetails.account.name).to.equal(attributes.account.name)
611 expect(videoDetails.account.host).to.equal(attributes.account.host)
612 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
613 expect(video.channel.name).to.equal(attributes.channel.name)
614 expect(videoDetails.channel.host).to.equal(attributes.account.host)
615 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
616 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
617 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
618 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
619 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
621 for (const attributeFile of attributes.files) {
622 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
623 expect(file).not.to.be.undefined
625 let extension = extname(attributes.fixture)
626 // Transcoding enabled: extension will always be .mp4
627 if (attributes.files.length > 1) extension = '.mp4'
629 expect(file.magnetUri).to.have.lengthOf.above(2)
631 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
632 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
634 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
635 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
638 makeRawRequest(file.torrentUrl, 200),
639 makeRawRequest(file.torrentDownloadUrl, 200),
640 makeRawRequest(file.metadataUrl, 200),
641 // Backward compatibility
642 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
645 expect(file.resolution.id).to.equal(attributeFile.resolution)
646 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
648 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
649 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
652 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
653 ).to.be.above(minSize).and.below(maxSize)
655 const torrent = await webtorrentAdd(file.magnetUri, true)
656 expect(torrent.files).to.be.an('array')
657 expect(torrent.files.length).to.equal(1)
658 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
661 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
663 if (attributes.previewfile) {
664 await testImage(url, attributes.previewfile, videoDetails.previewPath)
668 async function videoUUIDToId (url: string, id: number | string) {
669 if (validator.isUUID('' + id) === false) return id
671 const res = await getVideo(url, id)
675 async function uploadVideoAndGetId (options: {
679 privacy?: VideoPrivacy
683 const videoAttrs: any = { name: options.videoName }
684 if (options.nsfw) videoAttrs.nsfw = options.nsfw
685 if (options.privacy) videoAttrs.privacy = options.privacy
686 if (options.fixture) videoAttrs.fixture = options.fixture
688 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
690 return { id: res.body.video.id, uuid: res.body.video.uuid }
693 async function getLocalIdByUUID (url: string, uuid: string) {
694 const res = await getVideo(url, uuid)
699 // serverNumber starts from 1
700 async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) {
701 const server = servers.find(s => s.serverNumber === serverNumber)
702 const res = await uploadRandomVideo(server, false, additionalParams)
704 await waitJobs(servers)
709 async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) {
710 const prefixName = additionalParams.prefixName || ''
711 const name = prefixName + uuidv4()
713 const data = Object.assign({ name }, additionalParams)
714 const res = await uploadVideo(server.url, server.accessToken, data)
716 if (wait) await waitJobs([ server ])
718 return { uuid: res.body.video.uuid, name }
721 // ---------------------------------------------------------------------------
733 getVideoChannelVideos,
735 getVideoFileMetadataUrl,
739 getVideosListPagination,
742 getVideosListWithToken,
744 getVideosWithFilters,
745 uploadRandomVideoOnServers,
752 checkVideoFilesWereRemoved,