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, search?: string) {
165 const path = '/api/v1/users/me/videos'
167 const req = request(url)
169 .query({ start: start })
170 .query({ count: count })
171 .query({ search: search })
173 if (sort) req.query({ sort })
175 return req.set('Accept', 'application/json')
176 .set('Authorization', 'Bearer ' + accessToken)
178 .expect('Content-Type', /json/)
181 function getAccountVideos (
188 query: { nsfw?: boolean } = {}
190 const path = '/api/v1/accounts/' + accountName + '/videos'
192 return makeGetRequest({
195 query: immutableAssign(query, {
201 statusCodeExpected: 200
205 function getVideoChannelVideos (
208 videoChannelName: string,
212 query: { nsfw?: boolean } = {}
214 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
216 return makeGetRequest({
219 query: immutableAssign(query, {
225 statusCodeExpected: 200
229 function getPlaylistVideos (
232 playlistId: number | string,
235 query: { nsfw?: boolean } = {}
237 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
239 return makeGetRequest({
242 query: immutableAssign(query, {
247 statusCodeExpected: 200
251 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
252 const path = '/api/v1/videos'
254 const req = request(url)
256 .query({ start: start })
257 .query({ count: count })
259 if (sort) req.query({ sort })
261 return req.set('Accept', 'application/json')
263 .expect('Content-Type', /json/)
266 function getVideosListSort (url: string, sort: string) {
267 const path = '/api/v1/videos'
271 .query({ sort: sort })
272 .set('Accept', 'application/json')
274 .expect('Content-Type', /json/)
277 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
278 const path = '/api/v1/videos'
283 .set('Accept', 'application/json')
285 .expect('Content-Type', /json/)
288 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
289 const path = '/api/v1/videos'
292 .delete(path + '/' + id)
293 .set('Accept', 'application/json')
294 .set('Authorization', 'Bearer ' + token)
295 .expect(expectedStatus)
298 async function checkVideoFilesWereRemoved (
300 serverNumber: number,
308 join('playlists', 'hls'),
309 join('redundancy', 'hls')
312 for (const directory of directories) {
313 const directoryPath = buildServerDirectory(serverNumber, directory)
315 const directoryExists = await pathExists(directoryPath)
316 if (directoryExists === false) continue
318 const files = await readdir(directoryPath)
319 for (const file of files) {
320 expect(file).to.not.contain(videoUUID)
325 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
326 const path = '/api/v1/videos/upload'
327 let defaultChannelId = '1'
330 const res = await getMyUserInformation(url, accessToken)
331 defaultChannelId = res.body.videoChannels[0].id
332 } catch (e) { /* empty */ }
334 // Override default attributes
335 const attributes = Object.assign({
336 name: 'my super video',
340 channelId: defaultChannelId,
342 waitTranscoding: false,
343 description: 'my super description',
344 support: 'my super support text',
346 privacy: VideoPrivacy.PUBLIC,
347 commentsEnabled: true,
348 downloadEnabled: true,
349 fixture: 'video_short.webm'
350 }, videoAttributesArg)
352 const req = request(url)
354 .set('Accept', 'application/json')
355 .set('Authorization', 'Bearer ' + accessToken)
356 .field('name', attributes.name)
357 .field('nsfw', JSON.stringify(attributes.nsfw))
358 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
359 .field('downloadEnabled', JSON.stringify(attributes.downloadEnabled))
360 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
361 .field('privacy', attributes.privacy.toString())
362 .field('channelId', attributes.channelId)
364 if (attributes.support !== undefined) {
365 req.field('support', attributes.support)
368 if (attributes.description !== undefined) {
369 req.field('description', attributes.description)
371 if (attributes.language !== undefined) {
372 req.field('language', attributes.language.toString())
374 if (attributes.category !== undefined) {
375 req.field('category', attributes.category.toString())
377 if (attributes.licence !== undefined) {
378 req.field('licence', attributes.licence.toString())
381 const tags = attributes.tags || []
382 for (let i = 0; i < tags.length; i++) {
383 req.field('tags[' + i + ']', attributes.tags[i])
386 if (attributes.thumbnailfile !== undefined) {
387 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
389 if (attributes.previewfile !== undefined) {
390 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
393 if (attributes.scheduleUpdate) {
394 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
396 if (attributes.scheduleUpdate.privacy) {
397 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
401 if (attributes.originallyPublishedAt !== undefined) {
402 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
405 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
406 .expect(specialStatus)
409 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
410 const path = '/api/v1/videos/' + id
413 if (attributes.name) body['name'] = attributes.name
414 if (attributes.category) body['category'] = attributes.category
415 if (attributes.licence) body['licence'] = attributes.licence
416 if (attributes.language) body['language'] = attributes.language
417 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
418 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
419 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
420 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
421 if (attributes.description) body['description'] = attributes.description
422 if (attributes.tags) body['tags'] = attributes.tags
423 if (attributes.privacy) body['privacy'] = attributes.privacy
424 if (attributes.channelId) body['channelId'] = attributes.channelId
425 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
428 if (attributes.thumbnailfile || attributes.previewfile) {
429 const attaches: any = {}
430 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
431 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
433 return makeUploadRequest({
444 return makePutBodyRequest({
453 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
454 const path = '/api/v1/videos/' + id + '/rate'
458 .set('Accept', 'application/json')
459 .set('Authorization', 'Bearer ' + accessToken)
461 .expect(specialStatus)
464 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
465 return new Promise<any>((res, rej) => {
466 const torrentName = videoUUID + '-' + resolution + '.torrent'
467 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
468 readFile(torrentPath, (err, data) => {
469 if (err) return rej(err)
471 return res(parseTorrent(data))
476 async function completeVideoCheck (
485 commentsEnabled: boolean
486 downloadEnabled: boolean
490 originallyPublishedAt?: string,
512 thumbnailfile?: string
516 if (!attributes.likes) attributes.likes = 0
517 if (!attributes.dislikes) attributes.dislikes = 0
519 expect(video.name).to.equal(attributes.name)
520 expect(video.category.id).to.equal(attributes.category)
521 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
522 expect(video.licence.id).to.equal(attributes.licence)
523 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
524 expect(video.language.id).to.equal(attributes.language)
525 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
526 expect(video.privacy.id).to.deep.equal(attributes.privacy)
527 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
528 expect(video.nsfw).to.equal(attributes.nsfw)
529 expect(video.description).to.equal(attributes.description)
530 expect(video.account.id).to.be.a('number')
531 expect(video.account.host).to.equal(attributes.account.host)
532 expect(video.account.name).to.equal(attributes.account.name)
533 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
534 expect(video.channel.name).to.equal(attributes.channel.name)
535 expect(video.likes).to.equal(attributes.likes)
536 expect(video.dislikes).to.equal(attributes.dislikes)
537 expect(video.isLocal).to.equal(attributes.isLocal)
538 expect(video.duration).to.equal(attributes.duration)
539 expect(dateIsValid(video.createdAt)).to.be.true
540 expect(dateIsValid(video.publishedAt)).to.be.true
541 expect(dateIsValid(video.updatedAt)).to.be.true
543 if (attributes.publishedAt) {
544 expect(video.publishedAt).to.equal(attributes.publishedAt)
547 if (attributes.originallyPublishedAt) {
548 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
550 expect(video.originallyPublishedAt).to.be.null
553 const res = await getVideo(url, video.uuid)
554 const videoDetails: VideoDetails = res.body
556 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
557 expect(videoDetails.tags).to.deep.equal(attributes.tags)
558 expect(videoDetails.account.name).to.equal(attributes.account.name)
559 expect(videoDetails.account.host).to.equal(attributes.account.host)
560 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
561 expect(video.channel.name).to.equal(attributes.channel.name)
562 expect(videoDetails.channel.host).to.equal(attributes.account.host)
563 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
564 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
565 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
566 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
567 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
569 for (const attributeFile of attributes.files) {
570 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
571 expect(file).not.to.be.undefined
573 let extension = extname(attributes.fixture)
574 // Transcoding enabled: extension will always be .mp4
575 if (attributes.files.length > 1) extension = '.mp4'
577 expect(file.magnetUri).to.have.lengthOf.above(2)
578 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
579 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
580 expect(file.resolution.id).to.equal(attributeFile.resolution)
581 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
583 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
584 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
586 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
587 .to.be.above(minSize).and.below(maxSize)
590 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
593 if (attributes.previewfile) {
594 await testImage(url, attributes.previewfile, videoDetails.previewPath)
597 const torrent = await webtorrentAdd(file.magnetUri, true)
598 expect(torrent.files).to.be.an('array')
599 expect(torrent.files.length).to.equal(1)
600 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
604 async function videoUUIDToId (url: string, id: number | string) {
605 if (validator.isUUID('' + id) === false) return id
607 const res = await getVideo(url, id)
611 async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: string, nsfw?: boolean, token?: string }) {
612 const videoAttrs: any = { name: options.videoName }
613 if (options.nsfw) videoAttrs.nsfw = options.nsfw
615 const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
617 return { id: res.body.video.id, uuid: res.body.video.uuid }
620 // ---------------------------------------------------------------------------
631 getVideoChannelVideos,
635 getVideosListPagination,
638 getVideosListWithToken,
640 getVideosWithFilters,
647 checkVideoFilesWereRemoved,