1 /* tslint:disable:no-unused-expression */
3 import { expect } from 'chai'
4 import { existsSync, 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,
20 import { VideoDetails, VideoPrivacy } from '../../models/videos'
21 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
22 import { dateIsValid, webtorrentAdd } from '../miscs/miscs'
24 type VideoAttributes = {
30 commentsEnabled?: boolean
31 waitTranscoding?: boolean
35 privacy?: VideoPrivacy
37 thumbnailfile?: string
41 privacy?: VideoPrivacy
45 function getVideoCategories (url: string) {
46 const path = '/api/v1/videos/categories'
48 return makeGetRequest({
51 statusCodeExpected: 200
55 function getVideoLicences (url: string) {
56 const path = '/api/v1/videos/licences'
58 return makeGetRequest({
61 statusCodeExpected: 200
65 function getVideoLanguages (url: string) {
66 const path = '/api/v1/videos/languages'
68 return makeGetRequest({
71 statusCodeExpected: 200
75 function getVideoPrivacies (url: string) {
76 const path = '/api/v1/videos/privacies'
78 return makeGetRequest({
81 statusCodeExpected: 200
85 function getVideo (url: string, id: number | string, expectedStatus = 200) {
86 const path = '/api/v1/videos/' + id
90 .set('Accept', 'application/json')
91 .expect(expectedStatus)
94 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
95 const path = '/api/v1/videos/' + id + '/views'
97 const req = request(url)
99 .set('Accept', 'application/json')
102 req.set('X-Forwarded-For', xForwardedFor)
105 return req.expect(expectedStatus)
108 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
109 const path = '/api/v1/videos/' + id
113 .set('Authorization', 'Bearer ' + token)
114 .set('Accept', 'application/json')
115 .expect(expectedStatus)
118 function getVideoDescription (url: string, descriptionPath: string) {
120 .get(descriptionPath)
121 .set('Accept', 'application/json')
123 .expect('Content-Type', /json/)
126 function getVideosList (url: string) {
127 const path = '/api/v1/videos'
131 .query({ sort: 'name' })
132 .set('Accept', 'application/json')
134 .expect('Content-Type', /json/)
137 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
138 const path = '/api/v1/videos'
142 .set('Authorization', 'Bearer ' + token)
143 .query(immutableAssign(query, { sort: 'name' }))
144 .set('Accept', 'application/json')
146 .expect('Content-Type', /json/)
149 function getLocalVideos (url: string) {
150 const path = '/api/v1/videos'
154 .query({ sort: 'name', filter: 'local' })
155 .set('Accept', 'application/json')
157 .expect('Content-Type', /json/)
160 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
161 const path = '/api/v1/users/me/videos'
163 const req = request(url)
165 .query({ start: start })
166 .query({ count: count })
168 if (sort) req.query({ sort })
170 return req.set('Accept', 'application/json')
171 .set('Authorization', 'Bearer ' + accessToken)
173 .expect('Content-Type', /json/)
176 function getAccountVideos (
183 query: { nsfw?: boolean } = {}
185 const path = '/api/v1/accounts/' + accountName + '/videos'
187 return makeGetRequest({
190 query: immutableAssign(query, {
196 statusCodeExpected: 200
200 function getVideoChannelVideos (
203 videoChannelName: string,
207 query: { nsfw?: boolean } = {}
209 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
211 return makeGetRequest({
214 query: immutableAssign(query, {
220 statusCodeExpected: 200
224 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
225 const path = '/api/v1/videos'
227 const req = request(url)
229 .query({ start: start })
230 .query({ count: count })
232 if (sort) req.query({ sort })
234 return req.set('Accept', 'application/json')
236 .expect('Content-Type', /json/)
239 function getVideosListSort (url: string, sort: string) {
240 const path = '/api/v1/videos'
244 .query({ sort: sort })
245 .set('Accept', 'application/json')
247 .expect('Content-Type', /json/)
250 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
251 const path = '/api/v1/videos'
256 .set('Accept', 'application/json')
258 .expect('Content-Type', /json/)
261 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
262 const path = '/api/v1/videos'
265 .delete(path + '/' + id)
266 .set('Accept', 'application/json')
267 .set('Authorization', 'Bearer ' + token)
268 .expect(expectedStatus)
271 async function checkVideoFilesWereRemoved (
273 serverNumber: number,
274 directories = [ 'redundancy', 'videos', 'thumbnails', 'torrents', 'previews', 'captions' ]
276 const testDirectory = 'test' + serverNumber
278 for (const directory of directories) {
279 const directoryPath = join(root(), testDirectory, directory)
281 const directoryExists = existsSync(directoryPath)
282 expect(directoryExists).to.be.true
284 const files = await readdir(directoryPath)
285 for (const file of files) {
286 expect(file).to.not.contain(videoUUID)
291 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
292 const path = '/api/v1/videos/upload'
293 let defaultChannelId = '1'
296 const res = await getMyUserInformation(url, accessToken)
297 defaultChannelId = res.body.videoChannels[0].id
298 } catch (e) { /* empty */ }
300 // Override default attributes
301 const attributes = Object.assign({
302 name: 'my super video',
306 channelId: defaultChannelId,
308 waitTranscoding: false,
309 description: 'my super description',
310 support: 'my super support text',
312 privacy: VideoPrivacy.PUBLIC,
313 commentsEnabled: true,
314 fixture: 'video_short.webm'
315 }, videoAttributesArg)
317 const req = request(url)
319 .set('Accept', 'application/json')
320 .set('Authorization', 'Bearer ' + accessToken)
321 .field('name', attributes.name)
322 .field('nsfw', JSON.stringify(attributes.nsfw))
323 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
324 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
325 .field('privacy', attributes.privacy.toString())
326 .field('channelId', attributes.channelId)
328 if (attributes.description !== undefined) {
329 req.field('description', attributes.description)
331 if (attributes.language !== undefined) {
332 req.field('language', attributes.language.toString())
334 if (attributes.category !== undefined) {
335 req.field('category', attributes.category.toString())
337 if (attributes.licence !== undefined) {
338 req.field('licence', attributes.licence.toString())
341 for (let i = 0; i < attributes.tags.length; i++) {
342 req.field('tags[' + i + ']', attributes.tags[i])
345 if (attributes.thumbnailfile !== undefined) {
346 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
348 if (attributes.previewfile !== undefined) {
349 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
352 if (attributes.scheduleUpdate) {
353 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
355 if (attributes.scheduleUpdate.privacy) {
356 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
360 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
361 .expect(specialStatus)
364 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
365 const path = '/api/v1/videos/' + id
368 if (attributes.name) body['name'] = attributes.name
369 if (attributes.category) body['category'] = attributes.category
370 if (attributes.licence) body['licence'] = attributes.licence
371 if (attributes.language) body['language'] = attributes.language
372 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
373 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
374 if (attributes.description) body['description'] = attributes.description
375 if (attributes.tags) body['tags'] = attributes.tags
376 if (attributes.privacy) body['privacy'] = attributes.privacy
377 if (attributes.channelId) body['channelId'] = attributes.channelId
378 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
381 if (attributes.thumbnailfile || attributes.previewfile) {
382 const attaches: any = {}
383 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
384 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
386 return makeUploadRequest({
397 return makePutBodyRequest({
406 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
407 const path = '/api/v1/videos/' + id + '/rate'
411 .set('Accept', 'application/json')
412 .set('Authorization', 'Bearer ' + accessToken)
414 .expect(specialStatus)
417 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
418 return new Promise<any>((res, rej) => {
419 const torrentName = videoUUID + '-' + resolution + '.torrent'
420 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
421 readFile(torrentPath, (err, data) => {
422 if (err) return rej(err)
424 return res(parseTorrent(data))
429 async function completeVideoCheck (
438 commentsEnabled: boolean
463 thumbnailfile?: string
467 if (!attributes.likes) attributes.likes = 0
468 if (!attributes.dislikes) attributes.dislikes = 0
470 expect(video.name).to.equal(attributes.name)
471 expect(video.category.id).to.equal(attributes.category)
472 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
473 expect(video.licence.id).to.equal(attributes.licence)
474 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
475 expect(video.language.id).to.equal(attributes.language)
476 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
477 expect(video.privacy.id).to.deep.equal(attributes.privacy)
478 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
479 expect(video.nsfw).to.equal(attributes.nsfw)
480 expect(video.description).to.equal(attributes.description)
481 expect(video.account.id).to.be.a('number')
482 expect(video.account.uuid).to.be.a('string')
483 expect(video.account.host).to.equal(attributes.account.host)
484 expect(video.account.name).to.equal(attributes.account.name)
485 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
486 expect(video.channel.name).to.equal(attributes.channel.name)
487 expect(video.likes).to.equal(attributes.likes)
488 expect(video.dislikes).to.equal(attributes.dislikes)
489 expect(video.isLocal).to.equal(attributes.isLocal)
490 expect(video.duration).to.equal(attributes.duration)
491 expect(dateIsValid(video.createdAt)).to.be.true
492 expect(dateIsValid(video.publishedAt)).to.be.true
493 expect(dateIsValid(video.updatedAt)).to.be.true
495 if (attributes.publishedAt) {
496 expect(video.publishedAt).to.equal(attributes.publishedAt)
499 const res = await getVideo(url, video.uuid)
500 const videoDetails: VideoDetails = res.body
502 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
503 expect(videoDetails.tags).to.deep.equal(attributes.tags)
504 expect(videoDetails.account.name).to.equal(attributes.account.name)
505 expect(videoDetails.account.host).to.equal(attributes.account.host)
506 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
507 expect(video.channel.name).to.equal(attributes.channel.name)
508 expect(videoDetails.channel.host).to.equal(attributes.account.host)
509 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
510 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
511 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
512 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
514 for (const attributeFile of attributes.files) {
515 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
516 expect(file).not.to.be.undefined
518 let extension = extname(attributes.fixture)
519 // Transcoding enabled on server 2, extension will always be .mp4
520 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
522 const magnetUri = file.magnetUri
523 expect(file.magnetUri).to.have.lengthOf.above(2)
524 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
525 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
526 expect(file.resolution.id).to.equal(attributeFile.resolution)
527 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
529 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
530 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
532 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
533 .to.be.above(minSize).and.below(maxSize)
536 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
539 if (attributes.previewfile) {
540 await testImage(url, attributes.previewfile, videoDetails.previewPath)
543 const torrent = await webtorrentAdd(magnetUri, true)
544 expect(torrent.files).to.be.an('array')
545 expect(torrent.files.length).to.equal(1)
546 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
550 // ---------------------------------------------------------------------------
560 getVideoChannelVideos,
564 getVideosListPagination,
567 getVideosListWithToken,
569 getVideosWithFilters,
576 checkVideoFilesWereRemoved