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 downloadEnabled?: boolean
32 waitTranscoding?: boolean
34 originallyPublishedAt?: string
37 privacy?: VideoPrivacy
39 thumbnailfile?: string
43 privacy?: VideoPrivacy
47 function getVideoCategories (url: string) {
48 const path = '/api/v1/videos/categories'
50 return makeGetRequest({
53 statusCodeExpected: 200
57 function getVideoLicences (url: string) {
58 const path = '/api/v1/videos/licences'
60 return makeGetRequest({
63 statusCodeExpected: 200
67 function getVideoLanguages (url: string) {
68 const path = '/api/v1/videos/languages'
70 return makeGetRequest({
73 statusCodeExpected: 200
77 function getVideoPrivacies (url: string) {
78 const path = '/api/v1/videos/privacies'
80 return makeGetRequest({
83 statusCodeExpected: 200
87 function getVideo (url: string, id: number | string, expectedStatus = 200) {
88 const path = '/api/v1/videos/' + id
92 .set('Accept', 'application/json')
93 .expect(expectedStatus)
96 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
97 const path = '/api/v1/videos/' + id + '/views'
99 const req = request(url)
101 .set('Accept', 'application/json')
104 req.set('X-Forwarded-For', xForwardedFor)
107 return req.expect(expectedStatus)
110 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
111 const path = '/api/v1/videos/' + id
115 .set('Authorization', 'Bearer ' + token)
116 .set('Accept', 'application/json')
117 .expect(expectedStatus)
120 function getVideoDescription (url: string, descriptionPath: string) {
122 .get(descriptionPath)
123 .set('Accept', 'application/json')
125 .expect('Content-Type', /json/)
128 function getVideosList (url: string) {
129 const path = '/api/v1/videos'
133 .query({ sort: 'name' })
134 .set('Accept', 'application/json')
136 .expect('Content-Type', /json/)
139 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
140 const path = '/api/v1/videos'
144 .set('Authorization', 'Bearer ' + token)
145 .query(immutableAssign(query, { sort: 'name' }))
146 .set('Accept', 'application/json')
148 .expect('Content-Type', /json/)
151 function getLocalVideos (url: string) {
152 const path = '/api/v1/videos'
156 .query({ sort: 'name', filter: 'local' })
157 .set('Accept', 'application/json')
159 .expect('Content-Type', /json/)
162 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
163 const path = '/api/v1/users/me/videos'
165 const req = request(url)
167 .query({ start: start })
168 .query({ count: count })
170 if (sort) req.query({ sort })
172 return req.set('Accept', 'application/json')
173 .set('Authorization', 'Bearer ' + accessToken)
175 .expect('Content-Type', /json/)
178 function getAccountVideos (
185 query: { nsfw?: boolean } = {}
187 const path = '/api/v1/accounts/' + accountName + '/videos'
189 return makeGetRequest({
192 query: immutableAssign(query, {
198 statusCodeExpected: 200
202 function getVideoChannelVideos (
205 videoChannelName: string,
209 query: { nsfw?: boolean } = {}
211 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
213 return makeGetRequest({
216 query: immutableAssign(query, {
222 statusCodeExpected: 200
226 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
227 const path = '/api/v1/videos'
229 const req = request(url)
231 .query({ start: start })
232 .query({ count: count })
234 if (sort) req.query({ sort })
236 return req.set('Accept', 'application/json')
238 .expect('Content-Type', /json/)
241 function getVideosListSort (url: string, sort: string) {
242 const path = '/api/v1/videos'
246 .query({ sort: sort })
247 .set('Accept', 'application/json')
249 .expect('Content-Type', /json/)
252 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
253 const path = '/api/v1/videos'
258 .set('Accept', 'application/json')
260 .expect('Content-Type', /json/)
263 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
264 const path = '/api/v1/videos'
267 .delete(path + '/' + id)
268 .set('Accept', 'application/json')
269 .set('Authorization', 'Bearer ' + token)
270 .expect(expectedStatus)
273 async function checkVideoFilesWereRemoved (
275 serverNumber: number,
283 join('playlists', 'hls'),
284 join('redundancy', 'hls')
287 const testDirectory = 'test' + serverNumber
289 for (const directory of directories) {
290 const directoryPath = join(root(), testDirectory, directory)
292 const directoryExists = existsSync(directoryPath)
293 if (!directoryExists) continue
295 const files = await readdir(directoryPath)
296 for (const file of files) {
297 expect(file).to.not.contain(videoUUID)
302 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
303 const path = '/api/v1/videos/upload'
304 let defaultChannelId = '1'
307 const res = await getMyUserInformation(url, accessToken)
308 defaultChannelId = res.body.videoChannels[0].id
309 } catch (e) { /* empty */ }
311 // Override default attributes
312 const attributes = Object.assign({
313 name: 'my super video',
317 channelId: defaultChannelId,
319 waitTranscoding: false,
320 description: 'my super description',
321 support: 'my super support text',
323 privacy: VideoPrivacy.PUBLIC,
324 commentsEnabled: true,
325 downloadEnabled: true,
326 fixture: 'video_short.webm'
327 }, videoAttributesArg)
329 const req = request(url)
331 .set('Accept', 'application/json')
332 .set('Authorization', 'Bearer ' + accessToken)
333 .field('name', attributes.name)
334 .field('nsfw', JSON.stringify(attributes.nsfw))
335 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
336 .field('downloadEnabled', JSON.stringify(attributes.downloadEnabled))
337 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
338 .field('privacy', attributes.privacy.toString())
339 .field('channelId', attributes.channelId)
341 if (attributes.description !== undefined) {
342 req.field('description', attributes.description)
344 if (attributes.language !== undefined) {
345 req.field('language', attributes.language.toString())
347 if (attributes.category !== undefined) {
348 req.field('category', attributes.category.toString())
350 if (attributes.licence !== undefined) {
351 req.field('licence', attributes.licence.toString())
353 if (attributes.originallyPublishedAt !== undefined) {
354 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
357 for (let i = 0; i < attributes.tags.length; i++) {
358 req.field('tags[' + i + ']', attributes.tags[i])
361 if (attributes.thumbnailfile !== undefined) {
362 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
364 if (attributes.previewfile !== undefined) {
365 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
368 if (attributes.scheduleUpdate) {
369 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
371 if (attributes.scheduleUpdate.privacy) {
372 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
376 if (attributes.originallyPublishedAt !== undefined) {
377 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
380 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
381 .expect(specialStatus)
384 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
385 const path = '/api/v1/videos/' + id
388 if (attributes.name) body['name'] = attributes.name
389 if (attributes.category) body['category'] = attributes.category
390 if (attributes.licence) body['licence'] = attributes.licence
391 if (attributes.language) body['language'] = attributes.language
392 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
393 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
394 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
395 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
396 if (attributes.description) body['description'] = attributes.description
397 if (attributes.tags) body['tags'] = attributes.tags
398 if (attributes.privacy) body['privacy'] = attributes.privacy
399 if (attributes.channelId) body['channelId'] = attributes.channelId
400 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
403 if (attributes.thumbnailfile || attributes.previewfile) {
404 const attaches: any = {}
405 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
406 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
408 return makeUploadRequest({
419 return makePutBodyRequest({
428 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
429 const path = '/api/v1/videos/' + id + '/rate'
433 .set('Accept', 'application/json')
434 .set('Authorization', 'Bearer ' + accessToken)
436 .expect(specialStatus)
439 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
440 return new Promise<any>((res, rej) => {
441 const torrentName = videoUUID + '-' + resolution + '.torrent'
442 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
443 readFile(torrentPath, (err, data) => {
444 if (err) return rej(err)
446 return res(parseTorrent(data))
451 async function completeVideoCheck (
460 commentsEnabled: boolean
461 downloadEnabled: boolean
465 originallyPublishedAt?: string,
487 thumbnailfile?: string
491 if (!attributes.likes) attributes.likes = 0
492 if (!attributes.dislikes) attributes.dislikes = 0
494 expect(video.name).to.equal(attributes.name)
495 expect(video.category.id).to.equal(attributes.category)
496 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
497 expect(video.licence.id).to.equal(attributes.licence)
498 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
499 expect(video.language.id).to.equal(attributes.language)
500 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
501 expect(video.privacy.id).to.deep.equal(attributes.privacy)
502 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
503 expect(video.nsfw).to.equal(attributes.nsfw)
504 expect(video.description).to.equal(attributes.description)
505 expect(video.account.id).to.be.a('number')
506 expect(video.account.uuid).to.be.a('string')
507 expect(video.account.host).to.equal(attributes.account.host)
508 expect(video.account.name).to.equal(attributes.account.name)
509 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
510 expect(video.channel.name).to.equal(attributes.channel.name)
511 expect(video.likes).to.equal(attributes.likes)
512 expect(video.dislikes).to.equal(attributes.dislikes)
513 expect(video.isLocal).to.equal(attributes.isLocal)
514 expect(video.duration).to.equal(attributes.duration)
515 expect(dateIsValid(video.createdAt)).to.be.true
516 expect(dateIsValid(video.publishedAt)).to.be.true
517 expect(dateIsValid(video.updatedAt)).to.be.true
519 if (attributes.publishedAt) {
520 expect(video.publishedAt).to.equal(attributes.publishedAt)
523 if (attributes.originallyPublishedAt) {
524 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
526 expect(video.originallyPublishedAt).to.be.null
529 const res = await getVideo(url, video.uuid)
530 const videoDetails: VideoDetails = res.body
532 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
533 expect(videoDetails.tags).to.deep.equal(attributes.tags)
534 expect(videoDetails.account.name).to.equal(attributes.account.name)
535 expect(videoDetails.account.host).to.equal(attributes.account.host)
536 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
537 expect(video.channel.name).to.equal(attributes.channel.name)
538 expect(videoDetails.channel.host).to.equal(attributes.account.host)
539 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
540 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
541 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
542 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
543 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
545 for (const attributeFile of attributes.files) {
546 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
547 expect(file).not.to.be.undefined
549 let extension = extname(attributes.fixture)
550 // Transcoding enabled on server 2, extension will always be .mp4
551 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
553 const magnetUri = file.magnetUri
554 expect(file.magnetUri).to.have.lengthOf.above(2)
555 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
556 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
557 expect(file.resolution.id).to.equal(attributeFile.resolution)
558 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
560 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
561 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
563 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
564 .to.be.above(minSize).and.below(maxSize)
567 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
570 if (attributes.previewfile) {
571 await testImage(url, attributes.previewfile, videoDetails.previewPath)
574 const torrent = await webtorrentAdd(magnetUri, true)
575 expect(torrent.files).to.be.an('array')
576 expect(torrent.files.length).to.equal(1)
577 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
581 // ---------------------------------------------------------------------------
591 getVideoChannelVideos,
595 getVideosListPagination,
598 getVideosListWithToken,
600 getVideosWithFilters,
607 checkVideoFilesWereRemoved