1 /* tslint:disable:no-unused-expression */
3 import { expect } from 'chai'
4 import { existsSync, readFile } from 'fs'
5 import * as parseTorrent from 'parse-torrent'
6 import { extname, join } from 'path'
7 import * as request from 'supertest'
9 buildAbsoluteFixturePath,
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
33 privacy?: VideoPrivacy
35 thumbnailfile?: string
39 function getVideoCategories (url: string) {
40 const path = '/api/v1/videos/categories'
42 return makeGetRequest({
45 statusCodeExpected: 200
49 function getVideoLicences (url: string) {
50 const path = '/api/v1/videos/licences'
52 return makeGetRequest({
55 statusCodeExpected: 200
59 function getVideoLanguages (url: string) {
60 const path = '/api/v1/videos/languages'
62 return makeGetRequest({
65 statusCodeExpected: 200
69 function getVideoPrivacies (url: string) {
70 const path = '/api/v1/videos/privacies'
72 return makeGetRequest({
75 statusCodeExpected: 200
79 function getVideo (url: string, id: number | string, expectedStatus = 200) {
80 const path = '/api/v1/videos/' + id
84 .set('Accept', 'application/json')
85 .expect(expectedStatus)
88 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
89 const path = '/api/v1/videos/' + id + '/views'
91 const req = request(url)
93 .set('Accept', 'application/json')
96 req.set('X-Forwarded-For', xForwardedFor)
99 return req.expect(expectedStatus)
102 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
103 const path = '/api/v1/videos/' + id
107 .set('Authorization', 'Bearer ' + token)
108 .set('Accept', 'application/json')
109 .expect(expectedStatus)
112 function getVideoDescription (url: string, descriptionPath: string) {
114 .get(descriptionPath)
115 .set('Accept', 'application/json')
117 .expect('Content-Type', /json/)
120 function getVideosList (url: string) {
121 const path = '/api/v1/videos'
125 .query({ sort: 'name' })
126 .set('Accept', 'application/json')
128 .expect('Content-Type', /json/)
131 function getVideosListWithToken (url: string, token: string) {
132 const path = '/api/v1/videos'
136 .set('Authorization', 'Bearer ' + token)
137 .query({ sort: 'name' })
138 .set('Accept', 'application/json')
140 .expect('Content-Type', /json/)
143 function getLocalVideos (url: string) {
144 const path = '/api/v1/videos'
148 .query({ sort: 'name', filter: 'local' })
149 .set('Accept', 'application/json')
151 .expect('Content-Type', /json/)
154 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
155 const path = '/api/v1/users/me/videos'
157 const req = request(url)
159 .query({ start: start })
160 .query({ count: count })
162 if (sort) req.query({ sort })
164 return req.set('Accept', 'application/json')
165 .set('Authorization', 'Bearer ' + accessToken)
167 .expect('Content-Type', /json/)
170 function getAccountVideos (url: string, accessToken: string, accountId: number | string, start: number, count: number, sort?: string) {
171 const path = '/api/v1/accounts/' + accountId + '/videos'
173 return makeGetRequest({
182 statusCodeExpected: 200
186 function getVideoChannelVideos (
189 videoChannelId: number | string,
194 const path = '/api/v1/video-channels/' + videoChannelId + '/videos'
196 return makeGetRequest({
205 statusCodeExpected: 200
209 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
210 const path = '/api/v1/videos'
212 const req = request(url)
214 .query({ start: start })
215 .query({ count: count })
217 if (sort) req.query({ sort })
219 return req.set('Accept', 'application/json')
221 .expect('Content-Type', /json/)
224 function getVideosListSort (url: string, sort: string) {
225 const path = '/api/v1/videos'
229 .query({ sort: sort })
230 .set('Accept', 'application/json')
232 .expect('Content-Type', /json/)
235 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
236 const path = '/api/v1/videos'
239 .delete(path + '/' + id)
240 .set('Accept', 'application/json')
241 .set('Authorization', 'Bearer ' + token)
242 .expect(expectedStatus)
245 function searchVideo (url: string, search: string) {
246 const path = '/api/v1/videos'
247 const req = request(url)
248 .get(path + '/search')
250 .set('Accept', 'application/json')
252 return req.expect(200)
253 .expect('Content-Type', /json/)
256 function searchVideoWithToken (url: string, search: string, token: string) {
257 const path = '/api/v1/videos'
258 const req = request(url)
259 .get(path + '/search')
260 .set('Authorization', 'Bearer ' + token)
262 .set('Accept', 'application/json')
264 return req.expect(200)
265 .expect('Content-Type', /json/)
268 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
269 const path = '/api/v1/videos'
271 const req = request(url)
272 .get(path + '/search')
277 if (sort) req.query({ sort })
279 return req.set('Accept', 'application/json')
281 .expect('Content-Type', /json/)
284 function searchVideoWithSort (url: string, search: string, sort: string) {
285 const path = '/api/v1/videos'
288 .get(path + '/search')
291 .set('Accept', 'application/json')
293 .expect('Content-Type', /json/)
296 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
297 const testDirectory = 'test' + serverNumber
299 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
300 const directoryPath = join(root(), testDirectory, directory)
302 const directoryExists = existsSync(directoryPath)
303 expect(directoryExists).to.be.true
305 const files = await readdirPromise(directoryPath)
306 for (const file of files) {
307 expect(file).to.not.contain(videoUUID)
312 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
313 const path = '/api/v1/videos/upload'
314 let defaultChannelId = '1'
317 const res = await getMyUserInformation(url, accessToken)
318 defaultChannelId = res.body.videoChannels[0].id
319 } catch (e) { /* empty */ }
321 // Override default attributes
322 const attributes = Object.assign({
323 name: 'my super video',
327 channelId: defaultChannelId,
329 description: 'my super description',
330 support: 'my super support text',
332 privacy: VideoPrivacy.PUBLIC,
333 commentsEnabled: true,
334 fixture: 'video_short.webm'
335 }, videoAttributesArg)
337 const req = request(url)
339 .set('Accept', 'application/json')
340 .set('Authorization', 'Bearer ' + accessToken)
341 .field('name', attributes.name)
342 .field('nsfw', JSON.stringify(attributes.nsfw))
343 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
344 .field('privacy', attributes.privacy.toString())
345 .field('channelId', attributes.channelId)
347 if (attributes.description !== undefined) {
348 req.field('description', attributes.description)
350 if (attributes.language !== undefined) {
351 req.field('language', attributes.language.toString())
353 if (attributes.category !== undefined) {
354 req.field('category', attributes.category.toString())
356 if (attributes.licence !== undefined) {
357 req.field('licence', attributes.licence.toString())
360 for (let i = 0; i < attributes.tags.length; i++) {
361 req.field('tags[' + i + ']', attributes.tags[i])
364 if (attributes.thumbnailfile !== undefined) {
365 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
367 if (attributes.previewfile !== undefined) {
368 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
371 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
372 .expect(specialStatus)
375 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
376 const path = '/api/v1/videos/' + id
379 if (attributes.name) body['name'] = attributes.name
380 if (attributes.category) body['category'] = attributes.category
381 if (attributes.licence) body['licence'] = attributes.licence
382 if (attributes.language) body['language'] = attributes.language
383 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
384 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
385 if (attributes.description) body['description'] = attributes.description
386 if (attributes.tags) body['tags'] = attributes.tags
387 if (attributes.privacy) body['privacy'] = attributes.privacy
388 if (attributes.channelId) body['channelId'] = attributes.channelId
391 if (attributes.thumbnailfile || attributes.previewfile) {
392 const attaches: any = {}
393 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
394 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
396 return makeUploadRequest({
407 return makePutBodyRequest({
416 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
417 const path = '/api/v1/videos/' + id + '/rate'
421 .set('Accept', 'application/json')
422 .set('Authorization', 'Bearer ' + accessToken)
424 .expect(specialStatus)
427 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
428 return new Promise<any>((res, rej) => {
429 const torrentName = videoUUID + '-' + resolution + '.torrent'
430 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
431 readFile(torrentPath, (err, data) => {
432 if (err) return rej(err)
434 return res(parseTorrent(data))
439 async function completeVideoCheck (
448 commentsEnabled: boolean
471 thumbnailfile?: string
475 if (!attributes.likes) attributes.likes = 0
476 if (!attributes.dislikes) attributes.dislikes = 0
478 expect(video.name).to.equal(attributes.name)
479 expect(video.category.id).to.equal(attributes.category)
480 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
481 expect(video.licence.id).to.equal(attributes.licence)
482 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
483 expect(video.language.id).to.equal(attributes.language)
484 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
485 expect(video.privacy.id).to.deep.equal(attributes.privacy)
486 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
487 expect(video.nsfw).to.equal(attributes.nsfw)
488 expect(video.description).to.equal(attributes.description)
489 expect(video.account.id).to.be.a('number')
490 expect(video.account.uuid).to.be.a('string')
491 expect(video.account.host).to.equal(attributes.account.host)
492 expect(video.account.name).to.equal(attributes.account.name)
493 expect(video.channel.displayName).to.equal(attributes.channel.name)
494 expect(video.channel.name).to.have.lengthOf(36)
495 expect(video.likes).to.equal(attributes.likes)
496 expect(video.dislikes).to.equal(attributes.dislikes)
497 expect(video.isLocal).to.equal(attributes.isLocal)
498 expect(video.duration).to.equal(attributes.duration)
499 expect(dateIsValid(video.createdAt)).to.be.true
500 expect(dateIsValid(video.publishedAt)).to.be.true
501 expect(dateIsValid(video.updatedAt)).to.be.true
503 const res = await getVideo(url, video.uuid)
504 const videoDetails: VideoDetails = res.body
506 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
507 expect(videoDetails.tags).to.deep.equal(attributes.tags)
508 expect(videoDetails.account.name).to.equal(attributes.account.name)
509 expect(videoDetails.account.host).to.equal(attributes.account.host)
510 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
511 expect(videoDetails.channel.name).to.have.lengthOf(36)
512 expect(videoDetails.channel.host).to.equal(attributes.account.host)
513 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
514 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
515 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
516 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
518 for (const attributeFile of attributes.files) {
519 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
520 expect(file).not.to.be.undefined
522 let extension = extname(attributes.fixture)
523 // Transcoding enabled on server 2, extension will always be .mp4
524 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
526 const magnetUri = file.magnetUri
527 expect(file.magnetUri).to.have.lengthOf.above(2)
528 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
529 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
530 expect(file.resolution.id).to.equal(attributeFile.resolution)
531 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
533 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
534 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
535 expect(file.size).to.be.above(minSize).and.below(maxSize)
538 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
541 if (attributes.previewfile) {
542 await testImage(url, attributes.previewfile, videoDetails.previewPath)
545 const torrent = await webtorrentAdd(magnetUri, true)
546 expect(torrent.files).to.be.an('array')
547 expect(torrent.files.length).to.equal(1)
548 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
552 // ---------------------------------------------------------------------------
562 getVideoChannelVideos,
563 searchVideoWithToken,
567 getVideosListPagination,
571 searchVideoWithPagination,
573 getVideosListWithToken,
581 checkVideoFilesWereRemoved