1 /* tslint:disable:no-unused-expression */
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'
9 buildAbsoluteFixturePath,
19 import * as validator from 'validator'
20 import { VideoDetails, VideoPrivacy } from '../../models/videos'
21 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, loadLanguages, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
22 import { dateIsValid, webtorrentAdd, buildServerDirectory } from '../miscs/miscs'
26 type VideoAttributes = {
32 commentsEnabled?: boolean
33 downloadEnabled?: boolean
34 waitTranscoding?: boolean
36 originallyPublishedAt?: string
39 privacy?: VideoPrivacy
41 thumbnailfile?: string
45 privacy?: VideoPrivacy
49 function getVideoCategories (url: string) {
50 const path = '/api/v1/videos/categories'
52 return makeGetRequest({
55 statusCodeExpected: 200
59 function getVideoLicences (url: string) {
60 const path = '/api/v1/videos/licences'
62 return makeGetRequest({
65 statusCodeExpected: 200
69 function getVideoLanguages (url: string) {
70 const path = '/api/v1/videos/languages'
72 return makeGetRequest({
75 statusCodeExpected: 200
79 function getVideoPrivacies (url: string) {
80 const path = '/api/v1/videos/privacies'
82 return makeGetRequest({
85 statusCodeExpected: 200
89 function getVideo (url: string, id: number | string, expectedStatus = 200) {
90 const path = '/api/v1/videos/' + id
94 .set('Accept', 'application/json')
95 .expect(expectedStatus)
98 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
99 const path = '/api/v1/videos/' + id + '/views'
101 const req = request(url)
103 .set('Accept', 'application/json')
106 req.set('X-Forwarded-For', xForwardedFor)
109 return req.expect(expectedStatus)
112 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
113 const path = '/api/v1/videos/' + id
117 .set('Authorization', 'Bearer ' + token)
118 .set('Accept', 'application/json')
119 .expect(expectedStatus)
122 function getVideoDescription (url: string, descriptionPath: string) {
124 .get(descriptionPath)
125 .set('Accept', 'application/json')
127 .expect('Content-Type', /json/)
130 function getVideosList (url: string) {
131 const path = '/api/v1/videos'
135 .query({ sort: 'name' })
136 .set('Accept', 'application/json')
138 .expect('Content-Type', /json/)
141 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
142 const path = '/api/v1/videos'
146 .set('Authorization', 'Bearer ' + token)
147 .query(immutableAssign(query, { sort: 'name' }))
148 .set('Accept', 'application/json')
150 .expect('Content-Type', /json/)
153 function getLocalVideos (url: string) {
154 const path = '/api/v1/videos'
158 .query({ sort: 'name', filter: 'local' })
159 .set('Accept', 'application/json')
161 .expect('Content-Type', /json/)
164 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
165 const path = '/api/v1/users/me/videos'
167 const req = request(url)
169 .query({ start: start })
170 .query({ count: count })
172 if (sort) req.query({ sort })
174 return req.set('Accept', 'application/json')
175 .set('Authorization', 'Bearer ' + accessToken)
177 .expect('Content-Type', /json/)
180 function getAccountVideos (
187 query: { nsfw?: boolean } = {}
189 const path = '/api/v1/accounts/' + accountName + '/videos'
191 return makeGetRequest({
194 query: immutableAssign(query, {
200 statusCodeExpected: 200
204 function getVideoChannelVideos (
207 videoChannelName: string,
211 query: { nsfw?: boolean } = {}
213 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
215 return makeGetRequest({
218 query: immutableAssign(query, {
224 statusCodeExpected: 200
228 function getPlaylistVideos (
231 playlistId: number | string,
234 query: { nsfw?: boolean } = {}
236 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
238 return makeGetRequest({
241 query: immutableAssign(query, {
246 statusCodeExpected: 200
250 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
251 const path = '/api/v1/videos'
253 const req = request(url)
255 .query({ start: start })
256 .query({ count: count })
258 if (sort) req.query({ sort })
260 return req.set('Accept', 'application/json')
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')
273 .expect('Content-Type', /json/)
276 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
277 const path = '/api/v1/videos'
282 .set('Accept', 'application/json')
284 .expect('Content-Type', /json/)
287 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 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 checkVideoFilesWereRemoved (
299 serverNumber: number,
307 join('playlists', 'hls'),
308 join('redundancy', 'hls')
311 for (const directory of directories) {
312 const directoryPath = buildServerDirectory(serverNumber, directory)
314 const directoryExists = await pathExists(directoryPath)
315 if (directoryExists === false) continue
317 const files = await readdir(directoryPath)
318 for (const file of files) {
319 expect(file).to.not.contain(videoUUID)
324 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
325 const path = '/api/v1/videos/upload'
326 let defaultChannelId = '1'
329 const res = await getMyUserInformation(url, accessToken)
330 defaultChannelId = res.body.videoChannels[0].id
331 } catch (e) { /* empty */ }
333 // Override default attributes
334 const attributes = Object.assign({
335 name: 'my super video',
339 channelId: defaultChannelId,
341 waitTranscoding: false,
342 description: 'my super description',
343 support: 'my super support text',
345 privacy: VideoPrivacy.PUBLIC,
346 commentsEnabled: true,
347 downloadEnabled: true,
348 fixture: 'video_short.webm'
349 }, videoAttributesArg)
351 const req = request(url)
353 .set('Accept', 'application/json')
354 .set('Authorization', 'Bearer ' + accessToken)
355 .field('name', attributes.name)
356 .field('nsfw', JSON.stringify(attributes.nsfw))
357 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
358 .field('downloadEnabled', JSON.stringify(attributes.downloadEnabled))
359 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
360 .field('privacy', attributes.privacy.toString())
361 .field('channelId', attributes.channelId)
363 if (attributes.support !== undefined) {
364 req.field('support', attributes.support)
367 if (attributes.description !== undefined) {
368 req.field('description', attributes.description)
370 if (attributes.language !== undefined) {
371 req.field('language', attributes.language.toString())
373 if (attributes.category !== undefined) {
374 req.field('category', attributes.category.toString())
376 if (attributes.licence !== undefined) {
377 req.field('licence', attributes.licence.toString())
380 const tags = attributes.tags || []
381 for (let i = 0; i < tags.length; i++) {
382 req.field('tags[' + i + ']', attributes.tags[i])
385 if (attributes.thumbnailfile !== undefined) {
386 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
388 if (attributes.previewfile !== undefined) {
389 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
392 if (attributes.scheduleUpdate) {
393 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
395 if (attributes.scheduleUpdate.privacy) {
396 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
400 if (attributes.originallyPublishedAt !== undefined) {
401 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
404 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
405 .expect(specialStatus)
408 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
409 const path = '/api/v1/videos/' + id
412 if (attributes.name) body['name'] = attributes.name
413 if (attributes.category) body['category'] = attributes.category
414 if (attributes.licence) body['licence'] = attributes.licence
415 if (attributes.language) body['language'] = attributes.language
416 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
417 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
418 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
419 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
420 if (attributes.description) body['description'] = attributes.description
421 if (attributes.tags) body['tags'] = attributes.tags
422 if (attributes.privacy) body['privacy'] = attributes.privacy
423 if (attributes.channelId) body['channelId'] = attributes.channelId
424 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
427 if (attributes.thumbnailfile || attributes.previewfile) {
428 const attaches: any = {}
429 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
430 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
432 return makeUploadRequest({
443 return makePutBodyRequest({
452 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
453 const path = '/api/v1/videos/' + id + '/rate'
457 .set('Accept', 'application/json')
458 .set('Authorization', 'Bearer ' + accessToken)
460 .expect(specialStatus)
463 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
464 return new Promise<any>((res, rej) => {
465 const torrentName = videoUUID + '-' + resolution + '.torrent'
466 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
467 readFile(torrentPath, (err, data) => {
468 if (err) return rej(err)
470 return res(parseTorrent(data))
475 async function completeVideoCheck (
484 commentsEnabled: boolean
485 downloadEnabled: boolean
489 originallyPublishedAt?: string,
511 thumbnailfile?: string
515 if (!attributes.likes) attributes.likes = 0
516 if (!attributes.dislikes) attributes.dislikes = 0
518 expect(video.name).to.equal(attributes.name)
519 expect(video.category.id).to.equal(attributes.category)
520 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
521 expect(video.licence.id).to.equal(attributes.licence)
522 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
523 expect(video.language.id).to.equal(attributes.language)
524 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
525 expect(video.privacy.id).to.deep.equal(attributes.privacy)
526 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
527 expect(video.nsfw).to.equal(attributes.nsfw)
528 expect(video.description).to.equal(attributes.description)
529 expect(video.account.id).to.be.a('number')
530 expect(video.account.host).to.equal(attributes.account.host)
531 expect(video.account.name).to.equal(attributes.account.name)
532 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
533 expect(video.channel.name).to.equal(attributes.channel.name)
534 expect(video.likes).to.equal(attributes.likes)
535 expect(video.dislikes).to.equal(attributes.dislikes)
536 expect(video.isLocal).to.equal(attributes.isLocal)
537 expect(video.duration).to.equal(attributes.duration)
538 expect(dateIsValid(video.createdAt)).to.be.true
539 expect(dateIsValid(video.publishedAt)).to.be.true
540 expect(dateIsValid(video.updatedAt)).to.be.true
542 if (attributes.publishedAt) {
543 expect(video.publishedAt).to.equal(attributes.publishedAt)
546 if (attributes.originallyPublishedAt) {
547 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
549 expect(video.originallyPublishedAt).to.be.null
552 const res = await getVideo(url, video.uuid)
553 const videoDetails: VideoDetails = res.body
555 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
556 expect(videoDetails.tags).to.deep.equal(attributes.tags)
557 expect(videoDetails.account.name).to.equal(attributes.account.name)
558 expect(videoDetails.account.host).to.equal(attributes.account.host)
559 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
560 expect(video.channel.name).to.equal(attributes.channel.name)
561 expect(videoDetails.channel.host).to.equal(attributes.account.host)
562 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
563 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
564 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
565 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
566 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
568 for (const attributeFile of attributes.files) {
569 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
570 expect(file).not.to.be.undefined
572 let extension = extname(attributes.fixture)
573 // Transcoding enabled: extension will always be .mp4
574 if (attributes.files.length > 1) extension = '.mp4'
576 expect(file.magnetUri).to.have.lengthOf.above(2)
577 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
578 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
579 expect(file.resolution.id).to.equal(attributeFile.resolution)
580 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
582 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
583 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
585 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
586 .to.be.above(minSize).and.below(maxSize)
589 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
592 if (attributes.previewfile) {
593 await testImage(url, attributes.previewfile, videoDetails.previewPath)
596 const torrent = await webtorrentAdd(file.magnetUri, true)
597 expect(torrent.files).to.be.an('array')
598 expect(torrent.files.length).to.equal(1)
599 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
603 async function videoUUIDToId (url: string, id: number | string) {
604 if (validator.isUUID('' + id) === false) return id
606 const res = await getVideo(url, id)
610 async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: string, nsfw?: boolean, token?: string }) {
611 const videoAttrs: any = { name: options.videoName }
612 if (options.nsfw) videoAttrs.nsfw = options.nsfw
614 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
616 return { id: res.body.video.id, uuid: res.body.video.uuid }
619 // ---------------------------------------------------------------------------
630 getVideoChannelVideos,
634 getVideosListPagination,
637 getVideosListWithToken,
639 getVideosWithFilters,
646 checkVideoFilesWereRemoved,