1 /* tslint:disable:no-unused-expression */
3 import { expect } from 'chai'
4 import { existsSync, 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,
10 getMyUserInformation, immutableAssign,
18 import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
19 import { readdirPromise } from '../../../helpers/core-utils'
20 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers'
21 import { dateIsValid, webtorrentAdd } from '../index'
23 type VideoAttributes = {
29 commentsEnabled?: boolean
30 waitTranscoding?: boolean
34 privacy?: VideoPrivacy
36 thumbnailfile?: string
40 privacy?: VideoPrivacy
44 function getVideoCategories (url: string) {
45 const path = '/api/v1/videos/categories'
47 return makeGetRequest({
50 statusCodeExpected: 200
54 function getVideoLicences (url: string) {
55 const path = '/api/v1/videos/licences'
57 return makeGetRequest({
60 statusCodeExpected: 200
64 function getVideoLanguages (url: string) {
65 const path = '/api/v1/videos/languages'
67 return makeGetRequest({
70 statusCodeExpected: 200
74 function getVideoPrivacies (url: string) {
75 const path = '/api/v1/videos/privacies'
77 return makeGetRequest({
80 statusCodeExpected: 200
84 function getVideo (url: string, id: number | string, expectedStatus = 200) {
85 const path = '/api/v1/videos/' + id
89 .set('Accept', 'application/json')
90 .expect(expectedStatus)
93 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
94 const path = '/api/v1/videos/' + id + '/views'
96 const req = request(url)
98 .set('Accept', 'application/json')
101 req.set('X-Forwarded-For', xForwardedFor)
104 return req.expect(expectedStatus)
107 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
108 const path = '/api/v1/videos/' + id
112 .set('Authorization', 'Bearer ' + token)
113 .set('Accept', 'application/json')
114 .expect(expectedStatus)
117 function getVideoDescription (url: string, descriptionPath: string) {
119 .get(descriptionPath)
120 .set('Accept', 'application/json')
122 .expect('Content-Type', /json/)
125 function getVideosList (url: string) {
126 const path = '/api/v1/videos'
130 .query({ sort: 'name' })
131 .set('Accept', 'application/json')
133 .expect('Content-Type', /json/)
136 function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
137 const path = '/api/v1/videos'
141 .set('Authorization', 'Bearer ' + token)
142 .query(immutableAssign(query, { sort: 'name' }))
143 .set('Accept', 'application/json')
145 .expect('Content-Type', /json/)
148 function getLocalVideos (url: string) {
149 const path = '/api/v1/videos'
153 .query({ sort: 'name', filter: 'local' })
154 .set('Accept', 'application/json')
156 .expect('Content-Type', /json/)
159 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
160 const path = '/api/v1/users/me/videos'
162 const req = request(url)
164 .query({ start: start })
165 .query({ count: count })
167 if (sort) req.query({ sort })
169 return req.set('Accept', 'application/json')
170 .set('Authorization', 'Bearer ' + accessToken)
172 .expect('Content-Type', /json/)
175 function getAccountVideos (
182 query: { nsfw?: boolean } = {}
184 const path = '/api/v1/accounts/' + accountName + '/videos'
186 return makeGetRequest({
189 query: immutableAssign(query, {
195 statusCodeExpected: 200
199 function getVideoChannelVideos (
202 videoChannelName: string,
206 query: { nsfw?: boolean } = {}
208 const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
210 return makeGetRequest({
213 query: immutableAssign(query, {
219 statusCodeExpected: 200
223 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
224 const path = '/api/v1/videos'
226 const req = request(url)
228 .query({ start: start })
229 .query({ count: count })
231 if (sort) req.query({ sort })
233 return req.set('Accept', 'application/json')
235 .expect('Content-Type', /json/)
238 function getVideosListSort (url: string, sort: string) {
239 const path = '/api/v1/videos'
243 .query({ sort: sort })
244 .set('Accept', 'application/json')
246 .expect('Content-Type', /json/)
249 function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
250 const path = '/api/v1/videos'
255 .set('Accept', 'application/json')
257 .expect('Content-Type', /json/)
260 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
261 const path = '/api/v1/videos'
264 .delete(path + '/' + id)
265 .set('Accept', 'application/json')
266 .set('Authorization', 'Bearer ' + token)
267 .expect(expectedStatus)
270 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
271 const testDirectory = 'test' + serverNumber
273 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews', 'captions' ]) {
274 const directoryPath = join(root(), testDirectory, directory)
276 const directoryExists = existsSync(directoryPath)
277 expect(directoryExists).to.be.true
279 const files = await readdirPromise(directoryPath)
280 for (const file of files) {
281 expect(file).to.not.contain(videoUUID)
286 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
287 const path = '/api/v1/videos/upload'
288 let defaultChannelId = '1'
291 const res = await getMyUserInformation(url, accessToken)
292 defaultChannelId = res.body.videoChannels[0].id
293 } catch (e) { /* empty */ }
295 // Override default attributes
296 const attributes = Object.assign({
297 name: 'my super video',
301 channelId: defaultChannelId,
303 waitTranscoding: false,
304 description: 'my super description',
305 support: 'my super support text',
307 privacy: VideoPrivacy.PUBLIC,
308 commentsEnabled: true,
309 fixture: 'video_short.webm'
310 }, videoAttributesArg)
312 const req = request(url)
314 .set('Accept', 'application/json')
315 .set('Authorization', 'Bearer ' + accessToken)
316 .field('name', attributes.name)
317 .field('nsfw', JSON.stringify(attributes.nsfw))
318 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
319 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
320 .field('privacy', attributes.privacy.toString())
321 .field('channelId', attributes.channelId)
323 if (attributes.description !== undefined) {
324 req.field('description', attributes.description)
326 if (attributes.language !== undefined) {
327 req.field('language', attributes.language.toString())
329 if (attributes.category !== undefined) {
330 req.field('category', attributes.category.toString())
332 if (attributes.licence !== undefined) {
333 req.field('licence', attributes.licence.toString())
336 for (let i = 0; i < attributes.tags.length; i++) {
337 req.field('tags[' + i + ']', attributes.tags[i])
340 if (attributes.thumbnailfile !== undefined) {
341 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
343 if (attributes.previewfile !== undefined) {
344 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
347 if (attributes.scheduleUpdate) {
348 req.field('scheduleUpdate[updateAt]', attributes.scheduleUpdate.updateAt)
350 if (attributes.scheduleUpdate.privacy) {
351 req.field('scheduleUpdate[privacy]', attributes.scheduleUpdate.privacy)
355 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
356 .expect(specialStatus)
359 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
360 const path = '/api/v1/videos/' + id
363 if (attributes.name) body['name'] = attributes.name
364 if (attributes.category) body['category'] = attributes.category
365 if (attributes.licence) body['licence'] = attributes.licence
366 if (attributes.language) body['language'] = attributes.language
367 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
368 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
369 if (attributes.description) body['description'] = attributes.description
370 if (attributes.tags) body['tags'] = attributes.tags
371 if (attributes.privacy) body['privacy'] = attributes.privacy
372 if (attributes.channelId) body['channelId'] = attributes.channelId
373 if (attributes.scheduleUpdate) body['scheduleUpdate'] = attributes.scheduleUpdate
376 if (attributes.thumbnailfile || attributes.previewfile) {
377 const attaches: any = {}
378 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
379 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
381 return makeUploadRequest({
392 return makePutBodyRequest({
401 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
402 const path = '/api/v1/videos/' + id + '/rate'
406 .set('Accept', 'application/json')
407 .set('Authorization', 'Bearer ' + accessToken)
409 .expect(specialStatus)
412 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
413 return new Promise<any>((res, rej) => {
414 const torrentName = videoUUID + '-' + resolution + '.torrent'
415 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
416 readFile(torrentPath, (err, data) => {
417 if (err) return rej(err)
419 return res(parseTorrent(data))
424 async function completeVideoCheck (
433 commentsEnabled: boolean
458 thumbnailfile?: string
462 if (!attributes.likes) attributes.likes = 0
463 if (!attributes.dislikes) attributes.dislikes = 0
465 expect(video.name).to.equal(attributes.name)
466 expect(video.category.id).to.equal(attributes.category)
467 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
468 expect(video.licence.id).to.equal(attributes.licence)
469 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
470 expect(video.language.id).to.equal(attributes.language)
471 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
472 expect(video.privacy.id).to.deep.equal(attributes.privacy)
473 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
474 expect(video.nsfw).to.equal(attributes.nsfw)
475 expect(video.description).to.equal(attributes.description)
476 expect(video.account.id).to.be.a('number')
477 expect(video.account.uuid).to.be.a('string')
478 expect(video.account.host).to.equal(attributes.account.host)
479 expect(video.account.name).to.equal(attributes.account.name)
480 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
481 expect(video.channel.name).to.equal(attributes.channel.name)
482 expect(video.likes).to.equal(attributes.likes)
483 expect(video.dislikes).to.equal(attributes.dislikes)
484 expect(video.isLocal).to.equal(attributes.isLocal)
485 expect(video.duration).to.equal(attributes.duration)
486 expect(dateIsValid(video.createdAt)).to.be.true
487 expect(dateIsValid(video.publishedAt)).to.be.true
488 expect(dateIsValid(video.updatedAt)).to.be.true
490 if (attributes.publishedAt) {
491 expect(video.publishedAt).to.equal(attributes.publishedAt)
494 const res = await getVideo(url, video.uuid)
495 const videoDetails: VideoDetails = res.body
497 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
498 expect(videoDetails.tags).to.deep.equal(attributes.tags)
499 expect(videoDetails.account.name).to.equal(attributes.account.name)
500 expect(videoDetails.account.host).to.equal(attributes.account.host)
501 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
502 expect(video.channel.name).to.equal(attributes.channel.name)
503 expect(videoDetails.channel.host).to.equal(attributes.account.host)
504 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
505 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
506 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
507 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
509 for (const attributeFile of attributes.files) {
510 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
511 expect(file).not.to.be.undefined
513 let extension = extname(attributes.fixture)
514 // Transcoding enabled on server 2, extension will always be .mp4
515 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
517 const magnetUri = file.magnetUri
518 expect(file.magnetUri).to.have.lengthOf.above(2)
519 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
520 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
521 expect(file.resolution.id).to.equal(attributeFile.resolution)
522 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
524 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
525 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
527 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
528 .to.be.above(minSize).and.below(maxSize)
531 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
534 if (attributes.previewfile) {
535 await testImage(url, attributes.previewfile, videoDetails.previewPath)
538 const torrent = await webtorrentAdd(magnetUri, true)
539 expect(torrent.files).to.be.an('array')
540 expect(torrent.files.length).to.equal(1)
541 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
545 // ---------------------------------------------------------------------------
555 getVideoChannelVideos,
559 getVideosListPagination,
562 getVideosListWithToken,
564 getVideosWithFilters,
571 checkVideoFilesWereRemoved