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
30 waitTranscoding?: boolean
34 privacy?: VideoPrivacy
36 thumbnailfile?: string
40 function getVideoCategories (url: string) {
41 const path = '/api/v1/videos/categories'
43 return makeGetRequest({
46 statusCodeExpected: 200
50 function getVideoLicences (url: string) {
51 const path = '/api/v1/videos/licences'
53 return makeGetRequest({
56 statusCodeExpected: 200
60 function getVideoLanguages (url: string) {
61 const path = '/api/v1/videos/languages'
63 return makeGetRequest({
66 statusCodeExpected: 200
70 function getVideoPrivacies (url: string) {
71 const path = '/api/v1/videos/privacies'
73 return makeGetRequest({
76 statusCodeExpected: 200
80 function getVideo (url: string, id: number | string, expectedStatus = 200) {
81 const path = '/api/v1/videos/' + id
85 .set('Accept', 'application/json')
86 .expect(expectedStatus)
89 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
90 const path = '/api/v1/videos/' + id + '/views'
92 const req = request(url)
94 .set('Accept', 'application/json')
97 req.set('X-Forwarded-For', xForwardedFor)
100 return req.expect(expectedStatus)
103 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
104 const path = '/api/v1/videos/' + id
108 .set('Authorization', 'Bearer ' + token)
109 .set('Accept', 'application/json')
110 .expect(expectedStatus)
113 function getVideoDescription (url: string, descriptionPath: string) {
115 .get(descriptionPath)
116 .set('Accept', 'application/json')
118 .expect('Content-Type', /json/)
121 function getVideosList (url: string) {
122 const path = '/api/v1/videos'
126 .query({ sort: 'name' })
127 .set('Accept', 'application/json')
129 .expect('Content-Type', /json/)
132 function getVideosListWithToken (url: string, token: string) {
133 const path = '/api/v1/videos'
137 .set('Authorization', 'Bearer ' + token)
138 .query({ sort: 'name' })
139 .set('Accept', 'application/json')
141 .expect('Content-Type', /json/)
144 function getLocalVideos (url: string) {
145 const path = '/api/v1/videos'
149 .query({ sort: 'name', filter: 'local' })
150 .set('Accept', 'application/json')
152 .expect('Content-Type', /json/)
155 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
156 const path = '/api/v1/users/me/videos'
158 const req = request(url)
160 .query({ start: start })
161 .query({ count: count })
163 if (sort) req.query({ sort })
165 return req.set('Accept', 'application/json')
166 .set('Authorization', 'Bearer ' + accessToken)
168 .expect('Content-Type', /json/)
171 function getAccountVideos (url: string, accessToken: string, accountName: string, start: number, count: number, sort?: string) {
172 const path = '/api/v1/accounts/' + accountName + '/videos'
174 return makeGetRequest({
183 statusCodeExpected: 200
187 function getVideoChannelVideos (
190 videoChannelId: number | string,
195 const path = '/api/v1/video-channels/' + videoChannelId + '/videos'
197 return makeGetRequest({
206 statusCodeExpected: 200
210 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
211 const path = '/api/v1/videos'
213 const req = request(url)
215 .query({ start: start })
216 .query({ count: count })
218 if (sort) req.query({ sort })
220 return req.set('Accept', 'application/json')
222 .expect('Content-Type', /json/)
225 function getVideosListSort (url: string, sort: string) {
226 const path = '/api/v1/videos'
230 .query({ sort: sort })
231 .set('Accept', 'application/json')
233 .expect('Content-Type', /json/)
236 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
237 const path = '/api/v1/videos'
240 .delete(path + '/' + id)
241 .set('Accept', 'application/json')
242 .set('Authorization', 'Bearer ' + token)
243 .expect(expectedStatus)
246 function searchVideo (url: string, search: string) {
247 const path = '/api/v1/videos'
248 const req = request(url)
249 .get(path + '/search')
251 .set('Accept', 'application/json')
253 return req.expect(200)
254 .expect('Content-Type', /json/)
257 function searchVideoWithToken (url: string, search: string, token: string) {
258 const path = '/api/v1/videos'
259 const req = request(url)
260 .get(path + '/search')
261 .set('Authorization', 'Bearer ' + token)
263 .set('Accept', 'application/json')
265 return req.expect(200)
266 .expect('Content-Type', /json/)
269 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
270 const path = '/api/v1/videos'
272 const req = request(url)
273 .get(path + '/search')
278 if (sort) req.query({ sort })
280 return req.set('Accept', 'application/json')
282 .expect('Content-Type', /json/)
285 function searchVideoWithSort (url: string, search: string, sort: string) {
286 const path = '/api/v1/videos'
289 .get(path + '/search')
292 .set('Accept', 'application/json')
294 .expect('Content-Type', /json/)
297 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
298 const testDirectory = 'test' + serverNumber
300 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
301 const directoryPath = join(root(), testDirectory, directory)
303 const directoryExists = existsSync(directoryPath)
304 expect(directoryExists).to.be.true
306 const files = await readdirPromise(directoryPath)
307 for (const file of files) {
308 expect(file).to.not.contain(videoUUID)
313 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
314 const path = '/api/v1/videos/upload'
315 let defaultChannelId = '1'
318 const res = await getMyUserInformation(url, accessToken)
319 defaultChannelId = res.body.videoChannels[0].id
320 } catch (e) { /* empty */ }
322 // Override default attributes
323 const attributes = Object.assign({
324 name: 'my super video',
328 channelId: defaultChannelId,
330 waitTranscoding: false,
331 description: 'my super description',
332 support: 'my super support text',
334 privacy: VideoPrivacy.PUBLIC,
335 commentsEnabled: true,
336 fixture: 'video_short.webm'
337 }, videoAttributesArg)
339 const req = request(url)
341 .set('Accept', 'application/json')
342 .set('Authorization', 'Bearer ' + accessToken)
343 .field('name', attributes.name)
344 .field('nsfw', JSON.stringify(attributes.nsfw))
345 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
346 .field('waitTranscoding', JSON.stringify(attributes.waitTranscoding))
347 .field('privacy', attributes.privacy.toString())
348 .field('channelId', attributes.channelId)
350 if (attributes.description !== undefined) {
351 req.field('description', attributes.description)
353 if (attributes.language !== undefined) {
354 req.field('language', attributes.language.toString())
356 if (attributes.category !== undefined) {
357 req.field('category', attributes.category.toString())
359 if (attributes.licence !== undefined) {
360 req.field('licence', attributes.licence.toString())
363 for (let i = 0; i < attributes.tags.length; i++) {
364 req.field('tags[' + i + ']', attributes.tags[i])
367 if (attributes.thumbnailfile !== undefined) {
368 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
370 if (attributes.previewfile !== undefined) {
371 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
374 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
375 .expect(specialStatus)
378 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
379 const path = '/api/v1/videos/' + id
382 if (attributes.name) body['name'] = attributes.name
383 if (attributes.category) body['category'] = attributes.category
384 if (attributes.licence) body['licence'] = attributes.licence
385 if (attributes.language) body['language'] = attributes.language
386 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
387 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
388 if (attributes.description) body['description'] = attributes.description
389 if (attributes.tags) body['tags'] = attributes.tags
390 if (attributes.privacy) body['privacy'] = attributes.privacy
391 if (attributes.channelId) body['channelId'] = attributes.channelId
394 if (attributes.thumbnailfile || attributes.previewfile) {
395 const attaches: any = {}
396 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
397 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
399 return makeUploadRequest({
410 return makePutBodyRequest({
419 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
420 const path = '/api/v1/videos/' + id + '/rate'
424 .set('Accept', 'application/json')
425 .set('Authorization', 'Bearer ' + accessToken)
427 .expect(specialStatus)
430 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
431 return new Promise<any>((res, rej) => {
432 const torrentName = videoUUID + '-' + resolution + '.torrent'
433 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
434 readFile(torrentPath, (err, data) => {
435 if (err) return rej(err)
437 return res(parseTorrent(data))
442 async function completeVideoCheck (
451 commentsEnabled: boolean
475 thumbnailfile?: string
479 if (!attributes.likes) attributes.likes = 0
480 if (!attributes.dislikes) attributes.dislikes = 0
482 expect(video.name).to.equal(attributes.name)
483 expect(video.category.id).to.equal(attributes.category)
484 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
485 expect(video.licence.id).to.equal(attributes.licence)
486 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
487 expect(video.language.id).to.equal(attributes.language)
488 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
489 expect(video.privacy.id).to.deep.equal(attributes.privacy)
490 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
491 expect(video.nsfw).to.equal(attributes.nsfw)
492 expect(video.description).to.equal(attributes.description)
493 expect(video.account.id).to.be.a('number')
494 expect(video.account.uuid).to.be.a('string')
495 expect(video.account.host).to.equal(attributes.account.host)
496 expect(video.account.name).to.equal(attributes.account.name)
497 expect(video.channel.displayName).to.equal(attributes.channel.name)
498 expect(video.channel.name).to.have.lengthOf(36)
499 expect(video.likes).to.equal(attributes.likes)
500 expect(video.dislikes).to.equal(attributes.dislikes)
501 expect(video.isLocal).to.equal(attributes.isLocal)
502 expect(video.duration).to.equal(attributes.duration)
503 expect(dateIsValid(video.createdAt)).to.be.true
504 expect(dateIsValid(video.publishedAt)).to.be.true
505 expect(dateIsValid(video.updatedAt)).to.be.true
507 if (attributes.publishedAt) {
508 expect(video.publishedAt).to.equal(attributes.publishedAt)
511 const res = await getVideo(url, video.uuid)
512 const videoDetails: VideoDetails = res.body
514 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
515 expect(videoDetails.tags).to.deep.equal(attributes.tags)
516 expect(videoDetails.account.name).to.equal(attributes.account.name)
517 expect(videoDetails.account.host).to.equal(attributes.account.host)
518 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
519 expect(videoDetails.channel.name).to.have.lengthOf(36)
520 expect(videoDetails.channel.host).to.equal(attributes.account.host)
521 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
522 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
523 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
524 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
526 for (const attributeFile of attributes.files) {
527 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
528 expect(file).not.to.be.undefined
530 let extension = extname(attributes.fixture)
531 // Transcoding enabled on server 2, extension will always be .mp4
532 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
534 const magnetUri = file.magnetUri
535 expect(file.magnetUri).to.have.lengthOf.above(2)
536 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
537 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
538 expect(file.resolution.id).to.equal(attributeFile.resolution)
539 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
541 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
542 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
543 expect(file.size).to.be.above(minSize).and.below(maxSize)
546 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
549 if (attributes.previewfile) {
550 await testImage(url, attributes.previewfile, videoDetails.previewPath)
553 const torrent = await webtorrentAdd(magnetUri, true)
554 expect(torrent.files).to.be.an('array')
555 expect(torrent.files.length).to.equal(1)
556 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
560 // ---------------------------------------------------------------------------
570 getVideoChannelVideos,
571 searchVideoWithToken,
575 getVideosListPagination,
579 searchVideoWithPagination,
581 getVideosListWithToken,
589 checkVideoFilesWereRemoved