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 getPlaylistVideos (
229 playlistId: number | string,
232 query: { nsfw?: boolean } = {}
234 const path = '/api/v1/video-playlists/' + playlistId + '/videos'
236 return makeGetRequest({
239 query: immutableAssign(query, {
244 statusCodeExpected: 200
248 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
249 const path = '/api/v1/videos'
251 const req = request(url)
253 .query({ start: start })
254 .query({ count: count })
256 if (sort) req.query({ sort })
258 return req.set('Accept', 'application/json')
260 .expect('Content-Type', /json/)
263 function getVideosListSort (url: string, sort: string) {
264 const path = '/api/v1/videos'
268 .query({ sort: sort })
269 .set('Accept', 'application/json')
271 .expect('Content-Type', /json/)
274 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
275 const path = '/api/v1/videos'
280 .set('Accept', 'application/json')
282 .expect('Content-Type', /json/)
285 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
286 const path = '/api/v1/videos'
289 .delete(path + '/' + id)
290 .set('Accept', 'application/json')
291 .set('Authorization', 'Bearer ' + token)
292 .expect(expectedStatus)
295 async function checkVideoFilesWereRemoved (
297 serverNumber: number,
305 join('playlists', 'hls'),
306 join('redundancy', 'hls')
309 const testDirectory = 'test' + serverNumber
311 for (const directory of directories) {
312 const directoryPath = join(root(), testDirectory, directory)
314 const directoryExists = existsSync(directoryPath)
315 if (!directoryExists) 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.description !== undefined) {
364 req.field('description', attributes.description)
366 if (attributes.language !== undefined) {
367 req.field('language', attributes.language.toString())
369 if (attributes.category !== undefined) {
370 req.field('category', attributes.category.toString())
372 if (attributes.licence !== undefined) {
373 req.field('licence', attributes.licence.toString())
376 for (let i = 0; i < attributes.tags.length; i++) {
377 req.field('tags[' + i + ']', attributes.tags[i])
380 if (attributes.thumbnailfile !== undefined) {
381 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
383 if (attributes.previewfile !== undefined) {
384 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
387 if (attributes.scheduleUpdate) {
388 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
390 if (attributes.scheduleUpdate.privacy) {
391 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
395 if (attributes.originallyPublishedAt !== undefined) {
396 req.field('originallyPublishedAt', attributes.originallyPublishedAt)
399 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
400 .expect(specialStatus)
403 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
404 const path = '/api/v1/videos/' + id
407 if (attributes.name) body['name'] = attributes.name
408 if (attributes.category) body['category'] = attributes.category
409 if (attributes.licence) body['licence'] = attributes.licence
410 if (attributes.language) body['language'] = attributes.language
411 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
412 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
413 if (attributes.downloadEnabled !== undefined) body['downloadEnabled'] = JSON.stringify(attributes.downloadEnabled)
414 if (attributes.originallyPublishedAt !== undefined) body['originallyPublishedAt'] = attributes.originallyPublishedAt
415 if (attributes.description) body['description'] = attributes.description
416 if (attributes.tags) body['tags'] = attributes.tags
417 if (attributes.privacy) body['privacy'] = attributes.privacy
418 if (attributes.channelId) body['channelId'] = attributes.channelId
419 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
422 if (attributes.thumbnailfile || attributes.previewfile) {
423 const attaches: any = {}
424 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
425 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
427 return makeUploadRequest({
438 return makePutBodyRequest({
447 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
448 const path = '/api/v1/videos/' + id + '/rate'
452 .set('Accept', 'application/json')
453 .set('Authorization', 'Bearer ' + accessToken)
455 .expect(specialStatus)
458 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
459 return new Promise<any>((res, rej) => {
460 const torrentName = videoUUID + '-' + resolution + '.torrent'
461 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
462 readFile(torrentPath, (err, data) => {
463 if (err) return rej(err)
465 return res(parseTorrent(data))
470 async function completeVideoCheck (
479 commentsEnabled: boolean
480 downloadEnabled: boolean
484 originallyPublishedAt?: string,
506 thumbnailfile?: string
510 if (!attributes.likes) attributes.likes = 0
511 if (!attributes.dislikes) attributes.dislikes = 0
513 expect(video.name).to.equal(attributes.name)
514 expect(video.category.id).to.equal(attributes.category)
515 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
516 expect(video.licence.id).to.equal(attributes.licence)
517 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
518 expect(video.language.id).to.equal(attributes.language)
519 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
520 expect(video.privacy.id).to.deep.equal(attributes.privacy)
521 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
522 expect(video.nsfw).to.equal(attributes.nsfw)
523 expect(video.description).to.equal(attributes.description)
524 expect(video.account.id).to.be.a('number')
525 expect(video.account.uuid).to.be.a('string')
526 expect(video.account.host).to.equal(attributes.account.host)
527 expect(video.account.name).to.equal(attributes.account.name)
528 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
529 expect(video.channel.name).to.equal(attributes.channel.name)
530 expect(video.likes).to.equal(attributes.likes)
531 expect(video.dislikes).to.equal(attributes.dislikes)
532 expect(video.isLocal).to.equal(attributes.isLocal)
533 expect(video.duration).to.equal(attributes.duration)
534 expect(dateIsValid(video.createdAt)).to.be.true
535 expect(dateIsValid(video.publishedAt)).to.be.true
536 expect(dateIsValid(video.updatedAt)).to.be.true
538 if (attributes.publishedAt) {
539 expect(video.publishedAt).to.equal(attributes.publishedAt)
542 if (attributes.originallyPublishedAt) {
543 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
545 expect(video.originallyPublishedAt).to.be.null
548 const res = await getVideo(url, video.uuid)
549 const videoDetails: VideoDetails = res.body
551 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
552 expect(videoDetails.tags).to.deep.equal(attributes.tags)
553 expect(videoDetails.account.name).to.equal(attributes.account.name)
554 expect(videoDetails.account.host).to.equal(attributes.account.host)
555 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
556 expect(video.channel.name).to.equal(attributes.channel.name)
557 expect(videoDetails.channel.host).to.equal(attributes.account.host)
558 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
559 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
560 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
561 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
562 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
564 for (const attributeFile of attributes.files) {
565 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
566 expect(file).not.to.be.undefined
568 let extension = extname(attributes.fixture)
569 // Transcoding enabled on server 2, extension will always be .mp4
570 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
572 const magnetUri = file.magnetUri
573 expect(file.magnetUri).to.have.lengthOf.above(2)
574 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
575 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
576 expect(file.resolution.id).to.equal(attributeFile.resolution)
577 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
579 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
580 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
582 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
583 .to.be.above(minSize).and.below(maxSize)
586 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
589 if (attributes.previewfile) {
590 await testImage(url, attributes.previewfile, videoDetails.previewPath)
593 const torrent = await webtorrentAdd(magnetUri, true)
594 expect(torrent.files).to.be.an('array')
595 expect(torrent.files.length).to.equal(1)
596 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
600 // ---------------------------------------------------------------------------
610 getVideoChannelVideos,
614 getVideosListPagination,
617 getVideosListWithToken,
619 getVideosWithFilters,
626 checkVideoFilesWereRemoved,