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 { 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 getLocalVideos (url: string) {
132 const path = '/api/v1/videos'
136 .query({ sort: 'name', filter: 'local' })
137 .set('Accept', 'application/json')
139 .expect('Content-Type', /json/)
142 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
143 const path = '/api/v1/users/me/videos'
145 const req = request(url)
147 .query({ start: start })
148 .query({ count: count })
150 if (sort) req.query({ sort })
152 return req.set('Accept', 'application/json')
153 .set('Authorization', 'Bearer ' + accessToken)
155 .expect('Content-Type', /json/)
158 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
159 const path = '/api/v1/videos'
161 const req = request(url)
163 .query({ start: start })
164 .query({ count: count })
166 if (sort) req.query({ sort })
168 return req.set('Accept', 'application/json')
170 .expect('Content-Type', /json/)
173 function getVideosListSort (url: string, sort: string) {
174 const path = '/api/v1/videos'
178 .query({ sort: sort })
179 .set('Accept', 'application/json')
181 .expect('Content-Type', /json/)
184 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
185 const path = '/api/v1/videos'
188 .delete(path + '/' + id)
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + token)
191 .expect(expectedStatus)
194 function searchVideo (url: string, search: string) {
195 const path = '/api/v1/videos'
196 const req = request(url)
197 .get(path + '/search')
199 .set('Accept', 'application/json')
201 return req.expect(200)
202 .expect('Content-Type', /json/)
205 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
206 const path = '/api/v1/videos'
208 const req = request(url)
209 .get(path + '/search')
214 if (sort) req.query({ sort })
216 return req.set('Accept', 'application/json')
218 .expect('Content-Type', /json/)
221 function searchVideoWithSort (url: string, search: string, sort: string) {
222 const path = '/api/v1/videos'
225 .get(path + '/search')
228 .set('Accept', 'application/json')
230 .expect('Content-Type', /json/)
233 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
234 const testDirectory = 'test' + serverNumber
236 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
237 const directoryPath = join(root(), testDirectory, directory)
239 const directoryExists = existsSync(directoryPath)
240 expect(directoryExists).to.be.true
242 const files = await readdirPromise(directoryPath)
243 for (const file of files) {
244 expect(file).to.not.contain(videoUUID)
249 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
250 const path = '/api/v1/videos/upload'
251 let defaultChannelId = '1'
254 const res = await getMyUserInformation(url, accessToken)
255 defaultChannelId = res.body.videoChannels[0].id
256 } catch (e) { /* empty */ }
258 // Override default attributes
259 const attributes = Object.assign({
260 name: 'my super video',
264 channelId: defaultChannelId,
266 description: 'my super description',
267 support: 'my super support text',
269 privacy: VideoPrivacy.PUBLIC,
270 commentsEnabled: true,
271 fixture: 'video_short.webm'
272 }, videoAttributesArg)
274 const req = request(url)
276 .set('Accept', 'application/json')
277 .set('Authorization', 'Bearer ' + accessToken)
278 .field('name', attributes.name)
279 .field('nsfw', JSON.stringify(attributes.nsfw))
280 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
281 .field('privacy', attributes.privacy.toString())
282 .field('channelId', attributes.channelId)
284 if (attributes.description !== undefined) {
285 req.field('description', attributes.description)
287 if (attributes.language !== undefined) {
288 req.field('language', attributes.language.toString())
290 if (attributes.category !== undefined) {
291 req.field('category', attributes.category.toString())
293 if (attributes.licence !== undefined) {
294 req.field('licence', attributes.licence.toString())
297 for (let i = 0; i < attributes.tags.length; i++) {
298 req.field('tags[' + i + ']', attributes.tags[i])
301 if (attributes.thumbnailfile !== undefined) {
302 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
304 if (attributes.previewfile !== undefined) {
305 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
308 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
309 .expect(specialStatus)
312 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
313 const path = '/api/v1/videos/' + id
316 if (attributes.name) body['name'] = attributes.name
317 if (attributes.category) body['category'] = attributes.category
318 if (attributes.licence) body['licence'] = attributes.licence
319 if (attributes.language) body['language'] = attributes.language
320 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
321 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
322 if (attributes.description) body['description'] = attributes.description
323 if (attributes.tags) body['tags'] = attributes.tags
324 if (attributes.privacy) body['privacy'] = attributes.privacy
327 if (attributes.thumbnailfile || attributes.previewfile) {
328 const attaches: any = {}
329 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
330 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
332 return makeUploadRequest({
343 return makePutBodyRequest({
352 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
353 const path = '/api/v1/videos/' + id + '/rate'
357 .set('Accept', 'application/json')
358 .set('Authorization', 'Bearer ' + accessToken)
360 .expect(specialStatus)
363 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
364 return new Promise<any>((res, rej) => {
365 const torrentName = videoUUID + '-' + resolution + '.torrent'
366 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
367 readFile(torrentPath, (err, data) => {
368 if (err) return rej(err)
370 return res(parseTorrent(data))
375 async function completeVideoCheck (
384 commentsEnabled: boolean
407 thumbnailfile?: string
411 if (!attributes.likes) attributes.likes = 0
412 if (!attributes.dislikes) attributes.dislikes = 0
414 expect(video.name).to.equal(attributes.name)
415 expect(video.category.id).to.equal(attributes.category)
416 expect(video.category.label).to.equal(VIDEO_CATEGORIES[attributes.category] || 'Misc')
417 expect(video.licence.id).to.equal(attributes.licence)
418 expect(video.licence.label).to.equal(VIDEO_LICENCES[attributes.licence] || 'Unknown')
419 expect(video.language.id).to.equal(attributes.language)
420 expect(video.language.label).to.equal(VIDEO_LANGUAGES[attributes.language] || 'Unknown')
421 expect(video.nsfw).to.equal(attributes.nsfw)
422 expect(video.description).to.equal(attributes.description)
423 expect(video.account.host).to.equal(attributes.account.host)
424 expect(video.account.name).to.equal(attributes.account.name)
425 expect(video.likes).to.equal(attributes.likes)
426 expect(video.dislikes).to.equal(attributes.dislikes)
427 expect(video.isLocal).to.equal(attributes.isLocal)
428 expect(video.duration).to.equal(attributes.duration)
429 expect(dateIsValid(video.createdAt)).to.be.true
430 expect(dateIsValid(video.publishedAt)).to.be.true
431 expect(dateIsValid(video.updatedAt)).to.be.true
433 const res = await getVideo(url, video.uuid)
434 const videoDetails = res.body
436 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
437 expect(videoDetails.tags).to.deep.equal(attributes.tags)
438 expect(videoDetails.privacy.id).to.deep.equal(attributes.privacy)
439 expect(videoDetails.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
440 expect(videoDetails.account.name).to.equal(attributes.account.name)
441 expect(videoDetails.account.host).to.equal(attributes.account.host)
442 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
444 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
445 expect(videoDetails.channel.name).to.have.lengthOf(36)
446 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
447 expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
448 expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
450 for (const attributeFile of attributes.files) {
451 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
452 expect(file).not.to.be.undefined
454 let extension = extname(attributes.fixture)
455 // Transcoding enabled on server 2, extension will always be .mp4
456 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
458 const magnetUri = file.magnetUri
459 expect(file.magnetUri).to.have.lengthOf.above(2)
460 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
461 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
462 expect(file.resolution.id).to.equal(attributeFile.resolution)
463 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
465 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
466 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
467 expect(file.size).to.be.above(minSize).and.below(maxSize)
470 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
473 if (attributes.previewfile) {
474 await testImage(url, attributes.previewfile, videoDetails.previewPath)
477 const torrent = await webtorrentAdd(magnetUri, true)
478 expect(torrent.files).to.be.an('array')
479 expect(torrent.files.length).to.equal(1)
480 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
484 // ---------------------------------------------------------------------------
496 getVideosListPagination,
500 searchVideoWithPagination,
509 checkVideoFilesWereRemoved